from ete_dev import Tree # Loads a basic tree t = Tree( '(A:0.2,(B:0.4,(C:1.1,D:0.45):0.5):0.1);' ) print t # /-A #---------| # | /-B # \--------| # | /-C # \--------| # \-D # Counts leaves within the tree nleaves = 0 for leaf in t.get_leaves(): nleaves += 1 print "This tree has", nleaves, "terminal nodes" # But, like this is much simpler :) nleaves = len(t) print "This tree has", nleaves, "terminal nodes [proper way: len(tree) ]" # Counts leaves within the tree ninternal = 0 for node in t.get_descendants(): if not node.is_leaf(): ninternal +=1 print "This tree has", ninternal, "internal nodes" # Counts nodes with whose distance is higher than 0.3 nnodes = 0 for node in t.get_descendants(): if node.dist > 0.3: nnodes +=1 # or, translated into a better pythonic
import time from ete_dev import Tree # Creates a random tree with 10,000 leaf nodes tree = Tree() tree.populate(10000) # This code should be faster t1 = time.time() for leaf in tree.iter_leaves(): if "aw" in leaf.name: print "found a match:", leaf.name, break print "Iterating: ellapsed time:", time.time() - t1 # This slower t1 = time.time() for leaf in tree.get_leaves(): if "aw" in leaf.name: print "found a match:", leaf.name, break print "Getting: ellapsed time:", time.time() - t1 # Results in something like: # found a match: guoaw Iterating: ellapsed time: 0.00436091423035 secs # found a match: guoaw Getting: ellapsed time: 0.124316930771 secs
import time from ete_dev import Tree # Creates a random tree with 10,000 leaf nodes tree = Tree() tree.populate(10000) # This code should be faster t1 = time.time() for leaf in tree.iter_leaves(): if "aw" in leaf.name: print "found a match:", leaf.name, break print "Iterating: ellapsed time:", time.time()-t1 # This slower t1 = time.time() for leaf in tree.get_leaves(): if "aw" in leaf.name: print "found a match:", leaf.name, break print "Getting: ellapsed time:", time.time()-t1 # Results in something like: # found a match: guoaw Iterating: ellapsed time: 0.00436091423035 secs # found a match: guoaw Getting: ellapsed time: 0.124316930771 secs
from ete_dev import Tree # Loads a basic tree t = Tree('(A:0.2,(B:0.4,(C:1.1,D:0.45):0.5):0.1);') print t # /-A #---------| # | /-B # \--------| # | /-C # \--------| # \-D # Counts leaves within the tree nleaves = 0 for leaf in t.get_leaves(): nleaves += 1 print "This tree has", nleaves, "terminal nodes" # But, like this is much simpler :) nleaves = len(t) print "This tree has", nleaves, "terminal nodes [proper way: len(tree) ]" # Counts leaves within the tree ninternal = 0 for node in t.get_descendants(): if not node.is_leaf(): ninternal += 1 print "This tree has", ninternal, "internal nodes" # Counts nodes with whose distance is higher than 0.3 nnodes = 0 for node in t.get_descendants(): if node.dist > 0.3: nnodes += 1 # or, translated into a better pythonic