File: balance.R

package info (click to toggle)
r-cran-ape 5.8-1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,676 kB
  • sloc: ansic: 7,676; cpp: 116; sh: 17; makefile: 2
file content (31 lines) | stat: -rw-r--r-- 876 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
31
## balance.R (2022-04-25)

##   Balance of a Dichotomous Phylogenetic Tree

## Copyright 2002-2015 Emmanuel Paradis, 2022 Klaus Schliep

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

balance <- function(phy)
{
    if (!inherits(phy, "phylo"))
        stop('object "phy" is not of class "phylo"')
    phy <- reorder(phy, "postorder")
    N <- length(phy$tip.label)
    nb.node <- phy$Nnode
    if (nb.node != N - 1)
        stop('"phy" is not rooted and fully dichotomous')
    ans <- matrix(NA, nb.node, 2)
    nd <- node.depth(phy)
    i <- 1L
    while (i < nrow(phy$edge)) {
        node <- phy$edge[i, 1] - N
        ans[node, 1] <- nd[phy$edge[i,2]]
        ans[node, 2] <- nd[phy$edge[i+1,2]]
        i <- i + 2L
    }
    rownames(ans) <-
        if (is.null(phy$node.label)) N + 1:nb.node else phy$node.label
    ans
}