File: nj.R

package info (click to toggle)
r-cran-ape 5.8-1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,676 kB
  • sloc: ansic: 7,676; cpp: 116; sh: 17; makefile: 2
file content (30 lines) | stat: -rw-r--r-- 925 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
## nj.R (2021-05-10)

##   Neighbor-Joining Tree Estimation

## Copyright 2004-2021 Emmanuel Paradis

## This file is part of the R-package `ape'.
## See the file ../COPYING for licensing issues.

nj <- function(X)
{
    if (is.matrix(X)) X <- as.dist(X)
    if (anyNA(X))
        stop("missing values are not allowed in the distance matrix\nConsider using njs()")
    if (any(is.infinite(X)))
        stop("infinite values are not allowed in the distance matrix")
    N <- as.integer(attr(X, "Size"))
    if (N < 3) stop("cannot build an NJ tree with less than 3 observations")
    labels <- attr(X, "Labels")
    if (is.null(labels)) labels <- as.character(1:N)
    DIST <- numeric(length(X))
    DIST[] <- X[]
    obj <- .Call(C_nj, DIST, N)
    names(obj) <- c("edge", "edge.length")
    dim(obj[[1]]) <- c(2L * N - 3L, 2L)
    obj$tip.label <- labels
    obj$Nnode <- N - 2L
    class(obj) <- "phylo"
    reorder(obj)
}