Friday, October 14, 2016

Animated optimization method for cophylo

A couple of weeks ago I pushed a few updates to the phytools function for computing a co-phylogenetic object of class "cophylo" that animates the tree-traversal & optimization process.

Note that cophylo uses a greedy optimization routine involving pre-order tree-traversals and node rotations to attempt to minimize the objective function - by default the sum of squared vertical deviations of the tip heights in the two trees.

Here is a looping .gif demo of this animation:

library(phytools)
obj<-cophylo(t1,t2,anim.cophylo=TRUE,link.type="curved",
    link.lwd=3,link.lty="solid",lwd=2,
    link.col=make.transparent("blue",0.25),fsize=1.4)

The final result is here:

plot(obj,link.type="curved",link.lwd=3,link.lty="solid",
    lwd=2,link.col=make.transparent("blue",0.25),fsize=1.2)

plot of chunk unnamed-chunk-2

The updates permitting this visualization are here and can be obtained by installing phytools from CRAN.

Finally, if you too want to create an animated .gif using R, the easiest way to do it is by installing ImageMagick. Then you can do something like the following:

png(file="cophylo-%03d.png",width=600,height=600)
obj<-cophylo(t1,t2,anim.cophylo=TRUE,link.type="curved",
    link.lwd=3,link.lty="solid",lwd=2,
    link.col=make.transparent("blue",0.25),fsize=1.4)
dev.off()
system("magick convert -delay 20 -loop 0 *.png cophylo-anim.gif")
file.remove(list.files(pattern=".png"))

2 comments:

  1. I meant to say that this can be obtained by installing phytools from GitHub. It is not yet on CRAN.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete