This function splits a SPATA2 object into multiple sub-objects based on a specified grouping variable.

splitSpataObject(
  object,
  grouping,
  naming = "{sample_name}_{group_name}",
  spatial_proc = TRUE,
  reduce = FALSE,
  verbose = NULL
)

Arguments

object

An object of class SPATA2 or, in case of S4 generics, objects of classes for which a method has been defined.

grouping

Character value. The grouping variable of interest. Use getGroupingOptions() to obtain all valid input options.

naming

Character value. A glue expression based on which the respective new sample names are created. See details for more information.

spatial_proc

Logical value. Indicates whether the new sub-object is processed spatially. If TRUE, a new tissue outline is identified based on the remaining observations via identifyTissueOutline(). Then, spatial annotations are tested on being located on either of the remaining tissue sections. If they are not, they are removed.

If FALSE, these processing steps are skipped. Generally speaking, this is not recommended. Only set to FALSE, if you know what you're doing.

Only relevant, if barcodes is not NULL.

reduce

A logical value indicating whether to reduce the SPATA2 sub-objects after splitting via reduceSpataObject(). Default is FALSE.

verbose

Logical. If TRUE, informative messages regarding the computational progress will be printed.

(Warning messages will always be printed.)

Value

A named list of SPATA2 sub-objects split by the specified grouping.

Details

The input for naming defaults to the original sample name suffixed with the name of the group for which the sub-object contains the data. Both are separated with a '_'. Sample name can be accessed via 'sample_name' and group name can be accessed via 'group_name'.

Afterwards, if grouping is not 'tissue_section' a new tissue outline is identified for every sub-object using identifyTissueOutline() with default input.

Examples

library(SPATA2)
library(tidyverse)
library(patchwork)

# ----- Example 1: subsetSpataObject()
object <- loadExampleObject("UKF313T", meta = TRUE)

barcodes_keep <-
 getMetaDf(object) %>%
 filter(bayes_space %in% c("B3", "B2", "B1")) %>%
 pull(barcodes)

object_sub <- subsetSpataObject(object, barcodes = barcodes_keep)

show(object)
show(object_sub)

plotSpatialAnnotations(object) # plots all annotations
plotSpatialAnnotations(object_sub) # subsetting affects everything by default

ids <- getSpatAnnIds(object)
ids_sub <- getSpatAnnIds(object_sub)

# use patchwork to compare plots
plot_orig <-
  plotSurface(object, color_by = "bayes_space", outline = T) +
  ggpLayerSpatAnnOutline(object, ids = ids)

plot_sub <-
  plotSurface(object_sub, color_by = "bayes_space", outline = T) +
  ggpLayerSpatAnnOutline(object_sub, ids = ids_sub)

plot_orig + plot_sub

# ----- Example 2: splitSpataObject()
# uses subsetSpataObject() in the background

object_mouse <- loadExampleObject("LMU_MCI", process = TRUE, meta = TRUE)

orig_frame <- ggpLayerFrameByCoords(object_mouse)

ids <- getSpatAnnIds(object_mouse)

plotSurface(object_mouse, color_by = "tissue_section", pt_clr = "lightgrey") +
  ggpLayerSpatAnnOutline(object_mouse, ids = ids) +
  ggpLayerSpatAnnPointer(object_mouse, ids = ids, ptr_lengths = "0.45mm", text_dist = 10, text_size = 7)

obj_list <- splitSpataObject(object_mouse, grouping = "tissue_section")

# present resulting sub-objects
purrr::map(obj_list, .f = ~ .x)

# present remaining ids
purrr::map(obj_list, .f = ~ getSpatAnnIds(.x))

# show surface plot with all remaining spatial annotations
purrr::map(obj_list, .f = ~ plotSurface(.x) + ggpLayerSpatAnnOutline(.x) + orig_frame) %>%
  patchwork::wrap_plots()

# repeat with spatial_proc = FALSE
obj_list <- splitSpataObject(object_mouse, grouping = "tissue_section", spatial_proc = FALSE)

# present remaining spatial annotation ids
purrr::map(obj_list, .f = ~ getSpatAnnIds(.x))

# show surface plot with all remaining spatial annotations
purrr::map(obj_list, .f = ~ plotSurface(.x) + ggpLayerSpatAnnOutline(.x) + orig_frame) %>%
  patchwork::wrap_plots()

# -----  Example 3: cropSpataObject()
# uses subsetSpataObject() in the background
object <- loadExampleObject("UKF275T", meta = TRUE)

orig_frame <- ggpLayerFrameByCoords(object)

xcrop <- c("2.5mm", "5.5mm")
ycrop <- c("5mm", "7mm")

plotSurface(object, color_by = "bayes_space") +
 ggpLayerAxesSI(object) +
 ggpLayerRect(object, xrange = xcrop, yrange = ycrop)

object_cropped <-
 cropSpataObject(object, xrange = xcrop, yrange = ycrop)

plotSurface(object_cropped, color_by = "bayes_space", pt_size = 0.75) + orig_frame