1 ## bind.tree.R (2010-03-15)
5 ## Copyright 2003-2010 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
10 `+.phylo` <- function(x, y)
12 p <- if (is.null(x$root.edge)) 0 else x$root.edge
13 bind.tree(x, y, position = p)
16 bind.tree <- function(x, y, where = "root", position = 0, interactive = FALSE)
18 nx <- length(x$tip.label)
21 ny <- length(y$tip.label)
25 lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
26 if (lastPP$type != "phylogram" || lastPP$direction != "rightwards")
27 stop("you must plot tree 'x' as a 'rightward phylogram'")
28 cat("Click where you want to graft tree 'y'...\n")
30 d <- abs(xy$y - lastPP$yy)
31 d[lastPP$xx - xy$x < 0] <- Inf
33 position <- lastPP$xx[where] - xy$x
34 if (position < 0) position <- 0
35 cat("The following parameters are used:\n")
36 cat(" where =", where, " position =", position, "\n")
38 if (where == 0 || where == "root") where <- ROOTx
39 if (position < 0) position <- 0
41 stop("argument 'where' out of range for tree 'x'")
44 ## check whether both trees have branch lengths:
45 switch(is.null(x$edge.length) + is.null(y$edge.length) + 1L,
47 x$edge.length <- y$edge.length <- NULL
49 warning("one tree has no branch lengths, they will be ignored")
53 yHasNoRootEdge <- is.null(y$root.edge)
54 xHasNoRootEdge <- is.null(x$root.edge)
56 ## find the row of 'where' before renumbering
57 if (where == ROOTx) case <- 1 else {
58 i <- which(x$edge[, 2] == where)
59 case <- if (where <= nx) 2 else 3
61 ## case = 1 -> y is bound on the root of x
62 ## case = 2 -> y is bound on a tip of x
63 ## case = 3 -> y is bound on a node of x
65 ## check that 'position' is correct
68 stop("'position' is non-null but trees have no branch lengths")
71 stop("tree 'x' has no root edge")
72 if (position > x$root.edge)
73 stop("'position' is larger than x's root edge")
75 if (x$edge.length[i] < position)
76 stop("'position' is larger than the branch length")
83 ### because in all situations internal nodes need to be renumbered,
84 ### they are changed to negatives first, and nodes that eventually
85 ### will be added will be numbered sequentially
88 x$edge[nodes] <- -(x$edge[nodes] - nx) # -1, ..., -mx
90 y$edge[nodes] <- -(y$edge[nodes] - ny + mx) # -(mx+1), ..., -(mx+my)
91 ROOT <- -1L # may change later
92 next.node <- -(mx + my) - 1L
94 ## renumber now the tips in y:
95 new.nx <- if (where <= nx && !position) nx - 1L else nx
96 y$edge[!nodes] <- y$edge[!nodes] + new.nx
98 ## if 'y' as a root edge, use it:
99 if (!yHasNoRootEdge) {
100 y$edge <- rbind(c(0, y$edge[1]), y$edge)
101 ## ^ will be filled later
102 next.node <- next.node - 1L
103 if (wbl) y$edge.length <- c(y$root.edge, y$edge.length)
106 switch(case, { # case = 1
108 x$root.edge <- x$root.edge - position
109 x$edge <- rbind(c(next.node, x$edge[1]), x$edge)
111 if (wbl) x$edge.length <- c(position, x$edge.length)
113 if (yHasNoRootEdge) {
114 j <- which(y$edge[, 1] == y$edge[1])
116 } else y$edge[1] <- ROOT
117 x$edge <- rbind(x$edge, y$edge)
119 x$edge.length <- c(x$edge.length, y$edge.length)
122 x$edge[i, 2] <- next.node
123 x$edge <- rbind(x$edge[1:i, ], c(next.node, where), x$edge[-(1:i), ])
125 x$edge.length[i] <- x$edge.length[i] - position
126 x$edge.length <- c(x$edge.length[1:i], position, x$edge.length[-(1:i)])
129 if (yHasNoRootEdge) {
130 j <- which(y$edge[, 1] == y$edge[1])
131 y$edge[j, 1] <- x$edge[i, 1]
132 } else y$edge[1] <- x$edge[i, 1]
134 if (yHasNoRootEdge) x$edge[i, 2] <- y$edge[1]
136 ## the root edge of y is fused with the terminal edge of x
137 if (wbl) y$edge.length[1] <- y$edge.length[1] + x$edge.length[i]
138 y$edge[1] <- x$edge[i, 1]
139 ## delete i-th edge in x:
140 x$edge <- x$edge[-i, ]
142 if (wbl) x$edge.length <- x$edge.length[-i]
144 x$tip.label <- x$tip.label[-where]
145 ## renumber the tips that need to:
146 ii <- which(x$edge[, 2] > where & x$edge[, 2] <= nx)
147 x$edge[ii, 2] <- x$edge[ii, 2] - 1L
149 x$edge <- rbind(x$edge[1:i, ], y$edge, x$edge[-(1:i), ])
151 x$edge.length <- c(x$edge.length[1:i], y$edge.length, x$edge.length[-(1:i)])
154 if (yHasNoRootEdge) {
155 j <- which(y$edge[, 1] == y$edge[1])
156 y$edge[j, 1] <- next.node
157 } else y$edge[1] <- next.node
158 x$edge <- rbind(x$edge[1:i, ], c(next.node, x$edge[i, 2]), x$edge[-(1:i), ])
159 x$edge[i, 2] <- next.node
161 x$edge.length[i] <- x$edge.length[i] - position
162 x$edge.length <- c(x$edge.length[1:i], position, x$edge.length[-(1:i)])
166 if (yHasNoRootEdge) {
167 j <- which(y$edge[, 1] == y$edge[1])
168 y$edge[j, 1] <- x$edge[i, 2]
169 } else y$edge[1] <- x$edge[i, 2]
171 x$edge <- rbind(x$edge[1:i, ], y$edge, x$edge[-(1:i), ])
173 x$edge.length <- c(x$edge.length[1:i], y$edge.length, x$edge.length[-(1:i)])
176 x$tip.label <- c(x$tip.label, y$tip.label)
178 if (is.null(x$node.label)) {
179 if (!is.null(y$node.label))
180 x$node.label <- c(rep(NA, mx), y$node.label)
183 if (is.null(y$node.label)) c(x$node.label, rep(NA, my))
184 else c(x$node.label, y$node.label)
187 n <- length(x$tip.label)
188 x$Nnode <- dim(x$edge)[1] + 1L - n
190 ## update the node labels before renumbering (this adds NA for
191 ## the added nodes, and drops the label for those deleted)
192 if (!is.null(x$node.label))
193 x$node.label <- x$node.label[-unique(x$edge[, 1])]
196 newNb <- integer(x$Nnode)
197 newNb[-ROOT] <- n + 1L
198 sndcol <- x$edge[, 2] < 0
199 ## executed from right to left, so newNb is modified before x$edge:
200 x$edge[sndcol, 2] <- newNb[-x$edge[sndcol, 2]] <-
201 (n + 2):(n + x$Nnode)
202 x$edge[, 1] <- newNb[-x$edge[, 1]]
204 if (!is.null(x$node.label))
205 x$node.label <- x$node.label[newNb[newNb == 0] - n]