有没有办法重新排序 phylo 对象的尖端标签,以便它们与另一个 phylo 对象一致?

Is there a way to reorder tip labels of a phylo object so that they are consistent with another phylo object?

我要比较两棵树。一个来自 mrbayes,另一个来自 Paup。

当我使用 read.nexus 时,生成的 phylo 对象具有不同顺序的尖端标签,尽管这两个文件具有相同的 nexus 类群块。

我在下面复制了这些树:

library(ape)
#trees
t1 <- structure(list(edge = structure(c(29L, 30L, 30L, 30L, 30L, 31L, 
31L, 32L, 32L, 33L, 34L, 35L, 35L, 35L, 34L, 36L, 37L, 38L, 38L, 
38L, 38L, 37L, 39L, 40L, 40L, 39L, 39L, 39L, 39L, 36L, 36L, 36L, 
36L, 33L, 31L, 31L, 31L, 31L, 29L, 30L, 1L, 2L, 3L, 31L, 4L, 
32L, 5L, 33L, 34L, 35L, 6L, 7L, 8L, 36L, 37L, 38L, 9L, 10L, 11L, 
12L, 39L, 40L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 27L, 28L), .Dim = c(39L, 2L)), Nnode = 12L, 
    tip.label = c("Metaspriggina", "Eptatretus", "Mordacia", 
    "Lasanius", "Phlebolepis", "Zenaspis", "Diademaspis", "Ukrainaspis", 
    "Tamiobatis", "Ischnacanthus", "Mesacanthus", "Tetanopsyrus", 
    "Mimipiscis", "Raynerius", "Diabolepis", "Guiyu", "Gavinia", 
    "Qingmenodus", "Kujdanowiaspis", "Cowralepis", "Jagorina", 
    "Macropetalichthys", "Polybranchiaspis", "Cyathaspis", "Irregulareaspis", 
    "Listraspis", "Unarkaspis", "Pikaia")), class = "phylo", order = "cladewise")
    
t2 <- structure(list(edge = structure(c(29L, 29L, 30L, 30L, 31L, 32L, 
33L, 33L, 32L, 31L, 31L, 31L, 31L, 31L, 31L, 34L, 35L, 36L, 37L, 
37L, 36L, 35L, 38L, 39L, 40L, 41L, 42L, 42L, 42L, 42L, 41L, 43L, 
44L, 44L, 43L, 45L, 46L, 47L, 47L, 46L, 45L, 40L, 48L, 48L, 39L, 
38L, 34L, 1L, 30L, 2L, 31L, 32L, 33L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 34L, 35L, 36L, 37L, 11L, 12L, 13L, 38L, 39L, 40L, 41L, 
42L, 21L, 23L, 24L, 17L, 43L, 44L, 22L, 27L, 45L, 46L, 47L, 28L, 
26L, 25L, 16L, 48L, 15L, 19L, 18L, 20L, 14L), .Dim = c(47L, 2L
)), Nnode = 20L, tip.label = c("Pikaia", "Metaspriggina", "Eptatretus", 
"Mordacia", "Lasanius", "Phlebolepis", "Cyathaspis", "Irregulareaspis", 
"Listraspis", "Unarkaspis", "Zenaspis", "Diademaspis", "Ukrainaspis", 
"Polybranchiaspis", "Cowralepis", "Guiyu", "Ischnacanthus", "Jagorina", 
"Kujdanowiaspis", "Macropetalichthys", "Mesacanthus", "Mimipiscis", 
"Tamiobatis", "Tetanopsyrus", "Diabolepis", "Gavinia", "Raynerius", 
"Qingmenodus")), class = "phylo", order = "cladewise")

#tip labels are the same 
all.equal(sort(t1$tip.label), sort(t2$tip.label))
#[1] TRUE

#tip label order is different
all.equal(t1$tip.label, t2$tip.label)
#[1] "26 string mismatches" 

有没有办法重新排序t1的tip标签,使其与t2一致?

解决方案在很大程度上取决于您要对树木做什么。大多数系统发育比较方法在树中使用相同的名称并正确分配它们。可以找到两棵树的简单匹配:

  match(match(t1$tip.label, t2$tip.label)
  # [1]  2  3  4  5  6 11 12 13 23 17 21 24 22 27 25 16 26 28 19 15 18 20 14  7  8  9 10  1

要重新排序树,您可以使用 ladderize(t1),但这只会影响绘图,不会影响提示标签顺序。如何重新编号节点并在梯形显示中保留提示标签的一个很好、简单的技巧是保存树并再次加载它。

 write.tree(ladderize(t1), file = "t1.tre")
 t1 = read.tree("t1.tre")

但是,您的树具有不同的拓扑结构,因此必须针对下游分析匹配尖端标签。

cophyloplot(ladderize(t1), ladderize(t2), 
            assoc = matrix(rep(t1$tip.label, 2), ncol = 2))

我想这就是你想要的:

trees <- c(t1, t2) 
trees <- .compressTipLabel(trees)
t2 <- trees[[2]]
all.equal(t1$tip.label, t2$tip.label)