]> git.donarmstrong.com Git - ape.git/blob - R/nj.R
current 2.1 release
[ape.git] / R / nj.R
1 ## nj.R (2006-09-15)
2
3 ##   Neighbor-Joining Tree Estimation
4
5 ## Copyright 2004-2006 Emmanuel Paradis
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 nj <- function(X)
11 {
12     if (is.matrix(X)) X <- as.dist(X)
13     N <- attr(X, "Size")
14     labels <- attr(X, "Labels")
15     if (is.null(labels)) labels <- as.character(1:N)
16     edge1 <- edge2 <- integer(2*N - 3)
17     edge.length <- numeric(2*N - 3)
18     ans <- .C("nj", as.double(X), as.integer(N), as.integer(edge1),
19               as.integer(edge2), as.double(edge.length), PACKAGE = "ape")
20     obj <- list(edge = cbind(ans[[3]], ans[[4]]),
21                 edge.length = ans[[5]], tip.label = labels)
22     obj$Nnode <- N - 2
23     class(obj) <- "phylo"
24     reorder(obj)
25 }