从 cutree 组 R 的祖先节点获取树
Get tree from ancestor nodes of cutree groups R
我有一棵树,我想得到树的一部分,它是 cutree 组的祖先。
library(stats)
library(ape)
tree <- ape::read.tree(text = "((run2:1.2,run7:1.2)master3:1.5,((run8:1.1,run14:1.1)master5:0.2,(run9:1.0,run6:1.0)master6:0.3)master4:1.4)master2;")
plot(tree, show.node.label = TRUE)
我正在使用 cutree
获取一定数量的群组:
cutree(as.hclust(tree), k = 3)
run2 run7 run8 run14 run9 run6
1 1 2 2 3 3
如何获取树的左边部分?本质上是这个 tree2
:
tree2 <- ape::read.tree(text = "(master3:1.5,(master5:0.2,master6:0.3)master4:1.4)master2;")
plot(tree2, show.node.label = TRUE)
phangorn 包有一个 Ancestors 函数,但只会 return 列表结构中的索引,而不是树结构中的标签。所以我为你标记了它们(检查图表以确保标记的索引是正确的),但我会把它留给你把它变回一棵树。
library(phangorn)
tree.labels <- c(tree$tip.label, tree$node.label)
tree.ancestors <- Ancestors(tree)
names(tree.ancestors) <- tree.labels
tree.ancestors.labelled <- lapply(tree.ancestors, function(ind.vec) tree.labels[ind.vec])
tree.ancestors.labelled
$run2
[1] "master3" "master2"
$run7
[1] "master3" "master2"
$run8
[1] "master5" "master4" "master2"
$run14
[1] "master5" "master4" "master2"
$run9
[1] "master6" "master4" "master2"
$run6
[1] "master6" "master4" "master2"
$master2
character(0)
$master3
[1] "master2"
$master4
[1] "master2"
$master5
[1] "master4" "master2"
$master6
[1] "master4" "master2"
您可以通过 trim.internal = FALSE
选项使用函数 ape::drop.tip
:
## Dropping all the tips and their edges leading to the nodes (internals)
tree2 <- drop.tip(tree, tip = tree$tip.label, trim.internal = FALSE)
当然,如果你需要,你也可以指定只丢弃你的hclust::cutree
函数结果后的某个数字:
## Dropping the tips and their edges that have a cut value equal to the scalar k
k = 3
tips_to_drop <- names(which(cutree(as.hclust(tree), k = k) == k))
tree3 <- drop.tip(tree, tip = tips_to_drop, trim.internal = FALSE)
如果您还想删除节点和提示(比如删除所有提示和节点 "master6"
和 "master5"
,您可以再次将该树传递给相同的 drop.tip
函数:
## Removing master6 and master5 from the tree2
tree3 <- drop.tip(tree2, tip = c("master5", "master6"), trim.internal = FALSE)
## Or the same but with nested functions
tips_to_drop <- tree$tip.label
nodes_to_drop <- c("master5", "master6")
tree3 <- drop.tip(drop.tip(tree, tip = tips_to_drop, trim.internal = FALSE),
tip = nodes_to_drop, trim.internal = FALSE)
我有一棵树,我想得到树的一部分,它是 cutree 组的祖先。
library(stats)
library(ape)
tree <- ape::read.tree(text = "((run2:1.2,run7:1.2)master3:1.5,((run8:1.1,run14:1.1)master5:0.2,(run9:1.0,run6:1.0)master6:0.3)master4:1.4)master2;")
plot(tree, show.node.label = TRUE)
我正在使用 cutree
获取一定数量的群组:
cutree(as.hclust(tree), k = 3)
run2 run7 run8 run14 run9 run6
1 1 2 2 3 3
如何获取树的左边部分?本质上是这个 tree2
:
tree2 <- ape::read.tree(text = "(master3:1.5,(master5:0.2,master6:0.3)master4:1.4)master2;")
plot(tree2, show.node.label = TRUE)
phangorn 包有一个 Ancestors 函数,但只会 return 列表结构中的索引,而不是树结构中的标签。所以我为你标记了它们(检查图表以确保标记的索引是正确的),但我会把它留给你把它变回一棵树。
library(phangorn)
tree.labels <- c(tree$tip.label, tree$node.label)
tree.ancestors <- Ancestors(tree)
names(tree.ancestors) <- tree.labels
tree.ancestors.labelled <- lapply(tree.ancestors, function(ind.vec) tree.labels[ind.vec])
tree.ancestors.labelled
$run2
[1] "master3" "master2"
$run7
[1] "master3" "master2"
$run8
[1] "master5" "master4" "master2"
$run14
[1] "master5" "master4" "master2"
$run9
[1] "master6" "master4" "master2"
$run6
[1] "master6" "master4" "master2"
$master2
character(0)
$master3
[1] "master2"
$master4
[1] "master2"
$master5
[1] "master4" "master2"
$master6
[1] "master4" "master2"
您可以通过 trim.internal = FALSE
选项使用函数 ape::drop.tip
:
## Dropping all the tips and their edges leading to the nodes (internals)
tree2 <- drop.tip(tree, tip = tree$tip.label, trim.internal = FALSE)
当然,如果你需要,你也可以指定只丢弃你的hclust::cutree
函数结果后的某个数字:
## Dropping the tips and their edges that have a cut value equal to the scalar k
k = 3
tips_to_drop <- names(which(cutree(as.hclust(tree), k = k) == k))
tree3 <- drop.tip(tree, tip = tips_to_drop, trim.internal = FALSE)
如果您还想删除节点和提示(比如删除所有提示和节点 "master6"
和 "master5"
,您可以再次将该树传递给相同的 drop.tip
函数:
## Removing master6 and master5 from the tree2
tree3 <- drop.tip(tree2, tip = c("master5", "master6"), trim.internal = FALSE)
## Or the same but with nested functions
tips_to_drop <- tree$tip.label
nodes_to_drop <- c("master5", "master6")
tree3 <- drop.tip(drop.tip(tree, tip = tips_to_drop, trim.internal = FALSE),
tip = nodes_to_drop, trim.internal = FALSE)