Save and Load Seurat Objects from Rds files
SaveSeuratRds(
object,
file = NULL,
move = TRUE,
destdir = deprecated(),
relative = FALSE,
...
)
LoadSeuratRds(file, ...)A Seurat object
Path to save object to; defaults to
file.path(getwd(), paste0(Project(object), ".Rds"))
Move on-disk layers into dirname(file)
Save relative paths instead of absolute ones
Arguments passed on to base::saveRDS, base::readRDS
asciia logical. If TRUE or NA, an ASCII
representation is written; otherwise (default), a binary one is used.
See the comments in the help for save.
versionthe workspace format version to use. NULL
specifies the current default version (3). The only other supported
value is 2, the default from R 1.4.0 to R 3.5.0.
compressa logical specifying whether saving to a named file is
to use "gzip" compression, or one of "gzip",
"bzip2" or "xz" to indicate the type of compression to
be used. Ignored if file is a connection.
refhooka hook function for handling reference objects.
Invisibly returns file
This function requires the fs package to be installed
This function uses
progressr to
render status updates and progress bars. To enable progress updates, wrap
the function call in with_progress or run
handlers(global = TRUE) before running
this function. For more details about progressr, please read
vignette("progressr-intro")
if (requireNamespace("fs", quietly = TRUE)) {
# Write out with DelayedArray
if (requireNamespace("HDF5Array", quietly = TRUE)) {
pbmc <- pbmc_small
pbmc[["disk"]] <- CreateAssay5Object(list(
mem = LayerData(pbmc, "counts"),
disk = as(LayerData(pbmc, "counts"), "HDF5Array")
))
# Save `pbmc` to an Rds file
out <- tempfile(fileext = ".Rds")
SaveSeuratRds(pbmc, file = out)
# Object cache
obj <- readRDS(out)
Tool(obj, "SaveSeuratRds")
# Load the saved object with on-disk layers back into memory
pbmc2 <- LoadSeuratRds(out)
pbmc2
pbmc2[["disk"]]
}
# Write out with BPCells
if (requireNamespace("BPCells", quietly = TRUE)) {
pbmc <- pbmc_small
bpm <- BPCells::write_matrix_dir(LayerData(pbmc, "counts"), dir = tempfile())
bph <- BPCells::write_matrix_hdf5(
LayerData(pbmc, "counts"),
path = tempfile(fileext = ".h5"),
group = "counts"
)
pbmc[["disk"]] <- CreateAssay5Object(list(dir = bpm, h5 = bph))
# Save `pbmc` to an Rds file
out <- tempfile(fileext = ".Rds")
SaveSeuratRds(pbmc, file = out)
# Object cache
obj <- readRDS(out)
Tool(obj, "SaveSeuratRds")
# Load the saved object with on-disk layers back into memory
pbmc2 <- LoadSeuratRds(out)
pbmc2
pbmc2[["disk"]]
}
}
#> Warning: Matrix compression performs poorly with non-integers.
#> • Consider calling convert_matrix_type if a compressed integer matrix is intended.
#> This message is displayed once every 8 hours.
#> Warning: Trying to move ‘/tmp/RtmpLXiDfa/file803deaa2ce’ to itself, skipping
#> Warning: Trying to move ‘/tmp/RtmpLXiDfa/file803789caacc.h5’ to itself, skipping
#> Assay (v5) data with 230 features for 80 cells
#> First 10 features:
#> MS4A1, CD79B, CD79A, HLA-DRA, TCL1A, HLA-DQB1, HVCN1, HLA-DMB, LTB,
#> LINC00926
#> Layers:
#> counts.dir, counts.h5