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?

1 comment:

  1. Thank you Liam!! This labeling issue was driving me nuts. When I first read in my tree file with read.nexus and tried to plot bootstrap values, R would still plot node labels, but they didn't correspond to any numbers I could find in my nexus file. Any idea where it was pulling those numbers from? It's not too important but I was just puzzled, and curious.

    ReplyDelete

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