]> git.donarmstrong.com Git - ape.git/blobdiff - R/dist.topo.R
new image.DNAbin()
[ape.git] / R / dist.topo.R
index f2a10ae26352e16325763cea2cfda19fa5bcad46..d94866adfe1c65f9c46946567573196b6a4e51a9 100644 (file)
@@ -1,9 +1,9 @@
-## dist.topo.R (2010-01-22)
+## dist.topo.R (2011-02-21)
 
 ##      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.
 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)
     bp1 <- .Call("bipartition", x$edge, nx, x$Nnode, PACKAGE = "ape")
     bp1 <- lapply(bp1, function(xx) sort(x$tip.label[xx]))
     ny <- length(y$tip.label) # fix by Otto Cordero
@@ -45,7 +47,6 @@ dist.topo <- function(x, y, method = "PH85")
         for (i in 2:q1) {
             for (j in 2:q2) {
                 if (identical(bp1[[i]], bp2[[j]]) | identical(bp1[[i]], bp2.comp[[j]])) {
-                    if (i == 19) browser()
                     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
@@ -71,17 +72,24 @@ dist.topo <- function(x, y, method = "PH85")
         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
+        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
+        }
+        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
 }
@@ -92,17 +100,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)]
@@ -203,7 +210,10 @@ boot.phylo <- function(phy, x, FUN, B = 100, block = 1, trees = FALSE)
     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)
+    if (trees) {
+        class(boot.tree) <- "multiPhylo"
+        ans <- list(BP = ans, trees = boot.tree)
+    }
     ans
 }