File: drop.tip.R

package info (click to toggle)
r-cran-ape 5.8-1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,676 kB
  • sloc: ansic: 7,676; cpp: 116; sh: 17; makefile: 2
file content (286 lines) | stat: -rw-r--r-- 9,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
## drop.tip.R (2023-01-09)

##   Remove Tips in a Phylogenetic Tree

## Copyright 2003-2023 Emmanuel Paradis, 2017-2023 Klaus Schliep, 2018 Joseph Brown

## This file is part of the R-package `ape'.
## See the file ../COPYING for licensing issues.

keep.tip <- function(phy, tip, ...) UseMethod("keep.tip")

keep.tip.phylo <- function(phy, tip, ...)
{
    Ntip <- length(phy$tip.label)
    ## convert to indices if strings passed in
    if (is.character(tip)) {
        idx <- match(tip, phy$tip.label)
        ## stop on bad tip names
        ## alternative is to warn but proceed. not sure what stance is
        if (anyNA(idx)) {
            um <- c("unmatched tip labels:\n", paste(tip[is.na(idx)], collapse = " "))
            stop(um)
        }
        tip <- idx
    } else {
        # check that passed in indices are all valid
        out.of.range <- tip > Ntip
        if (any(out.of.range)) {
            warning("some tip numbers were larger than the number of tips: they were ignored")
            tip <- tip[!out.of.range]
        }
    }
    ## get complement tip indices to drop
    toDrop <- setdiff(1:Ntip, tip)
    drop.tip(phy, toDrop, ...)
}

extract.clade <- function(phy, node, root.edge = 0, collapse.singles = TRUE, interactive = FALSE)
{
    n <- length(phy$tip.label)
    if (interactive) {
        cat("Click close to the node...\n")
        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 <- match(node, phy$node.label) + n
            if (is.na(node)) stop("'node' not among the node labels.")
        }
        if (node <= n)
            stop("node number must be greater than the number of tips")
    }
    if (node == n + 1L) return(phy)
    keep <- prop.part(phy)[[node - n]]
    drop.tip(phy, (1:n)[-keep], root.edge = root.edge, rooted = TRUE,
             collapse.singles = collapse.singles)
}

drop.tip <- function(phy, tip, ...) UseMethod("drop.tip")

