1 ## dist.topo.R (2010-05-25)
3 ## Topological Distances, Tree Bipartitions,
4 ## Consensus Trees, and Bootstrapping Phylogenies
6 ## Copyright 2005-2010 Emmanuel Paradis
8 ## This file is part of the R-package `ape'.
9 ## See the file ../COPYING for licensing issues.
11 dist.topo <- function(x, y, method = "PH85")
13 if (method == "score" && (is.null(x$edge.length) || is.null(y$edge.length)))
14 stop("trees must have branch lengths for branch score distance.")
15 nx <- length(x$tip.label)
18 bp1 <- .Call("bipartition", x$edge, nx, x$Nnode, PACKAGE = "ape")
19 bp1 <- lapply(bp1, function(xx) sort(x$tip.label[xx]))
20 ny <- length(y$tip.label) # fix by Otto Cordero
21 ## fix by Tim Wallstrom:
22 bp2.tmp <- .Call("bipartition", y$edge, ny, y$Nnode, PACKAGE = "ape")
23 bp2 <- lapply(bp2.tmp, function(xx) sort(y$tip.label[xx]))
24 bp2.comp <- lapply(bp2.tmp, function(xx) setdiff(1:ny, xx))
25 bp2.comp <- lapply(bp2.comp, function(xx) sort(y$tip.label[xx]))
29 if (method == "PH85") {
33 if (identical(bp1[[i]], bp2[[j]]) | identical(bp1[[i]], bp2.comp[[j]])) {
39 dT <- q1 + q2 - 2 * p # same than:
40 ##dT <- if (q1 == q2) 2*(q1 - p) else 2*(min(q1, q2) - p) + abs(q1 - q2)
42 if (method == "score") {
49 if (identical(bp1[[i]], bp2[[j]]) | identical(bp1[[i]], bp2.comp[[j]])) {
50 dT <- dT + (x$edge.length[which(x$edge[, 2] == nx + i)] -
51 y$edge.length[which(y$edge[, 2] == ny + j)])^2
52 found1 <- found2[j] <- TRUE
56 if (found1) found1 <- FALSE
57 else dT <- dT + (x$edge.length[which(x$edge[, 2] == nx + i)])^2
60 dT <- dT + sum((y$edge.length[y$edge[, 2] %in% (ny + which(!found2))])^2)
66 .compressTipLabel <- function(x)
68 ## 'x' is a list of objects of class "phylo" possibly with no class
69 if (!is.null(attr(x, "TipLabel"))) return(x)
70 ref <- x[[1]]$tip.label
71 if (any(table(ref) != 1))
72 stop("some tip labels are duplicated in tree no. 1")
74 for (i in 2:length(x)) {
75 label <- x[[i]]$tip.label
76 if (!identical(label, ref)) {
77 if (length(label) != length(ref))
78 stop(paste("tree no.", i, "has a different number of tips"))
79 ilab <- match(label, ref)
80 ## can use tabulate here because 'ilab' contains integers
82 stop(paste("tree no.", i, "has different tip labels"))
83 ### <FIXME> the test below does not seem useful anymore
84 ### if (any(tabulate(ilab) > 1))
85 ### stop(paste("some tip labels are duplicated in tree no.", i))
87 ie <- match(1:n, x[[i]]$edge[, 2])
88 x[[i]]$edge[ie, 2] <- ilab
90 x[[i]]$tip.label <- NULL
92 x[[1]]$tip.label <- NULL
93 attr(x, "TipLabel") <- ref
97 prop.part <- function(..., check.labels = TRUE)
100 if (length(obj) == 1 && class(obj[[1]]) != "phylo")
103 ## class(obj) <- NULL # needed?
106 if (ntree == 1) check.labels <- FALSE
107 if (check.labels) obj <- .compressTipLabel(obj)
108 for (i in 1:ntree) storage.mode(obj[[i]]$Nnode) <- "integer"
110 ## The 1st must have tip labels
111 ## Maybe simply pass the number of tips to the C code??
112 if (!is.null(attr(obj, "TipLabel")))
113 for (i in 1:ntree) obj[[i]]$tip.label <- attr(obj, "TipLabel")
115 clades <- .Call("prop_part", obj, ntree, TRUE, PACKAGE = "ape")
116 attr(clades, "number") <- attr(clades, "number")[1:length(clades)]
117 attr(clades, "labels") <- obj[[1]]$tip.label
118 class(clades) <- "prop.part"
122 print.prop.part <- function(x, ...)
124 if (is.null(attr(x, "labels"))) {
125 for (i in 1:length(x)) {
126 cat("==>", attr(x, "number")[i], "time(s):")
127 print(x[[i]], quote = FALSE)
130 for (i in 1:length(attr(x, "labels")))
131 cat(i, ": ", attr(x, "labels")[i], "\n", sep = "")
133 for (i in 1:length(x)) {
134 cat("==>", attr(x, "number")[i], "time(s):")
135 print(x[[i]], quote = FALSE)
140 summary.prop.part <- function(object, ...) attr(object, "number")
142 plot.prop.part <- function(x, barcol = "blue", leftmar = 4, ...)
144 if (is.null(attr(x, "labels")))
145 stop("cannot plot this partition object; see ?prop.part for details.")
147 n <- length(attr(x, "labels"))
148 layout(matrix(1:2, 2, 1), heights = c(1, 3))
149 par(mar = c(0.1, leftmar, 0.1, 0.1))
150 plot(1:L, attr(x, "number"), type = "h", col = barcol, xlim = c(1, L),
151 xlab = "", ylab = "Frequency", xaxt = "n", bty = "n")
152 plot(0, type = "n", xlim = c(1, L), ylim = c(1, n),
153 xlab = "", ylab = "", xaxt = "n", yaxt = "n")
154 for (i in 1:L) points(rep(i, length(x[[i]])), x[[i]], ...)
155 mtext(attr(x, "labels"), side = 2, at = 1:n, las = 1)
158 prop.clades <- function(phy, ..., part = NULL)
162 if (length(obj) == 1 && class(obj[[1]]) != "phylo")
163 obj <- unlist(obj, recursive = FALSE)
164 part <- prop.part(obj, check.labels = TRUE)
166 bp <- .Call("bipartition", phy$edge, length(phy$tip.label),
167 phy$Nnode, PACKAGE = "ape")
168 if (!is.null(attr(part, "labels")))
169 for (i in 1:length(part))
170 part[[i]] <- sort(attr(part, "labels")[part[[i]]])
171 bp <- lapply(bp, function(xx) sort(phy$tip.label[xx]))
172 n <- numeric(phy$Nnode)
173 for (i in 1:phy$Nnode) {
174 for (j in 1:length(part)) {
175 if (identical(all.equal(bp[[i]], part[[j]]), TRUE)) {
176 n[i] <- attr(part, "number")[j]
185 boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE)
187 if (is.list(x) && !is.data.frame(x)) {
188 if (inherits(x, "DNAbin")) x <- as.matrix(x)
194 x <- matrix(x, n, nL/n, byrow = TRUE)
198 boot.tree <- vector("list", B)
201 y <- seq(block, ncol(x), block)
202 boot.i <- sample(y, replace = TRUE)
203 boot.samp <- numeric(ncol(x))
204 boot.samp[y] <- boot.i
205 for (j in 1:(block - 1))
206 boot.samp[y - j] <- boot.i - j
207 } else boot.samp <- sample(ncol(x), replace = TRUE)
208 boot.tree[[i]] <- FUN(x[, boot.samp])
210 for (i in 1:B) storage.mode(boot.tree[[i]]$Nnode) <- "integer"
211 storage.mode(phy$Nnode) <- "integer"
212 ans <- attr(.Call("prop_part", c(list(phy), boot.tree),
213 B + 1, FALSE, PACKAGE = "ape"), "number") - 1
215 class(boot.tree) <- "multiPhylo"
216 ans <- list(BP = ans, trees = boot.tree)
221 consensus <- function(..., p = 1, check.labels = TRUE)
223 foo <- function(ic, node) {
225 ## node: node number in the final tree
228 for (j in (ic + 1):m) {
229 wh <- match(pp[[j]], pool)
230 if (!any(is.na(wh))) {
231 edge[pos, 1] <<- node
233 edge[pos, 2] <<- nextnode <<- nextnode + 1L
241 ind <- pos:(pos + size - 1)
242 edge[ind, 1] <<- node
243 edge[ind, 2] <<- pool
248 if (length(obj) == 1) {
249 ## better than unlist(obj, recursive = FALSE)
250 ## because "[[" keeps the class of 'obj':
252 if (class(obj) == "phylo") return(obj)
254 if (!is.null(attr(obj, "TipLabel")))
255 labels <- attr(obj, "TipLabel")
257 labels <- obj[[1]]$tip.label
258 if (check.labels) obj <- .compressTipLabel(obj)
261 ## Get all observed partitions and their frequencies:
262 pp <- prop.part(obj, check.labels = FALSE)
263 ## Drop the partitions whose frequency is less than 'p':
264 pp <- pp[attr(pp, "number") >= p * ntree]
265 ## Get the order of the remaining partitions by decreasing size:
266 ind <- sort(unlist(lapply(pp, length)), decreasing = TRUE,
267 index.return = TRUE)$ix
271 edge <- matrix(0L, n + m - 1, 2)
280 structure(list(edge = edge, tip.label = labels,
281 Nnode = m), class = "phylo")