How to pass a Dendropy object with metadata to ete3 in python3?

206 views Asked by At

If I have a dendropy tree that I annotate with some metadata, for example with some taxonomies, how can I convert the whole tree with metadata into a ete3 object?

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")

I tried to pass it directly:

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'.

Also I tried to write as nexml format and read it with the nexml parser form 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]

However it seems that the tree in nexml might be strange format as it is not recognised by the xml parser from ete3.

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
0

There are 0 answers