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
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
3 answers
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")
In Biopython, using the module Bio.Phylo, you can reroot a tree object using the method root_with_outgroup.
Yes thanks, but I need to choose Phylo or ETE3, not both...they do not communicate each other
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).
Log in to answer this question.