gpt4 book ai didi

python - 将树状图保存为 newick 格式

转载 作者:太空狗 更新时间:2023-10-30 00:29:41 32 4
gpt4 key购买 nike

如何将 scipy 生成的树状图保存到 Newick format 中?

最佳答案

您需要链接矩阵 Z,它是 scipy 树状图函数的输入,并将其转换为 Newick 格式。此外,您需要一个包含叶子名称的列表“leaf_names”。这是一个可以完成这项工作的函数:

def get_newick(node, parent_dist, leaf_names, newick='') -> str:
"""
Convert sciply.cluster.hierarchy.to_tree()-output to Newick format.

:param node: output of sciply.cluster.hierarchy.to_tree()
:param parent_dist: output of sciply.cluster.hierarchy.to_tree().dist
:param leaf_names: list of leaf names
:param newick: leave empty, this variable is used in recursion.
:returns: tree in Newick format
"""
if node.is_leaf():
return "%s:%.2f%s" % (leaf_names[node.id], parent_dist - node.dist, newick)
else:
if len(newick) > 0:
newick = "):%.2f%s" % (parent_dist - node.dist, newick)
else:
newick = ");"
newick = get_newick(node.get_left(), node.dist, leaf_names, newick=newick)
newick = get_newick(node.get_right(), node.dist, leaf_names, newick=",%s" % (newick))
newick = "(%s" % (newick)
return newick

tree = hierarchy.to_tree(Z, False)
get_newick(tree, tree.dist, leaf_names)

关于python - 将树状图保存为 newick 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28222179/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com