【问题标题】:Adding new nodes to a Tree by dendroPy通过 dendroPy 向树中添加新节点
【发布时间】:2014-11-10 02:24:15
【问题描述】:

我想通过动态添加节点来创建一棵树 DendroPy 中的现有树。所以这就是我的处理方式,

>>> t1 = dendropy.Tree(stream=StringIO("(8,3)"),schema="newick")

现在这将创建一棵小树,其中有两个孩子的分类单元标签为 8 和 3。现在 我想向带有分类单元标签 3 的节点添加一个新叶。为此,我想要该节点 目的。

>>> cp = t1.find_node_with_taxon_label('3')

我想在那个时候使用添加子函数,它是节点的一个属性。

>>> n = dendropy.Node(taxon='5',label='5')  
>>> cp.add_child(n)

但即使在我打印 t1 中的所有节点对象时添加节点后,它也是 返回初始化时唯一的孩子 8 和 3。 请帮助我了解如何在 dendropy 中的现有树中添加节点..

现在,如果我们打印 t1,我们会看到树。但即使在添加元素之后 我找不到添加的对象。例如,如果我们做一个

>>> cp1 = t1.find_node_with_taxon_label('5')

它没有返回与5相关的对象。

【问题讨论】:

  • 如果我使用你的代码和print t1 毕竟,我得到(8,(5)3)。这似乎有一个叶子“5”附加到分支“3”。
  • @xbello 好的,问题是它显示它作为一个孩子附加,但该对象似乎丢失了,例如,如果我现在执行cp1 = t1.find_node_with_taxon_label('5'),它什么也没显示。这意味着如果我想再添加一个孩子,我就不能再这样做了。

标签: python bioinformatics biopython phylogeny dendropy


【解决方案1】:

要添加分类单元,您必须显式创建并将其添加到树中:

t1 = dendropy.Tree(stream=StringIO("(8,3)"),schema="newick")

# Explicitly create and add the taxon to the taxon set
taxon_1 = dendropy.Taxon(label="5")
t1.taxon_set.add_taxon(taxon_1)

# Create a new node and assign a taxon OBJECT to it (not a label)
n = dendropy.Node(taxon=taxon_1, label='5')

# Now this works
print t1.find_node_with_taxon_label("5")

关键是find_node_with_taxon_labelt1.taxon_set分类单元列表中搜索。

【讨论】:

  • 太棒了!我在 10 分钟前的代码中发现了这一点。但是非常感谢您的回答。好消息是 dendropy 标签从这个问题开始。现在很容易找到东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
相关资源
最近更新 更多