Sunday, December 20, 2015

Small update to read.newick

I just pushed a very small update to the function read.newick for reading phylogenetic trees in Newick format. This function is basically redundant with (though slower & less powerful than) ape's otherwise equivalent function read.tree; however read.newick does have some additional functionality, like the ability to correctly parse Newick strings with singleton nodes.

The update permits the user to pass arguments to the base function scan, which is used internally by read.newick. Most useful among these are probably the arguments skip & nlines, which allow the user to easily read only one or a subset of the trees in a file.

For instance:

## load packages
library(phytools)
## simulate some trees
trees<-list()
for(i in 1:10) trees[[i]]<-rtree(n=i*10)
class(trees)<-"multiPhylo"
## write to file;
write.tree(trees,file="example.trees")

Now reading:

## read all
obj<-read.newick(file="example.trees")
print(obj,details=TRUE)
## 10 phylogenetic trees
## tree 1 : 10 tips
## tree 2 : 20 tips
## tree 3 : 30 tips
## tree 4 : 40 tips
## tree 5 : 50 tips
## tree 6 : 60 tips
## tree 7 : 70 tips
## tree 8 : 80 tips
## tree 9 : 90 tips
## tree 10 : 100 tips
## read 1st
obj<-read.newick(file="example.trees",nlines=1)
print(obj,details=TRUE)
## 
## Phylogenetic tree with 10 tips and 9 internal nodes.
## 
## Tip labels:
##  t4, t2, t9, t5, t10, t8, ...
## 
## Rooted; includes branch lengths.
## read first 3
obj<-read.newick(file="example.trees",nlines=3)
print(obj,details=TRUE)
## 3 phylogenetic trees
## tree 1 : 10 tips
## tree 2 : 20 tips
## tree 3 : 30 tips
## read last 3
obj<-read.newick(file="example.trees",nlines=3,skip=7)
print(obj,details=TRUE)
## 3 phylogenetic trees
## tree 1 : 80 tips
## tree 2 : 90 tips
## tree 3 : 100 tips

The update is here and can be installed from GitHub using the devtools package as follows:

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

That's it!

Saturday, December 12, 2015

Apparent bug in the important ape function root

Here is a quick demo of an apparent bug in the ape function root. The bug seems to affect the branch lengths of the rerooted tree. (Specifically, one of the branch lengths is too long!)

This may affect any function in phytools that uses root internally, such as fastAnc and functions that depend on fastAnc.

library(ape)
tree<-read.tree(text="((A:1,B:1):1,C:1);")
plot(tree,type="unrooted")

plot of chunk unnamed-chunk-1

unroot(tree)$edge.length
## [1] 1 1 2
rerooted<-root(tree,node=5)
plot(rerooted,type="unrooted")

plot of chunk unnamed-chunk-1

unroot(rerooted)$edge.length
## [1] 1 1 3
packageVersion("ape")
## [1] '3.4'
sessionInfo()
## R version 3.2.2 (2015-08-14)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 8 x64 (build 9200)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ape_3.4    knitr_1.11
## 
## loaded via a namespace (and not attached):
## [1] magrittr_1.5    tools_3.2.2     stringi_1.0-1   nlme_3.1-122   
## [5] grid_3.2.2      stringr_1.0.0   lattice_0.20-33 evaluate_0.8

Thursday, December 10, 2015

New version of phytools on CRAN. Check it out!

There is a new version of phytools on CRAN.

