I have a large tree where I want to get for each leaf node the path (all nodes) till the root.
I'm trying to do this using ete2 but the tree is so large and it seems to be too slow.
Can anyone suggest a faster way to do so?
thats what I'm doing:
tr = Tree("anytree.nw", format=8)
path_leaf_root = {} ## all paths from leafs to root
root = tr.get_tree_root()
for 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 could try the following approach, which traverses the tree only once. In my computer, it processed a 50k tips tree in 0.24secs (a bit longer if you print or write the results):