]> git.donarmstrong.com Git - ape.git/blobdiff - R/drop.tip.R
final commit for ape 3.0-8
[ape.git] / R / drop.tip.R
index b339578abf490ab764704257d05cff6ee92a842f..113c448eaeaf8805f51460e8ce3a511175e6b017 100644 (file)
@@ -1,8 +1,8 @@
-## drop.tip.R (2011-05-19)
+## drop.tip.R (2012-11-29)
 
 ##   Remove Tips in a Phylogenetic Tree
 
-## Copyright 2003-2011 Emmanuel Paradis
+## Copyright 2003-2012 Emmanuel Paradis
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -54,7 +54,11 @@ extract.clade <- function(phy, node, root.edge = 0, interactive = FALSE)
     if (wbl) phy$edge.length <- phy$edge.length[keep]
     TIPS <- phy$edge[, 2] <= Ntip
     tip <- phy$edge[TIPS, 2]
-    phy$tip.label <- phy$tip.label[sort(tip)] # <- added sort to avoid shuffling of tip labels (2010-07-21)
+    ## Fix by Ludovic Mallet and Mahendra Mariadassou (2011-11-21):
+    name <- vector("character", length(tip))
+    name[order(tip)] <- phy$tip.label[tip]
+    phy$tip.label <- name
+    ## End of fix
     ## keep the ordering so no need to reorder tip.label:
     phy$edge[TIPS, 2] <- order(tip)
     if (!is.null(phy$node.label))
@@ -97,6 +101,7 @@ drop.tip <-
         if (is.character(tip))
             tip <- which(phy$tip.label %in% tip)
     }
+    if (!length(tip)) return(phy)
     if (any(tip > Ntip))
         warning("some tip numbers were higher than the number of tips")
 
@@ -199,20 +204,16 @@ drop.tip <-
             if (is.null(phy$node.label)) rep("NA", length(node2tip))
             else phy$node.label[node2tip - Ntip]
         }
-        if (!is.null(phy$node.label))
-            phy$node.label <- phy$node.label[-(node2tip - Ntip)]
+#        if (!is.null(phy$node.label))
+#            phy$node.label <- phy$node.label[-(node2tip - Ntip)]
         phy$tip.label <- c(phy$tip.label, new.tip.label)
     }
 
-    ## update node.label if needed:
-    if (!is.null(phy$node.label))
-        phy$node.label <- phy$node.label[sort(unique(phy$edge[, 1])) - Ntip]
-
     phy$Nnode <- dim(phy$edge)[1] - n + 1L # update phy$Nnode
 
     ## The block below renumbers the nodes so that they conform
-    ## to the "phylo" format -- same as in root()
-    newNb <- integer(n + phy$Nnode)
+    ## to the "phylo" format, same as in root()
+    newNb <- integer(Ntip + Nnode)
     newNb[NEWROOT] <- n + 1L
     sndcol <- phy$edge[, 2] > n
     ## executed from right to left, so newNb is modified before phy$edge:
@@ -220,6 +221,8 @@ drop.tip <-
         (n + 2):(n + phy$Nnode)
     phy$edge[, 1] <- newNb[phy$edge[, 1]]
     storage.mode(phy$edge) <- "integer"
+    if (!is.null(phy$node.label)) # update node.label if needed
+        phy$node.label <- phy$node.label[which(newNb > 0) - Ntip]
     collapse.singles(phy)
 }