Monday, August 2, 2021

Old & new function to read NEXUS tree file with certain annotation

Recently a phytools user contacted me because he was having difficulty reading a tree with bootstrap values as node labels (saved in NEXUS format from the software RAxML) into R.

The ape function read.nexus reads in a NEXUS tree file, but doesn't handle node annotation.

The phytools function read.simmap can read in a particular type of annotation created by the software SIMMAP.

The packages ips and treeio also read in specially formatted tree files, but I've forgotten how to use them, and so I wasn't able to help him get these to work.

I did suddenly remember that I'd written some code many* years ago (not kidding – 8 years, if you can believe it) to try and read a MrBayes annotated tree in NEXUS format from file. I never finished that code, but realized that I could re-purpose it to read in our RAxML tree.

I just added this to phytools in the form of readNexus. Let's give it a try using the phytools user's originally tree, but with tip labels hidden.

library(phytools)
packageVersion("phytools")
## [1] '0.7.84'
tree<-readNexus(file="RAxML_bipartitions.result.tre",
    format="raxml")

Now let's plot our tree:

plotTree(tree,ftype="i",fsize=0.5)
nodelabels(tree$node.label,cex=0.5)

plot of chunk unnamed-chunk-3

That's pretty hard to read. Let's first ladderize the tree and the show our bootstrap values using pie-charts instead of numbers. To do that, I will also need to convert my node labels to numeric mode, because they are currently stored as characters.

ladderized<-ladderize(tree)
plotTree(ladderized,ftype="i",fsize=0.5,lwd=1,
    direction="upwards")
pies<-cbind( 
    as.numeric(ladderized$node.label)/100,
    1-as.numeric(ladderized$node.label)/100)
nodelabels(pie=pies,piecol=c("black","white"),
    cex=0.3)
legend(5,0.35,c("0% bootstrap","100% bootstrap"),
    pch=c(21,21),pt.bg=c("white","black"),
    cex=0.8,bty="n",pt.cex=1.5)

plot of chunk unnamed-chunk-4

That's OK, right?

No comments:

Post a Comment

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