library(knitr)
opts_chunk$set(echo = TRUE)

Brief introduction

The current implementation of SMNN encompasses two major steps: one optional clustering step and the other batch effect correction step. In the first step, SMNN takes the expression matrix as input, and performs clustering using Seurat v. 3.0 (Butler et al., 2018). Corresponding clusters/cell types are then matched across batches based on marker genes specified by the user. This entire clustering step can be by-passed by feeding SMNN cell cluster labels. With cell cluster label information, SMNN searches mutual nearest neighbors within each cell type, and performs batch effect correction using SMNN function.

In this tutorial, we will perform batch effect correction using our SMNN package on a toy example of two batches. The first batch contains 400 cells from three cell types, namely fibroblasts, macrophages and endothelial cells. And the second batches has 500 cells from the same three cell types. Both two batches contain expression information of the same 3000 genes.


Set up the library

library("SMNN")

Load the input expression matrix

data("data_SMNN")
dim(data_SMNN$batch1.mat)
data_SMNN$batch1.mat[1:5, 1:5]
dim(data_SMNN$batch2.mat)

The optional clustering step

Provide cell-type specific marker gene information

Two pieces of information are needed: - Marker genes - Corresponding cell type labels for each marker gene

# Maker genes
markers <- c("Col1a1", "Pdgfra", "Ptprc", "Pecam1")
# Corresponding cell type labels for each marker gene
cluster.info <- c(1, 1, 2, 3)

Harmonize cluster labels across batches

The function unifiedClusterLabelling is used to match/harmonize the clusters/cell type labels across multiple scRNA-seq batches. It takes as input raw expression matrices from two or more batches, a list of marker genes and their corresponding cluster labels, and outputs harmonized cluster label for every single cells across all batches.

matched_clusters <- unifiedClusterLabelling(data_SMNN$batch1.mat, data_SMNN$batch2.mat, features.use = markers, cluster.labels = cluster.info, min.perc = 0.3)

The batch effect correction step using SMNNcorrect function

Set python version to be compatible with SMNNcorrect implementation

In the SMNNcorrect function, we call several python functions from mnnpy package at chriscainx/mnnpy to speed up the computation.

Package mnnpy can be installed with pip install mnnpy,

or

git clone https://github.com/chriscainx/mnnpy.git
cd mnnpy
pip install .

To ensure successful execution of these python functions, we need to first set the python version (we recommand python3 for SMNN version 0.99).

library(reticulate)

# please change the path below to your local path for python
use_python("/nas/longleaf/apps/python/3.5.1/bin/python3")

Perform batch effect correction

With harmonized cluster label information for single cells across batches, we perform batch effect correction. Specifically, we apply cosine normalization on both input and output data and set the number of mutual nearest neighbors at 20.

corrected.results <- SMNNcorrect(batches = list(data_SMNN$batch1.mat, data_SMNN$batch2.mat), batch.cluster.labels = matched_clusters, matched.labels=c(1,2,3), k=20, sigma=1, cos.norm.in=TRUE, cos.norm.out=TRUE)

SMNNcorrect function will output the following information: (1) the batch-corrected expression matrix for each batch; and (2) information regarding mutual nearest neighbors.

In the example below, we treat batch 1 as the reference batch and batch 2 as the batch to be corrected (such that batch 2 will be corrected towards the reference batch 1). Note that the reference batch (i.e., batch 1 in our example) will only applied cosine normalization.

# Output after correction for batch 
## Output (1): the batch-corrected expression matrix
corrected.results$corrected[[2]][1:10,1:10]
## Output (2): mutual nearest neighbor information
corrected.results$pairs[[2]]

Credits

Some functions are borrowed from or executed according to the orginal scran/MNN package (Haghverdi et al., 2018) and chriscainx/mnnpy (https://github.com/chriscainx/mnnpy).




yycunc/SMNN documentation built on Dec. 29, 2021, 12:17 p.m.