def mylayout(node): # If node is a leaf if node.is_leaf(): # And a line profile add_face_to_node(profileFace, node, 0, aligned=True) node.img_style["size"]=0 add_face_to_node(nameFace, node, 1, aligned=True) # If node is internal else: # If silhouette is good, creates a green bubble if node.silhouette>0: validationFace = TextFace("Silh=%0.2f" %node.silhouette, "Verdana", 10, "#056600") node.img_style["fgcolor"]="#056600" # Otherwise, use red bubbles else: validationFace = TextFace("Silh=%0.2f" %node.silhouette, "Verdana", 10, "#940000") node.img_style["fgcolor"]="#940000" # Sets node size proportional to the silhouette value. node.img_style["shape"]="sphere" if node.silhouette<=1 and node.silhouette>=-1: node.img_style["size"]= 15+int((abs(node.silhouette)*10)**2) # If node is very internal, draw also a bar diagram # with the average expression of the partition add_face_to_node(validationFace, node, 0) if len(node)>100: add_face_to_node(cbarsFace, node, 1)
def __init__(self, tree, alg_type=None, ncbitaxa=True): self.tree = tree self.tree.dist = 0 self.svg = None self.layouts = None self.treestyle = TreeStyle() self.alg_type = alg_type ts = self.treestyle ts.draw_aligned_faces_as_table = False ts.draw_guiding_lines = False ts.show_leaf_name = False ts.show_branch_support = False ts.tree_width = 250 ts.layout_fn = [self.ly_basic, self.ly_leaf_names, self.ly_supports] self.ncbitaxa = ncbitaxa if ncbitaxa: ts.layout_fn.append(self.ly_tax_labels) if self.alg_type == 'condensed': ts.layout_fn.append(self.ly_condensed_alg) elif self.alg_type == 'block': ts.layout_fn.append(self.ly_block_alg) self.TRACKED_CLADES = set() if ncbitaxa: tree.set_species_naming_function(spname) annotate_tree_with_ncbi(tree) for lf in tree: for lname in getattr(lf, 'named_lineage', [])[2:9]: self.TRACKED_CLADES.add(lname.lower()) self.TRACKED_CLADES.update(map(lower, ["Eukaryota", "Viridiplantae", "Fungi", "birds", "Basidiomycota", "Ascomycota", "Alveolata", "Metazoa", "Stramenopiles", "Rhodophyta", "mammalia", "Primates", "Amoebozoa", "Crypthophyta", "Bacteria","Archaea", "Arthropoda", "rodentia", "laurastheria", "Alphaproteobacteria", "Betaproteobacteria", "Cyanobacteria", "Gammaproteobacteria",])) colors = random_color(num=len(self.TRACKED_CLADES), s=0.45) self.LIN2COLOR = dict([(ln, colors[i]) for i, ln in enumerate(sorted(self.TRACKED_CLADES))]) self.LIN2FACE = {} for tname in self.TRACKED_CLADES: linF = TextFace(tname, fsize=10, fgcolor='white', tight_text=False, ftype="Arial") linF.margin_left = 3 linF.margin_right = 2 linF.inner_background.color = self.LIN2COLOR[tname.lower()] self.LIN2FACE[tname.lower()] = linF self.LABEL_START_COL = 100 self.ALG_START_COL = len(self.TRACKED_CLADES)+11 #tree.ladderize() self.svg, self.img_map = tree.render("%%return", tree_style=ts)
def rotation_layout(node): if node.is_leaf(): F = TextFace(node.name, tight_text=True) F.rotation = randint(0, 360) add_face_to_node(TextFace("third" ), node, column=8, position="branch-right") add_face_to_node(TextFace("second" ), node, column=2, position="branch-right") add_face_to_node(F, node, column=0, position="branch-right") F.border.width = 1 F.inner_border.width = 1
def ly_leaf_names(self, node): if node.is_leaf(): if node.species != 0 and self.ncbitaxa: spF = TextFace("%s" %getattr(node, "spname", ""), fsize=8, fgcolor='#777777', ftype='Helvetica') geneF = TextFace("(%s)" %node.name[:50], fsize=10, fgcolor='#444444', fstyle='italic', ftype='Helvetica') add_face_to_node(spF, node, column=0, position='branch-right') add_face_to_node(geneF, node, column=1, position='branch-right') else: geneF = TextFace("%s" %node.name, fsize=8, fgcolor='#777777', ftype='Helvetica') add_face_to_node(geneF, node, column=0, position='branch-right')
def ly_tax_labels(node): if node.is_leaf(): c = LABEL_START_COL largest = 0 for tname in TRACKED_CLADES: if hasattr(node, "named_lineage") and tname in node.named_lineage: linF = TextFace(tname, fsize=10, fgcolor='white') linF.margin_left = 3 linF.margin_right = 2 linF.background.color = lin2color[tname] add_face_to_node(linF, node, c, position='aligned') c += 1 for n in xrange(c, len(TRACKED_CLADES)): add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned') c+=1
def ly_tax_labels(self, node): if node.is_leaf(): c = self.LABEL_START_COL node_lin = getattr(node, "named_lineage", []) for tname in node_lin: if tname.lower() in self.TRACKED_CLADES: linF = self.LIN2FACE[tname.lower()] # linF = TextFace(tname, fsize=10, fgcolor='white') # linF.margin_left = 3 # linF.margin_right = 2 # linF.background.color = self.LIN2COLOR[tname.lower()] add_face_to_node(linF, node, c, position='aligned') c += 1 for n in xrange(c, len(self.TRACKED_CLADES)): add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned') c+=1
def get_example_tree(): t = Tree("((a,b),c);") right_c0_r0 = TextFace("right_col0_row0") right_c0_r1 = TextFace("right_col0_row1") right_c1_r0 = TextFace("right_col1_row0") right_c1_r1 = TextFace("right_col1_row1") right_c1_r2 = TextFace("right_col1_row2") top_c0_r0 = TextFace("top_col0_row0") top_c0_r1 = TextFace("top_col0_row1") bottom_c0_r0 = TextFace("bottom_col0_row0") bottom_c1_r0 = TextFace("bottom_col1_row0") aligned_c0_r0 = TextFace("aligned_col0_row0") aligned_c0_r1 = TextFace("aligned_col0_row1") aligned_c1_r0 = TextFace("aligned_col1_row0") aligned_c1_r1 = TextFace("aligned_col1_row1") t.add_face(right_c0_r1, column=1, position="branch-right") t.add_face(right_c0_r0, column=0, position="branch-right") t.add_face(right_c1_r2, column=2, position="branch-right") t.add_face(right_c1_r1, column=1, position="branch-right") t.add_face(right_c1_r0, column=0, position="branch-right") t.add_face(top_c0_r1, column=1, position="branch-top") t.add_face(top_c0_r0, column=0, position="branch-top") t.add_face(bottom_c0_r0, column=0, position="branch-bottom") t.add_face(bottom_c1_r0, column=1, position="branch-bottom") for leaf in t.iter_leaves(): leaf.add_face(aligned_c0_r1, 0, "aligned") leaf.add_face(aligned_c0_r0, 0, "aligned") leaf.add_face(aligned_c1_r1, 0, "aligned") leaf.add_face(aligned_c1_r0, 0, "aligned") return t, TreeStyle()
TTG GGA CTG GTC GGC TTC AAG GCA GTA AGT CAA TTC GTA ATA CGT CGT GCG >Chimp CAC GCC CGA TGG CTC AAC GAA AAG TTA AGA TGC GAA TTG AGA ACT CTG AAA AAA TTG GGA CTG GAC GGC TAC AAG GCA GTA AGT CAG TAC GTT AAA GGT CGT GCG >Orangutan GAT GCA CGC TGG ATC AAC GAA AAG TTA AGA TGC GTA TCG AGA ACT CTG AAA AAA TTG GGA CTG GAC GGC TAC AAG GGA GTA AGT CAA TAC GTT AAA GGT CGT CCG """) #try: # tree.run_model("fb") # tree.run_model("M2") #except: # pass tree.dist = 0 ts = TreeStyle() ts.title.add_face(TextFace( "Example for EvolTree, interactivity shows codons", fsize=15), column=0) ts.layout_fn = test_layout_evol #try: # tree.show(tree_style=ts, histfaces=["M2"]) #except: tree.show(tree_style=ts) except: tree = PhyloTree('(Orangutan,Human,Chimp);') tree.link_to_alignment(""" >Chimp HARWLNEKLRCELRTLKKLGLDGYKAVSQYVKGRA >Orangutan DARWINEKLRCVSRTLKKLGLDGYKGVSQYVKGRP >Human DARWHNVKLRCELRTLKKLGLVGFKAVSQFVIRRA
def ly_supports(self, node): if not node.is_leaf(): supFace = TextFace("%0.3g" %(node.support), fsize=7, fgcolor='indianred') add_face_to_node(supFace, node, column=0, position='branch-top')
def ly(node): node.img_style['vt_line_width'] = 1 node.img_style['hz_line_width'] = 1 if node.is_leaf(): add_face_to_node(TextFace( ' (%s)' % node.name.split()[0].replace("/exon2", ""), fsize=10, fgcolor='slategrey', tight_text=False), node, 1, position='branch-right') add_face_to_node(TextFace(node.species, fsize=12, fgcolor='black', fstyle='italic', tight_text=False), node, 0, position='branch-right') c = 1 for tname in tracked_clades: if tname in node.named_lineage: linF = TextFace(tname, fsize=10, fgcolor='white') linF.margin_left = 3 linF.background.color = lin2color[tname] add_face_to_node(linF, node, c, position='aligned') c += 1 for n in xrange(1, 20 - (c - 1)): add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned') c += 1 if draw_alg and 'sequence' in node.features: #seqFace = SequenceFace(node.sequence,"aa",13) seqFace = SeqMotifFace(node.sequence, []) # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"], motifs = [] last_lt = None for c, lt in enumerate(node.sequence): if lt != '-': if last_lt is None: last_lt = c if c + 1 == len(node.sequence): start, end = last_lt, c w = end - start motifs.append([ start, end, "[]", w, 13, "slategrey", "slategrey", None ]) last_lt = None elif lt == '-': if last_lt is not None: start, end = last_lt, c - 1 w = end - start motifs.append([ start, end, "[]", w, 13, "slategrey", "slategrey", None ]) last_lt = None if not motifs: print node, node.sequence seqFace = SeqMotifFace(node.sequence, motifs, intermotif_format="line", seqtail_format="line", scale_factor=1) add_face_to_node(seqFace, node, 20, aligned=True) else: if node.up: add_face_to_node(TextFace('% 3g' % node.support, fsize=11, fgcolor='indianred'), node, 0, position='branch-top') if hasattr(node, "support2") and node.up: add_face_to_node(TextFace('% 3g' % float(node.support2), fsize=11, fgcolor='steelblue'), node, 0, position='branch-bottom') node.img_style['size'] = 0 node.img_style['hz_line_color'] = 'black' node.img_style['vt_line_color'] = 'black'
def ly(node): node.img_style['vt_line_width'] = 1 node.img_style['hz_line_width'] = 1 if node.is_leaf(): add_face_to_node(TextFace(' (%s)' %node.name.split()[0].replace("/exon2", ""), fsize=10, fgcolor='slategrey', tight_text=False), node, 1, position='branch-right') add_face_to_node(TextFace(node.species, fsize=12, fgcolor='black', fstyle='italic', tight_text=False), node, 0, position='branch-right') c = 1 for tname in tracked_clades: if tname in node.named_lineage: linF = TextFace(tname, fsize=10, fgcolor='white') linF.margin_left = 3 linF.background.color = lin2color[tname] add_face_to_node(linF, node, c, position='aligned') c += 1 for n in xrange(1, 20-(c-1)): add_face_to_node(TextFace('', fsize=10, fgcolor='slategrey'), node, c, position='aligned') c+=1 if draw_alg and 'sequence' in node.features: #seqFace = SequenceFace(node.sequence,"aa",13) seqFace = SeqMotifFace(node.sequence, []) # [10, 100, "[]", None, 10, "black", "rgradient:blue", "arial|8|white|domain Name"], motifs = [] last_lt = None for c, lt in enumerate(node.sequence): if lt != '-': if last_lt is None: last_lt = c if c+1 == len(node.sequence): start, end = last_lt, c w = end-start motifs.append([start, end, "[]", w, 13, "slategrey", "slategrey", None]) last_lt = None elif lt == '-': if last_lt is not None: start, end = last_lt, c-1 w = end-start motifs.append([start, end, "[]", w, 13, "slategrey", "slategrey", None]) last_lt = None if not motifs: print node, node.sequence seqFace = SeqMotifFace(node.sequence, motifs, intermotif_format="line", seqtail_format="line", scale_factor=1) add_face_to_node(seqFace, node, 20, aligned=True) else: if node.up: add_face_to_node(TextFace('% 3g' %node.support, fsize=11, fgcolor='indianred'), node, 0, position='branch-top') if hasattr(node, "support2") and node.up: add_face_to_node(TextFace('% 3g' %float(node.support2), fsize=11, fgcolor='steelblue'), node, 0, position='branch-bottom') node.img_style['size'] = 0 node.img_style['hz_line_color'] = 'black' node.img_style['vt_line_color'] = 'black'
>Human DARWHNVKLRCELRTLKKLGLVGFKAVSQFVIRRA """) nt_sequences = { "Human": "GACGCACGGTGGCACAACGTAAAATTAAGATGTGAATTGAGAACTCTGAAAAAATTGGGACTGGTCGGCTTCAAGGCAGTAAGTCAATTCGTAATACGTCGTGCG", "Chimp": "CACGCCCGATGGCTCAACGAAAAGTTAAGATGCGAATTGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGCAGTAAGTCAGTACGTTAAAGGTCGTGCG", "Orangutan": "GATGCACGCTGGATCAACGAAAAGTTAAGATGCGTATCGAGAACTCTGAAAAAATTGGGACTGGACGGCTACAAGGGAGTAAGTCAATACGTTAAAGGTCGTCCG" } for l in nt_sequences: (tree & l).nt_sequence = nt_sequences[l] tree.dist = 0 ts = TreeStyle() ts.title.add_face(TextFace("Example for nucleotides...", fsize=15), column=0) ts.layout_fn = test_layout_evol tree.show(tree_style=ts) # Show very large algs tree = PhyloTree('(Orangutan,Human,Chimp);') tree.link_to_alignment(">Human\n" + ''.join([_aabgcolors.keys()[int(random() * len (_aabgcolors))] for _ in xrange (5000)]) + \ "\n>Chimp\n" + ''.join([_aabgcolors.keys()[int(random() * len (_aabgcolors))] for _ in xrange (5000)]) + \ "\n>Orangutan\n" + ''.join([_aabgcolors.keys()[int(random() * len (_aabgcolors))] for _ in xrange (5000)])) tree.dist = 0 ts = TreeStyle() ts.title.add_face(TextFace( "better not set interactivity if alg is very large", fsize=15), column=0) ts.layout_fn = test_layout_phylo_aa