]> git.donarmstrong.com Git - ape.git/blob - R/me.R
93cebd378303e1b2bfee2f70a941a38e7cbfa2f4
[ape.git] / R / me.R
1 ## me.R (2008-01-12)
2
3 ##   Tree Estimation Based on Minimum Evolution Algorithm
4
5 ## Copyright 2007 Vincent Lefort with modifications by
6 ##                 Emmanuel Paradis (2008)
7
8 ## This file is part of the R-package `ape'.
9 ## See the file ../COPYING for licensing issues.
10
11 .fastme <- function(X, nni, lib)
12 {
13     if (is.matrix(X)) X <- as.dist(X)
14     N <- as.integer(attr(X, "Size"))
15     labels <- sprintf("%6s", 1:N)
16     edge1 <- edge2 <- integer(2*N - 3)
17     ans <- .C(lib, as.double(X), N, labels, as.integer(nni),
18               edge1, edge2, double(2*N - 3), character(N),
19               PACKAGE = "ape")
20     labels <- substr(ans[[8]], 1, 6)
21     LABS <- attr(X, "Labels")
22     labels <- if (!is.null(LABS)) LABS[as.numeric(labels)]
23     else gsub("^ ", "", labels)
24     structure(list(edge =  cbind(ans[[5]], ans[[6]]), edge.length = ans[[7]],
25                    tip.label = labels, Nnode = N - 2),
26               class = "phylo")
27 }
28
29 fastme.bal <- function(X, nni = TRUE) .fastme(X, nni, "me_b")
30
31 fastme.ols <- function(X, nni = TRUE) .fastme(X, nni, "me_o")
32
33 bionj <- function(X)
34 {
35     if (is.matrix(X)) X <- as.dist(X)
36     N <- as.integer(attr(X, "Size"))
37     labels <- sprintf("%6s", 1:N)
38     edge1 <- edge2 <- integer(2*N - 3)
39     ans <- .C("bionj", as.double(X), N, labels, edge1, edge2,
40               double(2*N - 3), character(N), PACKAGE = "ape")
41     labels <- substr(ans[[7]], 1, 6)
42     LABS <- attr(X, "Labels")
43     labels <- if (!is.null(LABS)) LABS[as.numeric(labels)]
44     else gsub("^ ", "", labels)
45     structure(list(edge =  cbind(ans[[4]], ans[[5]]), edge.length = ans[[6]],
46                    tip.label = labels, Nnode = N - 2),
47               class = "phylo")
48 }