]> git.donarmstrong.com Git - ape.git/blob - R/me.R
some updates for ape 3.0-7
[ape.git] / R / me.R
1 ## me.R (2012-09-14)
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     obj <- list(edge =  cbind(ans[[7]], ans[[8]]),
24                 edge.length = ans[[9]],
25                 tip.label = labels, Nnode = N - 2L)
26     class(obj) <- "phylo"
27     attr(obj, "order") <- "cladewise"
28     obj
29 }
30
31 fastme.ols <- function(X, nni = TRUE)
32 {
33     if (is.matrix(X)) X <- as.dist(X)
34     N <- as.integer(attr(X, "Size"))
35     nedge <- 2L * N - 3L
36     ans <- .C("me_o", as.double(X), N, 1:N, as.integer(nni),
37               integer(nedge), integer(nedge), double(nedge),
38               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
39     labels <- attr(X, "Labels")
40     if (is.null(labels)) labels <- as.character(1:N)
41     labels <- labels[ans[[3]]]
42     obj <- list(edge =  cbind(ans[[5]], ans[[6]]),
43                 edge.length = ans[[7]],
44                 tip.label = labels, Nnode = N - 2L)
45     class(obj) <- "phylo"
46     attr(obj, "order") <- "cladewise"
47     obj
48 }
49
50 bionj <- function(X)
51 {
52     if (is.matrix(X)) X <- as.dist(X)
53     if (any(is.na(X)))
54         stop("missing values are not allowed in the distance matrix.\nConsider using bionjs()")
55     if (any(X > 100))
56         stop("at least one distance was greater than 100")
57     N <- as.integer(attr(X, "Size"))
58
59     ans <- .C("bionj", as.double(X), N, integer(2 * N - 3),
60               integer(2 * N - 3), double(2*N - 3),
61               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
62     labels <- attr(X, "Labels")
63     if (is.null(labels)) labels <- as.character(1:N)
64     obj <- list(edge =  cbind(ans[[3]], ans[[4]]), edge.length = ans[[5]],
65                 tip.label = labels, Nnode = N - 2L)
66     class(obj) <- "phylo"
67     reorder(obj)
68 }