Introduction

Here is a demonstration of how to use the package ‘gsdensity’ to perform gene set analysis on single-cell data when we want to investigate the relationship between the pathway and other information of the cells, such as clustering information and spatial information.

Load libraries

library(gsdensity)
library(ggplot2) # for plotting
library(ggrepel)
library(reshape2)
library(msigdbr) # for gathering gene sets
library(Seurat)
library(SeuratData)
library(future) # for parallel computing
library(future.apply) # for parallel computing

Preparation: Collect gene sets

This is the same as is shown in ‘pbmc3k_example’. Here we use gene ontology (GO) biological process gene sets.
# Collect a single category from the msigdbr database
# For more information please check: https://cran.r-project.org/web/packages/msigdbr/vignettes/msigdbr-intro.html

# Use mouse gene sets
mdb_c5 <- msigdbr(species = "Mus musculus", category = "C5")

# If we just want to do biological process:
mdb_c5_bp <- mdb_c5[mdb_c5$gs_subcat == "GO:BP", ]

# convert msigdbr gene sets to a list good for the input
gene.set.list <- list()
for (gene.set.name in unique(mdb_c5_bp$gs_name)){
        gene.set.list[[gene.set.name]] <- mdb_c5_bp[mdb_c5_bp$gs_name %in% gene.set.name, ]$gene_symbol
}
# head(gene.set.list)

Preparation: Single-cell datasets

Here we use the stxBrain data from SeuratData;
We first do the preprocessing following this tutorial https://satijalab.org/seurat/articles/spatial_vignette.html
brain <- LoadData("stxBrain", type = "anterior1")
print(brain)
## An object of class Seurat 
## 31053 features across 2696 samples within 1 assay 
## Active assay: Spatial (31053 features, 0 variable features)
# Transformation

brain <- SCTransform(brain, assay = "Spatial", verbose = FALSE)
# Dimensionality reduction, clustering, and visualization

brain <- RunPCA(brain, assay = "SCT", verbose = FALSE)
brain <- FindNeighbors(brain, reduction = "pca", dims = 1:30)
brain <- FindClusters(brain, verbose = FALSE)
brain <- RunUMAP(brain, reduction = "pca", dims = 1:30)

# plot the clustering information
DimPlot(brain, reduction = "umap", label = TRUE)

# map the clusters back onto the spatial map
SpatialDimPlot(brain, label = TRUE, label.size = 3)