3 ## Root of Phylogenetic Trees
5 ## Copyright 2004-2009 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 is.rooted <- function(phy)
12 if (!inherits(phy, "phylo"))
13 stop('object "phy" is not of class "phylo"')
14 if (!is.null(phy$root.edge)) TRUE
16 if (tabulate(phy$edge[, 1])[length(phy$tip.label) + 1] > 2)
20 unroot <- function(phy)
22 if (!inherits(phy, "phylo"))
23 stop('object "phy" is not of class "phylo"')
24 if (dim(phy$edge)[1] < 3)
25 stop("cannot unroot a tree with two edges.")
26 ## delete FIRST the root.edge (in case this is sufficient to
27 ## unroot the tree, i.e. there is a multichotomy at the root)
28 if (!is.null(phy$root.edge)) phy$root.edge <- NULL
29 if (!is.rooted(phy)) return(phy)
30 ## We remove one of the edges coming from the root, and
31 ## eventually adding the branch length to the other one
32 ## also coming from the root.
33 ## In all cases, the node deleted is the 2nd one (numbered
34 ## nb.tip+2 in `edge'), so we simply need to renumber the
35 ## nodes by adding 1, except the root (this remains the
36 ## origin of the tree).
37 nb.tip <- length(phy$tip.label)
39 EDGEROOT <- which(phy$edge[, 1] == ROOT)
40 ## j: the target where to stick the edge
41 ## i: the edge to delete
42 if (phy$edge[EDGEROOT[1], 2] == ROOT + 1) {
49 ## This should work whether the tree is in pruningwise or
51 phy$edge <- phy$edge[-i, ]
52 nodes <- phy$edge > ROOT # renumber all nodes except the root
53 phy$edge[nodes] <- phy$edge[nodes] - 1
54 if (!is.null(phy$edge.length)) {
55 phy$edge.length[j] <- phy$edge.length[j] + phy$edge.length[i]
56 phy$edge.length <- phy$edge.length[-i]
58 phy$Nnode <- phy$Nnode - 1
59 if (!is.null(phy$node.label))
60 phy$node.label <- phy$node.label[-2]
64 root <- function(phy, outgroup, node = NULL, resolve.root = FALSE)
66 if (!inherits(phy, "phylo"))
67 stop('object "phy" is not of class "phylo"')
69 n <- length(phy$tip.label)
73 stop("incorrect node#: should be greater than the number of taxa")
77 if (is.numeric(outgroup)) {
78 if (any(outgroup > n))
79 stop("incorrect taxa#: should not be greater than the number of taxa")
80 outgroup <- sort(outgroup) # used below
82 if (is.character(outgroup))
83 outgroup <- which(phy$tip.label %in% outgroup)
84 if (length(outgroup) == n) return(phy)
86 ## First check that the outgroup is monophyletic--
87 ## unless there's only one tip specified of course
88 if (length(outgroup) > 1) {
89 seq.nod <- .Call("seq_root2tip", phy$edge, n,
90 phy$Nnode, PACKAGE = "ape")
91 sn <- seq.nod[outgroup]
92 ## We go from the root to the tips: the sequence of nodes
93 ## is identical until the MRCA:
95 i <- 2 # we start at the 2nd position since the root
96 # of the tree is a common ancestor to all tips
98 x <- unique(unlist(lapply(sn, "[", i)))
99 if (length(x) != 1) break
103 ## Check that all descendants of this node
104 ## are included in the outgroup.
105 ## (below is slightly faster than calling "bipartition")
106 desc <- which(unlist(lapply(seq.nod,
107 function(x) any(x %in% newroot))))
108 msg <- "the specified outgroup is not monophyletic"
109 ingroup <- (1:n)[-outgroup]
110 ## 'outgroup' and 'desc' are already sorted:
111 if (newroot != ROOT) {
112 if (!identical(outgroup, desc) && !identical(ingroup, desc))
114 } else { # otherwise check monophyly of the ingroup
115 if (!is.monophyletic(phy, ingroup)) stop(msg)
117 } else newroot <- phy$edge[which(phy$edge[, 2] == outgroup), 1]
120 oldNnode <- phy$Nnode
121 if (newroot == ROOT) {
123 snw <- which(phy$edge[, 1] == newroot)
124 if (length(snw) > 2) {
125 a <- snw[1]:(snw[2] - 1)
127 newnod <- oldNnode + n + 1
128 phy$edge[snw[-1], 1] <- newnod
129 phy$edge <- rbind(phy$edge[a, ], c(ROOT, newnod),
131 if (!is.null(phy$edge.length))
133 c(phy$edge.length[a], 0, phy$edge.length[b])
134 phy$Nnode <- phy$Nnode + 1L
135 ## node renumbering (see comments below)
136 newNb <- integer(n + oldNnode)
137 newNb[newroot] <- n + 1L
138 sndcol <- phy$edge[, 2] > n
139 phy$edge[sndcol, 2] <- newNb[phy$edge[sndcol, 2]] <-
140 (n + 2):(n + phy$Nnode)
141 phy$edge[, 1] <- newNb[phy$edge[, 1]]
147 phy$root.edge <- NULL # just in case...
148 Nclade <- tabulate(phy$edge[, 1])[ROOT] # degree of the root node
149 ## if only 2 edges connect to the root, we have to fuse them:
150 fuseRoot <- Nclade == 2
152 start <- which(phy$edge[, 1] == ROOT)
153 end <- c(start[-1] - 1, N)
157 w <- which(phy$edge[, 2] == newroot)
158 nod <- phy$edge[w, 1]
162 ## The loop below starts from the new root and goes up in the edge
163 ## matrix reversing the edges that need to be as well as well
164 ## inverting their order. The other edges must not be changed, so
165 ## their indices are stored in `stack'.
166 ## We then bind the other edges in a straightforward way.
169 ## it is important that the 3 next lines
170 ## are inside this "if" statement
177 if (phy$edge[i, 2] == nod) {
179 o[NEXT:(NEXT + stack - 1L)] <- (i + 1L):(i + stack)
183 if (phy$edge[i, 1] == ROOT) break
187 nod <- phy$edge[i, 1]
188 } else stack <- stack + 1L
193 ## we keep the edge leading to the old root if needed:
200 endOfOutgroup <- which(phy$edge[(w+1):N, 1] < newroot)[1] + w - 1
201 if (is.na(endOfOutgroup)) endOfOutgroup <- N
202 endOfClade <- end[end >= endOfOutgroup][1]
204 ## bind the other clades...
205 for (j in 1:Nclade) {
206 if (end[j] == endOfClade) next
207 ## do we have to fuse the two basal edges?
209 phy$edge[start[j], 1] <- phy$edge[i, 2]
210 if (!is.null(phy$edge.length))
211 phy$edge.length[start[j]] <- phy$edge.length[start[j]] +
214 # o[NEXT] <- i#start[j]
219 o[NEXT:(NEXT + ne - 1L)] <- s
223 ## possibly bind the edges below the outgroup till the end of the clade
224 if (all(endOfOutgroup != end)) {
225 j <- (endOfOutgroup + 1L):endOfClade
226 ## we must take care that the branch inversions done above
227 ## may have changed the hierarchy of clades here, so we
228 ## travel from the bottom of this series of edges
230 inverted <- phy$edge[INV, 1] # <- fails if ', 2]' is used
232 if (any(phy$edge[k, 1] == inverted)) {
236 o[NEXT:(NEXT + stack - 1L)] <- (k + 1L):(k + stack)
240 } else stack <- stack + 1L
244 ## ... and the outgroup
245 s <- (w + 1L):endOfOutgroup
247 o[NEXT:(NEXT + ne - 1L)] <- s
250 phy$Nnode <- oldNnode - 1
253 phy$edge[INV, ] <- phy$edge[INV, 2:1]
254 phy$edge <- phy$edge[o, ]
255 if (!is.null(phy$edge.length))
256 phy$edge.length <- phy$edge.length[o]
259 newnod <- oldNnode + n + 1
260 if (length(outgroup) == 1L) {
261 wh <- which(phy$edge[, 2] == outgroup)
262 phy$edge[1] <- newnod
264 rbind(c(newroot, newnod), phy$edge[-wh, ], phy$edge[wh, ])
265 snw <- which(phy$edge[, 1] == newroot)
266 phy$edge[snw[length(snw) - 1], 1] <- newnod
267 if (!is.null(phy$edge.length)) {
269 c(0, phy$edge.length[-wh], phy$edge.length[wh])
272 wh <- which(phy$edge[, 1] == newroot)
273 phy$edge[wh[-1], 1] <- newnod
277 rbind(phy$edge[s1, ], c(newroot, newnod), phy$edge[s2, ])
278 if (!is.null(phy$edge.length)) {
279 tmp <- phy$edge.length[1]
280 phy$edge.length[1] <- 0
282 c(phy$edge.length[s1], tmp, phy$edge.length[s2])
285 ## N <- N + 1L ... not needed
286 phy$Nnode <- phy$Nnode + 1
289 ## The block below renumbers the nodes so that they conform
290 ## to the "phylo" format
291 newNb <- integer(n + oldNnode)
292 newNb[newroot] <- n + 1L
293 sndcol <- phy$edge[, 2] > n
294 ## executed from right to left, so newNb is modified before phy$edge:
295 phy$edge[sndcol, 2] <- newNb[phy$edge[sndcol, 2]] <-
296 (n + 2):(n + phy$Nnode)
297 phy$edge[, 1] <- newNb[phy$edge[, 1]]
299 if (!is.null(phy$node.label)) {
301 newNb <- newNb[-(1:n)]
304 phy$node.label <- phy$node.label[-1]
306 phy$node.label <- phy$node.label[order(newNb)]
308 phy$node.label[is.na(phy$node.label)] <- phy$node.label[1]
309 phy$node.label[1] <- NA
310 ##phy$node.label <- c(phy$node.label[1], NA, phy$node.label[-1])
311 ##phy$node.label <- c("NA", phy$node.label)