## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  cache = TRUE
)

## ----install, eval = FALSE----------------------------------------------------
# if (!require("BiocManager"))
#     install.packages("BiocManager")
# BiocManager::install("MultiAssaySpatialExperiment")

## ----load_packages, message = FALSE, warning = FALSE--------------------------
library(MultiAssaySpatialExperiment)
library(SummarizedExperiment)
library(S4Vectors)

## ----citation, eval = FALSE---------------------------------------------------
# citation("MultiAssaySpatialExperiment")

## ----libs, message = FALSE----------------------------------------------------
library(MultiAssaySpatialExperiment)
library(SummarizedExperiment)
library(S4Vectors)
library(sf)

## ----build_assays-------------------------------------------------------------
make_assay <- function(seed) {
  set.seed(seed)
  SummarizedExperiment(
    assays = list(counts = matrix(rpois(20, 10), nrow = 5, ncol = 4,
      dimnames = list(paste0("Gene", 1:5), paste0("S", 1:4)))))
}

rna <- make_assay(1)
protein <- make_assay(2)

## ----build_spatial------------------------------------------------------------
centroids <- DataFrame(
  x = c(1.5, 2.5, 4.5, 5.5),
  y = c(1.5, 2.5, 4.5, 5.5),
  instance_id = paste0("S", 1:4))

square <- function(x0, y0, side) {
  st_polygon(list(cbind(
    c(x0, x0 + side, x0 + side, x0, x0),
    c(y0, y0, y0 + side, y0 + side, y0))))
}

cells <- DataFrame(
  instance_id = c("left", "right"),
  geometry = st_sfc(square(0, 0, 3), square(3, 3, 3)))

## ----build_mase---------------------------------------------------------------
observations <- paste0("S", 1:4)
specimens <- c("P1", "P1", "P2", "P2")

mase <- MultiAssaySpatialExperiment(
  experiments = ExperimentList(rna = rna, protein = protein),
  colData = DataFrame(row.names = c("P1", "P2")),
  sampleMap = DataFrame(
    assay = factor(rep(c("rna", "protein"), each = 4)),
    primary = rep(specimens, 2),
    colname = rep(observations, 2)),
  points = PointsLayerList(centroids = centroids),
  shapes = ShapesLayerList(cells = cells),
  spatialMap = DataFrame(
    assay = factor(rep(c("rna", "protein"), each = 4)),
    colname = rep(observations, 2),
    element_type = "points",
    region = "centroids",
    instance_id = rep(observations, 2)))

mase

## ----accessors----------------------------------------------------------------
experiments(mase)
spatialPoints(mase)[["centroids"]]
spatialShapes(mase)[["cells"]]

## ----show_spatialmap----------------------------------------------------------
head(spatialMap(mase), 4)

## ----subset_region------------------------------------------------------------
corner <- subsetByBoundingBox(mase, xmin = 0, xmax = 3, ymin = 0, ymax = 3)

vapply(experiments(corner), ncol, integer(1))
spatialPoints(corner)[["centroids"]]

## ----subset_axes--------------------------------------------------------------
vapply(experiments(mase[, "P1"]), ncol, integer(1))
vapply(experiments(mase[, list(rna = c("S1", "S3"))]), ncol, integer(1))

## ----annotate-----------------------------------------------------------------
mase <- annotateWithRegions(mase, points = "centroids", shapes = "cells")
spatialMap(mase)

## ----aggregate----------------------------------------------------------------
aggregateByRegion(mase, by = "cells", FUN = "sum")

## ----coerce-------------------------------------------------------------------
library(SpatialExperiment)

spe <- SpatialExperiment(
  assays = list(counts = matrix(rpois(20, 5), 5, 4,
    dimnames = list(paste0("Gene", 1:5), paste0("S", 1:4)))),
  colData = DataFrame(sample_id = rep("P1", 4), row.names = paste0("S", 1:4)),
  spatialCoords = cbind(x = c(1.5, 2.5, 4.5, 5.5), y = c(1.5, 2.5, 4.5, 5.5)))

from_spe <- as(spe, "MultiAssaySpatialExperiment")
spatialPoints(from_spe)

identical(unname(spatialCoords(as(from_spe, "SpatialExperiment"))),
          unname(spatialCoords(spe)))

## ----sessionInfo--------------------------------------------------------------
sessionInfo()

