From cb1a8f11d819e22f00e483a6ea527268773fa7ad Mon Sep 17 00:00:00 2001 From: Daena Rys Date: Tue, 3 Nov 2026 15:59:01 +0200 Subject: [PATCH] Restrict getVariogram to pairs from selected samples with subset.by --- R/getVariogram.R | 36 ++++++++++++++++++++++++++++++ man/getVariogram.Rd | 10 +++++++++ tests/testthat/test-getVariogram.R | 23 +++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/R/getVariogram.R b/R/getVariogram.R index 8532d5c..fc4fb04 100644 --- a/R/getVariogram.R +++ b/R/getVariogram.R @@ -79,6 +79,11 @@ #' permutation test is not available, as levels are not assigned to #' subjects. #' +#' With \code{subset.by}, only pairs whose earlier sample is \code{TRUE} in +#' a logical column of \code{colData} are used, for example pairs starting +#' from a given community type. Subjects are then compared on those pairs, +#' and a sample-level state is compared within them. +#' #' \strong{Developing communities.} When change at a given lag depends on #' the time since a common origin (age in infant cohorts, time since an #' intervention), groups whose samples were taken at different ages differ @@ -174,6 +179,10 @@ #' sample-level state. \eqn{D(L)} is estimated for each of its levels, and #' each level is compared with the first. (Default: \code{NULL}) #' +#' @param subset.by \code{NULL} or \code{Character scalar}. Specifies a name +#' of a logical column of \code{colData}; only pairs whose earlier sample is +#' \code{TRUE} are used. See details. (Default: \code{NULL}) +#' #' @param time.point \code{NULL}, \code{numeric vector} or \code{"median"}. The #' time at which \eqn{D(L)} is estimated, for communities whose change #' depends on time since a common origin; one value, or one per lag. See @@ -277,6 +286,7 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), method = "bray", time.lag = NULL, split.by = NULL, + subset.by = NULL, time.point = NULL, window = 2, squared = FALSE, @@ -304,6 +314,12 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), temp <- .check_input( split.by, list(NULL, "character scalar"), colnames(colData(x))) .check_split_by_name(split.by, .VARIOGRAM_COLUMNS) + temp <- .check_input( + subset.by, list(NULL, "character scalar"), colnames(colData(x))) + if( !is.null(subset.by) && !is.logical(x[[subset.by]]) ){ + stop("'subset.by' must specify a logical column of colData(x).", + call. = FALSE) + } if( !(is.null(time.point) || identical(time.point, "median") || (is.numeric(time.point) && !anyNA(time.point))) ){ stop("'time.point' must be NULL, numeric or \"median\".", @@ -336,6 +352,9 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), pairs <- .get_within_subject_pairs( x, time.col, group, assay.type, method, split.by, squared, bounded, args) + if( !is.null(subset.by) ){ + pairs <- .subset_pairs(pairs, x, time.col, subset.by) + } if( is.null(time.lag) ){ time.lag <- .default_lags(pairs) } @@ -441,6 +460,23 @@ setMethod("getVariogram", signature = c(x = "SummarizedExperiment"), return(pairs) } +# The pairs whose earlier sample is TRUE in the logical column subset.by +.subset_pairs <- function(pairs, x, time.col, subset.by){ + samples <- attr(pairs, "samples") + pair_level <- attr(pairs, "pair.level") + keep <- x[[subset.by]][samples] + time <- x[[time.col]][samples] + first <- ifelse(time[pairs$k] <= time[pairs$l], pairs$k, pairs$l) + pairs <- pairs[keep[first] %in% TRUE, , drop = FALSE] + if( nrow(pairs) == 0L ){ + stop("No pair starts from a sample with 'subset.by' TRUE.", + call. = FALSE) + } + attr(pairs, "samples") <- samples + attr(pairs, "pair.level") <- pair_level + return(pairs) +} + # Squared, or on the scale -log(1 - d) for bounded dissimilarities .dissimilarity_scale <- function(d, squared, bounded){ if( squared ){ diff --git a/man/getVariogram.Rd b/man/getVariogram.Rd index 07a8ebd..c5d4e7d 100644 --- a/man/getVariogram.Rd +++ b/man/getVariogram.Rd @@ -21,6 +21,7 @@ addVariogram(x, ...) method = "bray", time.lag = NULL, split.by = NULL, + subset.by = NULL, time.point = NULL, window = 2, squared = FALSE, @@ -66,6 +67,10 @@ of a column from \code{colData}, with one value per subject or a sample-level state. \eqn{D(L)} is estimated for each of its levels, and each level is compared with the first. (Default: \code{NULL})} +\item{subset.by}{\code{NULL} or \code{Character scalar}. Specifies a name +of a logical column of \code{colData}; only pairs whose earlier sample is +\code{TRUE} are used. See details. (Default: \code{NULL})} + \item{time.point}{\code{NULL}, \code{numeric vector} or \code{"median"}. The time at which \eqn{D(L)} is estimated, for communities whose change depends on time since a common origin; one value, or one per lag. See @@ -192,6 +197,11 @@ of freedom. Both levels must meet the support rules on their own. The permutation test is not available, as levels are not assigned to subjects. +With \code{subset.by}, only pairs whose earlier sample is \code{TRUE} in +a logical column of \code{colData} are used, for example pairs starting +from a given community type. Subjects are then compared on those pairs, +and a sample-level state is compared within them. + \strong{Developing communities.} When change at a given lag depends on the time since a common origin (age in infant cohorts, time since an intervention), groups whose samples were taken at different ages differ diff --git a/tests/testthat/test-getVariogram.R b/tests/testthat/test-getVariogram.R index 0d2229d..d4e0679 100644 --- a/tests/testthat/test-getVariogram.R +++ b/tests/testthat/test-getVariogram.R @@ -256,3 +256,26 @@ test_that("Pairs are classified by a sample-level state", { args$split.by <- "type" expect_error(do.call(getVariogram, args), "output column") }) + +test_that("Pairs are restricted to those from samples with subset.by TRUE", { + data(hitchip1006, package = "miaTime") + tse <- transformAssay(hitchip1006, method = "hellinger") + tse$early <- tse$time < 3 + args <- list(tse, time.col = "time", group = "subject", + assay.type = "hellinger", method = "euclidean", squared = TRUE, + time.lag = c(1, 3)) + res <- do.call(getVariogram, c(args, subset.by = "early")) + # The same as the local estimate on the pairs from early samples + pairs <- miaTime:::.get_within_subject_pairs(tse, "time", "subject", + "hellinger", "euclidean", NULL, TRUE, FALSE, list()) + first <- pmin(pairs$mid - pairs$h / 2) + ref <- miaTime:::.local_variogram(pairs[first < 3, ], c(1, 3), 2, 0.95, + c(NA, NA)) + expect_equal(res$estimate, ref$estimate) + expect_equal(res$n.pairs, ref$n.pairs) + expect_error(do.call(getVariogram, c(args, subset.by = "time")), + "logical") + tse$none <- FALSE + args[[1]] <- tse + expect_error(do.call(getVariogram, c(args, subset.by = "none"))) +})