Monday, August 10, 2015

New version of phytools with many changes to the way object classes are checked

I just posted a new version of phytools (phytools 0.4-97). This is a “major” update over prior phytools version because almost every source file of phytools was updated; however it will (should) seem to be really minor, because the update should not affect the way that phytools functions for the user (except in a very small number of cases).

Specifically, what this update entails is a change to the way that phytools checks object classes. Specifically, phytools now checks object clasess (nearly always whether an object of is class "phylo" or "multiPhylo") using the R base function inherits. The advantage of inherits (which, I now understand, is the correct way that object classes should be checked) is that an object with multiple class attributes can pass a test using inherits when it would've failed a simple logical evaluation.

So, for instance, if we install our new phytools version:

install.packages("http://www.phytools.org/nonstatic/phytools_0.4-97.tar.gz",
    type="source",repos=NULL)
## Installing package into 'C:/Users/Liam/Documents/R/win-library/3.2'
## (as 'lib' is unspecified)
packageVersion("phytools")
## [1] '0.4.97'
library(phytools)
## Loading required package: ape
## Loading required package: maps

create an object of class "phylo" with a mapped discrete character history, for instance:

Q<-matrix(c(-1,1,1,-1),2,2)
rownames(Q)<-colnames(Q)<-letters[1:2]
Q
##    a  b
## a -1  1
## b  1 -1
obj<-sim.history(pbtree(n=26,tip.label=LETTERS,scale=1),
    Q,anc="a")
## Done simulation(s).
obj
## 
## Phylogenetic tree with 26 tips and 25 internal nodes.
## 
## Tip labels:
##  A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.

and then give it additional class attributes - for instance the class attribute value "simmap":

class(obj)<-c("simmap","phylo")
## print.phylo still works
obj
## 
## Phylogenetic tree with 26 tips and 25 internal nodes.
## 
## Tip labels:
##  A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.
## but a logical test fails
class(obj)=="phylo" ## fails
## [1] FALSE  TRUE
inherits(obj,"phylo") ## passes
## [1] TRUE

The point of doing this is, for starters, to change the way that objects of class "phylo" with mapped discrete character histories - prominent in so many different analyses of phytools - are handled, by doing just what is shown above. In fact, phytools in its latest version already has an S3 plotting method for objects of class "simmap":

plot(obj,colors=setNames(c("blue","red"),letters[1:2]))

plot of chunk unnamed-chunk-4

This tree can still be plotted with plot.phylo, of course, just by using:

plot.phylo(obj,no.margin=TRUE,edge.width=2)

plot of chunk unnamed-chunk-5

In addition to this, I made some other small changes to a short list of phytools functions that I may document in subsequent posts.

No comments:

Post a Comment