Skip to content

Commit f5f9fa4

Browse files
authored
+ as.Splits.integer() (#232)
2 parents 37dc588 + f12f0bf commit f5f9fa4

6 files changed

Lines changed: 63 additions & 8 deletions

File tree

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: 1.16.1.9000
3+
Version: 1.16.1.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
@@ -197,10 +197,12 @@ S3method(as.Newick,multiPhylo)
197197
S3method(as.Newick,phylo)
198198
S3method(as.Splits,Splits)
199199
S3method(as.Splits,character)
200+
S3method(as.Splits,integer)
200201
S3method(as.Splits,list)
201202
S3method(as.Splits,logical)
202203
S3method(as.Splits,matrix)
203204
S3method(as.Splits,multiPhylo)
205+
S3method(as.Splits,numeric)
204206
S3method(as.Splits,phylo)
205207
S3method(as.TreeNumber,MixedBase)
206208
S3method(as.TreeNumber,TreeNumber)

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# TreeTools 1.16.1.9000 (development) #
1+
# TreeTools 1.16.1.9001` (development) #
22

3+
- New method `as.Splits.integer()`.
34
- Fix `RoguePlot(sort = TRUE)`
45
[Rogue#33](https://github.com/ms609/Rogue/issues/33).
56

R/Splits.R

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ setClass("Splits", representation("raw"))
77
#' its constituent bipartition splits.
88
#'
99
#' @param x Object to convert into splits: perhaps a tree of class
10-
#' \code{\link[ape:read.tree]{phylo}}.
11-
#' If a logical matrix is provided, each row will be considered as a
12-
#' separate split.
10+
#' \code{\link[ape:read.tree]{phylo}}.
11+
#' If a logical matrix is provided, each row will be considered as a separate
12+
#' split.
13+
#' If an integer matrix is provided, entries assigned the same integer will
14+
#' be assigned to the same split.
15+
#'
1316
#' @param tipLabels Character vector specifying sequence in which to order
1417
#' tip labels. Label order must (currently) match to combine or compare separate
1518
#' `Splits` objects.
@@ -212,6 +215,28 @@ as.Splits.matrix <- function(x, tipLabels = NULL, ...) {
212215
}
213216
}
214217

218+
#' @rdname Splits
219+
#' @export
220+
as.Splits.integer <- function(x, tipLabels = NULL, ...) {
221+
membership <- t(vapply(sort(unique(x)), `==`, logical(length(x)), x))
222+
if (dim(membership)[[1]] == 2) {
223+
as.Splits.logical(membership[1, ])
224+
} else {
225+
as.Splits.logical(membership)
226+
}
227+
}
228+
229+
#' @rdname Splits
230+
#' @export
231+
as.Splits.numeric <- function(x, tipLabels = NULL, ...) {
232+
if (all(x == as.integer(x))) {
233+
as.Splits.integer(as.integer(x))
234+
} else {
235+
stop("no applicable method for 'as.Splits' applied to an object of class \"",
236+
class(x), "\"")
237+
}
238+
}
239+
215240
#' @rdname Splits
216241
#' @export
217242
as.Splits.logical <- function(x, tipLabels = NULL, ...) {

man/Splits.Rd

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

tests/testthat/test-Splits.R

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ test_that("as.Splits.Splits()", {
172172
})
173173

174174
test_that("as.Splits.matrix()", {
175-
expect_error(as.Splits(matrix(1, 3, 3)))
176175
expect_error(as.Splits(matrix(1, 2, 2,
177176
dimnames = list(c("edge", "Nnode"), NULL))))
178177
trees <- list(BalancedTree(8), PectinateTree(8),
@@ -241,6 +240,26 @@ test_that("as.Splits.logical()", {
241240
expect_splits_equal(as.Splits(FFTT), as.Splits(t(matrix(FFTT))))
242241
})
243242

243+
test_that("as.Splits.integer()", {
244+
. <- FALSE
245+
X <- TRUE
246+
expect_splits_equal(as.Splits(c(rep(1L, 4))),
247+
as.Splits(c(., ., ., .)))
248+
expect_splits_equal(as.Splits(c(rep(1L, 4), rep(2L, 4))),
249+
as.Splits(c(., ., ., ., X, X, X, X)))
250+
expect_splits_equal(as.Splits(c(rep(1L, 4), rep(2L, 4), 3L)),
251+
as.Splits(rbind(c(X, X, X, X, ., ., ., ., .),
252+
c(., ., ., ., X, X, X, X, .),
253+
c(., ., ., ., ., ., ., ., X))))
254+
expect_splits_equal(as.Splits(c(rep(33, 4), 0, rep(-1, 3), 0)),
255+
as.Splits(rbind(c(., ., ., ., ., X, X, X, .),
256+
c(., ., ., ., X, ., ., ., X),
257+
c(X, X, X, X, ., ., ., ., .))))
258+
expect_error(as.Splits(c(rep(1.45, 4), rep(sqrt(2), 3))),
259+
"no applicable method")
260+
})
261+
262+
244263
test_that("as.Splits.character()", {
245264
a..f <- letters[1:6]
246265
a..i <- letters[1:9]

0 commit comments

Comments
 (0)