Skip to contents

Coerce 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 GraphSpace object.

fdata.type

What the feature data (gs_fdata) hold, such as "counts" or "logcounts"; it sets where they are stored (see details). If NULL (default), the type recorded by as.GraphSpace is 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); if FALSE, the current node positions, e.g. normalized coordinates. See gs_coords.

unit

Optional length unit, such as "um" or "mm", to export the coordinates in a physical unit, using the unit set with gs_coords_unit. If NULL (default), coordinates are exported in coordinate units. See gs_coords. gs_to_sfe() exports in microns (SFE unit "micron") with unit = "um", or with NULL when a unit is set; otherwise in pixels ("full_res_image_pixel").

...

Additional arguments passed to SpatialExperiment (e.g. sample_id), toSpatialFeatureExperiment, SingleCellExperiment, or CreateSeuratObject (e.g. project).

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, SpatialFeatureExperiment and SingleCellExperiment, the assay named by fdata.type (e.g. "counts"); for Seurat, the counts or data layer ("logcounts") of the default assay. Seurat requires features.

  • Coordinates: spatialCoords() for SpatialExperiment and SpatialFeatureExperiment; a reduction for SingleCellExperiment; for Seurat, a field of view if recorded as spatial, else a reduction. Named as recorded by as.GraphSpace, or "gspace"; in unit if given.

  • Node data: colData() or meta.data columns, without structural columns, coordinates or styling attributes. Payloads go to colData(); for Seurat, nested tables are unnested into meta.data, and list-valued fields are not exported.

  • Geometries: colGeometries() for SpatialFeatureExperiment, colData() for SpatialExperiment and SingleCellExperiment, matching the exported coordinates; for Seurat, boundaries of the field of view (one outline per cell: largest part kept, holes removed).

  • Edges: a cell-cell graph with weight as values (1 without weights; parallel edges take the largest), in Graphs for Seurat and colPairs() 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".