]> git.donarmstrong.com Git - ape.git/blobdiff - R/me.R
final commit for ape 3.0-8
[ape.git] / R / me.R
diff --git a/R/me.R b/R/me.R
index 25cd4ab9e46f23544432f03f50e6a4f76505674b..533c40edab6d7b52079943f7e9ee57e1c668d6af 100644 (file)
--- a/R/me.R
+++ b/R/me.R
@@ -1,41 +1,68 @@
-## me.R (2007-07-12)
+## me.R (2012-09-14)
 
-##      Tree Estimation Based on Minimum Evolution Algorithm
+##   Tree Estimation Based on Minimum Evolution Algorithm
 
-## Copyright 2007 Vincent Lefort
+## Copyright 2007 Vincent Lefort with modifications by
+##                Emmanuel Paradis (2008-2012)
 
 ## This file is part of the R-package `ape'.
 ## See the file ../COPYING for licensing issues.
 
-fastme.bal <- function(X, nni = TRUE)
+fastme.bal <- function(X, nni = TRUE, spr = TRUE, tbr = TRUE)
 {
     if (is.matrix(X)) X <- as.dist(X)
-    N <- attr(X, "Size")
+    N <- as.integer(attr(X, "Size"))
+    nedge <- 2L * N - 3L
+    ans <- .C("me_b", as.double(X), N, 1:N, as.integer(nni),
+              as.integer(spr), as.integer(tbr), integer(nedge),
+              integer(nedge), double(nedge),
+              DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
     labels <- attr(X, "Labels")
     if (is.null(labels)) labels <- as.character(1:N)
-    ans <- .C("me_b", as.double(X), as.integer(N), as.character(labels),
-              "", as.integer(nni), PACKAGE = "ape")
-    read.tree(text = ans[[4]])
+    labels <- labels[ans[[3]]]
+    obj <- list(edge =  cbind(ans[[7]], ans[[8]]),
+                edge.length = ans[[9]],
+                tip.label = labels, Nnode = N - 2L)
+    class(obj) <- "phylo"
+    attr(obj, "order") <- "cladewise"
+    obj
 }
 
 fastme.ols <- function(X, nni = TRUE)
 {
     if (is.matrix(X)) X <- as.dist(X)
-    N <- attr(X, "Size")
+    N <- as.integer(attr(X, "Size"))
+    nedge <- 2L * N - 3L
+    ans <- .C("me_o", as.double(X), N, 1:N, as.integer(nni),
+              integer(nedge), integer(nedge), double(nedge),
+              DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
     labels <- attr(X, "Labels")
     if (is.null(labels)) labels <- as.character(1:N)
-    ans <- .C("me_o", as.double(X), as.integer(N), as.character(labels),
-              "", as.integer(nni), PACKAGE = "ape")
-    read.tree(text = ans[[4]])
+    labels <- labels[ans[[3]]]
+    obj <- list(edge =  cbind(ans[[5]], ans[[6]]),
+                edge.length = ans[[7]],
+                tip.label = labels, Nnode = N - 2L)
+    class(obj) <- "phylo"
+    attr(obj, "order") <- "cladewise"
+    obj
 }
 
 bionj <- function(X)
 {
     if (is.matrix(X)) X <- as.dist(X)
-    N <- attr(X, "Size")
+    if (any(is.na(X)))
+        stop("missing values are not allowed in the distance matrix.\nConsider using bionjs()")
+    if (any(X > 100))
+        stop("at least one distance was greater than 100")
+    N <- as.integer(attr(X, "Size"))
+
+    ans <- .C("bionj", as.double(X), N, integer(2 * N - 3),
+              integer(2 * N - 3), double(2*N - 3),
+              DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
     labels <- attr(X, "Labels")
     if (is.null(labels)) labels <- as.character(1:N)
-    ans <- .C("bionj", as.double(X), as.integer(N),
-              as.character(labels), "", PACKAGE = "ape")
-    read.tree(text = ans[[4]])
+    obj <- list(edge =  cbind(ans[[3]], ans[[4]]), edge.length = ans[[5]],
+                tip.label = labels, Nnode = N - 2L)
+    class(obj) <- "phylo"
+    reorder(obj)
 }