X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=R%2Fdist.topo.R;h=230f009e30d633935907327d2010f0846d2c9abd;hb=a2fc961dffe9f9b7994ed880e68c03b2334dc341;hp=fa9e869d7b0eb7b9d4925bd2538f640ea2a8a03f;hpb=52008daf7f708f3fcdc735f22af308dd1a461670;p=ape.git diff --git a/R/dist.topo.R b/R/dist.topo.R index fa9e869..230f009 100644 --- a/R/dist.topo.R +++ b/R/dist.topo.R @@ -1,9 +1,9 @@ -## dist.topo.R (2010-11-18) +## dist.topo.R (2012-12-12) ## Topological Distances, Tree Bipartitions, ## Consensus Trees, and Bootstrapping Phylogenies -## Copyright 2005-2010 Emmanuel Paradis +## Copyright 2005-2012 Emmanuel Paradis ## This file is part of the R-package `ape'. ## See the file ../COPYING for licensing issues. @@ -68,29 +68,28 @@ dist.topo <- function(x, y, method = "PH85") ## 'x' is a list of objects of class "phylo" possibly with no class if (!is.null(attr(x, "TipLabel"))) return(x) ref <- x[[1]]$tip.label - if (any(table(ref) != 1)) - stop("some tip labels are duplicated in tree no. 1") n <- length(ref) - for (i in 2:length(x)) { - label <- x[[i]]$tip.label + if (length(unique(ref)) != n) + stop("some tip labels are duplicated in tree no. 1") + + ## serious improvement by Joseph W. Brown! + relabel <- function (y) { + label <- y$tip.label if (!identical(label, ref)) { if (length(label) != length(ref)) - stop(paste("tree no.", i, "has a different number of tips")) + stop(paste("tree ", y, "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 + stop(paste("tree ", y, "has different tip labels")) + ie <- match(1:n, y$edge[, 2]) + y$edge[ie, 2] <- ilab } - x[[i]]$tip.label <- NULL + y$tip.label <- NULL + y } - x[[1]]$tip.label <- NULL + x <- lapply(x, relabel) attr(x, "TipLabel") <- ref + class(x) <- "multiPhylo" x } @@ -104,7 +103,7 @@ prop.part <- function(..., check.labels = TRUE) ## ntree <- length(obj) if (ntree == 1) check.labels <- FALSE - if (check.labels) .compressTipLabel(obj) # no need to store cause uncompress later + 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 @@ -154,24 +153,31 @@ plot.prop.part <- function(x, barcol = "blue", leftmar = 4, ...) mtext(attr(x, "labels"), side = 2, at = 1:n, las = 1) } -prop.clades <- function(phy, ..., part = NULL) +prop.clades <- function(phy, ..., part = NULL, rooted = FALSE) { if (is.null(part)) { + ## + ## Are we going to keep the '...' way of passing trees? obj <- list(...) if (length(obj) == 1 && class(obj[[1]]) != "phylo") - obj <- unlist(obj, recursive = FALSE) + obj <- unlist(obj, recursive = FALSE) + ## part <- prop.part(obj, check.labels = TRUE) } - bp <- .Call("bipartition", phy$edge, length(phy$tip.label), - phy$Nnode, PACKAGE = "ape") - if (!is.null(attr(part, "labels"))) - for (i in 1:length(part)) - part[[i]] <- sort(attr(part, "labels")[part[[i]]]) - bp <- lapply(bp, function(xx) sort(phy$tip.label[xx])) + + bp <- prop.part(phy) + if (!rooted) { + bp <- postprocess.prop.part(bp) + part <- postprocess.prop.part(part) # fix by Klaus Schliep + ## actually the above line in not needed if called from boot.phylo() + } + n <- numeric(phy$Nnode) - for (i in 1:phy$Nnode) { - for (j in 1:length(part)) { - if (identical(all.equal(bp[[i]], part[[j]]), TRUE)) { + for (i in seq_along(bp)) { + for (j in seq_along(part)) { + ## we rely on the fact the values returned by prop.part are + ## sorted and without attributes, so identical can be used: + if (identical(bp[[i]], part[[j]])) { n[i] <- attr(part, "number")[j] done <- TRUE break @@ -181,7 +187,8 @@ 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, rooted = FALSE) { if (is.list(x) && !is.data.frame(x)) { if (inherits(x, "DNAbin")) x <- as.matrix(x) @@ -195,6 +202,8 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) } } boot.tree <- vector("list", B) + if (!quiet) # suggestion by Alastair Potts + progbar <- utils::txtProgressBar(style = 3) for (i in 1:B) { if (block > 1) { y <- seq(block, ncol(x), block) @@ -202,14 +211,21 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) boot.samp <- numeric(ncol(x)) boot.samp[y] <- boot.i for (j in 1:(block - 1)) - boot.samp[y - j] <- boot.i - j + 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 + + pp <- prop.part(boot.tree) + if (!rooted) pp <- postprocess.prop.part(pp) + ans <- prop.clades(phy, part = pp, rooted = rooted) + + ##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) @@ -217,6 +233,61 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE) ans } +### The next function transforms an object of class "prop.part" so +### that the vectors which are identical in terms of split are aggregated. +### For instance if n = 5 tips, 1:2 and 3:5 actually represent the same +### split though they are different clades. The aggregation is done +### arbitrarily. The call to ONEwise() insures that all splits include +### the first tip. +postprocess.prop.part <- function(x) +{ + n <- length(x[[1]]) + N <- length(x) + w <- attr(x, "number") + + drop <- logical(N) + V <- numeric(n) + for (i in 2:(N - 1)) { + if (drop[i]) next + A <- x[[i]] + for (j in (i + 1):N) { + if (drop[j]) next + B <- x[[j]] + if (length(A) + length(B) != n) next + V[] <- 0L + V[A] <- 1L + V[B] <- 1L + if (all(V == 1L)) { + drop[j] <- TRUE + w[i] <- w[i] + w[j] + } + } + } + if (any(drop)) { + labels <- attr(x, "labels") + x <- x[!drop] + w <- w[!drop] + attr(x, "number") <- w + attr(x, "labels") <- labels + class(x) <- "prop.part" + } + ONEwise(x) +} + +### This function changes an object of class "prop.part" so that they +### all include the first tip. For instance if n = 5 tips, 3:5 is +### changed to 1:2. +ONEwise <- function(x) +{ + n <- length(x[[1L]]) + v <- 1:n + for (i in 2:length(x)) { + y <- x[[i]] + if (y[1] != 1) x[[i]] <- v[-y] + } + x +} + consensus <- function(..., p = 1, check.labels = TRUE) { foo <- function(ic, node) { @@ -260,6 +331,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,