File: is.binary.tree.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 (38 lines) | stat: -rw-r--r-- 1,172 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
32
33
34
35
36
37
38
## is.binary.tree.R (2023-02-07)

##    Test for Binary Tree

## Copyright 2016-2023 Emmanuel Paradis

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

is.binary <- function(phy) UseMethod("is.binary")

is.binary.phylo <- function(phy)
{
    n <- length(phy$tip.label)
    m <- phy$Nnode
    dgr <- tabulate(phy$edge, n + m)
    ref <- c(rep.int(1L, n), rep.int(3L, m))
    ## the root is assumed to be numbered n+1
    if (.is.rooted_ape(phy, n)) ref[n + 1L] <- 2L
    ## can use identical() as long as tabulate() returns integers
    identical(dgr, ref)
}

is.binary.tree <- function(phy)
{
    message("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\nis.binary.tree() is deprecated; using is.binary() instead.\n\nis.binary.tree() will be removed soon: see ?is.binary and update your code.\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    is.binary(phy)
}

is.binary.multiPhylo <- function(phy)
{
    phy <- unclass(phy)
    n <- length(attr(phy, "TipLabel"))
    if (n)
        n - sapply(phy, "[[", "Nnode") + is.rooted.multiPhylo(phy) == 2
    else
        sapply(phy, is.binary.phylo)
}