Nothing
# Slow file: skipped on CRAN to keep the check time under 10 minutes; runs
# with devtools::test() and on GitHub Actions.
skip_on_cran()
test_that("gl.filter.contamination removes exactly the individuals the report flags", {
x <- testset.gl
capture.output(r <- gl.report.contamination(x, plot.display = FALSE, verbose = 0))
expected.drop <- r$ind$id[r$ind$flag %in% c("suspect", "adjacent")]
out <- capture.output(x2 <- gl.filter.contamination(x, verbose = 0))
expect_length(out, 0)
expect_s4_class(x2, "genlight")
expect_equal(nInd(x2), nInd(x) - length(expected.drop))
expect_setequal(indNames(x2), setdiff(indNames(x), expected.drop))
expect_equal(nLoc(x2), nLoc(x))
expect_true(all(ploidy(x2) == 2))
expect_equal(nrow(x2@other$ind.metrics), nInd(x2))
expect_length(x2@other$history, length(x@other$history) + 1)
})
test_that("a synthetic mixture of two individuals is removed", {
# Same construction as the report test: the host is the WA animal with
# median heterozygosity, the donor the first SA animal
set.seed(1)
x <- bandicoot.gl
m <- as.matrix(x)
het0 <- rowMeans(m == 1, na.rm = TRUE)
wa <- which(pop(x) == "WA")
host <- wa[which.min(abs(het0[wa] - median(het0[wa])))]
donor <- which(pop(x) == "SA")[1]
loci <- which(!is.na(m[host, ]) & !is.na(m[donor, ]) & m[host, ] != m[donor, ])
m[host, sample(loci, round(0.5 * length(loci)))] <- 1
x2 <- x
x2@gen <- new("genlight", m, ploidy = 2)@gen
indNames(x2) <- indNames(x); pop(x2) <- pop(x)
out <- capture.output(x3 <- gl.filter.contamination(x2, verbose = 3))
expect_false(indNames(x)[host] %in% indNames(x3))
expect_true(indNames(x)[donor] %in% indNames(x3))
expect_true(any(grepl(indNames(x)[host], out, fixed = TRUE)))
expect_true(any(grepl("Number of individuals removed:", out)))
})
test_that("flag selects the classes removed", {
x <- testset.gl
capture.output(r <- gl.report.contamination(x, plot.display = FALSE, verbose = 0))
# only plate-adjacent suspects: testset.gl has no plate positions, so none
capture.output(x.adj <- gl.filter.contamination(x, flag = "adjacent", verbose = 0))
expect_equal(nInd(x.adj), nInd(x))
# all three classes
capture.output(x.all <- gl.filter.contamination(
x, flag = c("suspect", "adjacent", "rare-only"), verbose = 0))
expect_equal(nInd(x.all), sum(r$ind$flag == ""))
expect_error(capture.output(gl.filter.contamination(x, flag = "bogus", verbose = 0)),
"flag must be one or more of")
})
test_that("locus metric flags are reset when individuals are removed, or recalculated", {
x <- testset.gl
capture.output(r <- gl.report.contamination(x, plot.display = FALSE, verbose = 0))
skip_if(sum(r$ind$flag %in% c("suspect", "adjacent")) == 0,
"testset.gl yields no suspect at the default thresholds")
capture.output(x2 <- gl.filter.contamination(x, verbose = 0))
expect_false(isTRUE(x2@other$loc.metrics.flags$CallRate))
expect_false(isTRUE(x2@other$loc.metrics.flags$maf))
capture.output(x3 <- gl.filter.contamination(x, recalc = TRUE, verbose = 0))
expect_true(isTRUE(x3@other$loc.metrics.flags$CallRate))
expect_equal(nrow(x3@other$loc.metrics), nLoc(x3))
capture.output(x4 <- gl.filter.contamination(x, mono.rm = TRUE, verbose = 0))
expect_true(nLoc(x4) <= nLoc(x))
expect_equal(nrow(x4@other$loc.metrics), nLoc(x4))
})
test_that("presence/absence data and bad screen parameters stop", {
expect_error(capture.output(gl.filter.contamination(testset.gs, verbose = 0)))
expect_error(capture.output(gl.filter.contamination(testset.gl, z.flag = 0, verbose = 0)))
expect_error(capture.output(gl.filter.contamination(testset.gl, plate = data.frame(id = 1),
verbose = 0)))
})
# Characterization baseline (function-review, 2026-09-24)
test_that("baseline: individuals retained on packaged data", {
for (b in list(list("testset.gl", 249, 255), list("platypus.gl", 81, 1000),
list("bandicoot.gl", 89, 1000))) {
capture.output(y <- gl.filter.contamination(get(b[[1]]), verbose = 0))
expect_equal(c(nInd(y), nLoc(y)), c(b[[2]], b[[3]]), label = b[[1]])
}
})
test_that("the filter repeats the screen's caveats", {
x <- testset.gl
# no plate positions: a warning at verbose 1 when only "adjacent" is asked
out <- capture.output(gl.filter.contamination(x, flag = "adjacent", verbose = 1))
expect_true(any(grepl("no plate positions found", out)))
# with "suspect" too, the result is not empty by construction: verbose 2
out <- capture.output(gl.filter.contamination(x, verbose = 1))
expect_false(any(grepl("no plate positions found", out)))
out <- capture.output(gl.filter.contamination(x, verbose = 2))
expect_true(any(grepl("no plate positions found", out)))
# singleton populations are named
y <- bandicoot.gl[c(1:20, 90), ]
out <- capture.output(gl.filter.contamination(y, verbose = 2))
expect_true(any(grepl("single individual", out)))
# invalid rare.freq now stops through the filter too
expect_error(capture.output(gl.filter.contamination(x, rare.freq = 0.9, verbose = 0)),
"rare.freq must lie")
})
test_that("by default the filter removes exactly the suspects the report counts, mixed samples included", {
# reads.cohort() lives in test-gl.report.contamination.R; a mixed sample
# (80% one animal, 20% another of the same population) is built here
set.seed(2)
n.loc <- 3000
p <- runif(n.loc, 0.05, 0.5)
true <- matrix(rbinom(30 * n.loc, 2, rep(p, each = 30)), 30)
q <- rbind(true / 2, 0.8 * true[2, ] / 2 + 0.2 * true[3, ] / 2)
depth <- matrix(rpois(length(q), 40), nrow(q))
qq <- ifelse(q > 0 & q < 1, rbeta(length(q), 30 * q, 30 * (1 - q)),
pmin(pmax(q, 0.005), 0.995))
alt <- matrix(rbinom(length(q), depth, qq), nrow(q))
frac <- alt / depth
g <- ifelse(frac < 0.1, 0, ifelse(frac > 0.9, 2, 1))
x <- new("genlight", g, ploidy = 2)
indNames(x) <- c(paste0("i", 1:30), "mix")
pop(x) <- rep("A", 31)
x <- gl.compliance.check(x, verbose = 0)
x@other$geno.metrics <- list(ref.count = depth - alt, alt.count = alt)
for (mc in c(1000, 5000)) {
# min.calls = 5000 exceeds the loci, so nobody is screened by read
# balance: the filter must pass it on to agree with the report
out <- capture.output(r <- gl.report.contamination(x, min.calls = mc,
plot.display = FALSE,
verbose = 1))
flagged <- r$ind$id[r$ind$flag %in% c("suspect", "adjacent", "mixed")]
expect_true("mix" %in% flagged)
expect_true(any(grepl(paste("Suspect individuals:", length(flagged)),
out)))
capture.output(y <- gl.filter.contamination(x, min.calls = mc,
verbose = 0))
expect_setequal(setdiff(indNames(x), indNames(y)), flagged)
# removing only the mixed class shows that the read-balance arguments
# reach the screen: mix is mixed only when it is screened
mixed <- r$ind$id[r$ind$flag == "mixed"]
expect_equal(mixed, if (mc == 1000) "mix" else character(0))
capture.output(y <- gl.filter.contamination(x, flag = "mixed",
min.calls = mc, verbose = 0))
expect_setequal(setdiff(indNames(x), indNames(y)), mixed)
}
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.