def get_example_tree(): # Set dashed blue lines in all leaves nst1 = NodeStyle() nst1["bgcolor"] = "LightSteelBlue" nst2 = NodeStyle() nst2["bgcolor"] = "Moccasin" nst3 = NodeStyle() nst3["bgcolor"] = "DarkSeaGreen" nst4 = NodeStyle() nst4["bgcolor"] = "Khaki" t = Tree("((((a1,a2),a3), ((b1,b2),(b3,b4))), ((c1,c2),c3));") for n in t.traverse(): n.dist = 0 n1 = t.get_common_ancestor("a1", "a2", "a3") n1.set_style(nst1) n2 = t.get_common_ancestor("b1", "b2", "b3", "b4") n2.set_style(nst2) n3 = t.get_common_ancestor("c1", "c2", "c3") n3.set_style(nst3) n4 = t.get_common_ancestor("b3", "b4") n4.set_style(nst4) ts = TreeStyle() ts.layout_fn = layout ts.show_leaf_name = False ts.mode = "c" ts.root_opening_factor = 1 return t, ts
def get_example_tree(): # Random tree t = Tree() t.populate(20, random_branches=True) # Some random features in all nodes for n in t.traverse(): n.add_features(weight=random.randint(0, 50)) # Create an empty TreeStyle ts = TreeStyle() # Set our custom layout function ts.layout_fn = layout # Draw a tree ts.mode = "c" # We will add node names manually ts.show_leaf_name = False # Show branch data ts.show_branch_length = True ts.show_branch_support = True return t, ts
# the search_nodes method (I take only the first match ) A = t.search_nodes(name="A")[0] # and using the shorcut to finding nodes by name C = t & "C" H = t & "H" I = t & "I" # Let's now add some custom features to our nodes. add_features can be # used to add many features at the same time. C.add_features(vowel=False, confidence=1.0) A.add_features(vowel=True, confidence=0.5) ancestor.add_features(nodetype="internal") # Or, using the oneliner notation (t & "H").add_features(vowel=False, confidence=0.2) # But we can automatize this. (note that i will overwrite the previous # values) for leaf in t.traverse(): if leaf.name in "AEIOU": leaf.add_features(vowel=True, confidence=random.random()) else: leaf.add_features(vowel=False, confidence=random.random()) # Now we use these information to analyze the tree. print "This tree has", len(t.search_nodes(vowel=True)), "vowel nodes" print "Which are", [ leaf.name for leaf in t.iter_leaves() if leaf.vowel == True ] # But features may refer to any kind of data, not only simple # values. For example, we can calculate some values and store them # within nodes. # # Let's detect leaf nodes under "ancestor" with distance higher thatn # 1. Note that I'm traversing a subtree which starts from "ancestor"
def get_example_tree(): t = Tree() t.populate(10) # Margins, alignment, border, background and opacity can now be set for any face rs1 = faces.TextFace("branch-right\nmargins&borders", fsize=12, fgcolor="#009000") rs1.margin_top = 10 rs1.margin_bottom = 50 rs1.margin_left = 40 rs1.margin_right = 40 rs1.border.width = 1 rs1.background.color = "lightgreen" rs1.inner_border.width = 0 rs1.inner_border.line_style = 1 rs1.inner_border.color = "red" rs1.opacity = 0.6 rs1.hz_align = 2 # 0 left, 1 center, 2 right rs1.vt_align = 1 # 0 left, 1 center, 2 right br1 = faces.TextFace("branch-right1", fsize=12, fgcolor="#009000") br2 = faces.TextFace("branch-right3", fsize=12, fgcolor="#009000") # New face positions (branch-top and branch-bottom) bb = faces.TextFace("branch-bottom 1", fsize=8, fgcolor="#909000") bb2 = faces.TextFace("branch-bottom 2", fsize=8, fgcolor="#909000") bt = faces.TextFace("branch-top 1", fsize=6, fgcolor="#099000") # And faces can also be used as headers or foot notes of aligned # columns t1 = faces.TextFace("Header Face", fsize=12, fgcolor="#aa0000") t2 = faces.TextFace("Footer Face", fsize=12, fgcolor="#0000aa") # Attribute faces can now contain prefix and suffix fixed text aligned = faces.AttrFace("name", fsize=12, fgcolor="RoyalBlue", text_prefix="Aligned (", text_suffix=")") # horizontal and vertical alignment per face aligned.hz_align = 1 # 0 left, 1 center, 2 right aligned.vt_align = 1 # Node style handling is no longer limited to layout functions. You # can now create fixed node styles and use them many times, save them # or even add them to nodes before drawing (this allows to save and # reproduce an tree image design) style = NodeStyle() style["fgcolor"] = "Gold" style["shape"] = "square" style["size"] = 15 style["vt_line_color"] = "#ff0000" t.set_style(style) # add a face to the style. This face will be render in any node # associated to the style. fixed = faces.TextFace("FIXED branch-right", fsize=11, fgcolor="blue") t.add_face(fixed, column=1, position="branch-right") # Bind the precomputed style to the root node # ETE 2.1 has now support for general image properties ts = TreeStyle() # You can add faces to the tree image (without any node # associated). They will be used as headers and foot notes of the # aligned columns (aligned faces) ts.aligned_header.add_face(t1, column=0) ts.aligned_header.add_face(t1, 1) ts.aligned_header.add_face(t1, 2) ts.aligned_header.add_face(t1, 3) t1.hz_align = 1 # 0 left, 1 center, 2 right t1.border.width = 1 ts.aligned_foot.add_face(t2, column=0) ts.aligned_foot.add_face(t2, 1) ts.aligned_foot.add_face(t2, 2) ts.aligned_foot.add_face(t2, 3) t2.hz_align = 1 # Set tree image style. Note that aligned header and foot is only # visible in "rect" mode. ts.mode = "r" ts.scale = 10 for node in t.traverse(): # If node is a leaf, add the nodes name and a its scientific # name if node.is_leaf(): node.add_face(aligned, column=0, position="aligned") node.add_face(aligned, column=1, position="aligned") node.add_face(aligned, column=3, position="aligned") else: node.add_face(bt, column=0, position="branch-top") node.add_face(bb, column=0, position="branch-bottom") node.add_face(bb2, column=0, position="branch-bottom") node.add_face(br1, column=0, position="branch-right") node.add_face(rs1, column=0, position="branch-right") node.add_face(br2, column=0, position="branch-right") return t, ts
from ete_dev import Tree t = Tree("(A:1,(B:1,(C:1,D:1):0.5):0.5);") # Visit nodes in preorder (this is the default strategy) for n in t.traverse(): print n # It Will visit the nodes in the following order: # /-A # ---------| # | /-B # \--------| # | /-C # \--------| # \-D # --A # /-B # ---------| # | /-C # \--------| # \-D # --B # /-C # ---------| # \-D # --C # --D # Visit nodes in postorder for n in t.traverse("postorder"): print n # It Will visit the nodes in the following order: # --A
# the search_nodes method (I take only the first match ) A = t.search_nodes(name="A")[0] # and using the shorcut to finding nodes by name C= t&"C" H= t&"H" I= t&"I" # Let's now add some custom features to our nodes. add_features can be # used to add many features at the same time. C.add_features(vowel=False, confidence=1.0) A.add_features(vowel=True, confidence=0.5) ancestor.add_features(nodetype="internal") # Or, using the oneliner notation (t&"H").add_features(vowel=False, confidence=0.2) # But we can automatize this. (note that i will overwrite the previous # values) for leaf in t.traverse(): if leaf.name in "AEIOU": leaf.add_features(vowel=True, confidence=random.random()) else: leaf.add_features(vowel=False, confidence=random.random()) # Now we use these information to analyze the tree. print "This tree has", len(t.search_nodes(vowel=True)), "vowel nodes" print "Which are", [leaf.name for leaf in t.iter_leaves() if leaf.vowel==True] # But features may refer to any kind of data, not only simple # values. For example, we can calculate some values and store them # within nodes. # # Let's detect leaf nodes under "ancestor" with distance higher thatn # 1. Note that I'm traversing a subtree which starts from "ancestor" matches = [leaf for leaf in ancestor.traverse() if leaf.dist>1.0] # And save this pre-computed information into the ancestor node
#node.img_style["bgcolor"] = random_color() #Tree().show() ts = TreeStyle() ts.mode = "c" ts.layout_fn = layout ts.show_leaf_name = False ts.arc_span = 340 ts.arc_start = -70 #ts.allow_face_overlap = True #ts.show_branch_length = True ts.draw_guiding_lines = False ts.optimal_scale_level = "mid" ts.extra_branch_line_color = "red" ts.root_opening_factor = 0.50 ts.show_border = True ts.scale = None t = Tree() t.populate(200, random_branches=True, branch_range=(0, 0)) t.dist = 0.0 dists = [n.dist for n in t.traverse() if n.dist != 0] #print max(dists), min(dists) t.write(outfile="test.nw") #for s in [5, None]: # ts.scale = s # t.render("img_scale_%s.png" %s, tree_style = ts, w=600) t.show(tree_style=ts)
n.add_face(temp_facet, 0, "aligned") t, ts = seq_motif_faces.get_example_tree() temp_facet = TreeFace(t, ts) n = main_tree.add_child() n.add_face(temp_facet, 0, "aligned") t, ts = barchart_and_piechart_faces.get_example_tree() temp_facet = TreeFace(t, ts) n = main_tree.add_child() n.add_face(temp_facet, 0, "aligned") # Test orphan nodes and trees with 0 branch length t, ts = Tree(), TreeStyle() t.populate(5) for n in t.traverse(): n.dist = 0 temp_tface = TreeFace(t, ts) n = main_tree.add_child() n.add_face(temp_tface, 0, "aligned") ts.optimal_scale_level = "full" temp_tface = TreeFace(t, ts) n = main_tree.add_child() n.add_face(temp_tface, 0, "aligned") ts = TreeStyle() t.populate(5) ts.mode = "c" temp_tface = TreeFace(t, ts) n = main_tree.add_child()
from ete_dev import Tree t = Tree('(A:1,(B:1,(C:1,D:1):0.5):0.5);') # Visit nodes in preorder (this is the default strategy) for n in t.traverse(): print n # It Will visit the nodes in the following order: # /-A # ---------| # | /-B # \--------| # | /-C # \--------| # \-D # --A # /-B # ---------| # | /-C # \--------| # \-D # --B # /-C # ---------| # \-D # --C # --D # Visit nodes in postorder for n in t.traverse("postorder"): print n # It Will visit the nodes in the following order: # --A
from ete_dev import Tree t = Tree('((H:0.3,I:0.1):0.5, A:1, (B:0.4,(C:1,D:1):0.5):0.5);') # Create a small function to filter your nodes def conditional_function(node): if node.dist > 0.3: return True else: return False # Use previous function to find matches. Note that we use the traverse # method in the filter function. This will iterate over all nodes to # assess if they meet our custom conditions and will return a list of # matches. matches = filter(conditional_function, t.traverse()) print len(matches), "nodes have ditance >0.3" # depending on the complexity of your conditions you can do the same # in just one line with the help of lambda functions: matches = filter(lambda n: n.dist > 0.3 and n.is_leaf(), t.traverse()) print len(matches), "nodes have ditance >0.3 and are leaves"
def get_example_tree(): t = Tree() t.populate(10) # Margins, alignment, border, background and opacity can now be set for any face rs1 = faces.TextFace("branch-right\nmargins&borders", fsize=12, fgcolor="#009000") rs1.margin_top = 10 rs1.margin_bottom = 50 rs1.margin_left = 40 rs1.margin_right = 40 rs1.border.width = 1 rs1.background.color = "lightgreen" rs1.inner_border.width = 0 rs1.inner_border.line_style = 1 rs1.inner_border.color= "red" rs1.opacity = 0.6 rs1.hz_align = 2 # 0 left, 1 center, 2 right rs1.vt_align = 1 # 0 left, 1 center, 2 right br1 = faces.TextFace("branch-right1", fsize=12, fgcolor="#009000") br2 = faces.TextFace("branch-right3", fsize=12, fgcolor="#009000") # New face positions (branch-top and branch-bottom) bb = faces.TextFace("branch-bottom 1", fsize=8, fgcolor="#909000") bb2 = faces.TextFace("branch-bottom 2", fsize=8, fgcolor="#909000") bt = faces.TextFace("branch-top 1", fsize=6, fgcolor="#099000") # And faces can also be used as headers or foot notes of aligned # columns t1 = faces.TextFace("Header Face", fsize=12, fgcolor="#aa0000") t2 = faces.TextFace("Footer Face", fsize=12, fgcolor="#0000aa") # Attribute faces can now contain prefix and suffix fixed text aligned = faces.AttrFace("name", fsize=12, fgcolor="RoyalBlue", text_prefix="Aligned (", text_suffix=")") # horizontal and vertical alignment per face aligned.hz_align = 1 # 0 left, 1 center, 2 right aligned.vt_align = 1 # Node style handling is no longer limited to layout functions. You # can now create fixed node styles and use them many times, save them # or even add them to nodes before drawing (this allows to save and # reproduce an tree image design) style = NodeStyle() style["fgcolor"] = "Gold" style["shape"] = "square" style["size"] = 15 style["vt_line_color"] = "#ff0000" t.set_style(style) # add a face to the style. This face will be render in any node # associated to the style. fixed = faces.TextFace("FIXED branch-right", fsize=11, fgcolor="blue") t.add_face(fixed, column=1, position="branch-right") # Bind the precomputed style to the root node # ETE 2.1 has now support for general image properties ts = TreeStyle() # You can add faces to the tree image (without any node # associated). They will be used as headers and foot notes of the # aligned columns (aligned faces) ts.aligned_header.add_face(t1, column = 0) ts.aligned_header.add_face(t1, 1) ts.aligned_header.add_face(t1, 2) ts.aligned_header.add_face(t1, 3) t1.hz_align = 1 # 0 left, 1 center, 2 right t1.border.width = 1 ts.aligned_foot.add_face(t2, column = 0) ts.aligned_foot.add_face(t2, 1) ts.aligned_foot.add_face(t2, 2) ts.aligned_foot.add_face(t2, 3) t2.hz_align = 1 # Set tree image style. Note that aligned header and foot is only # visible in "rect" mode. ts.mode = "r" ts.scale = 10 for node in t.traverse(): # If node is a leaf, add the nodes name and a its scientific # name if node.is_leaf(): node.add_face(aligned, column=0, position="aligned") node.add_face(aligned, column=1, position="aligned") node.add_face(aligned, column=3, position="aligned") else: node.add_face(bt, column=0, position="branch-top") node.add_face(bb, column=0, position="branch-bottom") node.add_face(bb2, column=0, position="branch-bottom") node.add_face(br1, column=0, position="branch-right") node.add_face(rs1, column=0, position="branch-right") node.add_face(br2, column=0, position="branch-right") return t, ts
from ete_dev import Tree t = Tree( '((H:0.3,I:0.1):0.5, A:1, (B:0.4,(C:1,D:1):0.5):0.5);' ) # Create a small function to filter your nodes def conditional_function(node): if node.dist > 0.3: return True else: return False # Use previous function to find matches. Note that we use the traverse # method in the filter function. This will iterate over all nodes to # assess if they meet our custom conditions and will return a list of # matches. matches = filter(conditional_function, t.traverse()) print len(matches), "nodes have ditance >0.3" # depending on the complexity of your conditions you can do the same # in just one line with the help of lambda functions: matches = filter(lambda n: n.dist>0.3 and n.is_leaf(), t.traverse() ) print len(matches), "nodes have ditance >0.3 and are leaves"
# faces.add_face_to_node(f, node, 0, position="branch-right") f.border.width = 0 # node.img_style["bgcolor"] = random_color() # Tree().show() ts = TreeStyle() ts.mode = "c" ts.layout_fn = layout ts.show_leaf_name = False ts.arc_span = 340 ts.arc_start = -70 # ts.allow_face_overlap = True # ts.show_branch_length = True ts.draw_guiding_lines = False ts.optimal_scale_level = "mid" ts.extra_branch_line_color = "red" ts.root_opening_factor = 0.50 ts.show_border = True ts.scale = None t = Tree() t.populate(200, random_branches=True, branch_range=(0, 0)) t.dist = 0.0 dists = [n.dist for n in t.traverse() if n.dist != 0] # print max(dists), min(dists) t.write(outfile="test.nw") # for s in [5, None]: # ts.scale = s # t.render("img_scale_%s.png" %s, tree_style = ts, w=600) t.show(tree_style=ts)