简体   繁体   English

使用ete2保存从叶到根的路径

[英]Save path from leaves to root using ete2

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. 我正在尝试使用ete2进行此操作,但是树太大了,似乎太慢了。

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): 在我的计算机上,它在0.24秒内处理了一个50k的提示树(如果您打印或写入结果,则需要更长的时间):

from ete2 import Tree
t = Tree()
t.populate(50000)

import time
t1 = time.time()
current_path = [t]
for 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM