SpatialExperiment-methods {SpatialExperiment} | R Documentation

## Methods for spatial attributes

### Description

The `SpatialExperiment` class provides a family of methods to get and set spatial data attributes in `SpatialExperiment` objects. Spatial attributes include `spatialData`, `spatialCoords`, `imgData`, and `scaleFactors`.

### Usage

    ## S4 method for signature 'SpatialExperiment'
    spatialData(x, spatialCoords = FALSE)

    ## S4 replacement method for signature 'SpatialExperiment,DFrame'
    spatialData(x) <- value

    ## S4 replacement method for signature 'SpatialExperiment,'NULL''
    spatialData(x) <- value

    ## S4 method for signature 'SpatialExperiment'
    spatialDataNames(x)

    ## S4 replacement method for signature 'SpatialExperiment,character'
    spatialDataNames(x) <- value

    ## S4 method for signature 'SpatialExperiment'
    spatialCoords(x)

    ## S4 replacement method for signature 'SpatialExperiment,matrix'
    spatialCoords(x) <- value

    ## S4 replacement method for signature 'SpatialExperiment,'NULL''
    spatialCoords(x) <- value

    ## S4 method for signature 'SpatialExperiment'
    spatialCoordsNames(x)

    ## S4 replacement method for signature 'SpatialExperiment,character'
    spatialCoordsNames(x) <- value

    ## S4 replacement method for signature 'SpatialExperiment,'NULL''
    spatialCoordsNames(x) <- value

    ## S4 method for signature 'SpatialExperiment'
    scaleFactors(x, sample_id = TRUE, image_id = TRUE)

    ## S4 method for signature 'SpatialExperiment'
    imgData(x)

    ## S4 replacement method for signature 'SpatialExperiment,DataFrame'
    imgData(x) <- value

    ## S4 replacement method for signature 'SpatialExperiment,'NULL''
    imgData(x) <- value

### Arguments

`x` | A `SpatialExperiment` object.
`spatialCoords` | Logical specifying whether to include columns from `spatialCoords` in the output `DataFrame` from `spatialData`. Default = `FALSE`.
`value` | Replacement value for replacement methods.
`sample_id` | Logical value or character vector specifying sample identifier(s) for `scaleFactors`. Default = `TRUE` (all samples).
`image_id` | Logical value or character vector specifying image identifier(s) for `scaleFactors`. Default = `TRUE` (all images).
`spatialData` | Logical specifying whether to include columns from `spatialData` in the output `DataFrame` from `colData`. Default = `FALSE`

### Details

Additional details for each type of data attribute are provided below.

`spatialData` and `spatialCoords` are distinguished as follows: `spatialData` is a `DataFrame` containing all the data associated with the spatial information (optionally including spatial coordinates from `spatialCoords`), while `spatialCoords` is a numeric matrix containing only the defined spatial coordinates (e.g. columns `x` and `y`).

### Value

Return value varies depending on method, as described below.

### spatialData and spatialCoords methods

`spatialData(x)`:

  The `spatialData` getter provides the optional argument `spatialCoords`, which can be used to include the columns of spatial coordinates (`spatialCoords`) in the output `DataFrame`.

`spatialData(x) <- value`:

  The `spatialData` setter expects a `DataFrame`. If the input does not contain an `in_tissue` column, this will be included with a default value of `1`.

`spatialCoords(x)`:

  Getter for numeric matrix of spatial coordinates.

`spatialCoords(x) <- value`:

  Setter for numeric matrix of spatial coordinates.

### spatialDataNames and spatialCoordsNames methods

`spatialDataNames(x)`:

  Returns the column names of the `spatialData` `DataFrame`.

`spatialDataNames(x) <- value`:

  Setter to replace column names in the `spatialData` `DataFrame`.

`spatialCoordsNames(x)`:

  Returns the defined names of the spatial coordinates (e.g. `c("x", "y")`).

`spatialCoordsNames(x) <- value`:

  Setter to define the names of the spatial coordinate columns.

### imgData methods

`imgData(x)`:

  Getter to return the `imgData` `DataFrame`.

`imgData(x) <- value`:

  Setter to provide a `DataFrame` object as `imgData` of the `SpatialExperiment` object.

### Other methods

`scaleFactors(x, sample_id, image_id)`:

  Getter to return the scale factors associated with the `sample_id`(s) and `image_id`(s) provided. This is related to the stored image(s) in the `SpatialExperiment` `imgData` structure. See argument descriptions for further details.

### Examples

    example(read10xVisium)

    # spatialData returns a DataFrame
    spatialData(spe)

    # spatialCoords returns a numeric matrix
    head(spatialCoords(spe))

    # spatialData replacement method
    spdata <- spatialData(spe)
    spdata$array_col <- spdata$array_row
    spatialData(spe) <- spdata

    # return additional columns for spatialData
    spatialData(spe, spatialCoords=TRUE)

    # change spatial coordinate names
    spatialCoordsNames(spe)
    spatialCoordsNames(spe) <- c("x", "y")
    head(spatialCoords(spe))

    # imgData and scale factors
    imgData(spe)
    scaleFactors(spe)

    # tabulate number of spots mapped to tissue
    cd <- colData(spe, spatialData = TRUE)
    table(
      in_tissue = cd$in_tissue,
      sample_id = cd$sample_id)

[Package SpatialExperiment version 1.4.0 Index]