diff --git a/tests/testthat/test-compute-reference-prop.R b/tests/testthat/test-compute-reference-prop.R new file mode 100644 index 0000000..67ff4b9 --- /dev/null +++ b/tests/testthat/test-compute-reference-prop.R @@ -0,0 +1,46 @@ +# compute_reference_prop() gives the population frequency of each +# hair / skin / eye combination. On a hand-made table we know the answer. + +test_that("compute_reference_prop matches hand counts", { + # 5 people: (1,1,1) twice, (2,1,1), (2,2,1), (2,2,2) once each + d <- data.frame(hair_colour = c(1, 1, 2, 2, 2), + skin_colour = c(1, 1, 1, 2, 2), + eye_colour = c(1, 1, 1, 1, 2)) + out <- compute_reference_prop(d) + + expect_named(out, c("hair_colour", "skin_colour", "eye_colour", "f_h_s_y")) + # every combination of the observed levels is listed, 2 x 2 x 2 + expect_equal(nrow(out), 8) + expect_equal(sum(out$f_h_s_y), 1) + + key <- paste(out$hair_colour, out$skin_colour, out$eye_colour) + f <- setNames(out$f_h_s_y, key) + expect_equal(f[["1 1 1"]], 2 / 5) + expect_equal(f[["2 1 1"]], 1 / 5) + expect_equal(f[["2 2 1"]], 1 / 5) + expect_equal(f[["2 2 2"]], 1 / 5) + # combinations nobody has come out as 0, not missing + expect_equal(f[["1 2 2"]], 0) +}) + +test_that("compute_reference_prop agrees with sim_reference_pop counts", { + pop <- sim_reference_pop(n = 300, seed = 1) + out <- compute_reference_prop(pop) + + expect_equal(sum(out$f_h_s_y), 1) + # spot check one combination against a direct count + i <- which.max(out$f_h_s_y) + n_direct <- sum(pop$hair_colour == out$hair_colour[i] & + pop$skin_colour == out$skin_colour[i] & + pop$eye_colour == out$eye_colour[i]) + expect_equal(out$f_h_s_y[i], n_direct / 300) +}) + +test_that("compute_reference_prop rejects bad input", { + expect_error(compute_reference_prop(list(a = 1)), "data.frame") + expect_error(compute_reference_prop(data.frame(hair_colour = 1)), + "skin_colour, eye_colour") + empty <- data.frame(hair_colour = numeric(0), skin_colour = numeric(0), + eye_colour = numeric(0)) + expect_error(compute_reference_prop(empty), "no rows") +}) diff --git a/tests/testthat/test-cpt-population.R b/tests/testthat/test-cpt-population.R new file mode 100644 index 0000000..db38d41 --- /dev/null +++ b/tests/testthat/test-cpt-population.R @@ -0,0 +1,48 @@ +# cpt_population() is P(sex, age group, hair colour | H2) as a product of +# independent marginals, so every cell can be written down by hand. +# It is soft-deprecated, so the one-time notice is silenced here. + +cpt_pop <- function(...) suppressMessages(cpt_population(...)) + +test_that("cpt_population default table matches the hand calculation", { + out <- cpt_pop() + # MPa = 40, MPr = 6: P(T1) = 12 / 80 = 0.15 + sex_age <- c(`F-T1` = 0.5 * 0.15, `F-T0` = 0.5 * 0.85, + `M-T1` = 0.5 * 0.15, `M-T0` = 0.5 * 0.85) + hair <- c(0.3, 0.2, 0.25, 0.15, 0.1) + expected <- outer(sex_age, hair) + colnames(expected) <- as.character(1:5) + + expect_equal(out, expected) + expect_equal(out["F-T1", "1"], 0.0225) + expect_equal(out["M-T0", "5"], 0.0425) +}) + +test_that("cpt_population margins recover the inputs", { + propS <- c(0.6, 0.4) + propC <- c(0.4, 0.25, 0.2, 0.1, 0.05) + out <- cpt_pop(propS = propS, MPa = 35, MPr = 3, propC = propC) + t1 <- 6 / 80 + + expect_equal(sum(out), 1) + expect_equal(unname(colSums(out)), propC) + expect_equal(unname(rowSums(out)), + c(0.6 * t1, 0.6 * (1 - t1), 0.4 * t1, 0.4 * (1 - t1))) +}) + +test_that("cpt_population normalises proportions that don't sum to 1", { + expect_warning(a <- cpt_pop(propS = c(1, 1)), "propS does not sum to 1") + expect_equal(a, cpt_pop()) + expect_warning(b <- cpt_pop(propC = c(3, 2, 2.5, 1.5, 1)), + "propC does not sum to 1") + expect_equal(b, cpt_pop()) +}) + +test_that("cpt_population rejects bad input", { + expect_error(cpt_pop(propS = 1), "length 2") + expect_error(cpt_pop(propS = c(-0.5, 1.5)), "between 0 and 1") + expect_error(cpt_pop(MPa = 0), "between 1 and 80") + expect_error(cpt_pop(MPr = -1), "non-negative") + expect_error(cpt_pop(propC = c(0.5, 0.5)), "length 5") + expect_error(cpt_pop(propC = c(-0.1, 0.3, 0.3, 0.3, 0.2)), "non-negative") +}) diff --git a/tests/testthat/test-get-allele-freqs.R b/tests/testthat/test-get-allele-freqs.R new file mode 100644 index 0000000..56c21c6 --- /dev/null +++ b/tests/testthat/test-get-allele-freqs.R @@ -0,0 +1,48 @@ +# get_allele_freqs() turns the allele x marker data frames into the named +# list that pedtools wants. Mostly checking it doesn't mix up rows and +# columns, since that would silently give the wrong frequencies. + +test_that("get_allele_freqs reshapes a small table correctly", { + df <- data.frame(Allele = c(8, 9, 9.3, 10), + M1 = c(0.1, 0.2, 0.3, 0.4), + M2 = c(0, 0.5, 0, 0.5)) + out <- get_allele_freqs(df) + + expect_type(out, "list") + expect_named(out, c("M1", "M2")) + expect_identical(out$M1, c(`8` = 0.1, `9` = 0.2, `9.3` = 0.3, `10` = 0.4)) + expect_identical(out$M2, c(`8` = 0, `9` = 0.5, `9.3` = 0, `10` = 0.5)) +}) + +test_that("get_allele_freqs keeps every marker of the bundled databases", { + dbs <- list(Argentina = Argentina, Asia = Asia, Austria = Austria, + BosniaHerz = BosniaHerz, China = China, Europe = Europe, + Japan = Japan, USA = USA) + for (nm in names(dbs)) { + db <- dbs[[nm]] + out <- get_allele_freqs(db) + expect_identical(names(out), setdiff(names(db), "Allele"), info = nm) + for (m in names(out)) { + expect_identical(names(out[[m]]), as.character(db$Allele), info = nm) + } + } +}) + +test_that("get_allele_freqs picks the right cell for a given allele", { + # look up D3S1358 allele 15 directly in the table, then via the list + out <- get_allele_freqs(Argentina) + row <- which(Argentina$Allele == 15) + expect_length(row, 1) + expect_equal(unname(out$D3S1358["15"]), Argentina$D3S1358[row]) + expect_gt(out$D3S1358[["15"]], 0) +}) + +test_that("get_allele_freqs output works with pedtools::setMarkers", { + skip_if_not_installed("pedtools") + freqs <- get_allele_freqs(Argentina) + x <- pedtools::nuclearPed(1) + x <- pedtools::setMarkers(x, locusAttributes = freqs["D3S1358"]) + + af <- pedtools::afreq(x, "D3S1358") + expect_equal(af[names(freqs$D3S1358)], freqs$D3S1358) +})