From 94f474190e1744ce92838523b6df47ec8dbc23b7 Mon Sep 17 00:00:00 2001 From: Lambda Moses Date: Thu, 16 May 2024 12:51:27 -0700 Subject: [PATCH] Use tempfile instead of re-creating the same directory in tempdir --- .github/workflows/R-CMD-check.yaml | 14 ++- R/AllGenerics.R | 4 + R/dimGeometries.R | 4 +- R/formatTxSpots.R | 17 +-- R/geometry_operation.R | 4 + R/image.R | 4 +- R/read.R | 12 +- R/utils.R | 4 +- man/Img-set-SpatialExperiment-method.Rd | 4 +- man/formatTxSpots.Rd | 6 +- man/formatTxTech.Rd | 4 +- man/getPixelSize.Rd | 4 +- man/readCosMX.Rd | 4 +- man/readSelectTx.Rd | 7 +- man/readVizgen.Rd | 4 +- man/readXenium.Rd | 4 +- man/rowGeometries.Rd | 4 +- tests/testthat/test-formatTxSpots.R | 38 +++---- tests/testthat/test-geometry_operation.R | 17 +-- tests/testthat/test-image.R | 26 ++--- tests/testthat/test-read.R | 134 +++++++++++------------ tests/testthat/test-utils.R | 4 +- 22 files changed, 161 insertions(+), 162 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index a2fc78d..94c6370 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -12,7 +12,19 @@ permissions: read-all jobs: R-CMD-check: - runs-on: ubuntu-latest + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + #- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + #- {os: ubuntu-latest, r: 'oldrel-1'} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 3d3124d..fc08f76 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -269,3 +269,7 @@ if (!isGeneric("unwrap")) {setGeneric("unwrap", function(x, ...) standardGeneric #' @export setGeneric("Img<-", function(x, sample_id = 1L, image_id, scale_fct = 1, value) standardGeneric("Img<-")) + +if (!isGeneric("aggregate")) {setGeneric("aggregate", function(x, ...) standardGeneric("aggregate"))} + +if (!isGeneric("split")) {setGeneric("split", function(x, f, drop = FALSE, ...) standardGeneric("split"))} diff --git a/R/dimGeometries.R b/R/dimGeometries.R index 69112d6..318d893 100644 --- a/R/dimGeometries.R +++ b/R/dimGeometries.R @@ -398,8 +398,8 @@ nucSeg <- function(x, sample_id = 1L, withDimnames = TRUE) { #' @examples #' library(SFEData) #' library(RBioFormats) -#' fp <- tempdir() -#' dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' dir_use <- XeniumOutput("v2", file_path = fp) #' # RBioFormats issue #' try(sfe <- readXenium(dir_use, add_molecules = TRUE)) #' sfe <- readXenium(dir_use, add_molecules = TRUE) diff --git a/R/formatTxSpots.R b/R/formatTxSpots.R index 6d29447..c84f8bd 100644 --- a/R/formatTxSpots.R +++ b/R/formatTxSpots.R @@ -122,13 +122,14 @@ #' @examples #' library(SFEData) #' if (gdalParquetAvailable()) { -#' fp <- tempdir() -#' dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' dir_use <- XeniumOutput("v2", file_path = fp) #' fn_tx <- formatTxTech(dir_use, tech = "Xenium", flip = TRUE, return = FALSE, #' file_out = file.path(dir_use, "tx_spots.parquet")) #' gene_select <- c("ACE2", "BMX") #' df <- readSelectTx(fn_tx, gene_select) -#' +#' # RBioFormats null pointer error the first time +#' try(sfe <- readXenium(dir_use)) #' sfe <- readXenium(dir_use) #' sfe <- addSelectTx(sfe, fn_tx, head(rownames(sfe), 5), swap_rownames = "Symbol") #' unlink(dir_use, recursive = TRUE) @@ -292,14 +293,14 @@ addSelectTx <- function(sfe, file, gene_select, sample_id = 1L, #' @rdname formatTxSpots #' @examples #' # Default arguments are for MERFISH -#' fp <- tempdir() -#' dir_use <- SFEData::VizgenOutput(file_path = file.path(fp, "vizgen_test")) +#' fp <- tempfile() +#' dir_use <- SFEData::VizgenOutput(file_path = fp) #' g <- formatTxSpots(file.path(dir_use, "detected_transcripts.csv")) #' unlink(dir_use, recursive = TRUE) #' #' # For CosMX, note the colnames, also dest = "colGeometry" #' # Results are written to the tx_spots directory -#' dir_use <- SFEData::CosMXOutput(file_path = file.path(fp, "cosmx_test")) +#' dir_use <- SFEData::CosMXOutput(file_path = fp) #' cg <- formatTxSpots(file.path(dir_use, "Run5642_S3_Quarter_tx_file.csv"), #' dest = "colGeometry", z = "all", #' cell_col = c("cell_ID", "fov"), @@ -523,8 +524,8 @@ addTxSpots <- function(sfe, file, sample_id = 1L, #' @export #' @examples #' library(SFEData) -#' fp <- tempdir() -#' dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' dir_use <- XeniumOutput("v2", file_path = fp) #' fn_tx <- formatTxTech(dir_use, tech = "Xenium", flip = TRUE, return = FALSE, #' file_out = file.path(dir_use, "tx_spots.parquet")) #' diff --git a/R/geometry_operation.R b/R/geometry_operation.R index eb96d07..e7c6890 100644 --- a/R/geometry_operation.R +++ b/R/geometry_operation.R @@ -732,3 +732,7 @@ removeEmptySpace <- function(sfe, sample_id = "all") { } sfe } + +#' Aggregate data in SFE using geometry +#' +#' Gene expression and diff --git a/R/image.R b/R/image.R index 00771b9..ae2bd12 100644 --- a/R/image.R +++ b/R/image.R @@ -708,8 +708,8 @@ setMethod("dim", "BioFormatsImage", function(x) { #' library(EBImage) #' library(SFEData) #' library(RBioFormats) -#' fp <- tempdir() -#' fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' fn <- XeniumOutput("v2", file_path = fp) #' # Weirdly the first time I get the null pointer error #' try(sfe <- readXenium(fn)) #' sfe <- readXenium(fn) diff --git a/R/read.R b/R/read.R index 21df6f1..05c0bdb 100644 --- a/R/read.R +++ b/R/read.R @@ -388,8 +388,8 @@ read10xVisiumSFE <- function(samples = "", #' @importFrom data.table fread merge.data.table rbindlist is.data.table #' @importFrom stats na.omit #' @examples -#' fp <- tempdir() -#' dir_use <- SFEData::VizgenOutput(file_path = file.path(fp, "vizgen_test")) +#' fp <- tempfile() +#' dir_use <- SFEData::VizgenOutput(file_path = fp) #' sfe <- readVizgen(dir_use, z = 3L, image = "PolyT", #' flip = "geometry") #' @@ -677,8 +677,8 @@ readVizgen <- function(data_dir, #' @export #' @concept Read data into SFE #' @examples -#' fp <- tempdir() -#' dir_use <- SFEData::CosMXOutput(file_path = file.path(fp, "cosmx_test")) +#' fp <- tempfile() +#' dir_use <- SFEData::CosMXOutput(file_path = fp) #' sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE) #' # Clean up #' unlink(dir_use, recursive = TRUE) @@ -835,8 +835,8 @@ readCosMX <- function(data_dir, #' @examples #' library(SFEData) #' library(RBioFormats) -#' fp <- tempdir() -#' dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' dir_use <- XeniumOutput("v2", file_path = fp) #' # RBioFormats issue #' try(sfe <- readXenium(dir_use, add_molecules = TRUE)) #' sfe <- readXenium(dir_use, add_molecules = TRUE) diff --git a/R/utils.R b/R/utils.R index 5f887e2..cdcb500 100644 --- a/R/utils.R +++ b/R/utils.R @@ -173,8 +173,8 @@ bbox_center <- function(bbox) { #' @concept Utilities #' @examples #' library(SFEData) -#' fp <- tempdir() -#' dir_use <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) +#' fp <- tempfile() +#' dir_use <- XeniumOutput("v1", file_path = fp) #' # RBioFormats null pointer error #' try(getPixelSize(file.path(dir_use, "morphology_focus.ome.tif"))) #' getPixelSize(file.path(dir_use, "morphology_focus.ome.tif")) diff --git a/man/Img-set-SpatialExperiment-method.Rd b/man/Img-set-SpatialExperiment-method.Rd index 361a963..5b66fed 100644 --- a/man/Img-set-SpatialExperiment-method.Rd +++ b/man/Img-set-SpatialExperiment-method.Rd @@ -40,8 +40,8 @@ another object that inherits from \code{VirtualSpatialImage}, including library(EBImage) library(SFEData) library(RBioFormats) -fp <- tempdir() -fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +fn <- XeniumOutput("v2", file_path = fp) # Weirdly the first time I get the null pointer error try(sfe <- readXenium(fn)) sfe <- readXenium(fn) diff --git a/man/formatTxSpots.Rd b/man/formatTxSpots.Rd index cd9c451..fec732f 100644 --- a/man/formatTxSpots.Rd +++ b/man/formatTxSpots.Rd @@ -150,14 +150,14 @@ When `dest = "colGeometry"`, the geometries are always written to disk } \examples{ # Default arguments are for MERFISH -fp <- tempdir() -dir_use <- SFEData::VizgenOutput(file_path = file.path(fp, "vizgen_test")) +fp <- tempfile() +dir_use <- SFEData::VizgenOutput(file_path = fp) g <- formatTxSpots(file.path(dir_use, "detected_transcripts.csv")) unlink(dir_use, recursive = TRUE) # For CosMX, note the colnames, also dest = "colGeometry" # Results are written to the tx_spots directory -dir_use <- SFEData::CosMXOutput(file_path = file.path(fp, "cosmx_test")) +dir_use <- SFEData::CosMXOutput(file_path = fp) cg <- formatTxSpots(file.path(dir_use, "Run5642_S3_Quarter_tx_file.csv"), dest = "colGeometry", z = "all", cell_col = c("cell_ID", "fov"), diff --git a/man/formatTxTech.Rd b/man/formatTxTech.Rd index e395124..f34cdc0 100644 --- a/man/formatTxTech.Rd +++ b/man/formatTxTech.Rd @@ -100,8 +100,8 @@ technology. } \examples{ library(SFEData) -fp <- tempdir() -dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +dir_use <- XeniumOutput("v2", file_path = fp) fn_tx <- formatTxTech(dir_use, tech = "Xenium", flip = TRUE, return = FALSE, file_out = file.path(dir_use, "tx_spots.parquet")) diff --git a/man/getPixelSize.Rd b/man/getPixelSize.Rd index a60058a..5ac3398 100644 --- a/man/getPixelSize.Rd +++ b/man/getPixelSize.Rd @@ -22,8 +22,8 @@ pyramid in \code{\link{BioFormatsImage}}. } \examples{ library(SFEData) -fp <- tempdir() -dir_use <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +dir_use <- XeniumOutput("v1", file_path = fp) # RBioFormats null pointer error try(getPixelSize(file.path(dir_use, "morphology_focus.ome.tif"))) getPixelSize(file.path(dir_use, "morphology_focus.ome.tif")) diff --git a/man/readCosMX.Rd b/man/readCosMX.Rd index 4a562d9..d079aa0 100644 --- a/man/readCosMX.Rd +++ b/man/readCosMX.Rd @@ -71,8 +71,8 @@ This function reads the standard CosMX output into an SFE object, as in "Basic Data Files" on the Nanostring website. } \examples{ -fp <- tempdir() -dir_use <- SFEData::CosMXOutput(file_path = file.path(fp, "cosmx_test")) +fp <- tempfile() +dir_use <- SFEData::CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE) # Clean up unlink(dir_use, recursive = TRUE) diff --git a/man/readSelectTx.Rd b/man/readSelectTx.Rd index 06f1329..2303131 100644 --- a/man/readSelectTx.Rd +++ b/man/readSelectTx.Rd @@ -76,13 +76,14 @@ page for the Parquet driver}. \examples{ library(SFEData) if (gdalParquetAvailable()) { - fp <- tempdir() - dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + dir_use <- XeniumOutput("v2", file_path = fp) fn_tx <- formatTxTech(dir_use, tech = "Xenium", flip = TRUE, return = FALSE, file_out = file.path(dir_use, "tx_spots.parquet")) gene_select <- c("ACE2", "BMX") df <- readSelectTx(fn_tx, gene_select) - + # RBioFormats null pointer error the first time + try(sfe <- readXenium(dir_use)) sfe <- readXenium(dir_use) sfe <- addSelectTx(sfe, fn_tx, head(rownames(sfe), 5), swap_rownames = "Symbol") unlink(dir_use, recursive = TRUE) diff --git a/man/readVizgen.Rd b/man/readVizgen.Rd index b450631..f67f305 100644 --- a/man/readVizgen.Rd +++ b/man/readVizgen.Rd @@ -118,8 +118,8 @@ Since the transcript spots file is often very large, we recommend only memory. } \examples{ -fp <- tempdir() -dir_use <- SFEData::VizgenOutput(file_path = file.path(fp, "vizgen_test")) +fp <- tempfile() +dir_use <- SFEData::VizgenOutput(file_path = fp) sfe <- readVizgen(dir_use, z = 3L, image = "PolyT", flip = "geometry") diff --git a/man/readXenium.Rd b/man/readXenium.Rd index 9e492ca..799cc5d 100644 --- a/man/readXenium.Rd +++ b/man/readXenium.Rd @@ -104,8 +104,8 @@ time. \examples{ library(SFEData) library(RBioFormats) -fp <- tempdir() -dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +dir_use <- XeniumOutput("v2", file_path = fp) # RBioFormats issue try(sfe <- readXenium(dir_use, add_molecules = TRUE)) sfe <- readXenium(dir_use, add_molecules = TRUE) diff --git a/man/rowGeometries.Rd b/man/rowGeometries.Rd index 6fca6f0..aacc5a8 100644 --- a/man/rowGeometries.Rd +++ b/man/rowGeometries.Rd @@ -96,8 +96,8 @@ because they can have overlapping coordinate values. \examples{ library(SFEData) library(RBioFormats) -fp <- tempdir() -dir_use <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +dir_use <- XeniumOutput("v2", file_path = fp) # RBioFormats issue try(sfe <- readXenium(dir_use, add_molecules = TRUE)) sfe <- readXenium(dir_use, add_molecules = TRUE) diff --git a/tests/testthat/test-formatTxSpots.R b/tests/testthat/test-formatTxSpots.R index 08f1c61..b3b02dd 100644 --- a/tests/testthat/test-formatTxSpots.R +++ b/tests/testthat/test-formatTxSpots.R @@ -1,8 +1,8 @@ library(SFEData) library(sf) test_that("Read MERFISH transcript spots into rowGeometries", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) sfe <- readVizgen(dir_use, z = 3L, image = "PolyT", add_molecules = TRUE) expect_equal(rowGeometryNames(sfe), "txSpots") rg <- txSpots(sfe) @@ -15,8 +15,8 @@ test_that("Read MERFISH transcript spots into rowGeometries", { }) test_that("Format MERFISH transcript spots for colGeometries", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) expect_error(formatTxSpots(file.path(dir_use, "detected_transcripts.csv"), dest = "colGeometry"), "file_out must be specified") @@ -63,8 +63,8 @@ test_that("Format MERFISH transcript spots for colGeometries", { }) test_that("Error messages in formatTxSpots", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) file <- file.path(dir_use, "detected_transcripts.csv") expect_error(formatTxSpots(file, dest = "colGeometry", file_out = NULL), "file_out must be specified") @@ -84,8 +84,8 @@ test_that("Error messages in formatTxSpots", { }) test_that("Format CosMX spots for colGeometry, multiple z-planes", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) cg <- formatTxSpots(file.path(dir_use, "Run5642_S3_Quarter_tx_file.csv"), dest = "colGeometry", z = "all", z_option = "split", @@ -111,8 +111,8 @@ test_that("Format CosMX spots for colGeometry, multiple z-planes", { test_that("Read transcript spots from a subset of genes", { skip_if_not(gdalParquetAvailable()) - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) fn_tx <- formatTxTech(fn, tech = "Xenium", flip = TRUE, return = FALSE, file_out = file.path(fn, "tx_spots.parquet")) gene_select <- c("ACE2", "BMX") @@ -126,8 +126,8 @@ test_that("Read transcript spots from a subset of genes", { test_that("Error message when Parquet driver is unavailable", { skip_if(gdalParquetAvailable()) - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) fn_tx <- formatTxTech(fn, tech = "Xenium", flip = TRUE, return = FALSE, file_out = file.path(fn, "tx_spots.parquet")) gene_select <- c("ACE2", "BMX") @@ -139,8 +139,8 @@ test_that("Error message when Parquet driver is unavailable", { test_that("Add a subset of spots", { skip_if_not(gdalParquetAvailable()) library(RBioFormats) - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) try(sfe <- readXenium(fn)) sfe <- readXenium(fn) fn_tx <- formatTxTech(fn, tech = "Xenium", flip = TRUE, return = FALSE, @@ -155,8 +155,8 @@ test_that("Add a subset of spots", { test_that("Add subset of spots, multiple files in tx spots output", { skip_if_not(gdalParquetAvailable()) - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use) fn_tx <- formatTxTech(dir_use, tech = "CosMX", z = "all", z_option = "split", @@ -175,9 +175,3 @@ test_that("Add subset of spots, multiple files in tx spots output", { expect_true(all(is_empty1[-(1:5)])) unlink(dir_use, recursive = TRUE) }) - -# Final cleanup in case failed test messed with cleanup -fp <- tempdir() -unlink(file.path(fp, "cosmx_test"), recursive = TRUE) -unlink(file.path(fp, "vizgen_test"), recursive = TRUE) -unlink(file.path(fp, "xenium_test"), recursive = TRUE) diff --git a/tests/testthat/test-geometry_operation.R b/tests/testthat/test-geometry_operation.R index 5622f83..183d17c 100644 --- a/tests/testthat/test-geometry_operation.R +++ b/tests/testthat/test-geometry_operation.R @@ -253,8 +253,8 @@ test_that("Error when other spatial operations are specified", { }) test_that("Crop 3D geometry", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE, z_option = "3d") bbox1 <- c(xmin = 171500, ymin = 11500, xmax = 172000, ymax = 12000) @@ -823,8 +823,8 @@ test_that("Affine transformation of SFE object with image, after cropping", { }) test_that("Transformation when there's 3D geometry", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE, z_option = "3d") @@ -864,8 +864,8 @@ test_that("Translate SFE object with image", { # Affine transform of entire SFE object, for BioFormatsImage========= library(RBioFormats) library(EBImage) -fp <- tempdir() -xenium_path <- XeniumOutput(file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +xenium_path <- XeniumOutput(file_path = fp) try(sfe <- readXenium(xenium_path)) sfe <- readXenium(xenium_path, add_molecules = TRUE) set.seed(29) @@ -1143,8 +1143,3 @@ test_that("General affine transformation of SFE object with ExtImage", { int2 <- st_intersects(annotGeometry(sfe2), cellSeg(sfe2)) expect_equal(int1, int2) }) -# Final cleanup in case failed test messed with cleanup -fp <- tempdir() -unlink(file.path(fp, "cosmx_test"), recursive = TRUE) -unlink(file.path(fp, "vizgen_test"), recursive = TRUE) -unlink(file.path(fp, "xenium_test"), recursive = TRUE) diff --git a/tests/testthat/test-image.R b/tests/testthat/test-image.R index 5889043..6cfee21 100644 --- a/tests/testthat/test-image.R +++ b/tests/testthat/test-image.R @@ -12,8 +12,8 @@ sfe <- McKellarMuscleData("small") img_path <- system.file(file.path("extdata", "sample01", "outs", "spatial", "tissue_lowres_image.png"), package = "SpatialFeatureExperiment") -fp <- tempdir() -xenium_dir <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +xenium_dir <- XeniumOutput("v1", file_path = fp) xenium_fn <- file.path(xenium_dir, "morphology_mip.ome.tif") test_that("addImg, SpatRasterImage", { @@ -294,8 +294,8 @@ test_that("Convert SpatRasterImage to ExtImage, RGB", { imgRaster(ebi) |> as.array() |> aperm(c(2,1,3))) }) -fp <- tempdir() -vizgen_dir <- VizgenOutput(file_path = file.path(fp, "vizgen_test")) +fp <- tempfile() +vizgen_dir <- VizgenOutput(file_path = fp) fn <- file.path(vizgen_dir, "images", "mosaic_Cellbound1_z3.tif") test_that("Convert SpatRasterImage to ExtImage, grayscale", { @@ -437,8 +437,8 @@ test_that("Ignore resolution in toExtImage when there's only 1 resolution", { test_that("Convert BioFormatsImage to SpatRasterImage", { library(RBioFormats) - fp <- tempdir() - fn <- XeniumOutput(file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput(file_path = fp) fn1 <- file.path(fn, "morphology_mip.ome.tif") bfi <- BioFormatsImage(fn1, ext_use, isFull = FALSE) expect_message(spi <- toSpatRasterImage(bfi, resolution = 1L), "non OME") @@ -690,8 +690,8 @@ test_that("Crop ExtImage", { # Image setter------- test_that("Image setter, the image isn't already there", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) # Weirdly the first time I get the null pointer error sfe <- readXenium(fn) img <- getImg(sfe) |> toExtImage(resolution = 1L) @@ -705,8 +705,8 @@ test_that("Image setter, the image isn't already there", { }) test_that("Image setter, modify existing image", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) # Weirdly the first time I get the null pointer error sfe <- readXenium(fn) df_old <- imgData(sfe) @@ -720,9 +720,3 @@ test_that("Image setter, modify existing image", { expect_equal(nrow(df_new), nrow(df_old)) unlink(fn, recursive = TRUE) }) - -# Final cleanup in case failed test messed with cleanup -fp <- tempdir() -unlink(file.path(fp, "cosmx_test"), recursive = TRUE) -unlink(file.path(fp, "vizgen_test"), recursive = TRUE) -unlink(file.path(fp, "xenium_test"), recursive = TRUE) diff --git a/tests/testthat/test-read.R b/tests/testthat/test-read.R index 6622a9e..fadc7bd 100644 --- a/tests/testthat/test-read.R +++ b/tests/testthat/test-read.R @@ -136,8 +136,8 @@ test_that("Micron spot spacing works when there're singletons", { }) # Read Vizgen MERFISH============== test_that("readVizgen flip geometry, use cellpose", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) expect_message(sfe <- readVizgen(dir_use, z = 3L, use_cellpose = TRUE, flip = "geometry", min_area = 15), "with area less than 15") @@ -169,8 +169,8 @@ test_that("readVizgen flip geometry, use cellpose", { }) test_that("readVizgen write parquet file after reading hdf5", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) file.remove(file.path(dir_use, "cell_boundaries.parquet")) sfe <- readVizgen(dir_use, z = 3L, use_cellpose = FALSE, image = "PolyT") expect_true(file.exists(file.path(dir_use,"hdf5s_micron_space.parquet"))) @@ -181,8 +181,8 @@ test_that("readVizgen write parquet file after reading hdf5", { }) test_that("readVizgen flip geometry, don't use cellpose", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) sfe <- readVizgen(dir_use, z = 3L, use_cellpose = FALSE, image = "PolyT", flip = "geometry") expect_equal(unit(sfe), "micron") @@ -205,8 +205,8 @@ test_that("readVizgen flip geometry, don't use cellpose", { }) test_that("readVizgen flip image", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) sfe <- readVizgen(dir_use, z = 3L, use_cellpose = FALSE, image = "PolyT", flip = "image") expect_equal(unit(sfe), "micron") @@ -218,8 +218,8 @@ test_that("readVizgen flip image", { }) test_that("readVizgen don't flip image when image is too large", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) expect_error(readVizgen(dir_use, z = 3L, use_cellpose = FALSE, image = "PolyT", flip = "image", max_flip = "0.02 TB"), "max_flip must be in either MB or GB") @@ -236,8 +236,8 @@ test_that("readVizgen don't flip image when image is too large", { }) test_that("Don't flip image if it's GeoTIFF", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) sfe <- readVizgen(dir_use, z = 3L, use_cellpose = FALSE, image = "PolyT", flip = "image") terra::writeRaster(imgRaster(getImg(sfe)), @@ -250,8 +250,8 @@ test_that("Don't flip image if it's GeoTIFF", { }) test_that("Errors and warnings in readVizgen", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) expect_warning(sfe <- readVizgen(dir_use, z = 1L, image = "PolyT"), "don't exist") expect_equal(nrow(imgData(sfe)), 0L) @@ -261,8 +261,8 @@ test_that("Errors and warnings in readVizgen", { }) # Make toy examples of multiple pieces -fp <- tempdir() -dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) +fp <- tempfile() +dir_use <- VizgenOutput("hdf5", file_path = fp) parq <- st_read_parquet(file.path(dir_use, "cell_boundaries.parquet")) unlink(dir_use, recursive = TRUE) # One large piece and one small piece @@ -283,11 +283,11 @@ small_small <- st_multipolygon(list(small, small2)) large_large <- st_multipolygon(list(large, large2)) test_that("Deal with multiple pieces, remove pieces that are too small", { - fp <- tempdir() + fp <- tempfile() parq2 <- parq[1:4,] new_geo <- st_sfc(large_g, large_small, small_small, large_large) parq2$Geometry <- new_geo - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "multi")) + dir_use <- VizgenOutput("hdf5", file_path = fp) file.remove(file.path(dir_use, "cell_boundaries.parquet")) suppressWarnings(st_write_parquet(parq2, file.path(dir_use, "cell_boundaries.parquet"))) @@ -306,12 +306,12 @@ test_that("Deal with multiple pieces, remove pieces that are too small", { test_that("No polygons left", { # Like they're all too small, or when the polygon file is empty, unlikely # but otherwise we get a mysterious error - fp <- tempdir() + fp <- tempfile() parq2 <- parq[1:2,] small <- st_polygon(small) new_geo <- st_sfc(small, small) parq2$Geometry <- new_geo - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "small")) + dir_use <- VizgenOutput("hdf5", file_path = fp) file.remove(file.path(dir_use, "cell_boundaries.parquet")) suppressWarnings(st_write_parquet(parq2, file.path(dir_use, "cell_boundaries.parquet"))) @@ -322,8 +322,8 @@ test_that("No polygons left", { }) test_that("Image can be named polyT in older version", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "image")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) file.rename(file.path(dir_use, "images", "mosaic_PolyT_z3.tif"), file.path(dir_use, "images", "mosaic_polyT_z3.tif")) @@ -332,8 +332,8 @@ test_that("Image can be named polyT in older version", { }) test_that("Version with Cellpose directory", { - fp <- tempdir() - dir_use <- VizgenOutput("cellpose", file_path = file.path(fp, "cellpose")) + fp <- tempfile() + dir_use <- VizgenOutput("cellpose", file_path = fp) # Cropped geometry has 2 pieces in one cell suppressWarnings(sfe <- readVizgen(dir_use, z = 3L, use_cellpose = TRUE, flip = "geometry", min_area = 15)) @@ -365,8 +365,8 @@ test_that("Version with Cellpose directory", { }) test_that("Message when removing empty polygons", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "empty")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) parq <- st_read_parquet(file.path(dir_use, "cell_boundaries.parquet")) parq$Geometry[[1]] <- st_polygon() @@ -379,17 +379,17 @@ test_that("Message when removing empty polygons", { }) test_that("Read all z-planes for Vizgen", { - fp <- tempdir() + fp <- tempfile() # Only affecting images - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "all")) + dir_use <- VizgenOutput("hdf5", file_path = fp) sfe <- readVizgen(dir_use, z = "all", image = "DAPI") expect_equal(imgData(sfe)$image_id, paste0("DAPI_z", 1:3)) unlink(dir_use, recursive = TRUE) }) test_that("Error message when multiple parquet files are found in readVizgen", { - fp <- tempdir() - dir_use <- VizgenOutput("hdf5", file_path = file.path(fp, "vizgen_test")) + fp <- tempfile() + dir_use <- VizgenOutput("hdf5", file_path = fp) file.copy(file.path(dir_use, "cell_boundaries.parquet"), file.path(dir_use, "cellpose_micron_space.parquet")) m <- capture_messages(sfe <- readVizgen(dir_use, z = "all", image = "DAPI")) @@ -404,8 +404,8 @@ test_that("Error message when multiple parquet files are found in readVizgen", { }) test_that("Make cell bounding boxes when segmentation is absent", { - fp2 <- tempdir() - dir_use <- VizgenOutput("cellpose", file_path = file.path(fp, "vizgen_test")) + fp2 <- tempfile() + dir_use <- VizgenOutput("cellpose", file_path = fp) file.remove(file.path(dir_use, "Cellpose", "cellpose_micron_space.parquet")) expect_message( expect_warning(sfe <- readVizgen(dir_use, use_bboxes = TRUE), @@ -417,8 +417,8 @@ test_that("Make cell bounding boxes when segmentation is absent", { # Read CosMX=================== test_that("readCosMX, not reading transcript spots", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = 1L) expect_s4_class(sfe, "SpatialFeatureExperiment") expect_equal(colGeometryNames(sfe), c("centroids", "cellSeg")) @@ -436,8 +436,8 @@ test_that("readCosMX, not reading transcript spots", { }) test_that("readCosMX, reading spots, 1 z-plane", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = 1L, add_molecules = TRUE) fn <- file.path(dir_use, "tx_spots.parquet") expect_true(file.exists(fn)) @@ -459,8 +459,8 @@ test_that("readCosMX, reading spots, 1 z-plane", { }) test_that("readCosMX, 2 z-planes, split z, not splitting by cell compartments", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE, z_option = "split") @@ -490,8 +490,8 @@ test_that("readCosMX, 2 z-planes, split z, not splitting by cell compartments", }) test_that("readCosMX, don't split z, don't split cell compartments", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE, z_option = "3d") @@ -505,8 +505,8 @@ test_that("readCosMX, don't split z, don't split cell compartments", { }) test_that("readCosMX, split z, split cell compartments", { - fp <- tempdir() - dir_use <- CosMXOutput(file_path = file.path(fp, "cosmx_test")) + fp <- tempfile() + dir_use <- CosMXOutput(file_path = fp) sfe <- readCosMX(dir_use, z = "all", add_molecules = TRUE, split_cell_comps = TRUE, z_option = "split") @@ -542,8 +542,8 @@ test_that("readCosMX, split z, split cell compartments", { # Flip image test_that("readXenium, XOA v1", { library(RBioFormats) - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) # Weirdly the first time I get the null pointer error try(m <- read.omexml(file.path(fn, "morphology_focus.ome.tif"))) sfe <- readXenium(fn, add_molecules = TRUE) @@ -573,8 +573,8 @@ test_that("readXenium, XOA v1", { }) test_that("readXenium XOA v1, image not found", { - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) file.remove(file.path(fn, "morphology_focus.ome.tif")) expect_warning(sfe <- readXenium(fn, add_molecules = TRUE), "or have non-standard file name") @@ -583,8 +583,8 @@ test_that("readXenium XOA v1, image not found", { }) test_that("readXenium XOA v1, use parquet files, with annoying arrow raw bytes", { - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) file.remove(file.path(fn, "cell_boundaries.csv.gz")) file.remove(file.path(fn, "nucleus_boundaries.csv.gz")) file.rename(file.path(fn, "cell_boundaries_binary.parquet"), @@ -598,8 +598,8 @@ test_that("readXenium XOA v1, use parquet files, with annoying arrow raw bytes", test_that("readXenium XOA v1 when only cell but not nuclei segmentation is available", { # Since the `polys` object should be a list even if there's only one of cell or nuclei - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) file.remove(list.files(fn, "cell_boundaries\\.*", full.names = TRUE)) expect_warning(sfe <- readXenium(fn), 'No `cell_boundaries` file is available') expect_equal(colGeometryNames(sfe), c("centroids", "nucSeg")) @@ -610,8 +610,8 @@ test_that("readXenium XOA v1 when only cell but not nuclei segmentation is avail }) test_that("readXenium XOA v1 read the output _sf.parquet next time", { - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) sfe <- readXenium(fn) expect_true(file.exists(file.path(fn, "cell_boundaries_sf.parquet"))) expect_true(file.exists(file.path(fn, "nucleus_boundaries_sf.parquet"))) @@ -623,8 +623,8 @@ test_that("readXenium XOA v1 read the output _sf.parquet next time", { }) test_that("readXenium XOA v1 read cell metadata parquet when csv is absent", { - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) file.remove(file.path(fn, "cells.csv.gz")) expect_message(sfe <- readXenium(fn), ">>> Reading cell metadata -> `cells.parquet`") unlink(fn, recursive = TRUE) @@ -635,8 +635,8 @@ test_that("readXenium XOA v1 read cell metadata parquet when csv is absent", { # read the segmentations. You need GDAL to read GeoJSON. test_that("readXenium XOA v1 flip image", { - fp <- tempdir() - fn <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v1", file_path = fp) sfe <- readXenium(fn, add_molecules = TRUE, flip = "image") # That things are aligned bbox_rg <- st_bbox(txSpots(sfe)) |> st_as_sfc() @@ -653,8 +653,8 @@ test_that("readXenium XOA v1 flip image", { # Read Xenium XOA v2=================== test_that("readXenium XOA v2, normal stuff", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) # Weirdly the first time I get the null pointer error sfe <- readXenium(fn, add_molecules = TRUE) # Basic stuff @@ -683,8 +683,8 @@ test_that("readXenium XOA v2, normal stuff", { }) test_that("readXenium XOA v2, somes images not found", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) unlink(file.path(fn, "morphology_focus"), recursive = TRUE) expect_warning(sfe <- readXenium(fn), "morphology_focus images not found") expect_true(isEmpty(imgData(sfe))) @@ -692,8 +692,8 @@ test_that("readXenium XOA v2, somes images not found", { }) test_that("readXenium XOA v2, use csv files", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) file.remove(list.files(fn, pattern = "*.parquet$", full.names = TRUE)) expect_message(sfe <- readXenium(fn, add_molecules = TRUE), ">>> Cell segmentations are found in `.csv` file") @@ -723,8 +723,8 @@ test_that("readXenium XOA v2, use csv files", { }) test_that("readXenium, flip image", { - fp <- tempdir() - fn <- XeniumOutput("v2", file_path = file.path(fp, "xenium_test")) + fp <- tempfile() + fn <- XeniumOutput("v2", file_path = fp) sfe <- readXenium(fn, add_molecules = TRUE, flip = "image") sfe0 <- readXenium(fn) @@ -745,9 +745,3 @@ test_that("readXenium, flip image", { expect_equal(transformation(bfi), list(name = "mirror", direction = "vertical")) unlink(fn, recursive = TRUE) }) - -# Final cleanup in case failed test messed with cleanup -fp <- tempdir() -unlink(file.path(fp, "cosmx_test"), recursive = TRUE) -unlink(file.path(fp, "vizgen_test"), recursive = TRUE) -unlink(file.path(fp, "xenium_test"), recursive = TRUE) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index b1123e0..e316e71 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -52,8 +52,8 @@ test_that("bbox center", { bbox_use <- c(xmin = 10, xmax = 20, ymin = 5, ymax = 15) expect_equal(bbox_center(bbox_use), c(15, 10)) }) -fp <- tempdir() -dir_use <- XeniumOutput("v1", file_path = file.path(fp, "xenium_test")) +fp <- tempfile() +dir_use <- XeniumOutput("v1", file_path = fp) test_that("Get pixel size", { library(RBioFormats) fn <- file.path(dir_use, "morphology_focus.ome.tif")