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()
# Characterization tests for gl.pcoa
# Baseline snapshotted before review (review-gl.pcoa), upstream/dev ddaed27.
# Assertions marked [approved F<n>] were flipped in Phase C per the
# custodian's approval of findings 1, 3, 4, 5, 6 (2026-09-06).
test_that("glPca path: nfactors respected, eig full length, values stable", {
pdf(NULL); on.exit(dev.off())
for (nf in c(2, 5, 10)) {
p <- gl.pcoa(testset2.gl, nfactors = nf, plot.out = FALSE, verbose = 0)
expect_equal(dim(p$scores), c(274, nf))
expect_equal(dim(p$loadings), c(611, nf))
expect_length(p$eig, 273)
}
p5 <- gl.pcoa(testset2.gl, nfactors = 5, plot.out = FALSE, verbose = 0)
expect_equal(round(p5$eig[1:3], 6), c(1.382214, 1.344892, 1.114741))
expect_equal(round(sum(abs(p5$scores)), 3), 1186.296)
expect_s3_class(p5, "glPca")
})
test_that("glPca path: verbose = 3 summary on testset2.gl has no NA lines", {
pdf(NULL); on.exit(dev.off())
out <- capture.output(
invisible(gl.pcoa(testset2.gl, plot.out = FALSE, verbose = 3)))
expect_true(any(grepl("27 informative dimensions", out)))
expect_false(any(grepl("NA % of the total variance", out)))
})
test_that("low-rank object (<3 informative axes): verbose = 3 output", {
pdf(NULL); on.exit(dev.off())
lr <- testset2.gl[1:4, ]
out <- capture.output(plr <- gl.pcoa(lr, plot.out = FALSE, verbose = 3))
expect_length(plr$eig, 3)
# [approved F5] axis-combination lines are now printed only when enough
# informative axes exist; the NA line is gone and Axis 1-3 is suppressed.
expect_false(any(grepl("NA % of the total variance", out)))
expect_true(any(grepl("Axis 1 and 2 combined explain 61.7", out)))
expect_false(any(grepl("Axis 1-3 combined", out)))
})
test_that("dist path: enough entities, nfactors honoured, values stable", {
pdf(NULL); on.exit(dev.off())
set.seed(42)
m10 <- matrix(sample(0:2, 10 * 40, replace = TRUE), nrow = 10,
dimnames = list(paste0("I", 1:10), NULL))
D10 <- dist(m10)
pd <- gl.pcoa(D10, nfactors = 5, plot.out = FALSE, verbose = 0)
expect_equal(dim(pd$scores), c(10, 5))
expect_length(pd$eig, 9)
expect_equal(round(pd$eig[1:3], 4), c(44.7602, 38.2836, 33.5782))
# correction = "none": ape::pcoa returns no vectors.cor, loadings are NULL
expect_null(pd$loadings)
})
test_that("dist path: fewer entities than nfactors + 1", {
pdf(NULL); on.exit(dev.off())
set.seed(42)
m4 <- matrix(sample(0:2, 4 * 40, replace = TRUE), nrow = 4,
dimnames = list(paste0("I", 1:4), NULL))
D4 <- dist(m4)
# [approved F6] previously died with "subscript out of bounds"; now clamps
# nfactors to the available axes and warns.
out <- capture.output(pd4 <- gl.pcoa(D4, nfactors = 5, plot.out = FALSE,
verbose = 2))
expect_true(any(grepl("Warning.*nfactors", out)))
expect_equal(dim(pd4$scores), c(4, 3))
expect_length(pd4$eig, 3)
})
test_that("dist path: verbose = 3 with <3 positive eigenvalues", {
pdf(NULL); on.exit(dev.off())
set.seed(7)
m3 <- matrix(sample(0:2, 3 * 40, replace = TRUE), nrow = 3,
dimnames = list(paste0("I", 1:3), NULL))
D3 <- dist(m3)
out <- capture.output(p3 <- gl.pcoa(D3, nfactors = 2, plot.out = FALSE,
verbose = 3))
expect_equal(dim(p3$scores), c(3, 2))
# [approved F5] the Axis 1-3 line is suppressed when only two positive
# eigenvalues exist (guard was length(eig.top >= 3), a precedence bug).
expect_false(any(grepl("NA % of the total variance", out)))
expect_true(any(grepl("Axis 1 and 2 combined explain 100", out)))
expect_false(any(grepl("Axis 1-3 combined", out)))
})
test_that("FBM path: nfactors and eig length", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
fb <- gl.gen2fbm(testset2.gl)
pf <- gl.pcoa(fb, nfactors = 5, plot.out = FALSE, verbose = 0)
# [approved F1] nfactors was silently ignored on the big_SVD path (scores
# came back 274 x 273); scores/loadings are now truncated to nfactors,
# $eig stays full length as on the glPca path.
expect_equal(dim(pf$scores), c(274, 5))
expect_equal(dim(pf$loadings), c(611, 5))
expect_length(pf$eig, 273)
# [approved 2026-11-10] missing values on the FBM path are replaced by the
# locus mean, as glPca does, in place of gl.impute(method = "neighbour").
# The eigenvalues were c(2.259724, 2.184270, 1.652706) with neighbour
# imputation; they now equal the glPca values of the first test, up to the
# two-decimal rounding of the stored mean.
expect_equal(pf$eig[1:3], c(1.382214, 1.344892, 1.114741), tolerance = 1e-3)
})
# ---------------------------------------------------------------------------
# FBM path vs glPca path. Reported by the dartr_shiny session (2026-11-10):
# on FBM objects loadings came back twice as large as glPca's unit-norm
# loadings, and scores/loadings had no dimnames, so gl.report.factorloadings
# lost locus names and gl.filter.factorloadings filtered differently at the
# same threshold. Data are imputed first so missingness plays no part.
fbm_pcoa_pair <- function(x, nfactors = 5) {
pdf(NULL); on.exit(dev.off())
x <- gl.filter.monomorphs(x, verbose = 0)
x <- gl.impute(x, method = "neighbour", verbose = 0)
a <- gl.pcoa(x, nfactors = nfactors, plot.out = FALSE, verbose = 0)
b <- gl.pcoa(gl.gen2fbm(x, verbose = 0), nfactors = nfactors,
plot.out = FALSE, verbose = 0)
list(x = x, mem = a, fbm = b)
}
test_that("FBM path: loadings have unit norm like glPca, scores and eig agree", {
skip_if_not_installed("bigstatsr")
r <- fbm_pcoa_pair(platypus.gl)
a <- r$mem; b <- r$fbm
expect_equal(b$eig[1:5], a$eig[1:5], tolerance = 1e-6)
for (k in 1:5) {
expect_equal(unname(abs(b$scores[, k])), unname(abs(a$scores[, k])),
tolerance = 1e-6)
expect_equal(unname(abs(b$loadings[, k])), unname(abs(a$loadings[, k])),
tolerance = 1e-6)
}
expect_equal(unname(sqrt(colSums(b$loadings^2))), rep(1, 5),
tolerance = 1e-6)
})
test_that("FBM path: scores and loadings carry the same dimnames as glPca", {
skip_if_not_installed("bigstatsr")
r <- fbm_pcoa_pair(platypus.gl)
a <- r$mem; b <- r$fbm
expect_equal(rownames(b$scores), indNames(r$x))
expect_equal(colnames(b$scores), paste0("PC", 1:5))
expect_equal(rownames(b$loadings), rownames(a$loadings))
expect_equal(colnames(b$loadings), paste0("Axis", 1:5))
})
test_that("FBM path: fewer loci than individuals", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
# big_SVD() was asked for nInd - 1 = 273 components from a 50-locus matrix
# and stopped with "'k' must satisfy 0 < k < nrow(A)"; it can return at
# most min(nInd, nLoc) - 1. 50 polymorphic loci, so mono.rm keeps all 50.
poly <- gl.filter.monomorphs(testset2.gl, verbose = 0)
x <- gl.keep.loc(poly, loc.list = locNames(poly)[1:50], verbose = 0)
pf <- gl.pcoa(gl.gen2fbm(x), nfactors = 5, plot.out = FALSE, verbose = 0)
expect_equal(dim(pf$scores), c(274, 5))
expect_length(pf$eig, 49)
expect_true(all(is.finite(pf$scores)))
})
test_that("FBM path: a NaN trailing component does not make every score NaN", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
# big_SVD() returns the rank-deficient trailing component of testset.gl as
# NaN in d, u and v. Only d was zeroed, and NaN * 0 is NaN, so
# u %*% diag(d) turned all 250 x 5 scores into NaN.
pf <- gl.pcoa(gl.gen2fbm(testset.gl), nfactors = 5, plot.out = FALSE,
verbose = 0)
expect_true(all(is.finite(pf$scores)))
expect_true(all(is.finite(pf$loadings)))
expect_true(all(is.finite(pf$eig)))
})
# ---------------------------------------------------------------------------
# FBM path with missing data (2026-11-10). The FBM path imputed with
# gl.impute(method = "neighbour") before the PCA while glPca replaces a
# missing value by the locus mean, so the same data gave different
# ordinations in the two storage modes (first eigenvalue of platypus.gl 3.40
# against 2.82). It also read the whole matrix with as.matrix(x) to test for
# missing values. The PCA is now computed from the backing file in blocks,
# with locus-mean imputation in a temporary copy.
test_that("FBM path: missing data give the same ordination as glPca", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
expect_gt(sum(is.na(as.matrix(platypus.gl))), 0)
a <- gl.pcoa(platypus.gl, nfactors = 3, plot.out = FALSE, verbose = 0)
b <- gl.pcoa(gl.gen2fbm(platypus.gl, verbose = 0), nfactors = 3,
plot.out = FALSE, verbose = 0)
# tolerance: the imputed mean is stored rounded to two decimals
expect_equal(b$eig[1:10], a$eig[1:10], tolerance = 1e-3)
for (k in 1:3) {
expect_equal(unname(abs(b$scores[, k])), unname(abs(a$scores[, k])),
tolerance = 5e-3)
expect_equal(unname(abs(b$loadings[, k])), unname(abs(a$loadings[, k])),
tolerance = 5e-3)
}
expect_equal(dimnames(b$scores), dimnames(a$scores))
expect_equal(rownames(b$loadings), rownames(a$loadings))
expect_equal(unname(sqrt(colSums(b$loadings^2))), rep(1, 3),
tolerance = 1e-6)
})
test_that("FBM path: missing data, fewer loci than individuals", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
# the nLoc x nLoc branch; glPca keeps one eigenvalue more (50 against 49)
poly <- gl.filter.monomorphs(testset2.gl, verbose = 0)
x <- gl.keep.loc(poly, loc.list = locNames(poly)[1:50], verbose = 0)
expect_gt(sum(is.na(as.matrix(x))), 0)
a <- gl.pcoa(x, nfactors = 3, plot.out = FALSE, verbose = 0)
b <- gl.pcoa(gl.gen2fbm(x, verbose = 0), nfactors = 3, plot.out = FALSE,
verbose = 0)
expect_equal(b$eig[1:10], a$eig[1:10], tolerance = 1e-3)
for (k in 1:3) {
expect_equal(unname(abs(b$scores[, k])), unname(abs(a$scores[, k])),
tolerance = 5e-3)
}
expect_equal(dim(b$loadings), c(50, 3))
expect_true(all(is.finite(b$loadings)))
})
test_that("FBM path: the object passed in and its backing file are unchanged", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
# snp_fastImputeSimple() writes the imputed codes into the file it is
# given, so the PCA must impute a copy
fb <- gl.gen2fbm(platypus.gl, verbose = 0)
bk <- fb@fbm$backingfile
md5.before <- unname(tools::md5sum(bk))
na.before <- sum(is.na(as.matrix(fb)))
tmp.before <- list.files(tempdir(), pattern = "^gl_pcoa_")
out <- capture.output(
p <- gl.pcoa(fb, plot.out = FALSE, verbose = 2))
expect_true(any(grepl("Missing values replaced by the mean", out)))
expect_equal(unname(tools::md5sum(bk)), md5.before)
expect_equal(sum(is.na(as.matrix(fb))), na.before)
# the temporary imputed copy is removed
expect_setequal(list.files(tempdir(), pattern = "^gl_pcoa_"), tmp.before)
})
test_that("FBM path: no imputation message or copy without missing data", {
skip_if_not_installed("bigstatsr")
pdf(NULL); on.exit(dev.off())
x <- gl.filter.callrate(platypus.gl, threshold = 1, plot.display = FALSE,
verbose = 0)
expect_equal(sum(is.na(as.matrix(x))), 0)
a <- gl.pcoa(x, nfactors = 3, plot.out = FALSE, verbose = 0)
out <- capture.output(
b <- gl.pcoa(gl.gen2fbm(x, verbose = 0), nfactors = 3, plot.out = FALSE,
verbose = 2))
expect_false(any(grepl("Missing values replaced", out)))
expect_equal(b$eig[1:10], a$eig[1:10], tolerance = 1e-6)
})
test_that("FBM path: gl.pcoa never decodes the whole matrix", {
# Missing data (mean-imputed copy), a monomorphic and an all-NA locus
# (filtered first), and both orientations of the cross-product. Every
# step must read the backing file in blocks, never through as.matrix().
skip_if_not_installed("bigsnpr")
x <- gl.compliance.check(platypus.gl[, 1:200], verbose = 0)
m <- as.matrix(x)
m[, 3] <- 0L # monomorphic
m[, 4] <- NA # all missing
x@gen <- new("genlight", m, ploidy = ploidy(x))@gen
f <- gl.gen2fbm(x, verbose = 0)
f_few <- gl.gen2fbm(x[, 1:40], verbose = 0) # fewer loci than individuals
local_mocked_bindings(.fbm_as_matrix = function(x) {
stop("the whole FBM was decoded")
})
pdf(NULL); on.exit(dev.off(), add = TRUE)
expect_no_error(capture.output(
gl.pcoa(f, nfactors = 3, plot.out = TRUE, verbose = 3)))
expect_no_error(capture.output(
gl.pcoa(f_few, nfactors = 3, plot.out = FALSE, verbose = 3)))
})
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.