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_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_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_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)