]> git.donarmstrong.com Git - ape.git/blob - R/triangMtd.R
fix in birthdeath()
[ape.git] / R / triangMtd.R
1 ## treePop.R (2011-10-11)
2
3 ## Tree Reconstruction With the Triangles Method
4
5 ## Copyright 2011 Andrei-Alin Popescu
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 triangMtd <- function(X)
11 {
12     if (is.matrix(X)) X <- as.dist(X)
13     if (any(is.na(X)))
14         stop("missing values are not allowed in the distance matrix")
15     N <- attr(X, "Size")
16     labels <- attr(X, "Labels")
17     if (is.null(labels)) labels <- as.character(1:N)
18     ans <- .C("triangMtd", as.double(X), as.integer(N), integer(2*N - 3),
19               integer(2*N - 3), double(2*N - 3),
20               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
21     obj <- list(edge = cbind(ans[[3]], ans[[4]]), edge.length = ans[[5]],
22                 tip.label = labels, Nnode = N - 2L)
23     class(obj) <- "phylo"
24     reorder(obj)
25 }
26
27 triangMtds <- function(X)
28 {
29     if (is.matrix(X)) X <- as.dist(X)
30     X[is.na(X)] <- -1
31     X[X < 0] <- -1
32     N <- attr(X, "Size")
33     labels <- attr(X, "Labels")
34     if (is.null(labels)) labels <- as.character(1:N)
35     ans <- .C("triangMtds", as.double(X), as.integer(N), integer(2*N - 3),
36               integer(2*N - 3), double(2*N - 3),
37               DUP = FALSE, NAOK = TRUE, PACKAGE = "ape")
38     obj <- list(edge = cbind(ans[[3]], ans[[4]]), edge.length = ans[[5]],
39                 tip.label = labels, Nnode = N - 2L)
40     class(obj) <- "phylo"
41     reorder(obj)
42 }