I have a phylogenetic tree in Newick format, and I would like to remove all species from it that are on a specific list and rename it accordingly.
This is the tree:
((((A:0.1, B:0.2):0.3, C:0.3):0.15, (D:0.3, (E:0.1, (F:0.15, (G:0.1, H:0.1):0.1):0.1):0.1):0.1):0.15, I:0.2);
This is the table to rename:
| species | clade_renaming |
|------------|----------------|
| A, B | X |
| F, G, H | Y |
Expected result:
(((X:0.3, C:0.3):0.15, (D:0.3, (E:0.1, Y:0.1):0.1):0.1):0.15, I:0.2);
This is the current code that can collapse nodes:
from Bio import Phylo
import io
tree_structure = "((((A:0.1, B:0.2):0.3, C:0.3):0.15, (D:0.3, (E:0.1, (F:0.15, (G:0.1, H:0.1):0.1):0.1):0.1):0.1):0.15, I:0.2);"
tree = Phylo.read(io.StringIO(tree_structure), 'newick')
nodes_to_collapse = ["A", "B", "F", "G", "H"]
def collapse_nodes(tree, nodes_to_collapse):
for node in tree.find_elements(target=lambda x: x.name in nodes_to_collapse, order="postorder"):
tree.collapse(node)
collapse_nodes(tree, nodes_to_collapse)
Phylo.draw(tree)
Hi,
Is this also you? Naomi Sun