首页 > 解决方案 > 如何将带有元数据的 Dendropy 对象传递给 python3 中的 ete3?

问题描述

如果我有一个用一些元数据注释的 dendropy 树,例如一些分类法,我如何将带有元数据的整个树转换为 ete3 对象?

import dendropy

t = dendropy.Tree.get_from_string("(A,(B,(C,D)));", "newick")

categories = {"A" : "Human", "B": "Mouse", "C": "Bee", "D": "dolphin", "E":"Ecoli"}

for taxon in tree.taxon_namespace:
    taxon.category = categories[taxon.label]
    taxon.annotations.add_bound_attribute("category")

我试图直接通过它:

from ete3 import PhyloTree
tree = PhyloTree(t)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/dist-packages/ete3/phylo/phylotree.py", line 391, in __init__
    TreeNode.__init__(self, newick=newick, format=format, **kargs)
  File "/usr/local/lib/python3.4/dist-packages/ete3/coretype/tree.py", line 211, in __init__
     quoted_names=quoted_node_names)
  File "/usr/local/lib/python3.4/dist-packages/ete3/parser/newick.py", line 254, in read_newick
    raise NewickError("'newick' argument must be either a filename or a newick string.")
ete3.parser.newick.NewickError: 'newick' argument must be either a filename or a newick string.
You may want to check other newick loading flags like 'format' or 'quoted_node_names'.

我还尝试编写为 nexml 格式并使用 nexml 解析器表单 ete3 读取它:

tree.write_to_path(output_tree_file, nexml)

from ete3 import Nexml, nexml

nexml_project = Nexml()
# Load content from NeXML file
nexml_project.build_from_file(tree)
# Extracts all the collection of trees in the project
tree_collections = nexml_project.get_trees()
# Select the first collection
collection_1 = tree_collections[0]

但是,nexml 中的树似乎是奇怪的格式,因为 ete3 的 xml 解析器无法识别它。

Traceback (most recent call last):
    nexml_project.build_from_file(tree)
  File "/usr/local/lib/python3.4/dist-packages/ete3/nexml/__init__.py", line 59, in build_from_file
    doc = _nexml.parsexml_(fname)
  File "/usr/local/lib/python3.4/dist-packages/ete3/nexml/_nexml.py", line 103, in parsexml_
    doc = etree_.parse(*args, **kwargs)
  File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 1187, in parse
tree.parse(source, parser)
  File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 598, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 110886, column 107

标签: pythonpython-3.xetetoolkitete3dendropy

解决方案


推荐阅读