Here are some of the updates since the previous version:

  1. A bug fix in make.simmap for Q="mcmc".

  2. A new function fastDist to compute the patristic distance between two species.

  3. A minor bug fix in plotSimmap when the edge order of the input tree is "pruningwise".

  4. The addition of a scale bar for the plot.cophylo S3 method.

  5. A total re-write of the internally used function phyloDesign. (It's also now in the name space.)

  6. A small update to the phytools function to fix malconformed trees (untangle).

  7. Update to cophylo for trees without edge lengths (here).

  8. Functionality to add node, edge, and tip labels to a plotted object of class "cophylo".

  9. New functionality in plotTree.wBars for user control of the bar colors (here).

  10. New interactive modes for the phytools functions bind.tip and reroot.

  11. Update to phylo.to.map to permit right- facing trees.

  12. Update to phylo.to.map to permit use with nodelabels, tiplabels, and edgelabels from ape (here).

  13. An important update for describe.simmap (and thus the S3 summary method for objects of class "multiSimmap") when trees have potentially incongruent topologies.

  14. A new function to simulate a stochastic species-level tree from a genus backbone tree.

  15. An update to contMap to permit user-supplied node states.

  16. A new feature in fastAnc to permit some node states to be fixed.

  17. A new function for computing the relative frequency of different types of state changes along edges in a set of stochastic-mapped trees.

  18. A function to comute the matrix-representation of a tree for mrp.supertree - although it can also be used on it's own (here).

  19. A variety of bug fixes in phenogram, fastAnc, fitMk, and rerootingMethod, detailed here.

  20. And, finally, separate user control of edge & legend line widths in plot.contMap.

Along with some other changes & bug fixes. Check it out!

Wednesday, December 9, 2015

User control of legend width in contMap & densityMap plots

I just pushed a small series of updates to phytools to allow the functions contMap and densityMap (and their associated) S3 plotting methods) to use different line widths for the tree & legend.

This makes a lot of sense - because in some cases (such as for large trees) we might want to use very slender lines for the edges of the tree - but the legend can still be thick so that the color translation to phenotype is clear.

Here's a very quick demo:

library(phytools)
## Loading required package: ape
## Loading required package: maps
## 
##  # ATTENTION: maps v3.0 has an updated 'world' map.        #
##  # Many country borders and names have changed since 1990. #
##  # Type '?world' or 'news(package="maps")'. See README_v3. #
packageVersion("phytools")
## [1] '0.5.9'
tree<-pbtree(n=200,scale=10)
x<-fastBM(tree)
obj<-contMap(tree,x,plot=FALSE)
## change the color map:
obj<-setMap(obj,invert=TRUE)
plot(obj,ftype="off",fsize=c(0,0.9),lwd=c(2,7),
    outline=FALSE,legend=4)

plot of chunk unnamed-chunk-1

That's it. This release of phytools can be installed directly from GitHub using the devtools package as follows:

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

Friday, December 4, 2015

Plotting a color gradient from the bottom to the top of a tree

I just discovered this easy way to plot a constant color gradient from the bottom to the top of a tree using some new functionality of the phytools package:

library(phytools)
tree<-rtree(n=26)
tree$tip.label<-LETTERS
x<-fastBM(tree,sig2=0,mu=1,internal=TRUE)
a<-x[as.character(1:tree$Nnode+Ntip(tree))]
x<-x[tree$tip.label]
obj<-contMap(tree,x,anc.states=a,method="user",plot=FALSE)

Object created, now we're ready to plot:

plot(obj,legend=FALSE) ## defaults

plot of chunk unnamed-chunk-2

inv<-setMap(obj,invert=TRUE) ## inverted
plot(inv,legend=FALSE)

plot of chunk unnamed-chunk-2

gs<-setMap(obj,colors=c("white","black")) ## grey scale
plot(gs,legend=FALSE)

plot of chunk unnamed-chunk-2

It even works for circular trees:

tree<-pbtree(n=200)
x<-fastBM(tree,sig2=0,mu=1,internal=TRUE)
a<-x[as.character(1:tree$Nnode+Ntip(tree))]
x<-x[tree$tip.label]
obj<-contMap(tree,x,anc.states=a,method="user",plot=FALSE)
plot(obj,type="fan",ftype="off",legend=FALSE,lwd=4,outline=FALSE)

plot of chunk unnamed-chunk-3

Does anyone care? I'm not sure.