X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=R%2Fdist.topo.R;h=525178f2528ed1ba66321e178d8c79d2cf0c91ed;hb=c14b15358e1f21302d929a980484e9f08c748c20;hp=b57fb0a591535817da88b108448da12f308faefd;hpb=631de80fc64dd4b62c967f3d08399df612317f6e;p=ape.git diff --git a/R/dist.topo.R b/R/dist.topo.R index b57fb0a..525178f 100644 --- a/R/dist.topo.R +++ b/R/dist.topo.R @@ -1,55 +1,64 @@ -## dist.topo.R (2008-07-18) +## dist.topo.R (2011-07-13) ## Topological Distances, Tree Bipartitions, ## Consensus Trees, and Bootstrapping Phylogenies -## Copyright 2005-2008 Emmanuel Paradis +## Copyright 2005-2011 Emmanuel Paradis ## This file is part of the R-package `ape'. ## See the file ../COPYING for licensing issues. dist.topo <- function(x, y, method = "PH85") { - if (method == "BHV01" && (is.null(x$edge.length) || is.null(y$edge.length))) - stop("trees must have branch lengths for Billera et al.'s distance.") - n <- length(x$tip.label) - bp1 <- .Call("bipartition", x$edge, n, x$Nnode, PACKAGE = "ape") + if (method == "score" && (is.null(x$edge.length) || is.null(y$edge.length))) + stop("trees must have branch lengths for branch score distance.") + nx <- length(x$tip.label) + x <- unroot(x) + y <- unroot(y) + bp1 <- .Call("bipartition", x$edge, nx, x$Nnode, PACKAGE = "ape") bp1 <- lapply(bp1, function(xx) sort(x$tip.label[xx])) - bp2 <- .Call("bipartition", y$edge, n, y$Nnode, PACKAGE = "ape") - bp2 <- lapply(bp2, function(xx) sort(y$tip.label[xx])) + ny <- length(y$tip.label) # fix by Otto Cordero + ## fix by Tim Wallstrom: + bp2.tmp <- .Call("bipartition", y$edge, ny, y$Nnode, PACKAGE = "ape") + bp2 <- lapply(bp2.tmp, function(xx) sort(y$tip.label[xx])) + bp2.comp <- lapply(bp2.tmp, function(xx) setdiff(1:ny, xx)) + bp2.comp <- lapply(bp2.comp, function(xx) sort(y$tip.label[xx])) + ## End q1 <- length(bp1) q2 <- length(bp2) if (method == "PH85") { p <- 0 for (i in 1:q1) { for (j in 1:q2) { - if (identical(all.equal(bp1[[i]], bp2[[j]]), TRUE)) { + if (identical(bp1[[i]], bp2[[j]]) | identical(bp1[[i]], bp2.comp[[j]])) { p <- p + 1 break } } } - dT <- if (q1 == q2) 2*(q1 - p) else 2*(min(q1, q2) - p) + abs(q1 - q2) + dT <- q1 + q2 - 2 * p # same than: + ##dT <- if (q1 == q2) 2*(q1 - p) else 2*(min(q1, q2) - p) + abs(q1 - q2) } - if (method == "BHV01") { + if (method == "score") { dT <- 0 found1 <- FALSE found2 <- logical(q2) found2[1] <- TRUE for (i in 2:q1) { for (j in 2:q2) { - if (identical(bp1[[i]], bp2[[j]])) { - dT <- dT + abs(x$edge.length[which(x$edge[, 2] == n + i)] - - y$edge.length[which(y$edge[, 2] == n + j)]) + if (identical(bp1[[i]], bp2[[j]]) | identical(bp1[[i]], bp2.comp[[j]])) { + dT <- dT + (x$edge.length[which(x$edge[, 2] == nx + i)] - + y$edge.length[which(y$edge[, 2] == ny + j)])^2 found1 <- found2[j] <- TRUE break } } if (found1) found1 <- FALSE - else dT <- dT + x$edge.length[which(x$edge[, 2] == n + i)] + else dT <- dT + (x$edge.length[which(x$edge[, 2] == nx + i)])^2 } if (!all(found2)) - dT <- dT + sum(y$edge.length[y$edge[, 2] %in% (n + which(!found2))]) + dT <- dT + sum((y$edge.length[y$edge[, 2] %in% (ny + which(!found2))])^2) + dT <- sqrt(dT) } dT } @@ -62,18 +71,28 @@ dist.topo <- function(x, y, method = "PH85") if (any(table(ref) != 1)) stop("some tip labels are duplicated in tree no. 1") n <- length(ref) - for (i in 2:length(x)) { - if (identical(x[[i]]$tip.label, ref)) next - ilab <- match(x[[i]]$tip.label, ref) - ## can use tabulate here because 'ilab' contains integers - if (any(tabulate(ilab) > 1)) - stop(paste("some tip labels are duplicated in tree no.", i)) - if (any(is.na(ilab))) - stop(paste("tree no.", i, "has different tip labels")) - ie <- match(1:n, x[[i]]$edge[, 2]) - x[[i]]$edge[ie, 2] <- ilab + Ntree <- length(x) + if (Ntree > 1) { + for (i in 2:Ntree) { + label <- x[[i]]$tip.label + if (!identical(label, ref)) { + if (length(label) != length(ref)) + stop(paste("tree no.", i, "has a different number of tips")) + ilab <- match(label, ref) + ## can use tabulate here because 'ilab' contains integers + if (any(is.na(ilab))) + stop(paste("tree no.", i, "has different tip labels")) +### the test below does not seem useful anymore +### if (any(tabulate(ilab) > 1)) +### stop(paste("some tip labels are duplicated in tree no.", i)) +### + ie <- match(1:n, x[[i]]$edge[, 2]) + x[[i]]$edge[ie, 2] <- ilab + } + x[[i]]$tip.label <- NULL + } } - for (i in 1:length(x)) x[[i]]$tip.label <- NULL + x[[1]]$tip.label <- NULL attr(x, "TipLabel") <- ref x } @@ -84,17 +103,16 @@ prop.part <- function(..., check.labels = TRUE) if (length(obj) == 1 && class(obj[[1]]) != "phylo") obj <- obj[[1]] ## - ## class(obj) <- NULL # needed? + ## class(obj) <- NULL # needed? apparently not, see below (2010-11-18) ## ntree <- length(obj) if (ntree == 1) check.labels <- FALSE - if (check.labels) obj <- .compressTipLabel(obj) + if (check.labels) obj <- .compressTipLabel(obj) # fix by Klaus Schliep (2011-02-21) for (i in 1:ntree) storage.mode(obj[[i]]$Nnode) <- "integer" ## ## The 1st must have tip labels ## Maybe simply pass the number of tips to the C code?? - if (!is.null(attr(obj, "TipLabel"))) - for (i in 1:ntree) obj[[i]]$tip.label <- attr(obj, "TipLabel") + obj <- .uncompressTipLabel(obj) # fix a bug (2010-11-18) ## clades <- .Call("prop_part", obj, ntree, TRUE, PACKAGE = "ape") attr(clades, "number") <- attr(clades, "number")[1:length(clades)] @@ -166,10 +184,11 @@ prop.clades <- function(phy, ..., part = NULL) n } -boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) +boot.phylo <- function(phy, x, FUN, B = 100, block = 1, + trees = FALSE, quiet = FALSE) { - if (is.list(x)) { - if (class(x) == "DNAbin") x <- as.matrix(x) + if (is.list(x) && !is.data.frame(x)) { + if (inherits(x, "DNAbin")) x <- as.matrix(x) else { nm <- names(x) n <- length(x) @@ -180,6 +199,7 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) } } boot.tree <- vector("list", B) + if (!quiet) progbar <- utils::txtProgressBar(style = 3) # suggestion by Alastair Potts for (i in 1:B) { if (block > 1) { y <- seq(block, ncol(x), block) @@ -190,12 +210,18 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) boot.samp[y - j] <- boot.i - j } else boot.samp <- sample(ncol(x), replace = TRUE) boot.tree[[i]] <- FUN(x[, boot.samp]) + if (!quiet) utils::setTxtProgressBar(progbar, i/B) } + if (!quiet) close(progbar) for (i in 1:B) storage.mode(boot.tree[[i]]$Nnode) <- "integer" storage.mode(phy$Nnode) <- "integer" - ans <- attr(.Call("prop_part", c(list(phy), boot.tree), - B + 1, FALSE, PACKAGE = "ape"), "number") - 1 - if (trees) ans <- list(BP = ans, trees = boot.tree) + ans <- prop.clades(phy, boot.tree) + ##ans <- attr(.Call("prop_part", c(list(phy), boot.tree), + ## B + 1, FALSE, PACKAGE = "ape"), "number") - 1 + if (trees) { + class(boot.tree) <- "multiPhylo" + ans <- list(BP = ans, trees = boot.tree) + } ans } @@ -242,6 +268,7 @@ consensus <- function(..., p = 1, check.labels = TRUE) ## Get all observed partitions and their frequencies: pp <- prop.part(obj, check.labels = FALSE) ## Drop the partitions whose frequency is less than 'p': + if (p == 0.5) p <- 0.5000001 # avoid incompatible splits pp <- pp[attr(pp, "number") >= p * ntree] ## Get the order of the remaining partitions by decreasing size: ind <- sort(unlist(lapply(pp, length)), decreasing = TRUE,