Simulating trees for ape

Paul Staab

2024-01-08

The trees in ‘newick’ format produced by scrm’s -T option are compatible with the read.tree function from package ‘ape’. This quick example shows how we can exploid this to visualize the Ancestral Recombination Graph (ARG) simulated with scrm.

First, we call scrm to simulate the ARG:

library(scrm)
sum_stats <- scrm('5 1 -r 1.5 100 -T')
sum_stats$trees[[1]]
## [1] "[11]((1:0.0401804,2:0.0401804):0.892823,(4:0.483554,(5:0.184723,3:0.184723):0.298831):0.449449);"
## [2] "[67]((1:0.0401804,2:0.0401804):0.892823,(4:0.483554,(3:0.157116,5:0.157116):0.326438):0.449449);"
## [3] "[6](4:0.483554,((1:0.0401804,2:0.0401804):0.431344,(3:0.157116,5:0.157116):0.314408):0.0120295);"
## [4] "[7]((3:0.157116,5:0.157116):0.775887,(4:0.483554,(1:0.0401804,2:0.0401804):0.443373):0.449449);" 
## [5] "[9]((4:0.309287,(3:0.157116,5:0.157116):0.152171):0.623716,(1:0.0401804,2:0.0401804):0.892823);"

Now we can convert the trees into ape’s internal format using read.tree:

library(ape)
trees <- read.tree(text = paste0(sum_stats$trees[[1]]))
trees
## 5 phylogenetic trees

And – for example – print the trees:

plot(trees, no.margin = TRUE)