寻找otu1和otu2最接近的共同祖先,其中otu是可操作的分类单位

时间:2014-04-03 20:37:08

标签: python biopython

让函数正常工作时遇到一些麻烦

tree = {
    'ADBCE': [None, ('ADBC', 3.625), ('E', 17.625)],
    'AD': [('ADB', 5.25), ('A', 4.0), ('D', 4.0)],
    'ADB': [('ADBC', 4.75), ('AD', 5.25), ('B', 9.25)],
    'D': [('AD', 4.0), None, None],
    'E': [('ADBCE', 17.625), None, None],
    'ADBC': [('ADBCE', 3.625), ('ADB', 4.75), ('C', 14.0)],
    'A': [('AD', 4.0), None, None],
    'B': [('ADB', 9.25), None, None],
    'C': [('ADBC', 14.0), None, None]
}

otu =运营分类单位

def ClosestCommonAncestors(otu1,otu2, tree)

例如otu1 =" AD"和otu2 = C

输出=" ADBC"这是最近的共同祖先

1 个答案:

答案 0 :(得分:0)

假设您的树格式化为node : (parent, child_left, child_right),这里是使用generator来获取给定节点的祖先的次优解决方案:

def ancestor(a,tree):
    """ Return the ancestors of a until the root is reached """
    dist = 0
    while tree[a][0] != None:
        yield tree[a][0][0]
        a = tree[a][0][0]

def LCA(x,y,tree):
    """Return the last common ancestor of x and y in tree"""

    # First we build the set of all ancestors of one of the two entries.
    ancestor_x = frozenset(ancestor(x,tree))

    # Then we iterate trough the ancestors of the second entry until we find an ancestor of the first one.
    for j in ancestor(y,tree):
        if j in ancestor_x:
            return j

assert LCA("AD","C",tree) == "ADBC"
assert LCA("A","E",tree) == "ADBCE"
assert LCA("ADB","E",tree) == "ADBCE"

由于我们构建了两个条目之一的所有祖先的集合,除此之外,我的扩展性很差(特别是如果树不平衡)但它应该在小树上进行。