Skip to content

Commit 7735544

Browse files
committed
Deal with realoading SpatRasterImage from RDS #67
1 parent 848dd2c commit 7735544

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Description: A new S4 class integrating Simple Features with the R package sf
2020
be used.
2121
Imports:
2222
Biobase,
23-
BiocGenerics,
23+
BiocGenerics (>= 0.51.2),
2424
BiocNeighbors,
2525
BiocParallel,
2626
data.table,

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ exportMethods(annotGeometry)
192192
exportMethods(annotGeometryNames)
193193
exportMethods(bbox)
194194
exportMethods(cbind)
195+
exportMethods(containsOutOfMemoryData)
195196
exportMethods(cropImg)
196197
exportMethods(dim)
197198
exportMethods(dimGeometries)
@@ -230,6 +231,8 @@ importClassesFrom(SpatialExperiment,SpatialExperiment)
230231
importClassesFrom(SpatialExperiment,VirtualSpatialImage)
231232
importClassesFrom(terra,SpatRaster)
232233
importFrom(BiocGenerics,cbind)
234+
importFrom(BiocGenerics,containsOutOfMemoryData)
235+
importFrom(BiocGenerics,saveRDS)
233236
importFrom(BiocGenerics,updateObject)
234237
importFrom(BiocNeighbors,AnnoyParam)
235238
importFrom(BiocNeighbors,KmknnParam)

R/AllGenerics.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ setGeneric("affineImg", function(x, M, v, ...) standardGeneric("affineImg"))
246246
#' @export
247247
setGeneric("cropImg", function(x, bbox, ...) standardGeneric("cropImg"))
248248

249-
if (!isGeneric("saveRDS")) {setGeneric("saveRDS", function (object, file="", ascii=FALSE, version=NULL, compress=TRUE, refhook=NULL) standardGeneric("saveRDS"))}
249+
if (!isGeneric("readRDS")) {
250+
setGeneric("readRDS", function(file, refhook = NULL) standardGeneric("readRDS"))
251+
}
250252

251253
#' @export
252254
setGeneric("toExtImage", function(x, ...) standardGeneric("toExtImage"))

R/saveRDS.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @param object A \code{SpatialFeatureExperiment} object.
1414
#' @return Invisibly \code{NULL}.
1515
#' @importFrom terra wrap unwrap
16+
#' @importFrom BiocGenerics saveRDS
1617
#' @export
1718
#' @concept Utilities
1819
#' @examples
@@ -25,6 +26,10 @@
2526
setMethod("saveRDS", "SpatialFeatureExperiment",
2627
function(object, file = "", ascii = FALSE, version = NULL,
2728
compress = TRUE, refhook = NULL) {
29+
if (containsOutOfMemoryData(object)) {
30+
warning("This object contains out of memory data that may
31+
break when the RDS file is moved.")
32+
}
2833
if (!nrow(imgData(object)))
2934
base::saveRDS(object, file = file, ascii = ascii,
3035
version = version, compress = compress,
@@ -67,3 +72,35 @@ setMethod("unwrap", "SpatialFeatureExperiment",
6772
}
6873
x
6974
})
75+
76+
#' Whether an SFE object contains out of memory data
77+
#'
78+
#' Out of memory data, such as \code{DelayedArray}, some \code{SpatRasterImage}
79+
#' objects, and \code{BioFormatsImage}, will break if saved as RDS. This method
80+
#' of \code{\link[BiocGenerics]{containsOutOfMemoryData}} checks if an SFE
81+
#' object has out of memory data, specifically the images. Having out of memory
82+
#' data will result into an error when \code{saveRDS} is called; we recommend
83+
#' using the \code{alabaster.sfe} package instead.
84+
#'
85+
#' @param object An SFE object
86+
#' @return TRUE or FALSE
87+
#' @importFrom BiocGenerics containsOutOfMemoryData
88+
#' @export
89+
#' @concept Utilities
90+
#' @examples
91+
#' outdir <- system.file("extdata", package = "SpatialFeatureExperiment")
92+
#' samples <- file.path(outdir, paste0("sample0", 1:2))
93+
#' sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered")
94+
#' containsOutOfMemoryData(sfe)
95+
setMethod("containsOutOfMemoryData", "SpatialFeatureExperiment",
96+
function(object) {
97+
imgs <- imgData(object)
98+
imgData(object) <- NULL
99+
object <- as(object, "SingleCellExperiment")
100+
out_se <- containsOutOfMemoryData(object)
101+
is_img_om <- vapply(imgs$data, function(x) {
102+
!is.na(imgSource(x))
103+
}, FUN.VALUE = logical(1))
104+
any(c(out_se, is_img_om))
105+
})
106+

tests/testthat/test-saveRDS.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ samples <- file.path(outdir, paste0("sample0", 1:2))
33
sfe <- read10xVisiumSFE(samples, type = "sparse", data = "filtered")
44

55
test_that("Save SFE with SpatRaster images as RDS", {
6-
saveRDS(sfe, "foo.rds")
6+
expect_warning(saveRDS(sfe, "foo.rds"), "This object contains out of memory data")
77
sfe2 <- readRDS("foo.rds")
88
imgs <- imgData(sfe2)$data
99
classes <- vapply(imgs, function(x) class(x), FUN.VALUE = character(1))

0 commit comments

Comments
 (0)