python root two trees on the same root node

748 Views Asked by At

I wrote a program which calculates the distance between two trees. The trees are already rooted.

I want to make sure that the trees are rooted on the same root or outgroup.

As far as I know, in ete2 one can only set the root but cannot find the root and root the other tree on the same one.

I want to find the root in one tree and set the same root in the other. So the trees are rooted in the same way.

#>>> print t1
#
#         /-aaaaaaaaad
#      /-|
#   /-|   \-aaaaaaaaae
#  |  |
#--|   \-aaaaaaaaaa
#  |
#  |   /-aaaaaaaaab
#   \-|
#      \-aaaaaaaaac
#>>> print t2
#
#      /-aaaaaaaaaa
#   /-|
#  |  |   /-aaaaaaaaab
#  |   \-|
#--|      \-aaaaaaaaac
#  |
#  |   /-aaaaaaaaad
#   \-|
#      \-aaaaaaaaae
#

So in t1, the tree is rooted on the outgroup ending with b and c. I want to get this outgroup and root t2 on the same group.

Does anyone know if there is the posibility to make sure the trees are rooted the same? Or does another package include such a method?

1

There are 1 best solutions below

2
On

the etetoolkit provides the set_outgroup method for rooting trees. If you just want to have the same root in two trees for topology comparison, the simplest approach would be to pick the same tip name as the root in both trees.

from ete2 import Tree
# generate 2 random trees
t1 = Tree()
t2 = Tree()
t1.populate(5)
t2.populate(5)
# root both to the same tip name
root = t1.get_leaf_names()[0]
t1.set_outgroup(root)
t2.set_outgroup(root)

print t1
print t2
#
#  /-aaaaaaaaaa
#-|
# |   /-aaaaaaaaab
#  \-|
#    |   /-aaaaaaaaac
#     \-|
#       |   /-aaaaaaaaad
#        \-|
#           \-aaaaaaaaae
#
#  /-aaaaaaaaaa
# |
#-|      /-aaaaaaaaad
# |   /-|
# |  |   \-aaaaaaaaae
#  \-|
#    |   /-aaaaaaaaab
#     \-|
#        \-aaaaaaaaac