]> git.donarmstrong.com Git - ape.git/blobdiff - R/dist.topo.R
new as.list.DNAbin
[ape.git] / R / dist.topo.R
index f2a10ae26352e16325763cea2cfda19fa5bcad46..eb9ba6f81e6f0410d3b68d16c5d6335f4739ff7f 100644 (file)
@@ -1,4 +1,4 @@
-## dist.topo.R (2010-01-22)
+## dist.topo.R (2010-05-25)
 
 ##      Topological Distances, Tree Bipartitions,
 ##   Consensus Trees, and Bootstrapping Phylogenies
@@ -13,6 +13,8 @@ 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.")
     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
 }