简体   繁体   中英

How to get correct order of tip labels in APE after calling ladderize function

I'm trying to order the rows of a dataframe based on the tip labels found in a phylogenetic tree. The way I was going to do this was to use the match function similar to the answer from this question , however I'm stuck cause the tip.label property of the ape phylo object doesn't change if you reorder the nodes using the ladderize function.

library(ape)
tree <- read.tree(text = "(((A,B),(C,D)),E);")
tree2 <- ladderize(tree, right = FALSE)
tree$tip.label
#> [1] "A" "B" "C" "D" "E"
tree2$tip.label
#> [1] "A" "B" "C" "D" "E"

Notice that the order of the tip.label hasn't changed even though the visual representation of the tree does. In this simple example the visual order of the tree after the ladderize function is EABCD (reading from bottom to top on the tree after plotting). How can I get a copy of the tip.label vector where the order reflects the new order of the nodes in the tree?

It seems the key is to look at the edge property. The tips are always the first nodes to be given an ID, which will simply correspond to the position in the tip.label vector.

library(ape)
tree <- read.tree(text = "(((A,B),(C,D)),E);")
tree2 <- ladderize(tree, right = FALSE)
tree$tip.label
#> [1] "A" "B" "C" "D" "E"
tree2$tip.label
#> [1] "A" "B" "C" "D" "E"
plot(tree2)
nodelabels()
tiplabels()

First step is to filter out internal nodes from the the second column of the edge matrix:

is_tip <- tree2$edge[,2] <= length(tree2$tip.label)
#> [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE

ordered_tips <- tree2$edge[is_tip, 2]
#> [1] 5 1 2 3 4

Then you can use this vector to extract the tips in the right order:

tree2$tip.label[ordered_tips]
#> [1] "E" "A" "B" "C" "D"

I'm trying to order the rows of a dataframe based on the tip labels found in a phylogenetic tree. The way I was going to do this was to use the match function similar to the answer from this question , however I'm stuck cause the tip.label property of the ape phylo object doesn't change if you reorder the nodes using the ladderize function.

library(ape)
tree <- read.tree(text = "(((A,B),(C,D)),E);")
tree2 <- ladderize(tree, right = FALSE)
tree$tip.label
#> [1] "A" "B" "C" "D" "E"
tree2$tip.label
#> [1] "A" "B" "C" "D" "E"

Notice that the order of the tip.label hasn't changed even though the visual representation of the tree does. In this simple example the visual order of the tree after the ladderize function is EABCD (reading from bottom to top on the tree after plotting). How can I get a copy of the tip.label vector where the order reflects the new order of the nodes in the tree?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM