diff --git a/R/tree-sequences.R b/R/tree-sequences.R index 979bb2390..9397ef6c4 100644 --- a/R/tree-sequences.R +++ b/R/tree-sequences.R @@ -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) diff --git a/tests/testthat/test-ts.R b/tests/testthat/test-ts.R index 780966888..b609ca320 100644 --- a/tests/testthat/test-ts.R +++ b/tests/testthat/test-ts.R @@ -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") +})