Wednesday, 15 May 2013

python - Save path from leaves to root using ete2 -



python - Save path from leaves to root using ete2 -

i have big tree want each leaf node path (all nodes) till root.

i'm trying using ete2 tree big , seems slow.

can suggest faster way so?

thats i'm doing:

tr = tree("anytree.nw", format=8) path_leaf_root = {} ## paths leafs root root = tr.get_tree_root() le in tr: if not path_leaf_root.has_key(le.name): path_leaf_root[le.name]=[] le_up = le while not le_up.name == root.name: le_up=le.up path_leaf_root[le.name].append(le_up.name)

you seek next approach, traverses tree once. in computer, processed 50k tips tree in 0.24secs (a bit longer if print or write results):

from ete2 import tree t = tree() t.populate(50000) import time t1 = time.time() current_path = [t] postorder, node in t.iter_prepostorder(): if postorder: current_path.pop(-1) else: if not node.children: # print node.name, "path :", current_path pass else: current_path.append(node) print time.time() - t1 # 0.242053985596

python

No comments:

Post a Comment