如何在python3中将带有元数据的Dendropy对象传递给ete3?

2024-05-04 05:12:34 发布

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

如果我有一个树状目录树,我用一些元数据(例如一些分类法)对它进行了注释,那么如何将带有元数据的整个树转换为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

Tags: infrompytreelibpackagesusrlocal