Saturday, April 8, 2017

Update to treeSlice to crop the tipward end of a phylogenetic tree in R

Last night I received the following comment:

“I was wondering how I can exclude the past few million years of branches from my time tree. I have a phylogeny that began diversifying around 40MYA and I wanted to see how the γ statistic value changes when I exclude recent speciation events.”

Surprisingly, this could not be done with phytools - although the exact opposite (that is, extracting all subtrees descending from a point) was possible using the function treeSlice.

I believe that precisely this manipulation may in fact be possible using other R packages, such as David Bapst's paleotree package.

I nonetheless have now added this functionality as an option (orientation="rootwards") in the phytools function treeSlice. Here is a little demo just loading the source from GitHub.

library(phytools)
library(phangorn)
source("https://raw.githubusercontent.com/liamrevell/phytools/master/R/treeSlice.R") 
tree<-pbtree(n=26,tip.label=LETTERS,scale=10)
plotTree(tree,mar=c(4.1,1.1,1.1,1.1))
nodelabels(bg="white")
axis(1)
abline(v=8,lty="dashed")

plot of chunk unnamed-chunk-1

xlim<-get("last_plot.phylo", envir = .PlotPhyloEnv)$x.lim
obj<-treeSlice(tree,slice=8,orientation="rootwards")
plotTree(obj,mar=c(4.1,1.1,1.1,1.1),xlim=xlim)
axis(1)
abline(v=8,lty="dashed")

plot of chunk unnamed-chunk-1

We can see that the tip labels of the 'trimmed' tree correspond to either the descendant node number in the original tree, or the original tip label for cases in which the cropped subtree only contains one taxon.

The function also has a neat interactive mode when the argument prompt is set to TRUE. In this case, the cut position is determined by the height above the root clicked by the user on the plotting device.

2 comments:

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.