For single-cell RNA sequencing (scRNA-seq) data analysis, we will follow a tutorial on R-based analysis using Seurat (https://satijalab.org/seurat/articles/pbmc3k_tutorial). The dataset, freely available from 10x Genomics, comprises 2,700 peripheral blood mononuclear cells (PBMCs) sequenced on the Illumina NextSeq 500 platform.

The Read10X() function reads the output of the 10x Genomics Cell Ranger pipeline, returning a unique molecular identifier (UMI) count matrix, with genes as rows and cells as columns. Each entry represents the number of unique transcript molecules detected for a given gene in a given cell.

1. Load packages

Include all R and Bioconductor packages

library(dplyr)
library(Seurat)
library(patchwork)

2. Working directory and data resources

Set your preferred working directory and data resource

di<-"L:/MO/" # set directory path
setwd(di)  # set current directory 
url <- "https://icbi.i-med.ac.at/teaching-material/mo/" # URL to access count data
dir.create("pbmc", showWarnings = FALSE)
files <- c("matrix.mtx","genes.tsv","barcodes.tsv")
for (f in files) {
    download.file(url = paste0(url, f),destfile = file.path("pbmc", f),mode = "wb")
}

3. Data processing

The steps below encompass the standard workflow for scRNA-seq data analysis using Seurat, including cell filtering based on quality control (QC) metrics, data normalization, identification of highly variable features, and data scaling. Subsequent steps include linear dimensionality reduction (PCA), graph-based cell clustering, non-linear dimensionality reduction (UMAP or t-SNE) for visualization, identification of differentially expressed genes (cluster-specific markers), and assignment of cell type identities to clusters (cell type annotation).

pbmc.data <- Read10X(data.dir ="pbmc")

pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)
dense.size <- object.size(as.matrix(pbmc.data))
sparse.size <- object.size(pbmc.data)
pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")
VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)

head(pbmc@meta.data, 5)
plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")
plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")
plot1 + plot2

pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)
pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)
top10 <- head(VariableFeatures(pbmc), 10)
plot1 <- VariableFeaturePlot(pbmc)
plot2 <- LabelPoints(plot = plot1, points = top10, repel = TRUE)
plot1 + plot2

all.genes <- rownames(pbmc)
pbmc <- ScaleData(pbmc, features = all.genes)
pbmc <- ScaleData(pbmc, vars.to.regress = "percent.mt")
#pbmc1<-SCTransform(pbmc, vars.to.regress = "percent.mt")
pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))
print(pbmc[["pca"]], dims = 1:5, nfeatures = 5)
## PC_ 1 
## Positive:  CST3, TYROBP, LST1, AIF1, FTL 
## Negative:  MALAT1, LTB, IL32, IL7R, CD2 
## PC_ 2 
## Positive:  NKG7, PRF1, CST7, GZMA, GZMB 
## Negative:  CD79A, MS4A1, TCL1A, HLA-DQA1, HLA-DQB1 
## PC_ 3 
## Positive:  PPBP, PF4, SDPR, SPARC, GNG11 
## Negative:  HLA-DQA1, CD79A, CD79B, HLA-DQB1, HLA-DPA1 
## PC_ 4 
## Positive:  HLA-DQA1, CD79B, CD79A, MS4A1, HLA-DQB1 
## Negative:  VIM, IL7R, S100A6, S100A8, IL32 
## PC_ 5 
## Positive:  GZMB, FGFBP2, S100A8, NKG7, GNLY 
## Negative:  LTB, IL7R, CKB, MS4A7, RP11-290F20.3
VizDimLoadings(pbmc, dims = 1:2, reduction = "pca")

DimPlot(pbmc, reduction = "pca")

DimHeatmap(pbmc, dims = 1, cells = 500, balanced = TRUE)

DimHeatmap(pbmc, dims = 1:15, cells = 500, balanced = TRUE)

#pbmc <- JackStraw(pbmc, num.replicate = 100)
#pbmc <- ScoreJackStraw(pbmc, dims = 1:20)
#JackStrawPlot(pbmc, dims = 1:15)
#ElbowPlot(pbmc)
pbmc <- FindNeighbors(pbmc, dims = 1:10)
pbmc <- FindClusters(pbmc, resolution = 0.5)
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 2638
## Number of edges: 95905
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.8735
## Number of communities: 9
## Elapsed time: 0 seconds
#head(Idents(pbmc), 5)
pbmc <- RunUMAP(pbmc, dims = 1:10)
pbmc <- RunTSNE(pbmc,dims=1:10)
DimPlot(pbmc, reduction = "umap")

DimPlot(pbmc, reduction = "tsne")

saveRDS(pbmc, file = "pbmc_tutorial.rds")
cluster2.markers <- FindMarkers(pbmc, ident.1 = 2, min.pct = 0.25)
#head(cluster2.markers, n = 5)
cluster5.markers <- FindMarkers(pbmc, ident.1 = 5, ident.2 = c(0, 3), min.pct = 0.25)
#head(cluster5.markers, n = 5)
pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)
pbmc.markers %>%
  group_by(cluster) %>%
  slice_max(n = 2, order_by = avg_log2FC)
cluster0.markers <- FindMarkers(pbmc, ident.1 = 0, logfc.threshold = 0.25, test.use = "roc", only.pos = TRUE)
VlnPlot(pbmc, features = c("MS4A1", "CD79A"))

VlnPlot(pbmc, features = c("NKG7", "PF4"), slot = "counts", log = TRUE)

FeaturePlot(pbmc, features = c("MS4A1", "GNLY", "CD3E", "CD14", "FCER1A", "FCGR3A", "LYZ", "PPBP","CD8A"))

pbmc.markers %>%
  group_by(cluster) %>%
  top_n(n = 10, wt = avg_log2FC) -> top10
DoHeatmap(pbmc, features = top10$gene) + NoLegend()

new.cluster.ids <- c("Naive CD4 T", "CD14+ Mono", "Memory CD4 T", "B", "CD8 T", "FCGR3A+ Mono",
                     "NK", "DC", "Platelet")
names(new.cluster.ids) <- levels(pbmc)
pbmc <- RenameIdents(pbmc, new.cluster.ids)
DimPlot(pbmc, reduction = "umap", label = TRUE, pt.size = 0.5) + NoLegend()

saveRDS(pbmc, file = "pbmc3k_final.rds")