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:
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') 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:
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:
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.