Batch Correction Tutorial

Author: Hui Ma, Yiming Yang
Date: 2020-10-23
Notebook Source: batch_correction.ipynb

Dataset

In this tutorial, we'll use a gene-count matrix dataset on human bone marrow from 8 donors, and show how to use the batch correction methods in Pegasus to tackle the batch effects in the data.

The dataset is stored at https://storage.googleapis.com/terra-featured-workspaces/Cumulus/MantonBM_nonmix_subset.zarr.zip. You can also use gsutil to download it via its Google bucket URL (gs://terra-featured-workspaces/Cumulus/MantonBM_nonmix_subset.zarr.zip).

Now load the count matrix:

'Channel' is the batch key. Each batch is associated with one donor, so there are 8 batches in total.

Sections

Preprocessing

First, preprocess the data. This includes filtration, selecting robust genes, and log-normalization:

After quality-control, distribution of cells in all the 8 batches is:

Clustering without Correcting Batch Effects

We first perform downstream steps without considering batch effects. In this way, you can see where the batch effects exist, and moreover, we'll use this result as the baseline when comparing different batch correction methods.

In this tutorial, the downstream steps consists of:

Let's have a quick look at the UMAP plot above. If checking the cells in Louvain cluster 10 and 13 in the right-hand-side plot, you can see that most of them are from sample MantonBM3_HiSeq_1. This indicates strong batch effects.

Batch Correction Methods

Batch effect occurs when data samples are generated in different conditions, such as date, weather, lab setting, equipment, etc. Unless informed that all the samples were generated under the similar condition, people may suspect presumable batch effects if they see a visualization graph with samples kind-of isolated from each other.

In this tutorial, you'll see how to apply the batch correction methods in Pegasus to this dataset.

As a common step ahead, we need to re-select HVGs considering batch effects:

Harmony Algorithm

Harmony is a widely-used method for data integration. Pegasus uses harmony-pytorch package to perform Harmony batch correction.

Harmony works on PCA matrix. So we need to first calculate the original PCA matrix:

Now we are ready to run Harmony integration:

When finished, the corrected PCA matrix is stored in data_harmony.obsm['X_pca_harmony'], and run_harmony returns the representation key 'pca_harmony' as variable harmony_key. In the downstream steps, you can set rep parameter to either harmony_key or 'pca_harmony' in Pegasus functions whenever applicable.

For details on parameters of run_harmony other than the default setting, please see here.

With the new corrected PCA matrix, we can perform kNN-graph-based clustering and calculate UMAP embeddings as follows:

Then show UMAP plot:

L/S Adjustment Method

Pegasus also provides a canonical Location and Scale (L/S) Adjustment method (See reference) for batch correction. Different from Harmony, L/S method modifies the log-normalized count matrix. It is faster, and works well on large-scale datasets.

Pegasus uses this method for batch correction in Cumulus paper.

After HVG selection, directly run correct_batch function to perform L/S batch correction:

In correct_batch function, features parameter specifies which genes/features to consider in batch correction. By default, it considers all the features. Here, as we've already selected a HVG set, we can assign its key in data_ls.var field, which is 'highly_variable_features', to this parameter.

As shown above, the corrected count matrix is stored at adata.uns['fmt_highly_variable_features'], with dimension cell-by-HVG.

See its documnetation for customization.

Now we can perform the downstream analysis:

Scanorama Algorithm

Scanorama is another widely-used batch correction algorithm. Pegasus uses scanorama package.

Similarly as Harmony, Scanorama corrects the original PCA matrix of the dataset. But as PCA step is already integrated in run_scanorama function, we don't need to run pca seperately:

You can check details on run_scanorama parameters here.

By default, it considers count matrix only regarding the selected HVGs, and calculates the corrected PCA matrix of $50$ PCs. When finished, this new PCA matrix is stored in data_scan.obsm['X_scanorama'], and returns its representation key 'scanorama' as variable scan_key. In the downstream steps, you can set rep parameter to either scan_key or 'scanorama' in Pegasus functions whenever applicable:

Now check its UMAP plot:

Comparing Batch Correction Methods

To compare the performance on the three methods, one metric is runtime, which you can see from the logs in sections above: L/S Adjustment method is the fastest, then Harmony, and Scanorama is the slowest.

In this section, we'll use 2 other metrics for comparison:

We have 4 results: No batch correction (Baseline), Harmony, L/S, and Scanorama. For each result, kBET and kSIM acceptance rates are calculated on its 2D UMAP coordinates for comparison, which is consistent with Cumulus paper.

Details on these 2 metrics can also be found in Cumulus paper.

kBET Acceptance Rate

We can use calc_kBET function to calculate on kBET acceptance rates. Besides,

kSIM Acceptance Rate

We need pre-annotated cell type information as ground truth to calculate kSIM acceptance rate. This is achieved by:

This ground truth is stored at https://storage.googleapis.com/terra-featured-workspaces/Cumulus/cell_types.csv, or we can get it using gsutil from its Google bucket URL (gs://terra-featured-workspaces/Cumulus/cell_types.csv):

Now load this file, and attach its 'cell_types' column to the 4 resulting count matrices above:

We can then use calc_kSIM function to calculate kSIM acceptance rates. Besides,

Performance Plot

Now draw a scatterplot regarding these two metrics on the 4 results:

As this plot shows, a trade-off exists between good mixture of cells (in terms of kBET acceptance rate) and maintaining the biology well (in terms of kSIM acceptance rate). Harmony method achieves the best mixture of cells, while its consistency with the ground truth biology is the least. L/S and Scanorama both have a better balance between the two measurements.

Therefore, in general, the choice of batch correction method really depends on the dataset and your analysis goal.