Removing a node from a phylogenetic tree with its edges
0
1
Entering edit mode
3.4 years ago
Moses ▴ 130

Hi All,

I have phylogenetic trees with preset outgroups that I am using it to root my trees and convert it to binary trees. I would later want to remove this outgroup with its parent (which is the root for the tree) and continune the tree with the second child of this removed parent which in turn becomes the new root. I'm reading a newick format tree using pythons ete3 package, lets say my tree is stored in a file like this:

(S1_k99_151_71_451_+:0.04173,S1_k99_259_71_451_+:0.07108,((S2_k99_3184_162148_162525_+:0.32717,S1_k99_3103_231736_232113_+:0.23897)1.000:0.20714,(outgroup:5.97874,S1_k99_1153_6920_7300_-:0.21321)0.000:0.01464)1.000:0.25427);


then I run the following code on this newick tree:

t = Tree('tree1.tree')
t.set_outgroup('outgroup')
tree_root = t.get_tree_root()
tree_root.name = 'root'
root_node = t.search_nodes(name='root')[0]
outgroup_branch.detach()
outgroup_branch.delete()
root_node.detach()
root_node.delete()
t.write(format=1, outfile='rooted_tree.tree')


The resulting rooted tree is being outputted as this:

((S1_k99_1153_6920_7300_-:0.21321,((S2_k99_3184_162148_162525_+:0.32717,S1_k99_3103_231736_232113_+:0.23897):0.20714,(S1_k99_151_71_451_+:0.04173,S1_k99_259_71_451_+:0.07108):0.25427):0.01464):2.98937);


If you notice it's not removing the root, and keeping an edge with its descendant, hence im having a tree that starts with a node that has a single edge. I would want my code to give me this result:

(S1_k99_1153_6920_7300_-:0.21321,((S2_k99_3184_162148_162525_+:0.32717,S1_k99_3103_231736_232113_+:0.23897):0.20714,(S1_k99_151_71_451_+:0.04173,S1_k99_259_71_451_+:0.07108):0.25427):0.01464);


so to remove that last edge from the tree. I do not know how to delete the root of the tree so that it will delete its descending edge also. I tried deleting the outgroup first then the root node it still did not work. Any help.suggestions would really be appreciated. Thanks.

python3 ete3 phylogenetics • 1.8k views
1
Entering edit mode

For now I'm resorting to tree.prune function that the ete3 package provides. I was hesitant to use this at first because I thought there might (will) be more overhead by getting all the leaves first that I want to keep and then pruning that subtree instead of just removing two nodes from an existing tree. Offcourse I will repeat this process over many trees which are much larger from this one (100s to 1000s of leaves). Would it be faster to just delete the node instead?

0
Entering edit mode

Prune would have been my suggestion anyway, so I think you’re fine to proceed as you are.

You might also consider looking at the package dendropy for tree manipulations. It and ETE both have some subtly different strengths and weaknesses.

0
Entering edit mode

I see. Thanks for that I will check it out.