tests/testthat/test-gl.filter.maf.R

# Slow file: skipped on CRAN to keep the check time under 10 minutes; runs
# with devtools::test() and on GitHub Actions.
skip_on_cran()

# Characterization tests for gl.filter.maf
# Baseline snapshotted before review (review-gl.filter.maf).
# Assertions marked [approved diff] were flipped in Phase C.

gl4 <- gl.keep.pop(testset.gl,
                   pop.list = c("EmmacBrisWive", "EmmacBurdMist",
                                "EmmacClarJack", "EmmacRussEube"),
                   verbose = 0)

test_that("global path matches an independent recomputation", {
  pdf(NULL); on.exit(dev.off())
  o <- capture.output(f <- gl.filter.maf(gl4, threshold = 0.05,
        plot.display = FALSE, verbose = 0))
  expect_length(o, 0)
  x1 <- utils.recalc.maf(gl4, verbose = 0)
  mymaf <- x1@other$loc.metrics$maf
  expect_equal(nLoc(f), length(which(mymaf >= 0.05)))   # 17 on gl4
  expect_equal(nLoc(f), 17)
  expect_equal(nrow(f@other$loc.metrics), nLoc(f))
  # NA-maf (all-NA) loci are removed by the global path
  expect_equal(sum(is.na(mymaf)), 3)
})

test_that("MAC interpretation for threshold > 1", {
  pdf(NULL); on.exit(dev.off())
  invisible(capture.output(f <- gl.filter.maf(gl4, threshold = 5,
        plot.display = FALSE, verbose = 0)))
  expect_equal(nLoc(f), 15)
})

test_that("by.pop path counts and history", {
  pdf(NULL); on.exit(dev.off())
  xcopy <- gl4
  invisible(capture.output(f <- gl.filter.maf(gl4, threshold = 0.05,
        by.pop = TRUE, pop.limit = 1, ind.limit = 5,
        plot.display = FALSE, verbose = 0)))
  expect_equal(nLoc(gl4) - nLoc(f), 221)
  expect_identical(xcopy, gl4)
  h <- f@other$history
  expect_equal(deparse(h[[length(h)]][[1]]), "gl.filter.maf")
})

test_that("by.pop with no qualifying loci or populations", {
  pdf(NULL); on.exit(dev.off())
  # [approved diff F1] baseline: when no populations met ind.limit (or
  # no loci qualified), x[,-integer(0)] subset to ZERO loci and
  # crashed. Now the object is returned unchanged, silent at verbose 0.
  o <- capture.output(f <- gl.filter.maf(gl4, threshold = 0.05,
        by.pop = TRUE, ind.limit = 50, plot.display = FALSE,
        verbose = 0))
  expect_length(o, 0)
  expect_equal(nLoc(f), nLoc(gl4))
})

test_that("plot.file with display off", {
  pdf(NULL); on.exit(dev.off())
  # [approved diff F2] baseline: crashed with "object 'p3' not found".
  # Now a gated notice; nothing saved; silent at verbose 0.
  o <- capture.output(f <- gl.filter.maf(gl4, threshold = 0.05,
        plot.display = FALSE, plot.file = "maf-x", verbose = 0))
  expect_length(o, 0)
  expect_equal(nLoc(f), 17)
})

test_that("threshold coercion warning at verbose 0; visibility", {
  pdf(NULL); on.exit(dev.off())
  # [approved diff F4] baseline: the range warning printed at verbose 0.
  o <- capture.output(f <- gl.filter.maf(gl4, threshold = 0.9,
        plot.display = FALSE, verbose = 0))
  expect_length(o, 0)
  # [approved diff F5] baseline: visible return.
  v <- withVisible(gl.filter.maf(gl4, threshold = 0.05,
        plot.display = FALSE, verbose = 0))
  expect_false(v$visible)
})

test_that("SilicoDArT data rejected", {
  pdf(NULL); on.exit(dev.off())
  # [approved diff F6] baseline: SilicoDArT objects ran through
  # utils.recalc.maf producing meaningless MAF values; now fatal.
  expect_error(capture.output(gl.filter.maf(testset.gs, threshold = 0.05,
        plot.display = FALSE, verbose = 0)))
})

test_that("by-population plots work when loc.metrics has no maf column", {
  # Regression: with by.pop = TRUE and plots on, the pre-filter histogram
  # over all populations read x@other$loc.metrics$maf without computing
  # it, so an object without that column (testset.gl) stopped with
  # "'names' attribute [1] must be the same length as the vector [0]",
  # and a stale column was plotted as it was.
  pdf(NULL); on.exit(dev.off(), add = TRUE)
  x <- testset.gl
  expect_null(x@other$loc.metrics$maf)
  d <- tempfile("maf_plot"); dir.create(d)
  invisible(capture.output(res <- gl.filter.maf(
    x, by.pop = TRUE, ind.limit = 5, threshold = 0.05, plot.dir = d,
    plot.file = "p", verbose = 3)))
  expect_s4_class(res, "genlight")
  expect_true(length(list.files(d)) > 0)
})

Try the dartR.base package in your browser

Any scripts or data that you put into this service are public.

dartR.base documentation built on Nov. 18, 2026, 9:06 a.m.