crop: Crop an SFE object with a geometry

View source: R/geometry_operation.R

cropR Documentation

Crop an SFE object with a geometry

Description

Returns an SFE object whose specified colGeometry returns TRUE with a geometric predicate function (usually intersects) with another geometry of interest. This can be used to subset an SFE object with a tissue boundary or histological region polygon, or crop away empty spaces. After cropping, not only will the cells/spots be subsetted, but also all geometries will be cropped.

Usage

crop(
  x,
  y = NULL,
  colGeometryName = 1L,
  sample_id = NULL,
  pred = st_intersects,
  op = st_intersection,
  xmin = NULL,
  xmax = NULL,
  ymin = NULL,
  ymax = NULL
)

Arguments

x

An SFE object.

y

An object of class sf, sfg, or sfc with which to crop the SFE object. Optional if xmin, xmax, ymin, and ymax are specified for a bounding box.

colGeometryName

Column geometry to used to indicate which cells/spots to keep.

sample_id

Samples to crop. Optional when only one sample is present. Can be multiple samples, or "all", which means all samples. For multiple samples, y may have column sample_id indicating which geometry subsets which sample. Only samples included in the sample_id column are subsetted. If there is no sample_id column or y is not specified, then the same geometry or bounding box is used to subset all samples specified in the sample_id argument.

pred

A geometric binary predicate function to indicate which cells/spots to keep, defaults to st_intersects.

op

A geometric operation function to crop the geometries in the SFE object. Defaults to st_intersection.

xmin

Minimum x coordinate of bounding box. Ignored if y is specified.

xmax

Maximum x coordinate of bounding box.

ymin

Minimum y coordinate of bounding box.

ymax

Maximum y coordinate of bounding box.

Value

An SFE object. There is no guarantee that the geometries after cropping are still all valid or preserve the original geometry class.

Note

In this version, this function does NOT crop the image.

Examples

library(SFEData)
sfe <- McKellarMuscleData("small")
# Subset sfe to only keep spots on tissue
sfe_on_tissue <- crop(sfe, tissueBoundary(sfe),
    colGeometryName = "spotPoly",
    sample_id = "Vis5A"
)
# Subset sfe to only keep what's within a bounding box
# All geometries will be cropped
# sample_id is optional when only one sample is present
sfe_cropped <- crop(sfe,
    colGeometryName = "spotPoly",
    xmin = 5500, xmax = 6500, ymin = 13500, ymax = 14500
)

pachterlab/SpatialFeatureExperiment documentation built on March 11, 2024, 11:11 p.m.