rphylogenyape-phylo

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


I have two trees that I am trying to compare. One from mrbayes, another from Paup.

When I use read.nexus, the resulting phylo objects have differently ordered tip labels, despite the fact that both files have the same nexus taxa block.

I've reproduced these trees below:

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" 

Is there a way to reorder the tip labels of t1, so they are consistent with t2?


Solution

  • I think this is what you are after:

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