]> git.donarmstrong.com Git - ape.git/blob - R/me.R
various fixes in C files
[ape.git] / R / me.R
1 ## me.R (2012-04-30)
2
3 ##   Tree Estimation Based on Minimum Evolution Algorithm
4
5 ## Copyright 2007 Vincent Lefort with modifications by
6 ##                Emmanuel Paradis (2008-2012)
7
8 ## This file is part of the R-package `ape'.
9 ## See the file ../COPYING for licensing issues.
10
11 fastme.bal <- function(X, nni = TRUE, spr = TRUE, tbr = TRUE)
12 {
13     if (is.matrix(X)) X <- as.dist(X)
14     N <- as.integer(attr(X, "Size"))
15     nedge <- 2L * N - 3L
16     ans <- .C("me_b", as.double(X), N, 1:N, as.integer(nni),
17               as.integer(spr), as.integer(tbr), integer(nedge),
18               integer(nedge), double(nedge),
19               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
20     labels <- attr(X, "Labels")
21     if (is.null(labels)) labels <- as.character(1:N)
22     labels <- labels[ans[[3]]]
23     structure(list(edge =  cbind(ans[[7]], ans[[8]]),
24                    edge.length = ans[[9]],
25                    tip.label = labels, Nnode = N - 2L),
26               class = "phylo")
27 }
28
29 fastme.ols <- function(X, nni = TRUE)
30 {
31     if (is.matrix(X)) X <- as.dist(X)
32     N <- as.integer(attr(X, "Size"))
33     nedge <- 2L * N - 3L
34     ans <- .C("me_o", as.double(X), N, 1:N, as.integer(nni),
35               integer(nedge), integer(nedge), double(nedge),
36               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
37     labels <- attr(X, "Labels")
38     if (is.null(labels)) labels <- as.character(1:N)
39     labels <- labels[ans[[3]]]
40     structure(list(edge =  cbind(ans[[5]], ans[[6]]),
41                    edge.length = ans[[7]],
42                    tip.label = labels, Nnode = N - 2L),
43               class = "phylo")
44 }
45
46 bionj <- function(X)
47 {
48     if (is.matrix(X)) X <- as.dist(X)
49     if (any(is.na(X)))
50         stop("missing values are not allowed in the distance matrix.\nConsider using bionjs()")
51     if (any(X > 100))
52         stop("at least one distance was greater than 100")
53     N <- as.integer(attr(X, "Size"))
54
55     ans <- .C("bionj", as.double(X), N, integer(2 * N - 3),
56               integer(2 * N - 3), double(2*N - 3),
57               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
58     labels <- attr(X, "Labels")
59     if (is.null(labels)) labels <- as.character(1:N)
60     obj <- list(edge =  cbind(ans[[3]], ans[[4]]), edge.length = ans[[5]],
61                 tip.label = labels, Nnode = N - 2L)
62     class(obj) <- "phylo"
63     reorder(obj)
64 }