]> git.donarmstrong.com Git - ape.git/blobdiff - R/dist.topo.R
various bug fixes
[ape.git] / R / dist.topo.R
index eb9ba6f81e6f0410d3b68d16c5d6335f4739ff7f..455107ec924c43eab1527bbc52503b3fb3e72643 100644 (file)
@@ -1,9 +1,9 @@
-## dist.topo.R (2010-05-25)
+## dist.topo.R (2011-06-14)
 
 ##      Topological Distances, Tree Bipartitions,
 ##   Consensus Trees, and Bootstrapping Phylogenies
 
-## Copyright 2005-2010 Emmanuel Paradis
+## Copyright 2005-2011 Emmanuel Paradis
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
@@ -11,7 +11,7 @@
 dist.topo <- function(x, y, method = "PH85")
 {
     if (method == "score" && (is.null(x$edge.length) || is.null(y$edge.length)))
-        stop("trees must have branch lengths for Billera et al.'s distance.")
+        stop("trees must have branch lengths for branch score distance.")
     nx <- length(x$tip.label)
     x <- unroot(x)
     y <- unroot(y)
@@ -71,23 +71,26 @@ 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)) {
-        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"))
+    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"))
 ### <FIXME> the test below does not seem useful anymore
 ###            if (any(tabulate(ilab) > 1))
 ###                stop(paste("some tip labels are duplicated in tree no.", i))
 ### </FIXME>
-            ie <- match(1:n, x[[i]]$edge[, 2])
-            x[[i]]$edge[ie, 2] <- ilab
+                ie <- match(1:n, x[[i]]$edge[, 2])
+                x[[i]]$edge[ie, 2] <- ilab
+            }
+            x[[i]]$tip.label <- NULL
         }
-        x[[i]]$tip.label <- NULL
     }
     x[[1]]$tip.label <- NULL
     attr(x, "TipLabel") <- ref
@@ -100,17 +103,16 @@ prop.part <- function(..., check.labels = TRUE)
     if (length(obj) == 1 && class(obj[[1]]) != "phylo")
         obj <- obj[[1]]
     ## <FIXME>
-    ## class(obj) <- NULL # needed?
+    ## class(obj) <- NULL # needed? apparently not, see below (2010-11-18)
     ## </FIXME>
     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"
     ## <FIXME>
     ## 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)
     ## </FIXME>
     clades <- .Call("prop_part", obj, ntree, TRUE, PACKAGE = "ape")
     attr(clades, "number") <- attr(clades, "number")[1:length(clades)]
@@ -182,7 +184,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)
 {
     if (is.list(x) && !is.data.frame(x)) {
         if (inherits(x, "DNAbin")) x <- as.matrix(x)
@@ -196,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)
@@ -206,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
 }