def plot_phylo(nw_tree, out_name, parenthesis_classif=True, show_support=False, radial_mode=False, root=False): from ete3 import Tree, AttrFace, TreeStyle, NodeStyle, TextFace import orthogroup2phylogeny_best_refseq_uniprot_hity ete2_tree = Tree(nw_tree, format=0) if root: R = ete2_tree.get_midpoint_outgroup() # and set it as tree outgroup ete2_tree.set_outgroup(R) ete2_tree.set_outgroup('Bacillus subtilis') ete2_tree.ladderize() if parenthesis_classif: print('parenthesis_classif!') name2classif = {} for lf in ete2_tree.iter_leaves(): print(lf) try: classif = lf.name.split('_')[-2][0:-1] print('classif', classif) #lf.name = lf.name.split('(')[0] name2classif[lf.name] = classif except: pass classif_list = list(set(name2classif.values())) classif2col = dict( zip( classif_list, orthogroup2phylogeny_best_refseq_uniprot_hity. get_spaced_colors(len(classif_list)))) for lf in ete2_tree.iter_leaves(): #try: if parenthesis_classif: try: col = classif2col[name2classif[lf.name]] except: col = 'black' else: col = 'black' #print col #lf.name = '%s|%s-%s' % (lf.name, accession2name_and_phylum[lf.name][0],accession2name_and_phylum[lf.name][1]) if radial_mode: ff = AttrFace("name", fsize=12, fstyle='italic') else: ff = AttrFace("name", fsize=12, fstyle='italic') #ff.background.color = 'red' ff.fgcolor = col lf.add_face(ff, column=0) if not show_support: print('support') for n in ete2_tree.traverse(): print(n.support) nstyle = NodeStyle() if float(n.support) < 1: nstyle["fgcolor"] = "red" nstyle["size"] = 4 n.set_style(nstyle) else: nstyle["fgcolor"] = "red" nstyle["size"] = 0 n.set_style(nstyle) else: for n in ete2_tree.traverse(): nstyle = NodeStyle() nstyle["fgcolor"] = "red" nstyle["size"] = 0 n.set_style(nstyle) #nameFace = AttrFace(lf.name, fsize=30, fgcolor=phylum2col[accession2name_and_phylum[lf.name][1]]) #faces.add_face_to_node(nameFace, lf, 0, position="branch-right") # #nameFace.border.width = 1 ''' except: col = 'red' print col lf.name = '%s| %s' % (lf.name, locus2organism[lf.name]) ff = AttrFace("name", fsize=12) #ff.background.color = 'red' ff.fgcolor = col lf.add_face(ff, column=0) ''' #n = TextFace(lf.name, fgcolor = "black", fsize = 12, fstyle = 'italic') #lf.add_face(n, 0) ''' for n in ete2_tree.traverse(): nstyle = NodeStyle() if n.support < 90: nstyle["fgcolor"] = "black" nstyle["size"] = 4 n.set_style(nstyle) else: nstyle["fgcolor"] = "red" nstyle["size"] = 0 n.set_style(nstyle) ''' ts = TreeStyle() ts.show_leaf_name = False #ts.scale=2000 #ts.scale=20000 ts.show_branch_support = show_support if radial_mode: ts.mode = "c" ts.arc_start = -90 ts.arc_span = 360 ts.tree_width = 370 ts.complete_branch_lines_when_necessary = True ete2_tree.render(out_name, tree_style=ts, w=900)
def ete_layout(node): """ Formatting of tree nodes while tree is rendered :param node: ete node """ nstyle = NodeStyle() nstyle["shape"] = "sphere" nstyle["size"] = 0 nstyle["vt_line_width"] = 1 # line width of vertical branches nstyle["hz_line_width"] = 1 # line width of horizontal branches node.set_style(nstyle) if 'median_vaf' in node.features and not np.isnan( node.median_vaf) and logger.isEnabledFor(logging.DEBUG): # # Creates a sphere face whose size is proportional to the given VAF or CCF of the acquired muts # cf = CircleFace(radius=node.median_vaf*50, color="RoyalBlue", style="sphere") # # Let's make the sphere transparent # cf.opacity = 0.6 # # And place as a float face over the tree # faces.add_face_to_node(cf, node, column=0, position="float-behind") vaf_face = TextFace('({:.0%}) '.format(node.median_vaf), fsize=9, fgcolor='SlateGray') vaf_face.opacity = 0.8 # from 0 to 1 faces.add_face_to_node(vaf_face, node, column=2, position="branch-top") if node.is_root(): # tf_root = TextFace(node.name, fsize=14, fgcolor="Black") # tf_root.margin_bottom = 25 # faces.add_face_to_node(tf_root, node, column=0, position='branch-right') return bt_face_muts = AttrFace("dist", fsize=11, fgcolor="Black", text_prefix=' ', text_suffix=' ', formatter='%d') bt_face_muts.margin_bottom = 2 bt_face_muts.margin_top = 2 faces.add_face_to_node(bt_face_muts, node, column=1, position="branch-top") if 'drivers' in node.features and len(node.drivers) > 0: drivers_face = TextFace(node.drivers, fsize=11, fstyle='italic', fgcolor='OrangeRed') drivers_face.opacity = 0.8 # from 0 to 1 drivers_face.margin_left = 1 drivers_face.margin_right = 1 faces.add_face_to_node(drivers_face, node, column=0, position="branch-top") # If node is a leaf, add the nodes name and a its scientific name if node.is_leaf(): # for colors see: http://etetoolkit.org/docs/latest/reference/reference_treeview.html#color-names leaf_face = AttrFace("name", fsize=14, fgcolor="Black", text_prefix=' ', text_suffix=' ') if node.name.startswith('PT') or node.name.startswith( 'Primary'): # primary tumor sample leaf_face.fgcolor = 'SteelBlue' elif node.name.startswith('LiM'): # liver met leaf_face.fgcolor = 'DarkOliveGreen' elif node.name.startswith('LuM'): # lung met leaf_face.fgcolor = 'SaddleBrown' elif node.name.startswith('NoM'): # lymph node met leaf_face.fgcolor = 'DarkViolet' elif node.name.startswith('PeM'): # peritoneal met leaf_face.fgcolor = 'Purple' elif node.name.startswith('Met') or node.name.startswith( 'M'): # some metastasis leaf_face.fgcolor = 'Magenta' elif node.name.startswith('BrM'): # brain met leaf_face.fgcolor = 'Crimson' leaf_face.border.type = 0 leaf_face.border.width = 1 leaf_face.margin_bottom = 1 leaf_face.margin_top = 1 faces.add_face_to_node(leaf_face, node, column=1) # , position="aligned" else: # inner node # if not np.isnan(node.support): # bt_face_conf = AttrFace("support", fsize=12, fgcolor="DimGrey", text_prefix=' ', # text_suffix=' ', formatter='%0.2f') if node.confidence is not None and node.confidence != '': tf_conf = TextFace(node.confidence, fsize=12, fgcolor='DimGrey') tf_conf.margin_left = 3 tf_conf.margin_right = 3 faces.add_face_to_node(tf_conf, node, column=1, position="branch-bottom")
def plot_tree(ete3_tree, orthogroup, biodb, mysql_host="localhost", mysql_user="******", mysql_pwd="baba", mysql_db="blastnr"): import MySQLdb import manipulate_biosqldb from ete3 import Tree, TreeStyle, faces, AttrFace conn = MySQLdb.connect( host=mysql_host, # your host, usually localhost user=mysql_user, # your username passwd=mysql_pwd, # your password db=mysql_db) # name of the data base cursor = conn.cursor() locus_list = [lf.name for lf in ete3_tree.iter_leaves()] filter = '"' + '","'.join(locus_list) + '"' print('get uniprot taxnomy') sql1 = 'select subject_accession,subject_scientific_name,t2.phylum from blast_swissprot_%s t1 ' \ ' inner join blastnr_taxonomy as t2 on t1.subject_taxid=t2.taxon_id where subject_accession in (%s);' % (biodb, filter) sql1 = 'select subject_accession,subject_scientific_name,t4.phylum from biosqldb.orthology_detail_%s t1 ' \ ' inner join custom_tables.locus2seqfeature_id_%s t2 ' \ ' on t1.locus_tag=t2.locus_tag ' \ ' inner join blast_swissprot_%s t3 on t2.seqfeature_id=t3.seqfeature_id ' \ ' inner join blastnr_taxonomy as t4 on t3.subject_taxid=t4.taxon_id ' \ ' where t1.orthogroup="%s"' % (biodb, biodb, biodb, orthogroup) print('get refseq taxonomy') cursor.execute(sql1, ) accession2name_and_phylum = manipulate_biosqldb.to_dict(cursor.fetchall()) sql2 = 'select subject_accession,subject_scientific_name,t4.phylum from biosqldb.orthology_detail_%s t1 ' \ ' inner join custom_tables.locus2seqfeature_id_%s t2 ' \ ' on t1.locus_tag=t2.locus_tag ' \ ' inner join blastnr_%s t3 on t2.seqfeature_id=t3.seqfeature_id ' \ ' inner join blastnr_taxonomy as t4 on t3.subject_taxid=t4.taxon_id ' \ ' where t1.orthogroup="%s"' % (biodb, biodb, biodb, orthogroup) print(sql2) cursor.execute(sql2, ) accession2name_and_phylum.update( manipulate_biosqldb.to_dict(cursor.fetchall())) print('plotting tree') phylum_list = list( set([ accession2name_and_phylum[i][1] for i in accession2name_and_phylum.keys() ])) sql = 'select locus_tag, organism from biosqldb.orthology_detail_%s' % biodb cursor.execute(sql, ) locus2organism = manipulate_biosqldb.to_dict(cursor.fetchall()) phylum2col = dict(zip(phylum_list, get_spaced_colors(len(phylum_list)))) R = ete3_tree.get_midpoint_outgroup() # and set it as tree outgroup ete3_tree.set_outgroup(R) for lf in ete3_tree.iter_leaves(): try: col = phylum2col[accession2name_and_phylum[lf.name][1]] lf.name = '%s|%s-%s' % (lf.name, accession2name_and_phylum[lf.name][0], accession2name_and_phylum[lf.name][1]) ff = AttrFace("name", fsize=12) #ff.background.color = 'red' ff.fgcolor = col lf.add_face(ff, column=0) #nameFace = AttrFace(lf.name, fsize=30, fgcolor=phylum2col[accession2name_and_phylum[lf.name][1]]) #faces.add_face_to_node(nameFace, lf, 0, position="branch-right") # #nameFace.border.width = 1 except: col = 'red' try: lf.name = '%s| %s' % (lf.name, locus2organism[lf.name]) except: lf.name = '%s| ??' % (lf.name) ff = AttrFace("name", fsize=12) #ff.background.color = 'red' ff.fgcolor = col lf.add_face(ff, column=0) ts = TreeStyle() ts.show_leaf_name = False ts.show_branch_support = True return ete3_tree, ts