Wednesday, May 4, 2016

as.phylo method for "simmap" and "multiSimmap" objects

I have just added some generic methods/functions as.phylo (method "simmap") and as.multiPhylo to strip the elements and class attributes from objects of class "simmap" and "multiSimmap". The reason for this, well, is because I discovered that the S3 method reorder for objects of class "simmap" was causing some problems for phangorn functions used internally in some of the new consensus tree functions of the phytools package.

This is really pretty simple. In the case of as.phylo, I just added the following function:

as.phylo.simmap<-function(x,...){
    x$maps<-NULL
    x$mapped.edge<-NULL
    if(!is.null(x$node.states)) x$node.states<-NULL
    if(!is.null(x$states)) x$states<-NULL
    if(!is.null(x$Q)) x$Q<-NULL
    if(!is.null(x$logL)) x$logL<-NULL
    if(!is.null(attr(x,"map.order"))) attr(x,"map.order")<-NULL
    class(x)<-setdiff(class(x),"simmap")
    x
}

and then exported an S3 method in NAMESPACE as follows:

S3method(as.phylo, simmap)

and that was all there was to it!

Here is a quick example using as.multiPhylo:

library(phytools)
packageVersion("phytools")
## [1] '0.5.29'
tree
## 
## Phylogenetic tree with 82 tips and 81 internal nodes.
## 
## Tip labels:
##  Anolis_ahli, Anolis_allogus, Anolis_rubribarbus, Anolis_imias, Anolis_sagrei, Anolis_bremeri, ...
## 
## Rooted; includes branch lengths.
head(ecomorph)
##        Anolis_ahli     Anolis_allogus Anolis_rubribarbus 
##                 TG                 TG                 TG 
##       Anolis_imias      Anolis_sagrei     Anolis_bremeri 
##                 TG                 TG                 TG 
## Levels: CG GB TC TG Tr Tw
map.trees<-make.simmap(tree,ecomorph,model="ER",nsim=10)
## make.simmap is sampling character histories conditioned on the transition matrix
## 
## Q =
##             CG          GB          TC          TG          Tr          Tw
## CG -0.11570723  0.02314145  0.02314145  0.02314145  0.02314145  0.02314145
## GB  0.02314145 -0.11570723  0.02314145  0.02314145  0.02314145  0.02314145
## TC  0.02314145  0.02314145 -0.11570723  0.02314145  0.02314145  0.02314145
## TG  0.02314145  0.02314145  0.02314145 -0.11570723  0.02314145  0.02314145
## Tr  0.02314145  0.02314145  0.02314145  0.02314145 -0.11570723  0.02314145
## Tw  0.02314145  0.02314145  0.02314145  0.02314145  0.02314145 -0.11570723
## (estimated using likelihood);
## and (mean) root node prior probabilities
## pi =
##        CG        GB        TC        TG        Tr        Tw 
## 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
## Done.
map.trees
## 10 phylogenetic trees with mapped discrete characters
map.trees[[1]]
## 
## Phylogenetic tree with 82 tips and 81 internal nodes.
## 
## Tip labels:
##  Anolis_ahli, Anolis_allogus, Anolis_rubribarbus, Anolis_imias, Anolis_sagrei, Anolis_bremeri, ...
## 
## The tree includes a mapped, 6-state discrete character with states:
##  CG, GB, TC, TG, Tr, Tw
## 
## Rooted; includes branch lengths.
par(mfrow=c(4,3))
plot(map.trees,fsize=0.3,lwd=1)
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"
## no colors provided. using the following legend:
##        CG        GB        TC        TG        Tr        Tw 
##   "black"     "red"  "green3"    "blue"    "cyan" "magenta"

plot of chunk unnamed-chunk-3

trees<-as.multiPhylo(map.trees)
trees
## 10 phylogenetic trees
trees[[1]]
## 
## Phylogenetic tree with 82 tips and 81 internal nodes.
## 
## Tip labels:
##  Anolis_ahli, Anolis_allogus, Anolis_rubribarbus, Anolis_imias, Anolis_sagrei, Anolis_bremeri, ...
## 
## Rooted; includes branch lengths.
str(trees[[1]])
## List of 4
##  $ edge       : int [1:162, 1:2] 83 84 85 86 87 88 89 90 90 89 ...
##  $ Nnode      : int 81
##  $ tip.label  : chr [1:82] "Anolis_ahli" "Anolis_allogus" "Anolis_rubribarbus" "Anolis_imias" ...
##  $ edge.length: num [1:162] 0.268 0.209 0.402 0.826 0.768 ...
##  - attr(*, "class")= chr "phylo"
##  - attr(*, "order")= chr "cladewise"
par(mfrow=c(4,3))
nulo<-lapply(trees,plot,cex=0.3,no.margin=TRUE)

plot of chunk unnamed-chunk-4

Of course, the same could have been accomplished by simply setting the class attribute of the "simmap" objects to "phylo", "multiSimmap" to "multiPhylo", and so on. The only advantage of stripping away all the components associated with the object class is that these do not have to be continually copied & re-copied whenever the object is copied.

phytools can be updated thusly from GitHub using devtools:

library(devtools)
install_github("liamrevell/phytools")

No comments:

Post a Comment