def GetTreeListOrder(treefile, outpath): #Create the Itol class itl = Itol() #Set the tree file tree = treefile dirname = os.path.dirname(treefile) if dirname == "": dirname = "." if outpath == "": outpath = dirname elif not os.path.exists(outpath): os.system("mkdir -p %s" % (outpath)) rootname = os.path.basename(os.path.splitext(treefile)[0]) #=================================== itl.add_file(tree) itl.params['treeName'] = rootname itl.params['treeFormat'] = 'newick' #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') good_upload = itl.upload() if good_upload == False: print('There was an error:' + itl.comm.upload_output) return 1 #Export to pdf tree_id = itl.comm.tree_id itol_exporter = itl.get_itol_export() itol_exporter.set_export_param_value('format', "pdf") itol_exporter.set_export_param_value( 'display_mode', "1") #(1=normal, 2=circular, 3=unrooted) itol_exporter.set_export_param_value('line_width', "1") #epsfile = outpath + os.sep + rootname + '.itolnormal.eps' pdffile = outpath + os.sep + rootname + '.itolnormal.pdf' print('Exporting to pdffile %s' % (pdffile)) itol_exporter.export(pdffile) orderfile = outpath + os.sep + rootname + '.listorder.txt' os.system("pdftotext -nopgbrk %s %s" % (pdffile, orderfile)) os.system("rm -f %s" % pdffile) print('list order output to ', orderfile)
class ItolTest(unittest.TestCase): def setUp(self): self.itol = Itol() def test_initializes(self): self.assertEqual(type(self.itol.params), dict) self.assertEqual(self.itol.files, []) self.assertIsNotNone(self.itol.comm) def test_add_file(self): with tempfile.NamedTemporaryFile() as temp: self.itol.add_file(temp.name) with self.assertRaises(IOError): self.itol.add_file(' ') def test_good_upload(self): with patch('itolapi.Comm.upload_tree') as mock_upload: mock_upload.return_value = True self.itol.comm.tree_id = 1234 self.assertEqual(self.itol.upload(), 1234) def test_bad_upload(self): with patch('itolapi.Comm.upload_tree') as mock_upload: mock_upload.return_value = False self.itol.comm.tree_id = 1234 self.assertEqual(self.itol.upload(), 0) def test_get_webpage(self): self.itol.comm.tree_id = 'asdf' webpage = self.itol.get_webpage() self.assertIn('itol.embl.de', webpage) self.assertIn('asdf', webpage) def test_get_itol_export(self): self.itol.comm.tree_id = 1234 export = self.itol.get_itol_export() self.assertEqual(export.params['tree'], 1234) @patch('itolapi.itol.pprint.pprint') def test_print_variables(self, mock_print): self.itol.params['treeName'] = 'test' with tempfile.NamedTemporaryFile() as temp: self.itol.add_file(temp.name) self.itol.print_variables() self.assertEqual(mock_print.call_args_list[0][0][0], self.itol.files) self.assertEqual(mock_print.call_args_list[1][0][0], self.itol.params)
def test_upload_download(self): current_dir = os.path.dirname(os.path.realpath(__file__)) examples_dir = os.path.join(current_dir, '..', '..', 'examples') test = Itol() tree = os.path.join(examples_dir, 'tree_of_life.tree.txt') test.add_file(tree) dataset = os.path.join(examples_dir, 'colors_tol.txt') test.add_file(dataset) upload_status = test.upload() self.assertTrue(upload_status) exporter = test.get_itol_export() exporter.set_export_param_value('format', 'pdf') with tempfile.NamedTemporaryFile() as temp: exporter.export(temp.name)
# Read the tree ID print('Tree ID: ' + str(test.comm.tree_id)) # Read the iTOL API return statement print('iTOL output: ' + str(test.comm.upload_output)) # Website to be redirected to iTOL tree print('Tree Web Page URL: ' + test.get_webpage()) # Warnings associated with the upload print('Warnings: ' + str(test.comm.warnings)) # Export a pre-made tree to pdf itol_exporter = ItolExport() itol_exporter.set_export_param_value('tree', '18793532031912684633930') itol_exporter.set_export_param_value('format', 'pdf') itol_exporter.set_export_param_value('datasetList', 'dataset1') # itol_exporter.export('example_pdf.pdf') # print('exported tree to ',export_location) # Export the tree above to pdf print('Exporting to pdf') itol_exporter = test.get_itol_export() export_location = os.path.join(current_dir, 'example_pdf.pdf') itol_exporter.set_export_param_value('format', 'pdf') itol_exporter.set_export_param_value('datasetList', 'dataset1') itol_exporter.export(export_location) print('exported tree to ', export_location)
sys.exit(1) # Read the tree ID print('Tree ID: ' + str(test.comm.tree_id)) # Read the iTOL API return statement print('iTOL output: ' + str(test.comm.upload_output)) # Website to be redirected to iTOL tree print('Tree Web Page URL: ' + test.get_webpage()) # Warnings associated with the upload print('Warnings: ' + str(test.comm.warnings)) # Export a pre-made tree to pdf #itol_exporter = ItolExport() #itol_exporter.set_export_param_value('tree', '18793532031912684633930') #itol_exporter.set_export_param_value('format', 'pdf') #itol_exporter.set_export_param_value('datasetList', 'dataset1') # itol_exporter.export('example_pdf.pdf') # print('exported tree to ',export_location) # Export the tree above to pdf print('Exporting to pdf') itol_exporter = test.get_itol_export() export_location = os.path.join(current_dir, family_domain_ID + '_tree.pdf') itol_exporter.set_export_param_value('format', 'pdf') itol_exporter.set_export_param_value('datasetList', 'dataset1') itol_exporter.export(export_location) print('exported tree to ', export_location)
class ITOL(): """ Tree with branch lengths:: (A:0.1,(B:0.1,C:0.1))); Tree with bootstrap and branch lengths:: (A:0.1,(B:0.1,C:0.1)90:0.1)98:0.3); .. plot:: from pylab import imshow, imread from easydev import TempFile from sequana import ITOL, sequana_data itol = ITOL(sequana_data("test_itol_basic.tree.txt")) itol.upload() # You can change the parameters in itol.params itol.params["display_mode"] = 1 # use linear layout instead of circular # finally export your image locally: with TempFile(suffix=".png") as fout: itol.export(fout.name) imshow(imread(fout.name)) """ def __init__(self, tree): self.itol = Itol() assert tree.endswith( ".tree.txt"), "Your input tree must end in .tree.txt" self.itol.add_file(tree) self._datasets = 0 self.status = None # datasets_visible should not be used if there is no datasets. # self.params = { "display_mode": 2, # rotation "ignore_branch_length": 1, "line_width": 5, #"datasets_visible": "0,1", 'bootstrap_display': 1, 'bootstrap_type': 2, 'bootstrap_label_size': 32 } def add_file(self, filename): self.itol.add_file(filename) N = len(self.itol.files) - 1 # remove the input tree file self.params['datasets_visible'] = ",".join( [str(x) for x in range(0, N)]) def upload(self): self.status = self.itol.upload() assert self.status, "Something is wrong with your input tree" def export(self, filename='test.png'): if self.status is None: logger.error("Upload the tree first with upload() method") export = self.itol.get_itol_export() # Set the format if filename.endswith(".png"): logger.info("Exporting in {} format".format("png")) export.params['format'] = "png" elif filename.endswith(".svg"): logger.info("Exporting in {} format".format("svg")) export.params['format'] = "svg" elif filename.endswith(".pdf"): logger.info("Exporting in {} format".format("pdf")) export.params['format'] = "pdf" elif filename.endswith(".eps"): logger.info("Exporting in {} format".format("eps")) export.params['format'] = "eps" else: raise ValueError("filename must end in pdf, png, svg or eps") export.params.update(**self.params) export.export(filename)
def Itol_Tree_m0(pfamid, datapath, outpath):#{{{ #Create the Itol class itl = Itol() #Set the tree file tree = datapath + os.sep + pfamid + '.tree' (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "") if not os.path.exists(tree): print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr) return 1 t = Tree(tree) leaves = t.get_leaves() numLeave = len(leaves) fontsize = GetFontSize(numLeave) datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt' # datafile2 = datapath + os.sep + pfamid + '.cmpclass.colordef.txt' # datafile3 = datapath + os.sep + pfamid + '.ntermstate.colordef.txt' datafile4 = datapath + os.sep + pfamid + '.cluster.colordef.txt' colordeffile = datapath + os.sep + pfamid + '.pfam.colordef.txt' branchlabelfile = datapath + os.sep + pfamid + '.branchlabel.txt' datafileList = [datafile1, datafile2, datafile3, datafile4, colordeffile, branchlabelfile] rootname = os.path.basename(os.path.splitext(tree)[0]) #=================================== itl.add_file(tree) itl.params['treeName'] = rootname itl.params['treeFormat'] = 'newick' valid_datafileList = [] for datafile in datafileList: if os.path.exists(datafile): itl.add_file(datafile) valid_datafileList.append(datafile) datasets_list = [str(x) for x in range(len(valid_datafileList))] # Check parameters # itl.print_variables() #Submit the tree print('') print('Uploading the tree. This may take some time depending on how large the tree is and how much load there is on the itol server') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) sys.exit(1) #Read the tree ID print('Tree ID: '+str(itl.comm.tree_id)) #Read the iTOL API return statement print('iTOL output: '+str(itl.comm.upload_output)) #Website to be redirected to iTOL tree print('Tree Web Page URL: '+itl.get_webpage()) # Warnings associated with the upload print('Warnings: '+str(itl.comm.warnings)) #Export to pdf print('Exporting to pdf') itol_exporter = itl.get_itol_export() #itol_exporter = itolexport.ItolExport() #itol_exporter.set_export_param_value('tree','18793532031912684633930') itol_exporter.set_export_param_value('format', 'pdf') itol_exporter.set_export_param_value('display_mode',"2") #itol_exporter.set_export_param_value('current_font_size',fontsize) itol_exporter.set_export_param_value('align_labels',"1") itol_exporter.set_export_param_value('datasets_visible',",".join(datasets_list)) epsfile = outpath + os.sep + pfamid + '-itol.eps' pdffile = outpath + os.sep + pfamid + '-itol.pdf' jpgfile = outpath + os.sep + pfamid + '-itol.jpg' thumbfile = outpath + os.sep + "thumb." + pfamid + '-itol.jpg' itol_exporter.export(pdffile) #os.system("epstopdf %s" % epsfile ) os.system("convert %s %s" % (pdffile, jpgfile) ) os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile)) print('exported tree to ',pdffile)
def Itol_Tree_linear(treefile, fastafile, outpath):# {{{ """ Generate itol tree linear """ if not treefile.endswith(".tree") or treefile.endswith(".tree.txt"): newfile = treefile + ".tree" shutil.copy2(treefile, newfile) treefile = newfile #Create the Itol class itl = Itol() #Set the tree file tree = treefile dirname = os.path.dirname(treefile) if dirname == "": dirname = "." if outpath == "": outpath = dirname elif not os.path.exists(outpath): os.system("mkdir -p %s"%(outpath)) rootname = os.path.basename(os.path.splitext(treefile)[0]) rtname_fastafile = os.path.basename(os.path.splitext(fastafile)[0]) colorragefile = dirname + os.sep + rtname_fastafile + ".colorrage.txt" datafileList = [colorragefile] if os.path.exists(fastafile): cmd = "python %s/fasta2colorrange.py %s %s > %s"%(rundir, fastafile, treefile,colorragefile) os.system(cmd) #=================================== itl.add_file(tree) itl.params['treeName'] = rootname itl.params['treeFormat'] = 'newick' valid_datafileList = [] for datafile in datafileList: if os.path.exists(datafile): itl.add_file(datafile) valid_datafileList.append(datafile) datasets_list = [str(x) for x in range(len(valid_datafileList))] #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) return 1 #Export to pdf tree_id = itl.comm.tree_id itol_exporter = itl.get_itol_export() itol_exporter.set_export_param_value('format',"pdf") itol_exporter.set_export_param_value('display_mode',"1") #(1=normal, 2=circular, 3=unrooted) itol_exporter.set_export_param_value('line_width',"1") #itol_exporter.set_export_param_value('align_labels',"1") print(('datasets_visible',",".join(datasets_list))) itol_exporter.set_export_param_value('datasets_visible',",".join(datasets_list)) #epsfile = outpath + os.sep + rootname + '.itolnormal.eps' pdffile = outpath + os.sep + rootname + '.itol_linear.pdf' print('Exporting to pdffile %s'%(pdffile)) itol_exporter.export(pdffile) print(("Phylogenetic tree has been output to %s"%(pdffile)))
def Itol_Tree_m_sd3(pfamid, datapath, outpath):#{{{ """Phylogenetic tree with species definition the Kindom use branch colordefinition, and others using color strips """ tree = datapath + os.sep + pfamid + '.tree' t = Tree(tree) leaves = t.get_leaves() lst_leaves_name = [] for leaf in leaves: lst_leaves_name.append(leaf.name) numLeave = len(lst_leaves_name) leaves_name_set = set(lst_leaves_name) # read species definition speciesfile = "%s/%s.species"%(datapath, pfamid) speciesDict = myfunc.Read_species_sd(speciesfile) # create branch color definition file for kingdom lst_kingdom = ["Archaea","Bacteria", "Eukaryota" ] lst_color_kingdom = ["#ff0000", "#0066ff","#cc6600"] species_colordef_file = "%s/%s.kingdom.colordef.txt"%(outpath, pfamid) color_dict_kingdom = {} this_speciesDict = {} for seqid in speciesDict: speciesname = speciesDict[seqid][0] this_speciesDict[seqid] = speciesname for i in range(len(lst_kingdom)): idd = lst_kingdom[i] color_dict_kingdom[idd] = lst_color_kingdom[i] myfunc.WriteKingdomColorDefFile(species_colordef_file, this_speciesDict, leaves_name_set, color_dict_kingdom) # generate the next three levels of classification for level in [1,2,3]: outfile = "%s/%s.species.level_%d.txt"%(outpath, pfamid, level) this_speciesDict = {} speciesIDSet = set([]) for seqid in speciesDict: try: speciesname = speciesDict[seqid][level] speciesIDSet.add(speciesname) this_speciesDict[seqid] = speciesname except IndexError as KeyError: pass color_dict = {} lst_color = list(blue.range_to(red,len(speciesIDSet))) lst_speciesID = list(speciesIDSet) for i in range(len(lst_speciesID)): idd = lst_speciesID[i] color_dict[idd] = lst_color[i].get_hex_l() myfunc.WriteSpeciesColorStripDefFile(outfile, this_speciesDict, leaves_name_set, color_dict) #Create the Itol class itl = Itol() #Set the tree file (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "") if not os.path.exists(tree): print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr) return 1 fontsize = GetFontSize(numLeave) datafile1 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 1) datafile2 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 2) datafile3 = "%s/%s.species.level_%d.txt"%(outpath, pfamid, 3) colordeffile = species_colordef_file if os.path.exists(colordeffile): itl.add_variable('colorDefinitionFile', colordeffile) itl.add_variable('colorDefinitionLabel', "Kingdom") #=================================== itl.add_variable('treeFile',tree) itl.add_variable('treeName','SD3') itl.add_variable('treeFormat','newick') #=================================== if os.path.exists(datafile1): itl.add_variable('datafile1File',datafile1) itl.add_variable('datafile1Label','Phylum') itl.add_variable('datafile1Separator','comma') itl.add_variable('datafile1Type','colorstrip') itl.add_variable('datafile1StripWidth','100') itl.add_variable('datafile1ColoringType','both') itl.add_variable('datafile1PreventOverlap','1') #=================================== if os.path.exists(datafile2): itl.add_variable('datafile2File',datafile2) itl.add_variable('datafile2Label','Class') itl.add_variable('datafile2Separator','comma') itl.add_variable('datafile2Type','colorstrip') itl.add_variable('datafile2StripWidth','100') itl.add_variable('datafile2ColoringType','both') itl.add_variable('datafile2PreventOverlap','1') #=================================== if os.path.exists(datafile3): itl.add_variable('datafile3File',datafile3) itl.add_variable('datafile3Label','Order') itl.add_variable('datafile3Separator','comma') itl.add_variable('datafile3Type','colorstrip') itl.add_variable('datafile3StripWidth','100') itl.add_variable('datafile3ColoringType','both') itl.add_variable('datafile3PreventOverlap','1') #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') print('Uploading the tree. This may take some time depending on how large the tree is and how much load there is on the itol server') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) sys.exit(1) #Read the tree ID print('Tree ID: '+str(itl.comm.tree_id)) #Read the iTOL API return statement print('iTOL output: '+str(itl.comm.upload_output)) #Website to be redirected to iTOL tree print('Tree Web Page URL: '+itl.get_webpage()) # Warnings associated with the upload print('Warnings: '+str(itl.comm.warnings)) #Export to pdf itol_exporter = itl.get_itol_export() #itol_exporter = itolexport.ItolExport() #itol_exporter.set_export_param_value('tree','18793532031912684633930') itol_exporter.set_export_param_value('format', 'eps') itol_exporter.set_export_param_value('display_mode',"2") itol_exporter.set_export_param_value('current_font_size',fontsize) itol_exporter.set_export_param_value('align_labels',"1") itol_exporter.set_export_param_value('datafileList','dataset1') extname = "-itol-sd3" epsfile = outpath + os.sep + pfamid + extname + '.eps' pdffile = outpath + os.sep + pfamid + extname + '.pdf' jpgfile = outpath + os.sep + pfamid + extname + '.jpg' pngfile = outpath + os.sep + pfamid + extname + '.png' thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg' itol_exporter.export(epsfile) os.system("epstopdf %s" % epsfile ) os.system("convert %s %s" % (epsfile, jpgfile) ) os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile)) print('exported tree to ',pdffile)
def Itol_Tree_m_sd2(pfamid, datapath, outpath):#{{{ tree = datapath + os.sep + pfamid + '.tree' t = Tree(tree) leaves = t.get_leaves() lst_leaves_name = [] for leaf in leaves: lst_leaves_name.append(leaf.name) numLeave = len(lst_leaves_name) leaves_name_set = set(lst_leaves_name) # read seqlen file seqlenfile = "%s/%s.seqlen.txt"%(datapath, pfamid) seqlen_dict = myfunc.ReadSeqLengthDict(seqlenfile) # read subfamily definition domain_idlist = [] domainfile = "%s/%s.mdp"%(datapath, pfamid) domain_dict = myfunc.Read_domain_sd(domainfile, domain_idlist) domain_colordef_file = "%s/%s.mdp.colordef.txt"%(datapath, pfamid) WriteDomainColorDefFile(domain_colordef_file, domain_dict, domain_idlist, seqlen_dict, leaves_name_set) #Create the Itol class itl = Itol() #Set the tree file (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "") if not os.path.exists(tree): print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr) return 1 fontsize = GetFontSize(numLeave) datafile1 = domain_colordef_file # colordeffile = subfam_colordef_file # if os.path.exists(colordeffile): # itl.add_variable('colorDefinitionFile', colordeffile) # itl.add_variable('colorDefinitionLabel', "Subfamilies") #=================================== itl.add_variable('treeFile',tree) itl.add_variable('treeName','SD2') itl.add_variable('treeFormat','newick') #=================================== if os.path.exists(datafile1): itl.add_variable('datafile1File',datafile1) itl.add_variable('datafile1Label','Domain architecture') itl.add_variable('datafile1Separator','comma') itl.add_variable('datafile1Type','domains') itl.add_variable('datafile1ProtSizeMax','1000') itl.add_variable('datafile1PreventOverlap','1') itl.add_variable('datafile1CirclesSpacing','100') #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') print('Uploading the tree. This may take some time depending on how large the tree is and how much load there is on the itol server') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) sys.exit(1) #Read the tree ID print('Tree ID: '+str(itl.comm.tree_id)) #Read the iTOL API return statement print('iTOL output: '+str(itl.comm.upload_output)) #Website to be redirected to iTOL tree print('Tree Web Page URL: '+itl.get_webpage()) # Warnings associated with the upload print('Warnings: '+str(itl.comm.warnings)) #Export to pdf itol_exporter = itl.get_itol_export() #itol_exporter = itolexport.ItolExport() #itol_exporter.set_export_param_value('tree','18793532031912684633930') itol_exporter.set_export_param_value('format', 'eps') itol_exporter.set_export_param_value('display_mode',"2") itol_exporter.set_export_param_value('current_font_size',fontsize) itol_exporter.set_export_param_value('align_labels',"1") itol_exporter.set_export_param_value('datafileList','dataset1') extname = "-itol-sd2" epsfile = outpath + os.sep + pfamid + extname + '.eps' pdffile = outpath + os.sep + pfamid + extname + '.pdf' jpgfile = outpath + os.sep + pfamid + extname + '.jpg' pngfile = outpath + os.sep + pfamid + extname + '.png' thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg' itol_exporter.export(epsfile) os.system("epstopdf %s" % epsfile ) os.system("convert %s %s" % (epsfile, jpgfile) ) os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile)) print('exported tree to ',pdffile)
def Itol_Tree_m_sd1(pfamid, datapath, outpath):#{{{ """Phylogenetic tree with numTM_io and subfamilies branch coloring """ tree = datapath + os.sep + pfamid + '.tree' t = Tree(tree) leaves = t.get_leaves() lst_leaves_name = [] for leaf in leaves: lst_leaves_name.append(leaf.name) numLeave = len(lst_leaves_name) # read subfamily definition subfamfile = "%s/%s.subfamilies"%(datapath, pfamid) subfam_idlist = [] subfamDict = myfunc.Read_subfamily(subfamfile, subfam_idlist) numSubFam = len(subfam_idlist) # create subfamily branch color definition file subfam_colordef_file = "%s/%s.subfamilies.colordef.txt"%(outpath, pfamid) lst_color = list(blue.range_to(red,numSubFam)) color_dict = {} for i in range(numSubFam): famid = subfam_idlist[i] color = lst_color[i].get_hex_l() color_dict[famid] = lst_color[i].get_hex_l() myfunc.WriteSubFamColorDef(subfam_colordef_file, subfamDict, lst_leaves_name, color_dict) #Create the Itol class itl = Itol() #Set the tree file (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "") if not os.path.exists(tree): print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr) return 1 fontsize = GetFontSize(numLeave) datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt' colordeffile = subfam_colordef_file if os.path.exists(colordeffile): itl.add_variable('colorDefinitionFile', colordeffile) itl.add_variable('colorDefinitionLabel', "Subfamilies") #=================================== itl.add_variable('treeFile',tree) itl.add_variable('treeName','SD1') itl.add_variable('treeFormat','newick') #=================================== if os.path.exists(datafile1): itl.add_variable('datafile1File',datafile1) itl.add_variable('datafile1Label','numTM_and_io') itl.add_variable('datafile1Separator','comma') itl.add_variable('datafile1Type','multibar') itl.add_variable('datafile1PreventOverlap','1') itl.add_variable('datafile1Color','#FF0000') #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') print('Uploading the tree. This may take some time depending on how large the tree is and how much load there is on the itol server') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) sys.exit(1) #Read the tree ID print('Tree ID: '+str(itl.comm.tree_id)) #Read the iTOL API return statement print('iTOL output: '+str(itl.comm.upload_output)) #Website to be redirected to iTOL tree print('Tree Web Page URL: '+itl.get_webpage()) # Warnings associated with the upload print('Warnings: '+str(itl.comm.warnings)) #Export to pdf itol_exporter = itl.get_itol_export() #itol_exporter = itolexport.ItolExport() #itol_exporter.set_export_param_value('tree','18793532031912684633930') itol_exporter.set_export_param_value('format', 'eps') itol_exporter.set_export_param_value('display_mode',"2") itol_exporter.set_export_param_value('current_font_size',fontsize) itol_exporter.set_export_param_value('align_labels',"1") itol_exporter.set_export_param_value('datafileList','dataset1') extname = "-itol-sd1" epsfile = outpath + os.sep + pfamid + extname + '.eps' pdffile = outpath + os.sep + pfamid + extname + '.pdf' jpgfile = outpath + os.sep + pfamid + extname + '.jpg' pngfile = outpath + os.sep + pfamid + extname + '.png' thumbfile = outpath + os.sep + "thumb." + pfamid + extname + '.jpg' itol_exporter.export(epsfile) os.system("epstopdf %s" % epsfile ) os.system("convert %s %s" % (epsfile, jpgfile) ) os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile)) print('exported tree to ',pdffile)
def Itol_Tree_m1(pfamid, datapath, outpath):#{{{ # TM helices are treated as domains #Create the Itol class itl = Itol() #Set the tree file tree = datapath + os.sep + pfamid + '.tree' (datafile1, datafile2, datafile3, datafile4) = ("", "", "", "") if not os.path.exists(tree): print("tree file %s does not exist. Ignore" %(tree), file=sys.stderr) return 1 t = Tree(tree) leaves = t.get_leaves() numLeave = len(leaves) fontsize = GetFontSize(numLeave) datafile1 = datapath + os.sep + pfamid + '.numTM_and_io.txt' datafile2 = datapath + os.sep + pfamid + '.cmpclass.colordef.txt' # datafile3 = datapath + os.sep + pfamid + '.ntermstate.colordef.txt' datafile4 = datapath + os.sep + pfamid + '.cluster.colordef.txt' colordeffile = datapath + os.sep + pfamid + '.pfam.colordef.txt' branchlabelfile = datapath + os.sep + pfamid + '.branchlabel.txt' #=================================== itl.add_variable('treeFile',tree) itl.add_variable('treeName','PF00854') itl.add_variable('treeFormat','newick') if os.path.exists(colordeffile): itl.add_variable('colorDefinitionFile', colordeffile) if os.path.exists(branchlabelfile): itl.add_variable('branchLabelsFile', branchlabelfile) #=================================== if os.path.exists(datafile1): itl.add_variable('datafile1File',datafile1) itl.add_variable('datafile1Label','numTM_and_io') itl.add_variable('datafile1Separator','comma') itl.add_variable('datafile1Type','multibar') itl.add_variable('datafile1PreventOverlap','1') itl.add_variable('datafile1Color','#FF0000') #=================================== if os.path.exists(datafile2): itl.add_variable('datafile2File', datafile2) itl.add_variable('datafile2Label', 'cmpclass') itl.add_variable('datafile2Separator','comma') itl.add_variable('datafile2Type','colorstrip') itl.add_variable('datafile2StripWidth','200') itl.add_variable('datafile2PreventOverlap','1') itl.add_variable('datafile2ColoringType','both') #=================================== if os.path.exists(datafile3): itl.add_variable('datafile3File', datafile3) itl.add_variable('datafile3Label', 'NtermState') itl.add_variable('datafile3Separator','comma') itl.add_variable('datafile3Type','colorstrip') itl.add_variable('datafile3StripWidth','200') itl.add_variable('datafile3PreventOverlap','1') itl.add_variable('datafile3ColoringType','both') #=================================== if os.path.exists(datafile4): itl.add_variable('datafile4File', datafile4) itl.add_variable('datafile4Label', 'cluster') itl.add_variable('datafile4Separator','comma') itl.add_variable('datafile4Type','colorstrip') itl.add_variable('datafile4StripWidth','200') itl.add_variable('datafile4PreventOverlap','1') itl.add_variable('datafile4ColoringType','both') itl.add_variable('datafile4BranchColoringType','both') #itl.add_variable('datafile1BarSizeMax','1') #=================================== # Check parameters # itl.print_variables() #Submit the tree print('') print('Uploading the tree. This may take some time depending on how large the tree is and how much load there is on the itol server') good_upload = itl.upload() if good_upload == False: print('There was an error:'+itl.comm.upload_output) sys.exit(1) #Read the tree ID print('Tree ID: '+str(itl.comm.tree_id)) #Read the iTOL API return statement print('iTOL output: '+str(itl.comm.upload_output)) #Website to be redirected to iTOL tree print('Tree Web Page URL: '+itl.get_webpage()) # Warnings associated with the upload print('Warnings: '+str(itl.comm.warnings)) #Export to pdf print('Exporting to pdf') itol_exporter = itl.get_itol_export() #itol_exporter = itolexport.ItolExport() #itol_exporter.set_export_param_value('tree','18793532031912684633930') itol_exporter.set_export_param_value('format', 'eps') itol_exporter.set_export_param_value('display_mode',"2") itol_exporter.set_export_param_value('current_font_size',fontsize) itol_exporter.set_export_param_value('align_labels',"1") itol_exporter.set_export_param_value('datafileList','dataset1') epsfile = outpath + os.sep + pfamid + '-itol.eps' pdffile = outpath + os.sep + pfamid + '-itol.pdf' jpgfile = outpath + os.sep + pfamid + '-itol.jpg' thumbfile = outpath + os.sep + "thumb." + pfamid + '-itol.jpg' itol_exporter.export(epsfile) os.system("epstopdf %s" % epsfile ) os.system("convert %s %s" % (epsfile, jpgfile) ) os.system("convert -thumbnail 200 %s %s" % (jpgfile, thumbfile)) print('exported tree to ',pdffile)
# Submit the tree print('') print( ('Uploading the tree. This may take some time depending on how large the ' 'tree is and how much load there is on the itol server')) good_upload = boom.upload() if not good_upload: print('There was an error:' + boom.comm.upload_output) sys.exit(1) # Read the tree ID print('Tree ID: ' + str(boom.comm.tree_id)) # Read the iTOL API return statement print('iTOL output: ' + str(boom.comm.upload_output)) # Website to be redirected to iTOL tree print('Tree Web Page URL: ' + boom.get_webpage()) # Warnings associated with the upload print('Warnings: ' + str(boom.comm.warnings)) # Export the tree to pdf print('Exporting to pdf') itol_exporter = boom.get_itol_export() export_location = os.path.join(current_dir, 'MERS_CoV.pdf') itol_exporter.set_export_param_value('format', 'pdf') itol_exporter.set_export_param_value('datasetList', 'dataset1') itol_exporter.export(export_location) print('exported tree to ', export_location)