all_times <- list()  # store the time for each chunk
knitr::knit_hooks$set(time_it = local({
  now <- NULL
  function(before, options) {
    if (before) {
      now <<- Sys.time()
    } else {
      res <- difftime(Sys.time(), now, units = "secs")
      all_times[[options$label]] <<- res
    }
  }
}))
knitr::opts_chunk$set(
  tidy = TRUE,
  tidy.opts = list(width.cutoff = 95),
  fig.width = 10,
  message = FALSE,
  warning = FALSE,
  time_it = TRUE,
  error = TRUE
)

Setup the Seurat objects

library(Seurat)
options(Seurat.object.assay.version = "v5")
library(SeuratData)
library(patchwork)
# install dataset
InstallData('pbmcsca')
# load dataset
pbmcsca <- LoadData("pbmcsca")
pbmcsca <-  UpdateSeuratObject(object = pbmcsca)
pbmcsca[["RNA"]] <- as(pbmcsca[["RNA"]], Class = "Assay5")

# split the dataset into layers 
pbmcsca[["RNA"]] <- split(pbmcsca[["RNA"]], f = pbmcsca$Method)

Run SCTransform

pbmcsca <- SCTransform(pbmcsca)
pbmcsca <- RunPCA(pbmcsca, npcs = 30, verbose = FALSE)

Perform integration

We then integrate all the layers using the IntegrateLayers() function.

pbmcsca <- IntegrateLayers(object = pbmcsca, 
                           method = RPCAIntegration,
                           normalization.method="SCT",
                           verbose = F)
pbmcsca <- FindNeighbors(pbmcsca, dims = 1:30)
pbmcsca <- FindClusters(pbmcsca, resolution = 2)
pbmcsca <- RunUMAP(pbmcsca, dims = 1:30)
# Visualization
p1 <- DimPlot(pbmcsca, reduction = "umap", group.by = "Method")
p2 <- DimPlot(pbmcsca, reduction = "umap", group.by = "CellType", label = TRUE, repel = TRUE)
p1 + p2


satijalab/seurat documentation built on May 11, 2024, 4:04 a.m.