Friday, June 5, 2026

ansi_phylo ANSI text phylogeny simulator in R

While I was taking a break from fiddling around with the endeavor of implementing a discrete-character-dependent multi-$\theta$ OU model (if such a thing is even possible) using the discretized diffusion approximation, I stumbled on an old phytools called ansi_phylo for ANSI text style phylogenetic tree plotting in R.

It creates a pretty fun visual, so I thought it might be worth a re-post. Here’s a quick demo:

## load packages
library(phytools)
library(phangorn)
## load data
data(Laurasiatherian)
Laurasiatherian
## 47 sequences with 3179 character and 1605 different site patterns.
## The states are a c g t
## estimate tree using ML in phangorn
mammal_mle<-pml_bb(Laurasiatherian,model="K80")
## optimize edge weights:  -54315.47 --> -54130.45 
## optimize rate matrix:  -54130.45 --> -51361.62 
## ...
## optimize topology:  -52200.08 --> -51341.22  NNI moves:  13 
## Ratchet iteration  100 , best pscore so far: -51341.22 
## optimize rate matrix:  -51341.22 --> -51341.22 
## optimize edge weights:  -51341.22 --> -51341.22 
## optimize topology:  -51341.22 --> -51341.22  NNI moves:  0
mammal_mle
## model: K80 
## loglikelihood: -51341.22 
## unconstrained loglikelihood: -17300.92 
## 
## Rate matrix:
##          a        c        g        t
## a 0.000000 1.000000 5.025414 1.000000
## c 1.000000 0.000000 1.000000 5.025414
## g 5.025414 1.000000 0.000000 1.000000
## t 1.000000 5.025414 1.000000 0.000000
## 
## Base frequencies:  
##    a    c    g    t 
## 0.25 0.25 0.25 0.25
## get ML tree and root using Platypus as outgroup
mammal_mle.phy<-root(mammal_mle$tree,outgroup="Platypus",
  resolve.root=TRUE)
ansi_phylo(mammal_mle.phy,horizontal="=")

plot of chunk unnamed-chunk-11

That’s it!

Here, let’s try another one.

data(yeast)
yeast
## 8 sequences with 127026 character and 8899 different site patterns.
## The states are a c g t
yeast_mle<-pml_bb(yeast,model="K80")
## optimize edge weights:  -739078.1 --> -734615.7 
## optimize rate matrix:  -734615.7 --> -715533.2 
## ...
## optimize topology:  -718794.8 --> -715391.4  NNI moves:  1 
## Ratchet iteration  100 , best pscore so far: -715391.4 
## optimize rate matrix:  -715391.4 --> -715387.4 
## optimize edge weights:  -715387.4 --> -715387.3 
## optimize topology:  -715387.3 --> -715387.3  NNI moves:  0 
## optimize rate matrix:  -715387.3 --> -715387.3 
## optimize edge weights:  -715387.3 --> -715387.3
yeast_mle
## model: K80 
## loglikelihood: -715387.3 
## unconstrained loglikelihood: -659104.8 
## 
## Rate matrix:
##          a        c        g        t
## a 0.000000 1.000000 3.291576 1.000000
## c 1.000000 0.000000 1.000000 3.291576
## g 3.291576 1.000000 0.000000 1.000000
## t 1.000000 3.291576 1.000000 0.000000
## 
## Base frequencies:  
##    a    c    g    t 
## 0.25 0.25 0.25 0.25
rooted.yeast_tree<-midpoint(yeast_mle$tree)
ansi_phylo(rooted.yeast_tree,vertical="*")

plot of chunk unnamed-chunk-15

No comments:

Post a Comment

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