Thursday, July 14, 2016

Update to backbonePhylo methods to permit multiple clades to have the same label

I have just added a feature to the functions phylo.toBackbone and plot.backbonePhylo to permit multiple clades to have the same names. This could be handy to plot (for instance) a phylogeographic tree in which multiple subtrees come from the same region. The way that I did this is by creating a unique clade ID for each subtree independent of the clade label.

Here is a simulated case just to illustrate the point. Remember we can make this object using an interactive mode.

library(phytools)
## Loading required package: ape
## Loading required package: maps
## 
##  # maps v3.1: updated 'world': all lakes moved to separate new #
##  # 'lakes' database. Type '?world' or 'news(package="maps")'.  #
## now let's create our backbone tree
tree<-phytools:::lambdaTree(pbtree(n=10),lambda=0.5)

## create a translation table
## leaving a couple of single-taxon clades for fun
tip.label<-sample(tree$tip.label,8)
clade.label<-sample(c("Puerto Rico","Vieques","Virgin Islands"),8,
    replace=TRUE)
N<-ceiling(runif(n=8,min=1,max=20))
## set crown node depth to 1/2 the maximum depth
depth<-sapply(tip.label,function(x,y) 
    0.5*y$edge.length[which(tree$edge[,2]==which(y$tip.label==
    x))],y=tree)
trans<-data.frame(tip.label,clade.label,N,depth)
rownames(trans)<-NULL
rm(tip.label,clade.label,N,depth)
## this is what our translation table looks like for NON-interactive mode
trans
##   tip.label    clade.label  N     depth
## 1        t1    Puerto Rico  7 0.7765093
## 2        t6    Puerto Rico  5 0.5923332
## 3        t7    Puerto Rico  6 0.5611997
## 4        t2    Puerto Rico 10 0.7693265
## 5       t10        Vieques 11 0.5050155
## 6        t3        Vieques  8 0.7459787
## 7        t4 Virgin Islands  5 0.7459787
## 8        t5    Puerto Rico  8 0.5923332
## convert to "backbonePhylo" object
obj<-phylo.toBackbone(tree,trans)
## just to see, here are the clade labels & the unique clade IDs
sapply(obj$tip.clade,function(x) x$label)
##  [1] "Vieques"        "Virgin Islands" "t9"             "Vieques"       
##  [5] "Puerto Rico"    "Puerto Rico"    "Puerto Rico"    "t8"            
##  [9] "Puerto Rico"    "Puerto Rico"
sapply(obj$tip.clade,function(x) x$id)
##  [1] "IDGhhL" "1VY7KJ" "28HUn7" "DDSSM8" "41RQvF" "Ip26tj" "XxOFYs"
##  [8] "jOJTgb" "XRlU1x" "pBtF6T"
## plot
cols<-setNames(c("white","blue","red"),
    c("Vieques","Puerto Rico","Virgin Islands"))
plot(obj,col=cols,fixed.height=TRUE,print.clade.size=TRUE)

plot of chunk unnamed-chunk-1

This update can be obtained by getting the latest development version of phytools from GitHub.

1 comment:

  1. I may not understand much about how coding works using backbonephylo methods, but I know that with your guidelines and proper integration of the code in phytools in will all be good. Not like the guideline codes from custom writings reviews which are not illustrated properly.

    ReplyDelete

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