]> git.donarmstrong.com Git - ape.git/blob - R/speciesTree.R
some changes for ape 3.0-6
[ape.git] / R / speciesTree.R
1 speciesTree <- function(x, FUN = min)
2 ### FUN = min => MAXTREE (Liu et al. 2010)
3 ### FUN = sum => shallowest divergence (Maddison & Knowles 2006)
4 {
5     test.ultra <- which(!unlist(lapply(x, is.ultrametric)))
6     if (length(test.ultra))
7         stop(paste("the following trees were not ultrametric:\n",
8                    paste(test.ultra, collapse = " ")))
9
10     Ntree <- length(x)
11     D <- lapply(x, cophenetic.phylo)
12     nms <- rownames(D[[1]])
13     n <- length(nms)
14     M <- matrix(0, n*(n - 1)/2, Ntree)
15     for (i in 1:Ntree) M[, i] <- as.dist(D[[i]][nms, nms])
16     Y <- apply(M, 1, FUN)
17     attributes(Y) <- list(Size = n, Labels = nms, Diag = FALSE,
18                           Upper = FALSE, class = "dist")
19     as.phylo(stats::hclust(Y, "single"))
20 }