Skip to content

Commit

Permalink
Catch incorrect individual names used in tskit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bodkan committed Sep 28, 2022
1 parent cee535c commit da7e0bb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/tree-sequences.R
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,8 @@ get_node_ids <- function(ts, x) {
if (is.null(x)) {
ids <- ts_nodes(ts) %>% .[.$sampled, ]$node_id
} else if (is.character(x)) {
if (length(intersect(x, ts_samples(ts)$name)) != length(x))
stop("Not all individual names are among those recorded in the tree sequence", call. = FALSE)
ids <- ts_nodes(ts) %>% dplyr::filter(name %in% x) %>% .$node_id
} else if (is.numeric(x)) {
ids <- as.integer(x)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -704,3 +704,9 @@ test_that("metadata is the same for SLiM and msprime conditional on a model", {
expect_equal(sdata5$time, mdata5$time)
expect_equal(sdata6$time, mdata6$time)
})

test_that("all names of individuals must be present in the tree sequence", {
ts <- ts_load(file = msprime_ts, model = model)
expect_error(ts_diversity(ts, c("p1_1", "p2_2")), "Not all individual names")
expect_s3_class(ts_diversity(ts, c("pop1_1", "pop2_2")), "data.frame")
})

0 comments on commit da7e0bb

Please sign in to comment.