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!

No comments:

Post a Comment