drop.tip.phylo <- function(phy, tip, trim.internal = TRUE, subtree = FALSE,
    root.edge = 0, rooted = is.rooted(phy), collapse.singles = TRUE,
    interactive = FALSE, ...)
{
    Ntip <- length(phy$tip.label)
    ## find the tips to drop:
    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)
    }

    out.of.range <- tip > Ntip
    if (any(out.of.range)) {
        warning("some tip numbers were larger than the number of tips: they were ignored")
        tip <- tip[!out.of.range]
    }

    if (!length(tip)) return(phy)

    if (length(tip) == Ntip) {
        if (Nnode(phy) < 3 || trim.internal) { # by Klaus (2018-06-21)
            warning("drop all tips of the tree: returning NULL")
            return(NULL)
        }
    }

    wbl <- !is.null(phy$edge.length)

    if (length(tip) == Ntip - 1 && trim.internal) {
        i <- which(phy$edge[, 2] == (1:Ntip)[-tip])
        res <- list(edge = matrix(2:1, 1, 2),
                    tip.label = phy$tip.label[phy$edge[i, 2]],
                    Nnode = 1L)
        class(res) <- "phylo"
        if (wbl) res$edge.length <- phy$edge.length[i]
        if (!is.null(phy$node.label))
            res$node.label <- phy$node.label[phy$edge[i, 1] - Ntip]
        return(res)
    }

    if (!rooted) {
        phy$root.edge <- NULL # moved from below (2021-09-29)
        if (subtree) {
            phy <- root(phy, (1:Ntip)[-tip][1])
            root.edge <- 0
        }
    }

    phy <- reorder(phy)
    NEWROOT <- ROOT <- Ntip + 1
    Nnode <- phy$Nnode
    Nedge <- dim(phy$edge)[1]
    if (subtree) {
        trim.internal <- TRUE
        tr <- reorder(phy, "postorder")
        N <- .C(node_depth, as.integer(Ntip),
                as.integer(tr$edge[, 1]), as.integer(tr$edge[, 2]),
                as.integer(Nedge), double(Ntip + Nnode), 1L)[[5]]
    }

    edge1 <- phy$edge[, 1] # local copies
    edge2 <- phy$edge[, 2] #
    keep <- !logical(Nedge)

    ## delete the terminal edges given by `tip':
    keep[match(tip, edge2)] <- FALSE

    if (trim.internal) {
        ints <- edge2 > Ntip
        ## delete the internal edges that do not have anymore
        ## descendants (ie, they are in the 2nd col of `edge' but
        ## not in the 1st one)
        repeat {
            sel <- !(edge2 %in% edge1[keep]) & ints & keep
            if (!sum(sel)) break
            keep[sel] <- FALSE
        }
        if (subtree) {
            ## keep the subtending edge(s):
            subt <- edge1 %in% edge1[keep] & edge1 %in% edge1[!keep]
            keep[subt] <- TRUE
        }
        if (root.edge && wbl) {
            degree <- tabulate(edge1[keep])
            if (degree[ROOT] == 1) {
                j <- integer(0) # will store the indices of the edges below the new root
                repeat {
                    i <- which(edge1 == NEWROOT & keep)
                    j <- c(i, j)
                    NEWROOT <- edge2[i]
                    ## degree <- tabulate(edge1[keep]) # utile ?
                    if (degree[NEWROOT] > 1) break
                }
                keep[j] <- FALSE
                ## if (length(j) > root.edge) j <- 1:root.edge
                j <- j[1:root.edge]
                NewRootEdge <- sum(phy$edge.length[j])
                if (length(j) < root.edge && !is.null(phy$root.edge))
                    NewRootEdge <- NewRootEdge + phy$root.edge
                phy$root.edge <- NewRootEdge
            }
        }
    }

    ##if (!root.edge) phy$root.edge <- NULL # moved above (2021-09-29)

    ## drop the edges
    phy$edge <- phy$edge[keep, ]
    if (wbl) phy$edge.length <- phy$edge.length[keep]

    ## find the new terminal edges (works whatever 'subtree' and 'trim.internal'):
    TERMS <- !(phy$edge[, 2] %in% phy$edge[, 1])

    ## 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 in the
    ## 2nd col of edge, so no need to reorder $tip.label
    phy$edge[TERMS, 2] <- rank(phy$edge[TERMS, 2])
    ## fix by Thomas Sibley (2017-10-28):
    if (length(tip)) phy$tip.label <- phy$tip.label[-tip]

    ## make new tip labels if necessary:
    if (subtree || !trim.internal) {
        ## get the numbers of the nodes that become tips:
        node2tip <- oldNo.ofNewTips[oldNo.ofNewTips > Ntip]
        ## fix by Thomas Sibley (2017-10-28):
        new.tip.label <-
            if (!length(node2tip)) {
                character(0)
            } else if (subtree && is.null(phy$node.label)) {
                paste("[", N[node2tip], "_tips]", sep = "")
            } else {
                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)]
        phy$tip.label <- c(phy$tip.label, new.tip.label)
    }

    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
    newNb <- integer(Ntip + Nnode)
    newNb[NEWROOT] <- n + 1L
    sndcol <- phy$edge[, 2] > n
    newNb[sort(phy$edge[sndcol, 2])] <- (n + 2):(n + phy$Nnode)
    phy$edge[sndcol, 2] <- newNb[phy$edge[sndcol, 2]]
    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]
    if (collapse.singles) phy <- collapse.singles(phy)
    phy
}

## "multiPhylo" methods by Klaus:

keep.tip.multiPhylo <- function(phy, tip, ...)
{
    if (is.null(attr(phy, "TipLabel"))) {
        tmp <- try(.compressTipLabel(phy), TRUE)
        if (!inherits(tmp, "try-error")) phy <- tmp
    }
    if (!is.null(attr(phy, "TipLabel"))) {
        phy <- lapply(phy, keep.tip, tip, ...)
        class(phy) <- "multiPhylo"
        phy <- .compressTipLabel(phy)
    } else {
        if (!inherits(tip, "character"))
            stop("Trees have different labels, tip needs to be of class character!")
        phy <- lapply(phy, keep.tip, tip, ...)
        class(phy) <- "multiPhylo"
    }
    phy
}

drop.tip.multiPhylo <- function(phy, tip, ...)
{
    interactive <- if (hasArg(interactive)) list(...)$interactive else FALSE
    if (interactive)
        stop("interactive = TRUE does not work for drop.tip.multiPhylo().")
    if (is.null(attr(phy, "TipLabel"))) {
        tmp <- try(.compressTipLabel(phy), TRUE)
        if (!inherits(tmp, "try-error")) phy <- tmp
    }
    if (!is.null(attr(phy, "TipLabel"))) {
        phy <- lapply(phy, drop.tip, tip, ...)
        class(phy) <- "multiPhylo"
        phy <- .compressTipLabel(phy)
    } else {
        if (!inherits(tip, "character"))
            stop("Trees have different labels, tip needs to be of class character!")
        phy <- lapply(phy, drop.tip, tip, ...)
        class(phy) <- "multiPhylo"
    }
    phy
}