从R中的随机系统树中建立0/1字符矩阵?

时间:2019-01-27 15:52:50

标签: r phylogeny ape-phylo

是否有可能从类似左边的分支系统树中生成0/1字符矩阵,如右下方所示。矩阵中的1表示存在将进化枝团结在一起的共享字符。

这段代码会生成漂亮的随机树,但我不知道从哪里开始将结果转换为字符矩阵。

library(ape) # Other package solutions are acceptable

forest <- rmtree(N = 2, n = 10, br = NULL)
plot(forest)

为清楚起见,我可以使用以下代码生成随机矩阵,然后绘制树。

library(ape)
library(phangorn)

ntaxa <- 10
nchar <- ntaxa - 1

char_mat <- array(0, dim = c(ntaxa, ntaxa - 1))

for (i in 1:nchar) {
  char_mat[,i] <- replace(char_mat[,i], seq(1, (ntaxa+1)-i), 1)
}

char_mat <- char_mat[sample.int(nrow(char_mat)), # Shuffle rows 
                     sample.int(ncol(char_mat))] # and cols

# Ensure all branch lengths > 0
dist_mat <- dist.gene(char_mat) + 0.5
upgma_tree <- upgma(dist_mat)
plot.phylo(upgma_tree, "phylo")

我想要的是生成随机树,然后从这些树中创建矩阵。 This solution的矩阵类型不正确。

为清楚起见进行编辑:我正在生成二进制字符矩阵,学生可以使用它们使用简单的简约来绘制系统树。 1字符表示将分类单元合并为进化枝的同源性。因此,所有行必须共享一个字符(一列中所有行上的1),并且某些字符必须仅由两个分类单元共享。 (我打折autapomorphies。)

示例:

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以直接看一下rTraitDisc中的ape函数:

library(ape)
## You'll need to simulate branch length!
forest <- rmtree(N = 2, n = 10)

## Generate on equal rate model character
(one_character <- rTraitDisc(forest[[1]], type = "ER", states = c(0,1)))
# t10  t7  t5  t9  t1  t4  t2  t8  t3  t6 
#   0   0   0   1   0   0   0   0   0   0 
# Levels: 0 1

## Generate a matrix of ten characters
(replicate(10, rTraitDisc(forest[[1]], type = "ER", states = c(0,1))))

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# t10 "0"  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  
# t7  "0"  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  
# t5  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t9  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t1  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t4  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t2  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t8  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t3  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t6  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"

要将其应用于多棵树,最好的方法是创建一个lapply函数,如下所示:

## Lapply wrapper function
generate.characters <- function(tree) {
    return(replicate(10, rTraitDisc(tree, type = "ER", states = c(0,1))))
}

## Generate 10 character matrices for each tree
lapply(forest, generate.characters)

# [[1]]
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# t10 "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  
# t7  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  
# t5  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  
# t9  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t1  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t4  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t2  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t8  "0"  "0"  "0"  "1"  "0"  "1"  "0"  "0"  "0"  "1"  
# t3  "0"  "0"  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  
# t6  "0"  "0"  "0"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  

# [[2]]
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# t7  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t9  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t5  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t2  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t4  "0"  "1"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  
# t6  "0"  "1"  "0"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  
# t10 "0"  "1"  "1"  "0"  "1"  "1"  "0"  "0"  "0"  "1"  
# t8  "0"  "1"  "1"  "0"  "1"  "0"  "0"  "0"  "0"  "0"  
# t3  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  
# t1  "0"  "1"  "0"  "0"  "0"  "0"  "0"  "0"  "0"  "0" 

另一种选择是使用sim.morpho包中的dispRity。该函数重用了rTraitDisc函数,但是实现了更多模型,并且降低了作为采样分布的速率。它还允许字符看起来更“逼真”,而无需太多不变的数据,并确保生成的字符“看起来”像真实的形态字符(例如具有适当数量的同质体等)。

library(dispRity)
## You're first tree
tree <- forest[[1]]
## Setting up the parameters
my_rates = c(rgamma, rate = 10, shape = 5)
my_substitutions = c(runif, 2, 2)

## HKY binary (15*50)
matrixHKY <- sim.morpho(tree, characters = 50, model = "HKY",
     rates = my_rates, substitution = my_substitutions)

## Mk matrix (15*50) (for Mkv models)
matrixMk <- sim.morpho(tree, characters = 50, model = "ER", rates = my_rates) 

## Mk invariant matrix (15*50) (for Mk models)
matrixMk <- sim.morpho(tree, characters = 50, model = "ER", rates = my_rates,
     invariant = FALSE)

## MIXED model invariant matrix (15*50)
matrixMixed <- sim.morpho(tree, characters = 50, model = "MIXED",
     rates = my_rates, substitution = my_substitutions,  invariant = FALSE,
     verbose = TRUE)

我建议您阅读sim.morpho函数,以获取有关模型工作方式的适当参考,或阅读dispRity package manual中的相关部分。

答案 1 :(得分:0)

我想出了如何使用phangorn包中的Descendants来制作矩阵。我仍然必须使用合适的节点标签对其进行调整,以匹配原始问题中的示例矩阵,但是框架在那里。

library(ape)
library(phangorn)

ntaxa <- 8
nchar <- ntaxa - 1

tree <- rtree(ntaxa, br = NULL)

# Gets descendants, but removes the first ntaxa elements,
# which are the individual tips
desc <- phangorn::Descendants(tree)[-seq(1, ntaxa)]

char_mat <- array(0, dim = c(ntaxa, nchar))

for (i in 1:nchar) {
  char_mat[,i] <- replace(char_mat[,i], y <- desc[[i]], 1)
}

rownames(char_mat) <- tree$tip.label
char_mat
#>    [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> t6    1    1    0    0    0    0    0
#> t3    1    1    1    0    0    0    0
#> t7    1    1    1    1    0    0    0
#> t2    1    1    1    1    1    0    0
#> t5    1    1    1    1    1    0    0
#> t1    1    0    0    0    0    1    1
#> t8    1    0    0    0    0    1    1
#> t4    1    0    0    0    0    1    0

plot(tree)

reprex package(v0.2.1)于2019-01-28创建