1

I have a tree and I want to get part of the tree that is ancestors of cutree groups.

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)

Full tree

I am using cutree to get a certain number of groups:

cutree(as.hclust(tree), k = 3)
 run2  run7  run8 run14  run9  run6 
    1     1     2     2     3     3 

How to get the left part of tree? Essentially this 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)

ancestor tree

Shubham Gupta
  • 650
  • 6
  • 18

2 Answers2

1

You can use the function ape::drop.tip using the trim.internal = FALSE option:

## Dropping all the tips and their edges leading to the nodes (internals)
tree2 <- drop.tip(tree, tip = tree$tip.label, trim.internal = FALSE)

Of course, should you need, you can also specify to drop only a certain number following your hclust::cutree function results:

## 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)

If you want to also remove nodes as well as tips (say removing all tips AND nodes "master6" and "master5" you can pass that tree to the same drop.tip function again:

## 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)
Thomas Guillerme
  • 1,747
  • 4
  • 16
  • 23
0

The phangorn package has an Ancestors function, but will only return indices in a list structure, not labels in a tree structure. So I labelled them for you (checked the graph to make sure the index to label is correct), but I'll leave it up to you to turn it back into a tree.

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"
RonB
  • 1