|
13 | 13 | #' @param object A \code{SpatialFeatureExperiment} object. |
14 | 14 | #' @return Invisibly \code{NULL}. |
15 | 15 | #' @importFrom terra wrap unwrap |
| 16 | +#' @importFrom BiocGenerics saveRDS |
16 | 17 | #' @export |
17 | 18 | #' @concept Utilities |
18 | 19 | #' @examples |
|
25 | 26 | setMethod("saveRDS", "SpatialFeatureExperiment", |
26 | 27 | function(object, file = "", ascii = FALSE, version = NULL, |
27 | 28 | 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 | + } |
28 | 33 | if (!nrow(imgData(object))) |
29 | 34 | base::saveRDS(object, file = file, ascii = ascii, |
30 | 35 | version = version, compress = compress, |
@@ -67,3 +72,35 @@ setMethod("unwrap", "SpatialFeatureExperiment", |
67 | 72 | } |
68 | 73 | x |
69 | 74 | }) |
| 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 | + |
0 commit comments