如何从 R 内存中提取树?

How to extract a tree from R memory?

我使用 "sim.bd.taxa.age" 创建了系统发育树。我只是想知道如何以 newick 格式导出树,因为目前它不是一个 phylo 对象!

library(TreeSim)
Tree <- sim.bd.taxa.age(n=300, numbsim=1, lambda=2, mu=0.5, frac = 1, age=250, mrca = FALSE)

如果你看一下 Tree

的结构
str(Tree)
List of 1
 $ :List of 5
  ..$ edge       : int [1:598, 1:2] 301 301 302 302 305 305 307 307 303 303 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:598] "" "" "" "" ...
  .. .. ..$ : NULL
  ..$ tip.label  : chr [1:300] "t116" "t42" "t124" "t47" ...
  ..$ edge.length: num [1:598] 0.431 2.182 2.292 1.42 1.055 ...
  ..$ Nnode      : int 299
  ..$ root.edge  : num 245
  ..- attr(*, "class")= chr "phylo"

我们看到它 returns 一个长度为 1 的列表,其中一个元素是 class phylo。据推测,它 returns 为您请求的每个模拟生成一棵树 (numbsim)。要提取树,只需执行

Tree[[1]]

所以你可以用

写出这三个
write.tree(Tree[[1]], "newtreefile.txt")

默认以Newick格式写出