Using python 3.x how can I pass a Tree object from ete3 to DendroPy without writing to file

284 views Asked by At

I'm using the ete3 package in python to build phylogenetic trees from data I've generated with a stochastic model and it works well. I have previously written these trees to newick format and then used another script, with the package Dendropy, to read these trees and do some analysis of them. Both of these scripts work fine.

I am now trying to do a large amount of this sort of data processing and want to write a single script in which I skip the file writing. Both methods are called Tree, so I got around this by importing the dendropy method like:

from dendropy import Tree as DTree

and the ete3 method like:

from ete3 import Tree

which seems to be ok.

The question I have is how to pass the object from one package to the other. I have a loop in which I first build the tree object using the ete3 methods, and I call it 't'. My plan was then to use the Tree.write method in ete3 to pass the tree obect to Dendropy using the 'get' method and skipping the actual outfile bit, like this:

treePass = t.write(format = 1)
DendroTree = DTree.get(treePass, schema = 'newick')

but this gives the error:

    DendroTree = DTree.get(treePass)
TypeError: get() takes 1 positional argument but 2 were given

Any thoughts are welcome.

1

There are 1 answers

3
Tomasz Plaskota On BEST ANSWER

DTree.get() only takes self as actual argument and rest is given through keywords. This basically means you cannot pass treePass to DTree.get() as an argument.

I haven't used either of those libs, but I have found a way to import data to dendropy tree here.

tree = DTree.get(data="((A,B),(C,D));",schema="newick")

Which means you'd have to get your tree from ete3 in this format. it doesn't seem that unusual for a tree, so after a bit more looking there seems to be supported format in ete3, which you can read here. I believe it's number 9.

So in the end I'd try this:

from dendropy import Tree as DTree
from ete3 import Tree

#do your Tree generation magic here
DendroTree = DTree.get(data=t.write(format = 9),schema = 'newick')

Edit:

As I'm reading more and more, I believe that any format should be read so basically all you have to add to your example is data here: DendroTree = DTree.get(data=treePass, schema = 'newick')