Skip to contents

Save SFE objects to disk in an interoperable, language agnostic format that may also facilitate out of memory operations via HDF5 (non-spatial, inherited from alabaster.sce) and Apache Parquet (geometries).

Usage

# S4 method for class 'SpatialFeatureExperiment'
saveObject(x, path, ...)

Arguments

x

A Bioconductor object of the specified class.

path

String containing the path to a directory in which to save x.

...

Additional named arguments to pass to specific methods.

Value

x is saved into path and NULL is invisibly returned.

Details

There's no new arguments for ... for the SFE, SpatialExperiment, and SingleCellExperiment methods, but there is an argument that can be specified for ... in the SummarizedExperiment method of saveObject.

At present, spatial results in featureData for geometries and dimension reductions (see colFeatureData) and parameters of spatial analyses (see getParams) are not save because those parts of the SFE object are more experimental and are subject to change. colFeatureData where global spatial results are stored for columns of colData(x) is saved by the alabaster saveObject method for DataFrame.

Examples

library(SpatialFeatureExperiment)
library(Voyager)
library(SFEData)
library(SingleCellExperiment)
#> Loading required package: SummarizedExperiment
#> Loading required package: MatrixGenerics
#> Loading required package: matrixStats
#> 
#> Attaching package: ‘matrixStats’
#> The following object is masked from ‘package:alabaster.base’:
#> 
#>     anyMissing
#> 
#> Attaching package: ‘MatrixGenerics’
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
#>     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
#>     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
#>     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
#>     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
#>     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
#>     colWeightedMeans, colWeightedMedians, colWeightedSds,
#>     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
#>     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
#>     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
#>     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
#>     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
#>     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
#>     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
#>     rowWeightedSds, rowWeightedVars
#> Loading required package: GenomicRanges
#> Loading required package: stats4
#> Loading required package: BiocGenerics
#> Loading required package: generics
#> 
#> Attaching package: ‘generics’
#> The following objects are masked from ‘package:base’:
#> 
#>     as.difftime, as.factor, as.ordered, intersect, is.element, setdiff,
#>     setequal, union
#> 
#> Attaching package: ‘BiocGenerics’
#> The following object is masked from ‘package:SpatialFeatureExperiment’:
#> 
#>     saveRDS
#> The following objects are masked from ‘package:stats’:
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from ‘package:base’:
#> 
#>     Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
#>     as.data.frame, basename, cbind, colnames, dirname, do.call,
#>     duplicated, eval, evalq, get, grep, grepl, is.unsorted, lapply,
#>     mapply, match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
#>     rank, rbind, rownames, sapply, saveRDS, table, tapply, unique,
#>     unsplit, which.max, which.min
#> Loading required package: S4Vectors
#> 
#> Attaching package: ‘S4Vectors’
#> The following object is masked from ‘package:utils’:
#> 
#>     findMatches
#> The following objects are masked from ‘package:base’:
#> 
#>     I, expand.grid, unname
#> Loading required package: IRanges
#> Loading required package: GenomeInfoDb
#> Loading required package: Biobase
#> Welcome to Bioconductor
#> 
#>     Vignettes contain introductory material; view with
#>     'browseVignettes()'. To cite Bioconductor, see
#>     'citation("Biobase")', and for packages 'citation("pkgname")'.
#> 
#> Attaching package: ‘Biobase’
#> The following object is masked from ‘package:MatrixGenerics’:
#> 
#>     rowMedians
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     anyMissing, rowMedians
#> The following object is masked from ‘package:alabaster.base’:
#> 
#>     anyMissing
library(scater)
#> Loading required package: scuttle
#> Loading required package: ggplot2
#> 
#> Attaching package: ‘ggplot2’
#> The following object is masked from ‘package:SpatialFeatureExperiment’:
#> 
#>     unit

fp <- tempfile()
fn <- file.path(fp, "vizgen")
d <- VizgenOutput(dataset = "cellpose", file_path = fn)
#> see ?SFEData and browseVignettes('SFEData') for documentation
#> loading from cache
#> The downloaded files are in /tmp/RtmpfBErth/file2aa8232a841e/vizgen/vizgen_cellbound_Cellpose 
suppressWarnings(sfe1 <- readVizgen(d, add_molecules = TRUE))
#> >>> 1 `.parquet` files exist:
#> /tmp/RtmpfBErth/file2aa8232a841e/vizgen/vizgen_cellbound_Cellpose/Cellpose/cellpose_micron_space.parquet
#> >>> using -> /tmp/RtmpfBErth/file2aa8232a841e/vizgen/vizgen_cellbound_Cellpose/Cellpose/cellpose_micron_space.parquet
#> >>> Cell segmentations are found in `.parquet` file
#> Sanity checks on cell segmentation polygons:
#> >>> ..found 1 cells with (nested) polygon lists
#> >>> ..applying filtering
#> >>> Casting MULTIPOLYGON geometry to POLYGON
#> >>> Checking polygon validity
#> >>> Reading transcript coordinates
#> >>> Converting transcript spots to geometry
#> >>> Writing reformatted transcript spots to disk

colGraph(sfe1, "knn5") <- findSpatialNeighbors(sfe1, method = "knearneigh", k = 5)
SpatialFeatureExperiment::centroids(sfe1)$foo <- rnorm(ncol(sfe1))
sfe1 <- logNormCounts(sfe1)
sfe1 <- runMoransI(sfe1, colGraphName = "knn5")
sfe1 <- colDataMoransI(sfe1, features = c("transcript_count", "anisotropy", 
                                          "perimeter_area_ratio", "solidity"))
sfe1 <- colGeometryMoransI(sfe1, colGeometryName = "centroids", features = "foo")
sfe1 <- runPCA(sfe1, ncomponents = 10)
sfe1 <- reducedDimMoransI(sfe1, components = 1:10)

sfe1 <- runUnivariate(sfe1, type = "localmoran", features = rownames(sfe1)[1])

fsave <- file.path(fp, "sfe_vizgen")
saveObject(sfe1, fsave)
#> >>> Saving SpatialExperiment
#> >>> Saving colgeometries
#> >>> Saving rowgeometries
#> >>> Saving localResults
#> >>> Saving spatial graphs
sfe2 <- readObject(fsave)
#> >>> Reading SpatialExperiment
#> >>> Reading colgeometries
#> >>> Reading rowgeometries
#> >>> Reading spatial graphs
#> >>> Reading localResults

unlink(fsave, recursive = TRUE)