reroot at any possible node in ete or with any other python
3
0
Entering edit mode
7.1 years ago
mavino ▴ 10

Hi all, I am new to python. I a have a phylogenetic tree and I need to reroot it at any possible branch, including terminal branches. From there I need to store all of the resulting trees as newick files. I was wondering if that is possible with ete or any other python related library. Thank you very much

Python • 3.3k views
ADD COMMENT
1
Entering edit mode
7.1 years ago
mavino ▴ 10

At the end, desperate, I managed to do it myself:

import ete3
from ete3 import Tree
t = Tree('(((A,C),((H,F),(L,M))),((B,(J,K)),(E,D)));')
# to get all possible rooting on terminal branches
ls = []
for leaf in t:
    ls.appendleaf.name)    # add a parenthesis before leaf 
for i in ls:
    t.set_outgroup(t&i)
    t.write(format=1, outfile="myfolder/"+ i + ".nwk")

# to get all possible rooting on internal branches
edge = 0
ancestor = []
for node in t.traverse():
    if not node.is_leaf():
        if not node.is_root():
            node.name = "NODE_%d" %edge
            ancestor.appendnode.name)      # add a parenthesis after append
            edge += 1
for i in ancestor:
    t.set_outgroup( t&i )
t.write(format=1, outfile="myfolder/"+ i + ".nwk")
ADD COMMENT
0
Entering edit mode

sorry for the missing parenthesis between append and leaf at line 7...but I do not know how to fix it, it shows that parenthesis when I try to edit

ADD REPLY
0
Entering edit mode

I think this may be due to a bug in Biostars code that was recently discussed. See if you can add an extra space and then make a note that should be removed when running.

ADD REPLY
0
Entering edit mode

Done! Thanks. I also added a comment for the other mistake at line 18

ADD REPLY
0
Entering edit mode
7.1 years ago
Eric T. ★ 2.8k

In Biopython, using the module Bio.Phylo, you can reroot a tree object using the method root_with_outgroup.

ADD COMMENT
0
Entering edit mode

Yes thanks, but I need to choose Phylo or ETE3, not both...they do not communicate each other

ADD REPLY
0
Entering edit mode
7.1 years ago
mavino ▴ 10

Use this script when you do not have duplicated labels in your tree, otherwise when it generates outgroups by terminal tips it will overwrite the one with the same name per terminal tip. You can change the format of the trees to be written as you like (according to ETE3 format rules).

ADD COMMENT

Login before adding your answer.

Traffic: 1660 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6