python中嵌套树词典的common_ancestor函数



我正在尝试创建一个称为" common_ancestor()"的函数,该函数获取两个输入:第一个A列表字符串分类单位名称列表,第二个是系统发育树词典。它应该返回一个弦,给出分类单元的名称,该名称是所有的最接近的祖先输入列表中的物种。已经制作了一个称为" List_anceStors"的单独函数,它为我提供了列表中元素的总体祖先。另外,请与我一起工作的字典。

    tax_dict = { 
'Pan troglodytes': 'Hominoidea',       'Pongo abelii': 'Hominoidea', 
'Hominoidea': 'Simiiformes',           'Simiiformes': 'Haplorrhini', 
'Tarsius tarsier': 'Tarsiiformes',     'Haplorrhini': 'Primates',
'Tarsiiformes': 'Haplorrhini',         'Loris tardigradus':'Lorisidae',
'Lorisidae': 'Strepsirrhini',          'Strepsirrhini': 'Primates',
'Allocebus trichotis': 'Lemuriformes', 'Lemuriformes': 'Strepsirrhini',
'Galago alleni': 'Lorisiformes',       'Lorisiformes': 'Strepsirrhini',
'Galago moholi': 'Lorisiformes'
} 
def halfroot(tree):
    taxon = random.choice(list(tree))
    result = [taxon]
    for i in range(0,len(tree)): 
        result.append(tree.get(taxon))
        taxon = tree.get(taxon)
    return result

def root(tree):
    rootlist = halfroot(tree)
    rootlist2 = rootlist[::-1]
    newlist = []
    for e in range(0,len(rootlist)):
        if rootlist2[e] != None:
        newlist.append(rootlist2[e])
    return newlist[0]

def list_ancestors(taxon, tree):
    result = [taxon]
    while taxon != root(tree):
        result.append(tree.get(taxon))
        taxon = tree.get(taxon)
    return result
def common_ancestors(inputlist,tree)
    biglist1 = []
    for i in range(0,len(listname)):
        biglist1.append(list_ancestors(listname[i],tree))
        "continue so that I get three separate lists where i can cross reference all elements from the first list to every other list to find a common ancestor "

结果看起来像

  print(common_ancestor([’Hominoidea’, ’Pan troglodytes’,’Lorisiformes’], tax_dict)
  Output: ’Primates’"

一种方法是为每个物种收集所有祖先,将它们放入一个集合中,然后获得十字路口以获得它们的共同点:

def common_ancestor(species_list, tree):
    result = None  # initiate a `None` result
    for species in species_list:  # loop through each species in the species_list
        ancestors = {species}  # initiate the ancestors set with the species itself
        while True:  # rinse & repeat until there are leaves in the ancestral tree
            try:
                species = tree[species]  # get the species' ancestor
                ancestors.add(species)  # store it in the ancestors set
            except KeyError:
                break
        # initiate the result or intersect it with ancestors from the previous species
        result = ancestors if result is None else result & ancestors
    # finally, return the ancestor if there is only one in the result, or None
    return result.pop() if result and len(result) == 1 else None
print(common_ancestor(["Hominoidea", "Pan troglodytes", "Lorisiformes"], tax_dict))
# Primates

您也可以将此功能的"中间"部分用于list_ancestors() - 无需通过尝试找到树的根来使其复杂化:

def list_ancestors(species, tree, include_self=True):
    ancestors = [species] if include_self else []
    while True:
        try:
            species = tree[species]
            ancestors.append(species)
        except KeyError:
            break
    return ancestors

当然,两者都依靠有效的祖先词典 - 如果某些祖先要重复出现,或者链中有损坏,则无法正常工作。另外,如果您要进行很多此类操作,则可能值得将您的平面字典变成合适的树。

相关内容

最新更新