1

私はツリーを持っており、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)

祖先の木

4

2 に答える 2

1

オプションを使用して関数ape::drop.tipを使用trim.internal = FALSEできます。

## 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)
于 2021-03-02T10:54:10.580 に答える