[英]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.我打算这样做的方法是使用类似于这个问题的答案的match
函数,但是我卡住了,因为如果你使用ladderize
重新排序节点, ape
phylo 对象的tip.label
属性不会改变功能。
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.注意tip.label
的顺序没有改变,即使树的视觉表示发生了改变。 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).在这个简单的例子中,在ladderize
函数之后树的视觉顺序是EABCD
(绘制后在树上从下到上阅读)。 How can I get a copy of the tip.label
vector where the order reflects the new order of the nodes in the tree?如何获取tip.label
向量的副本,其中的顺序反映了树中节点的新顺序?
It seems the key is to look at the edge
property.看来关键是看edge
属性。 The tips are always the first nodes to be given an ID, which will simply correspond to the position in the tip.label
vector.提示总是第一个被赋予 ID 的节点,它将简单地对应于tip.label
向量中的位置。
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.我要执行此操作的方法是使用match
该问题的答案类似的match
函数,但是我卡住了,这是因为如果使用ladderize
对节点重新排序,则ape
门对象的tip.label
属性不会改变功能。
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.请注意,即使树的视觉表示发生了变化, tip.label
的顺序也没有改变。 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).在这个简单的示例中, ladderize
函数之后的树的视觉顺序为EABCD
(绘制后从树的底部到顶部读取)。 How can I get a copy of the tip.label
vector where the order reflects the new order of the nodes in the tree?我如何获得tip.label
向量的副本,其中的顺序反映了树中节点的新顺序?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.