Convert GraphSpace objects to SpatialExperiment, SpatialFeatureExperiment, SingleCellExperiment or Seurat
Source:R/gspace-coercion.R
GraphSpace-export.RdCoerce a GraphSpace object into a
SpatialExperiment, a
SpatialFeatureExperiment, a
SingleCellExperiment or a
Seurat object, the reverse of
as.GraphSpace.
Usage
gs_to_spe(x, fdata.type = NULL, raw = TRUE, unit = NULL, ...)
gs_to_sfe(x, fdata.type = NULL, raw = TRUE, unit = NULL, ...)
gs_to_sce(x, fdata.type = NULL, raw = TRUE, unit = NULL, ...)
gs_to_seurat(x, fdata.type = NULL, raw = TRUE, unit = NULL, ...)
# S3 method for class 'GraphSpace'
as.Seurat(x, ...)Arguments
- x
A
GraphSpaceobject.- fdata.type
What the feature data (
gs_fdata) hold, such as"counts"or"logcounts"; it sets where they are stored (see details). IfNULL(default), the type recorded byas.GraphSpaceis used, or"counts"if none was recorded.- raw
A single logical value. If
TRUE(default), use the raw node coordinates,gs_coords(x, raw = TRUE); ifFALSE, the current node positions, e.g. normalized coordinates. Seegs_coords.- unit
Optional length unit, such as
"um"or"mm", to export the coordinates in a physical unit, using the unit set withgs_coords_unit. IfNULL(default), coordinates are exported in coordinate units. Seegs_coords.gs_to_sfe()exports in microns (SFE unit"micron") withunit = "um", or withNULLwhen a unit is set; otherwise in pixels ("full_res_image_pixel").- ...
Additional arguments passed to
SpatialExperiment(e.g.sample_id),toSpatialFeatureExperiment,SingleCellExperiment, orCreateSeuratObject(e.g.project).
Value
A SpatialExperiment, a
SpatialFeatureExperiment, a
SingleCellExperiment or a
Seurat object.
Details
A new object is created with the defaults of the target class. Nodes become cells (columns), and node names become cell names:
Features: stored as features x cells. For
SpatialExperiment,SpatialFeatureExperimentandSingleCellExperiment, the assay named byfdata.type(e.g."counts"); forSeurat, thecountsordatalayer ("logcounts") of the default assay. Seurat requires features.Coordinates:
spatialCoords()forSpatialExperimentandSpatialFeatureExperiment; a reduction forSingleCellExperiment; forSeurat, a field of view if recorded as spatial, else a reduction. Named as recorded byas.GraphSpace, or"gspace"; inunitif given.Node data:
colData()ormeta.datacolumns, without structural columns, coordinates or styling attributes. Payloads go tocolData(); forSeurat, nested tables are unnested intometa.data, and list-valued fields are not exported.Geometries:
colGeometries()forSpatialFeatureExperiment,colData()forSpatialExperimentandSingleCellExperiment, matching the exported coordinates; forSeurat, boundaries of the field of view (one outline per cell: largest part kept, holes removed).Edges: a cell-cell graph with
weightas values (1 without weights; parallel edges take the largest), inGraphsforSeuratandcolPairs()otherwise, named after the imported graph or"gspace".
Images are not exported. The coercion serves downstream analysis of the nodes (e.g. annotation, clustering); to keep every component of the original object, retain it.
gs_to_seurat() is also available as as.Seurat(), the
generic of SeuratObject. The target packages are optional and
only needed for the conversion.
Examples
data('gtoy1', package = 'RGraphSpace')
gs <- GraphSpace(gtoy1)
#> Validating the 'igraph' object...
#> Ignoring graph-level attributes: 'name', 'mode', 'center'
#> Creating a 'GraphSpace' object...
# Add a feature matrix (nodes x features)
feats <- matrix(as.numeric(seq_len(gs_vcount(gs) * 3)), ncol = 3,
dimnames = list(rownames(gs), c("geneA", "geneB", "geneC")))
gs <- gs_add_features(gs, feats)
if (requireNamespace("SpatialExperiment", quietly = TRUE)) {
spe <- gs_to_spe(gs)
}
#> Coercing 'GraphSpace' to 'SpatialExperiment'...
#> ℹ Feature data are assumed to hold counts.
#> • Use `fdata.type` to set what they hold, e.g. "logcounts".
if (requireNamespace("SingleCellExperiment", quietly = TRUE)) {
sce <- gs_to_sce(gs)
}
#> Coercing 'GraphSpace' to 'SingleCellExperiment'...
#> ℹ Feature data are assumed to hold counts.
#> • Use `fdata.type` to set what they hold, e.g. "logcounts".
if (requireNamespace("SeuratObject", quietly = TRUE)) {
seu <- gs_to_seurat(gs)
}
#> Coercing 'GraphSpace' to 'Seurat'...
#> ℹ Feature data are assumed to hold counts.
#> • Use `fdata.type` to set what they hold, e.g. "logcounts".
# Coordinates in micrometers, with one coordinate unit = 0.5 um
gs_coords_unit(gs) <- c(um = 0.5)
if (requireNamespace("SpatialExperiment", quietly = TRUE)) {
spe <- gs_to_spe(gs, unit = "um")
}
#> Coercing 'GraphSpace' to 'SpatialExperiment'...
#> Coordinates exported in 'um'.
#> ℹ Feature data are assumed to hold counts.
#> • Use `fdata.type` to set what they hold, e.g. "logcounts".