Using the {ape} function drop.tip() we can easily excise a single taxon or a list of taxa from our "phylo" tree object in R. However, it is not immediately obvious how to prune the tree to include, rather than exclude, a specific list of tips. Trina Roberts (now at NESCent) shared a trick to do this with me some time ago, and I thought I'd pass it along to the readers of this blog.
First, let's start with a tree of 10 species:
> tree<-rtree(10)
> write.tree(tree)
[1] "(t8:0.22,((((t3:0.9,(t7:0.48,t2:0.5):0.12):0.47,t6:0.55):0.08,(t5:0.49,(t9:0.71,t10:0.13):0.15):0.7):0.87,(t1:0.72,t4:0.62):0.55):0.47);"
Now, say we want to keep the species t2, t4, t6, t8, and t10 in our pruned tree, we just put these tip names into a vector:
> species<-c("t2","t4","t6","t8","t10")
[More commonly, this vector will probably come from the row names in our data matrix, or we might read it from a text file.]
We create the pruned tree with one command:
> pruned.tree<-drop.tip(tree,tree$tip.label[-match(species, tree$tip.label)])
Now we have our pruned tree, as desired:
> write.tree(pruned.tree)
[1] "(t8:0.22,(((t2:1.09,t6:0.55):0.08,t10:0.98):0.87,t4:1.17):0.47);"
If there are tips in the "species" vector that are not in the tree, match(species,tree$tip.label) will one or mulitple NAs, and the procedure will fail. To avoid this problem, one can just do:
ReplyDelete> pruned.tree<-drop.tip(tree, tree$tip.label[-na.omit(match(species, tree$tip.label))])
Hi Liam-
ReplyDeleteEven less code than the -match trick:
pruned.tree<-drop.tip(tree, setdiff(tree$tip.label, species));
setdiff is very handy....(as is intersect and %in%)
Cool! Very handy indeed.
ReplyDeleteDan's method will also work even if some of the labels in "species" are not in "tree."
Hey Liam, congratulations for this great blog, just one question related to this, suppose you want to prune a bunch of taxa from a large amount of trees (for example, you want to use the trees from the posterior of BEAST but you want to get rid of some taxa for all the trees). I am a beginner in R and I have no clue of how to do it.
ReplyDeleteAny idea?
thanks a lot!
J.
Hi. Yes, this is pretty easy. I will write a quick post about this now.
ReplyDeleteThe post is here.
ReplyDelete