]> git.donarmstrong.com Git - ape.git/blobdiff - R/drop.tip.R
new compute.brtime() + minor change in dist.topo.R
[ape.git] / R / drop.tip.R
index 2d2a8f0165847347777db326584b10c1811543f1..b339578abf490ab764704257d05cff6ee92a842f 100644 (file)
@@ -1,29 +1,31 @@
-## drop.tip.R (2009-09-09)
+## drop.tip.R (2011-05-19)
 
 ##   Remove Tips in a Phylogenetic Tree
 
-## Copyright 2003-2009 Emmanuel Paradis
+## Copyright 2003-2011 Emmanuel Paradis
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
 
-extract.clade <- function(phy, node, root.edge = 0)
+extract.clade <- function(phy, node, root.edge = 0, interactive = FALSE)
 {
     Ntip <- length(phy$tip.label)
     ROOT <- Ntip + 1
     Nedge <- dim(phy$edge)[1]
     wbl <- !is.null(phy$edge.length)
-    if (length(node) > 1) {
-        node <- node[1]
-        warning("only the first value of 'node' has been considered")
-    }
-    if (is.character(node)) {
-        if (is.null(phy$node.label))
-            stop("the tree has no node labels")
-        node <- which(phy$node.label %in% node) + Ntip
+    if (interactive) node <- identify(phy)$nodes else {
+        if (length(node) > 1) {
+            node <- node[1]
+            warning("only the first value of 'node' has been considered")
+        }
+        if (is.character(node)) {
+            if (is.null(phy$node.label))
+                stop("the tree has no node labels")
+            node <- which(phy$node.label %in% node) + Ntip
+        }
+        if (node <= Ntip)
+            stop("node number must be greater than the number of tips")
     }
-    if (node <= Ntip)
-        stop("node number must be greater than the number of tips")
     if (node == ROOT) return(phy)
     phy <- reorder(phy) # insure it is in cladewise order
     root.node <- which(phy$edge[, 2] == node)
@@ -52,7 +54,7 @@ extract.clade <- function(phy, node, root.edge = 0)
     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[tip]
+    phy$tip.label <- phy$tip.label[sort(tip)] # <- added sort to avoid shuffling of tip labels (2010-07-21)
     ## keep the ordering so no need to reorder tip.label:
     phy$edge[TIPS, 2] <- order(tip)
     if (!is.null(phy$node.label))
@@ -73,15 +75,30 @@ extract.clade <- function(phy, node, root.edge = 0)
 
 drop.tip <-
     function(phy, tip, trim.internal = TRUE, subtree = FALSE,
-             root.edge = 0, rooted = is.rooted(phy))
+             root.edge = 0, rooted = is.rooted(phy), interactive = FALSE)
 {
     if (!inherits(phy, "phylo"))
         stop('object "phy" is not of class "phylo"')
+    if (!length(tip)) return(phy)
 
     Ntip <- length(phy$tip.label)
     ## find the tips to drop:
-    if (is.character(tip))
-        tip <- which(phy$tip.label %in% tip)
+    if (interactive) {
+        cat("Left-click close to the tips you want to drop; right-click when finished...\n")
+        xy <- locator()
+        nToDrop <- length(xy$x)
+        tip <- integer(nToDrop)
+        lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
+        for (i in 1:nToDrop) {
+            d <- sqrt((xy$x[i] - lastPP$xx)^2 + (xy$y[i] - lastPP$yy)^2)
+            tip[i] <- which.min(d)
+        }
+    } else {
+        if (is.character(tip))
+            tip <- which(phy$tip.label %in% tip)
+    }
+    if (any(tip > Ntip))
+        warning("some tip numbers were higher than the number of tips")
 
     if (!rooted && subtree) {
         phy <- root(phy, (1:Ntip)[-tip][1])
@@ -105,15 +122,6 @@ drop.tip <-
     edge2 <- phy$edge[, 2] #
     keep <- !logical(Nedge)
 
-    ## find the tips to drop:
-    if (is.character(tip))
-        tip <- which(phy$tip.label %in% tip)
-
-    if (!rooted && subtree) {
-        phy <- root(phy, (1:Ntip)[-tip][1])
-        root.edge <- 0
-    }
-
     ## delete the terminal edges given by `tip':
     keep[match(tip, edge2)] <- FALSE
 
@@ -165,21 +173,27 @@ drop.tip <-
     ## get the old No. of the nodes and tips that become tips:
     oldNo.ofNewTips <- phy$edge[TERMS, 2]
 
+    ## in case some tips are dropped but kept because of 'subtree = TRUE':
+    if (subtree) {
+        i <- which(tip %in% oldNo.ofNewTips)
+        if (length(i)) {
+            phy$tip.label[tip[i]] <- "[1_tip]"
+            tip <- tip[-i]
+        }
+    }
+
     n <- length(oldNo.ofNewTips) # the new number of tips in the tree
 
-    ## the tips may not be sorted in increasing order of their
-    ## in the 2nd col of edge, so no need to reorder $tip.label
+    ## the tips may not be sorted in increasing order in the
+    ## 2nd col of edge, so no need to reorder $tip.label
     phy$edge[TERMS, 2] <- rank(phy$edge[TERMS, 2])
+    phy$tip.label <- phy$tip.label[-tip]
 
     ## make new tip labels if necessary:
     if (subtree || !trim.internal) {
-        ## get the logical indices of the tips that are kept within 'oldNo.ofNewTips':
-        tips.kept <- oldNo.ofNewTips <= Ntip & !(oldNo.ofNewTips %in% tip)
-        new.tip.label <- character(n)
-        new.tip.label[tips.kept] <- phy$tip.label[-tip]
         ## get the numbers of the nodes that become tips:
-        node2tip <- oldNo.ofNewTips[!tips.kept]
-        new.tip.label[!tips.kept] <- if (subtree) {
+        node2tip <- oldNo.ofNewTips[oldNo.ofNewTips > Ntip]
+        new.tip.label <- if (subtree) {
             paste("[", N[node2tip], "_tips]", sep = "")
         } else {
             if (is.null(phy$node.label)) rep("NA", length(node2tip))
@@ -187,8 +201,8 @@ drop.tip <-
         }
         if (!is.null(phy$node.label))
             phy$node.label <- phy$node.label[-(node2tip - Ntip)]
-        phy$tip.label <- new.tip.label
-    } else phy$tip.label <- phy$tip.label[-tip]
+        phy$tip.label <- c(phy$tip.label, new.tip.label)
+    }
 
     ## update node.label if needed:
     if (!is.null(phy$node.label))
@@ -208,3 +222,4 @@ drop.tip <-
     storage.mode(phy$edge) <- "integer"
     collapse.singles(phy)
 }
+