Skip to content

Commit e115688

Browse files
authored
EdgeRatio() (#253)
1 parent 7e5f175 commit e115688

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: TreeTools
22
Title: Create, Modify and Analyse Phylogenetic Trees
3-
Version: 2.1.0.9000
3+
Version: 2.1.0.9001
44
Authors@R: c(
55
person("Martin R.", 'Smith', role = c("aut", "cre", "cph"),
66
email = "martin.smith@durham.ac.uk",

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ S3method(DropTip,Splits)
5353
S3method(DropTip,list)
5454
S3method(DropTip,multiPhylo)
5555
S3method(DropTip,phylo)
56+
S3method(EdgeRatio,phylo)
5657
S3method(KeepTip,"NULL")
5758
S3method(KeepTip,Splits)
5859
S3method(KeepTip,list)
@@ -298,6 +299,7 @@ export(DropTip)
298299
export(DropTipPhylo)
299300
export(EdgeAncestry)
300301
export(EdgeDistances)
302+
export(EdgeRatio)
301303
export(EndSentence)
302304
export(ExtractTaxa)
303305
export(FirstMatchingSplit)

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# TreeTools 2.1.0.9001 (2026-02-19) #
2+
3+
- `EdgeRatio()` reports the ratio of external:internal edges.
4+
15
# TreeTools 2.1.0.9000 (2026-02-16) #
26

37
- `SplitInformation()` supports `Splits` and `phylo` objects.

R/EdgeRatio.R

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#' Ratio of external:internal edge length
2+
#'
3+
#' Reports the ratio of tree length associated with external edges (i.e.
4+
#' edges whose child is a leaf) and internal edges.
5+
#' Where tree length is dominated by internal edges, variation between tips
6+
#' is dominantly controlled by phylogenetic history.
7+
#'
8+
#' @param x A tree of class \code{\link[ape:read.tree]{phylo}}.
9+
#' @returns `EdgeRatio()` returns a numeric specifying the ratio of external
10+
#' to internal edge length (> 1 means the length of a tree is predominantly
11+
#' in external edges), with attributes `external`, `internal`, and `total`
12+
#' specifying the total length associated with edges of that nature.
13+
#' @template MRS
14+
#' @export
15+
EdgeRatio <- function(x) UseMethod("EdgeRatio")
16+
17+
#' @rdname EdgeRatio
18+
#' @export
19+
EdgeRatio.phylo <- function(x) {
20+
el <- x[["edge.length"]]
21+
if (is.null(el)) {
22+
warning("Edge lengths not specified")
23+
return(NA_real_)
24+
}
25+
ed <- x[["edge"]]
26+
nTip <- NTip(x)
27+
external <- ed[, 2] <= nTip
28+
exLen <- sum(el[external])
29+
inLen <- sum(el[!external])
30+
structure(exLen / inLen,
31+
external = exLen,
32+
internal = inLen,
33+
total = sum(exLen, inLen))
34+
}

man/EdgeRatio.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-EdgeRatio.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
test_that("EdgeRatio() works", {
2+
expect_warning(expect_equal(EdgeRatio(BalancedTree(5)), NA_real_),
3+
"[Ee]dge lengths not spec")
4+
5+
ex2 <- BalancedTree(4)
6+
ex2[["edge.length"]] <- c(1, 2, 2, 1, 2, 2)
7+
expect_equal(EdgeRatio(ex2),
8+
structure(8/2, external = 8, internal = 2, total = 10))
9+
})

0 commit comments

Comments
 (0)