使用ete3获取祖先节点编号

2024-06-13 16:39:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要获取ete3中树中的节点号

下面是一个树的示例:

rooted_tree = Tree( "((A,B),(C,D));" )

print rooted_tree
#
#          /-A
#     /---|
#    |     \-B
#----|
#    |     /-C
#     \---|
#          \-D

然后我从这棵树中计算一些东西(对于这个问题来说并不重要),这些东西给我树中每个节点的值,然后我在ggplot树中绘制。但是ggplot tree需要节点号来绘制这些信息,这里的问题是我无法找到代码来获取树的节点号"rooted_tree"

假设我想要AB的祖先的节点号,我如何获得它?我只知道这样做:

ancestor = tree.get_common_ancestor("A","B")

但是像ancestor.numancestor.node_number这样的东西不起作用


Tags: 代码信息tree示例get节点绘制common
1条回答
网友
1楼 · 发布于 2024-06-13 16:39:32

ete树中的所有节点都是对象,它们没有数字,但有一个标记为拓扑透视的哈希ID

您可以通过以下方式访问这些拓扑id:

ancestor = tree.get_common_ancestor("A","B")
print(ancestor.get_topology_id())

引用ete3文档:

get_topology_id(attr='name') New in version 2.3.

Returns the unique ID representing the topology of the current tree. Two trees with the same topology will produce the same id. If trees are unrooted, make sure that the root node is not binary or use the tree.unroot() function before generating the topology id.

This is useful to detect the number of unique topologies over a bunch of trees, without requiring full distance methods.

The id is, by default, calculated based on the terminal node’s names. Any other node attribute could be used instead.

进一步参考: http://etetoolkit.org/docs/latest/reference/reference_tree.html#get_topology_id

相关问题 更多 >