本文介绍了系统发育树-如何按物种矩阵创建分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用R中的系统发生树,我想创建一个矩阵,该矩阵指示树的每个分支(B1至B8)是否与每个物种(A至E)相关联,其中1s表示该分支与相关联. (如下所示)

Working with a phylogenetic tree in R, I would like to create a matrix which indicates if each branch of the tree (B1 to B8) is associated with each species (A to E), where 1s indicate that the branch is associated. (Shown below)

R函数which.edge()可用于识别物种的末端分支.但它不能识别与每个物种相关的所有分支.我可以使用什么功能来识别每个物种从树根到树梢的所有树枝?

The R function which.edge() is useful for identifying the terminal branch for a species. but it doesn't identify ALL the branches associated with each species. What function could I use to identify all the branches in the tree that go from the root to the tip for each species?

示例树

library(ape)
ex.tree <- read.tree(text="(A:4,((B:1,C:1):2,(D:2,E:2):1):1);")
plot(ex.tree)
edgelabels() #shows branches 1-8

这是我要创建的矩阵(物种A-E为列,分支B1-B8为行),但是具有简单的功能,而不是手工创建.

The is the matrix I would like to create (Species A-E as columns, Branches B1-B8 as rows), but with an easy function rather than by hand.

B1 <- c(1,0,0,0,0)
B2 <- c(0,1,1,1,1)
B3 <- c(0,1,1,0,0)
B4 <- c(0,1,0,0,0)
B5 <- c(0,0,1,0,0)
B6 <- c(0,0,0,1,1)
B7 <- c(0,0,0,1,0)
B8 <- c(0,0,0,0,1)
Mat <- rbind(B1,B2,B3,B4,B5,B6,B7,B8)
colnames(Mat) <- c("A","B","C","D","E")
Mat

例如,分支B2进入物种B-E,但不进入物种A.对于物种E,存在分支B2,B6,B8.

For example, Branch B2 goes to species B-E, but not to species A. For Species E, branches B2, B6, B8 are present.

哪个R函数最合适?预先感谢!

Which R function(s) would be best? Thanks in advance!

推荐答案

我不知道执行此操作的任何内置函数.我编写了一个辅助函数,可以根据存储在tree对象中的边缘数据来计算此函数.

I am unaware of any built-in function that does this. I wrote a helper function that can calculate this from the edge data stored in the tree object.

branchNodeAdjacency <- function(x) {
    m <- matrix(0, ncol=nt, nrow=nrow(x$edge))
    from <- x$edge[,1]
    to <- x$edge[,2]
    g <- seq_along(x$tip.label)
    while (any(!is.na(g))) {
        i <- match(g, to)
        m[cbind(i, seq_along(i))] <- 1
        g <- from[i]
    }
    rownames(m) <- paste0("B", seq.int(nrow(m)))
    colnames(m) <- x$tip.label
    m
}

branchNodeAdjacency(ex.tree)
#    A B C D E
# B1 1 0 0 0 0
# B2 0 1 1 1 1
# B3 0 1 1 0 0
# B4 0 1 0 0 0
# B5 0 0 1 0 0
# B6 0 0 0 1 1
# B7 0 0 0 1 0
# B8 0 0 0 0 1

我们的想法是跟踪每个内部节点代表哪些叶子节点值.

The idea is we keep track of which leaf node values are represented by each internal node.

这篇关于系统发育树-如何按物种矩阵创建分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 01:11