Skip to content

Commit

Permalink
Merge branch 'saveterrards' into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamoses committed Aug 17, 2023
2 parents 17eff19 + be42b5f commit 22a0c24
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Collate:
'localResults.R'
'read.R'
'reexports.R'
'saveRDS.R'
'spatialGraphs.R'
'subset.R'
'updateObject.R'
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ exportMethods(localResultNames)
exportMethods(localResults)
exportMethods(mirrorImg)
exportMethods(rowGraphs)
exportMethods(saveRDS)
exportMethods(show)
exportMethods(spatialGraph)
exportMethods(spatialGraphNames)
Expand Down Expand Up @@ -206,6 +207,7 @@ importFrom(methods,is)
importFrom(methods,new)
importFrom(methods,setAs)
importFrom(methods,setClass)
importFrom(methods,setClassUnion)
importFrom(methods,setGeneric)
importFrom(methods,setMethod)
importFrom(methods,setReplaceMethod)
Expand Down Expand Up @@ -261,7 +263,9 @@ importFrom(stats,setNames)
importFrom(terra,"ext<-")
importFrom(terra,ext)
importFrom(terra,rast)
importFrom(terra,unwrap)
importFrom(terra,vect)
importFrom(terra,wrap)
importFrom(utils,getFromNamespace)
importFrom(utils,packageVersion)
importFrom(utils,read.csv)
1 change: 1 addition & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,4 @@ setGeneric("unit", function(x) standardGeneric("unit"))
#' @export
setGeneric("transposeImg", function(x, ...) standardGeneric("transposeImg"))

if (!isGeneric("saveRDS")) {setGeneric("saveRDS", function (object, file="", ascii=FALSE, version=NULL, compress=TRUE, refhook=NULL) standardGeneric("saveRDS"))}
17 changes: 16 additions & 1 deletion R/image.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#' imgSource getImg
#' @importFrom terra ext ext<-
#' @importClassesFrom terra SpatRaster
#' @importFrom methods setClassUnion
#' @examples
#' library(SFEData)
#' sfe <- McKellarMuscleData("small")
Expand All @@ -48,10 +49,24 @@
#' @aliases transposeImg
NULL

# Not exported by terra
setClass("PackedSpatRaster",
representation (
definition = "character",
values = "matrix",
attributes = "list"
),
prototype (
attributes = list()
)
)

setClassUnion("GeneralizedSpatRaster", members = c("SpatRaster", "PackedSpatRaster"))

#' @rdname SFE-image
#' @export
setClass("SpatRasterImage", contains="VirtualSpatialImage",
slots=c(image="SpatRaster"))
slots=c(image="GeneralizedSpatRaster"))

#' @rdname SFE-image
#' @export
Expand Down
42 changes: 42 additions & 0 deletions R/saveRDS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Save SpatialFeatureExperiment as RDS file
#'
#' Saving SFE objects as RDS files is complicated by the \code{SpatRaster} class
#' of the images. If present, the images need to be wrapped with the \code{\link{wrap}}
#' function in \code{terra} before serializing the SFE object. Otherwise the
#' images will be invalid pointers when the RDS is reloaded. If the image does
#' not fit in memory and its file source is unknown, then it will be written to
#' a temporary file, which is reloaded when the RDS file is loaded. When an SFE
#' object with images is read from an RDS file, the images will not be unwrapped
#' until necessary.
#'
#' @inheritParams base::saveRDS
#' @param object A \code{SpatialFeatureExperiment} object.
#' @return Invisibly \code{NULL}.
#' @importFrom terra wrap unwrap
#' @export
#' @examples
#' outdir <- system.file("extdata", package = "SpatialFeatureExperiment")
#' samples <- file.path(outdir, paste0("sample0", 1:2))
#' sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered")
#' saveRDS(sfe, "foo.rds")
#'
setMethod("saveRDS", "SpatialFeatureExperiment",
function(object, file = "", ascii = FALSE, version = NULL,
compress = TRUE, refhook = NULL) {
if (is.null(imgData(object)))
base::saveRDS(object, file = file, ascii = ascii,
version = version, compress = compress,
refhook = refhook)
else {
for (i in seq_len(nrow(imgData(object)))) {
img <- int_metadata(object)$imgData$data[[i]]
if (is(imgRaster(img), "SpatRaster"))
img@image <- wrap(img@image)
int_metadata(object)$imgData$data[[i]] <- img
# Then I need to add unwrap to functions that use the image
}
base::saveRDS(object, file = file, ascii = ascii,
version = version, compress = compress,
refhook = refhook)
}
})
56 changes: 56 additions & 0 deletions man/saveRDS-SpatialFeatureExperiment-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-saveRDS.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
outdir <- system.file("extdata", package = "SpatialFeatureExperiment")
samples <- file.path(outdir, paste0("sample0", 1:2))
sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered")

test_that("Save SFE with SpatRaster images as RDS", {
saveRDS(sfe, "foo.rds")
sfe2 <- readRDS("foo.rds")
imgs <- imgData(sfe2)$data
classes <- vapply(imgs, function(x) class(imgRaster(x)), FUN.VALUE = character(1))
expect_true(all(classes == "PackedSpatRaster"))
expect_s4_class(sfe2, "SpatialFeatureExperiment")
unlink("foo.rds")
})

test_that("Save SFE without images", {
imgData(sfe) <- NULL
saveRDS(sfe, "bar.rds")
sfe2 <- readRDS("bar.rds")
expect_equal(sfe, sfe2)
unlink("bar.rds")
})

0 comments on commit 22a0c24

Please sign in to comment.