Modify phylogenetic tree structure using Biopython Bio.Phylo. Use when rooting trees with outgroups or midpoint, pruning taxa, collapsing clades, ladderizing branches, or extracting subtrees.
Reference examples tested with: BioPython 1.83+
Before using code patterns, verify installed versions match. If versions differ:
pip show <package> then help(module.function) to check signaturesIf code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
"Root and prune my phylogenetic tree" → Modify tree topology by rooting with outgroups or midpoint, pruning taxa, collapsing low-support clades, ladderizing branches, or extracting subtrees.
tree.root_with_outgroup(), tree.prune(), tree.ladderize() (Bio.Phylo)Modify phylogenetic tree structure: rooting, pruning, ladderizing, and subtree extraction.
from Bio import Phylo
from io import StringIO
tree = Phylo.read('tree.nwk', 'newick')
# Root with single taxon
tree.root_with_outgroup({'name': 'Outgroup'})
# Root with multiple taxa (must be monophyletic)
outgroup = [{'name': 'TaxonA'}, {'name': 'TaxonB'}]
if tree.is_monophyletic(outgroup):
tree.root_with_outgroup(*outgroup)