From dd0f2a46226e466a386ebad008fba10c613c0816 Mon Sep 17 00:00:00 2001 From: HelenaLC Date: Thu, 20 Jun 2024 12:22:23 +0200 Subject: [PATCH] #151 --- DESCRIPTION | 2 +- R/imgData-utils.R | 11 ++++++++++- tests/testthat/test_imgData-methods.R | 10 ++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7bfefc1..0ac8039 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: SpatialExperiment -Version: 1.15.0 +Version: 1.15.1 Title: S4 Class for Spatially Resolved -omics Data Description: Defines an S4 class for storing data from spatial -omics experiments. The class extends SingleCellExperiment to diff --git a/R/imgData-utils.R b/R/imgData-utils.R index bb120bc..79e9e05 100644 --- a/R/imgData-utils.R +++ b/R/imgData-utils.R @@ -4,8 +4,17 @@ # - NULL return first available entry # - character string returns matching entry(ies) .get_img_idx <- function(x, sample_id=NULL, image_id=NULL) { - # TODO: validity checks img <- imgData(x) + for (i in c("sample_id", "image_id")) { + j <- get(i) + if (is.factor(j) || is.numeric(j)) + assign(i, as.character(j)) + if (!(is.null(j) || j %in% img[[i]] || + length(j) == 1 && is.logical(j))) + stop(sprintf(c( + "'%s' invalid; should be NULL, TRUE/FALSE,", + " or matching entries in imgData(.)$%s"), i)) + } if (is.character(sample_id) && is.character(image_id)) { sid <- img$sample_id == sample_id iid <- img$image_id == image_id diff --git a/tests/testthat/test_imgData-methods.R b/tests/testthat/test_imgData-methods.R index f86eede..318841d 100644 --- a/tests/testthat/test_imgData-methods.R +++ b/tests/testthat/test_imgData-methods.R @@ -28,6 +28,16 @@ test_that("getImg,sample_id=image_id=TRUE returns a list", { expect_identical(x, y) }) +test_that("getImg handles non-character sample/image_id (#151)", { + . <- (spe$sample_id <- factor(spe$sample_id))[1] + expect_silent(getImg(spe, sample_id=.)) + . <- (spe$sample_id <- as.integer(spe$sample_id))[1] + expect_silent(getImg(spe, sample_id=.)) + . <- imgData(spe)$image_id <- 1 + expect_silent(getImg(spe, image_id=.)) + expect_silent(getImg(spe, image_id=as.factor(.))) +}) + # addImg ----------------------------------------------------------------------- test_that("addImg for existing sample_id,image_id throws error", {