# How to get smallest subtree containing a set of nodes from BioPhylo?

I'm testing out various phylogenetic libraries in Python. I want to read in a Newick tree, then, given a list of taxa, generate the smallest tree that contains them all. This task is quite simple and efficient in dendropy and ete2:

newick = '((raccoon, bear),((sea_lion,seal),((monkey,cat), weasel)),dog);'
taxa = ['raccoon', 'sea_lion']

import ete2
tree = ete2.Tree(newick)
pruned = tree.prune(taxa)

import dendropy
tree = dendropy.Tree.get_from_string(newick, 'newick')
pruned = tree.prune_taxa_with_labels(taxa)


I'm trying but failing to find equivalent functionality in the Bio.Phylo package. Trees do have a "prune" method, but it prunes a single node from the tree.

-

## 2 Answers

The common_ancestor function should give you the result you want: it gives you the tree (I think) that is the most recent common ancestor of all the given targets.

-
Perfect! Thank you. – Ben Morris Nov 20 '12 at 16:35

Sorry for digging out this old question. But I think that given you have an unrooted tree, the common ancestor maybe not the correct answer.

What you need to do is this: Suppose you have an unrooted tree with n tips (species), and thus 2n-3 branches. You should cut the tree in half in all these possible branches. By doing this, you will have 4n-6 subtrees. The smallest subtree containing all your selected species will be your answer.

This is different from common ancestor because for an unrooted tree, it could be put in a way that those species not picked by you are clustered first, all your selected species becomes the outgroup. But if you "re-root" the tree properly, you could still make your selected species cluster together as a small, exclusive clade.

-
You are right that this wouldn't be correct for unrooted trees, so thanks for responding. I failed to mention in the question that the trees I was working with were in fact rooted (so the example tree was perhaps a bit misleading.) – Ben Morris Oct 9 '13 at 15:31