
Bio Single Cell Batch Integration
- 5 installs
- 1.1k repo stars
- Updated July 25, 2026
- gptomics/bioskills
Integrate multiple scRNA-seq batches using Harmony, scVI, Seurat anchors, or fastMNN to remove batch effects.
About
Integrates multiple scRNA-seq samples or batches using Harmony, scVI, Seurat anchors, and fastMNN. Developers use it to remove technical variation while preserving biological differences across datasets.
- Harmony, scVI, Seurat anchors, and fastMNN methods
- Remove technical variation while preserving biology
Bio Single Cell Batch Integration by the numbers
- 5 all-time installs (skills.sh)
- Ranked #1,598 of 2,064 Data Science & ML skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/gptomics/bioskills --skill bio-single-cell-batch-integrationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 5 |
|---|---|
| repo stars | ★ 1.1k |
| Last updated | July 25, 2026 |
| Repository | gptomics/bioskills ↗ |
What it does
Integrate multiple scRNA-seq batches using Harmony, scVI, Seurat anchors, or fastMNN to remove batch effects.
Files
Version Compatibility
Reference examples tested with: anndata 0.10+, scanpy 1.10+, scikit-learn 1.4+, scvi-tools 1.1+
Before using code patterns, verify installed versions match. If versions differ:
- Python:
pip show <package>thenhelp(module.function)to check signatures - R:
packageVersion('<pkg>')then?function_nameto verify parameters
If code throws ImportError, AttributeError, or TypeError, introspect the installed package and adapt the example to match the actual API rather than retrying.
Batch Integration
Integrate multiple scRNA-seq datasets to remove batch effects while preserving biological variation.
Tool Comparison
| Tool | Speed | Scalability | Best For |
|---|---|---|---|
| Harmony | Fast | Good | Quick integration, most use cases |
| scVI | Moderate | Excellent | Large datasets, deep learning |
| Seurat CCA/RPCA | Moderate | Good | Conserved biology across batches |
| fastMNN | Fast | Good | MNN-based correction |
Harmony (R/Python)
Goal: Remove batch effects from merged scRNA-seq datasets using Harmony's iterative correction of PCA embeddings.
Approach: Run PCA on merged data, iteratively adjust embeddings to mix batches while preserving biological variation, and use corrected embeddings for downstream analysis.
"Integrate my batches" → Merge samples, preprocess jointly, correct technical variation in the embedding space, and cluster on corrected coordinates.
R with Seurat
library(Seurat)
library(harmony)
# Merge datasets first
merged <- merge(sample1, y = list(sample2, sample3), add.cell.ids = c('S1', 'S2', 'S3'))
# Standard preprocessing
merged <- NormalizeData(merged)
merged <- FindVariableFeatures(merged)
merged <- ScaleData(merged)
merged <- RunPCA(merged)
# Run Harmony on PCA embeddings
merged <- RunHarmony(merged, group.by.vars = 'orig.ident', dims.use = 1:30)
# Use harmony embeddings for downstream
merged <- RunUMAP(merged, reduction = 'harmony', dims = 1:30)
merged <- FindNeighbors(merged, reduction = 'harmony', dims = 1:30)
merged <- FindClusters(merged, resolution = 0.5)Multiple Batch Variables
# Correct for both sample and technology
merged <- RunHarmony(merged, group.by.vars = c('sample', 'technology'),
dims.use = 1:30, max.iter.harmony = 20)Python with Scanpy
import scanpy as sc
import scanpy.external as sce
adata = sc.read_h5ad('merged.h5ad')
# Standard preprocessing
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata, batch_key='batch')
adata = adata[:, adata.var.highly_variable]
sc.pp.scale(adata)
sc.tl.pca(adata)
# Run Harmony
sce.pp.harmony_integrate(adata, key='batch')
# Use corrected embedding
sc.pp.neighbors(adata, use_rep='X_pca_harmony')
sc.tl.umap(adata)
sc.tl.leiden(adata)scVI (Python)
Goal: Integrate batches using a deep generative model that learns a shared latent space.
Approach: Train a variational autoencoder (scVI) conditioned on batch to learn batch-invariant latent representations, then use the latent space for clustering and visualization.
import scvi
import scanpy as sc
adata = sc.read_h5ad('merged.h5ad')
# Setup for scVI
scvi.model.SCVI.setup_anndata(adata, batch_key='batch')
# Train model
model = scvi.model.SCVI(adata, n_latent=30, n_layers=2)
model.train(max_epochs=100, early_stopping=True)
# Get latent representation
adata.obsm['X_scVI'] = model.get_latent_representation()
# Use for downstream
sc.pp.neighbors(adata, use_rep='X_scVI')
sc.tl.umap(adata)
sc.tl.leiden(adata)scVI with Covariates
# Include continuous covariates
scvi.model.SCVI.setup_anndata(adata, batch_key='batch',
continuous_covariate_keys=['percent_mito'])
model = scvi.model.SCVI(adata, n_latent=30)
model.train()scANVI (with cell type labels)
# If you have reference labels for some cells
scvi.model.SCANVI.setup_anndata(adata, batch_key='batch', labels_key='cell_type',
unlabeled_category='Unknown')
model = scvi.model.SCANVI(adata, n_latent=30)
model.train(max_epochs=100)
# Predict labels for unlabeled cells
adata.obs['predicted_type'] = model.predict()Seurat Integration (R)
Goal: Integrate batches using Seurat's anchor-based framework (CCA or RPCA).
Approach: Find shared biological anchors between datasets via canonical correlation analysis, then use anchors to correct expression values into a unified space.
CCA-based Integration
library(Seurat)
# Split by batch
obj_list <- SplitObject(merged, split.by = 'batch')
# Normalize each
obj_list <- lapply(obj_list, function(x) {
x <- NormalizeData(x)
x <- FindVariableFeatures(x, selection.method = 'vst', nfeatures = 2000)
return(x)
})
# Find integration anchors
anchors <- FindIntegrationAnchors(object.list = obj_list, dims = 1:30)
# Integrate
integrated <- IntegrateData(anchorset = anchors, dims = 1:30)
# Switch to integrated assay for downstream
DefaultAssay(integrated) <- 'integrated'
integrated <- ScaleData(integrated)
integrated <- RunPCA(integrated)
integrated <- RunUMAP(integrated, dims = 1:30)RPCA (Faster for Large Datasets)
# Use reciprocal PCA for faster integration
anchors <- FindIntegrationAnchors(object.list = obj_list, dims = 1:30,
reduction = 'rpca')
integrated <- IntegrateData(anchorset = anchors, dims = 1:30)Seurat v5 Integration
# Seurat v5 uses layers
merged[['RNA']] <- split(merged[['RNA']], f = merged$batch)
merged <- IntegrateLayers(merged, method = CCAIntegration, orig.reduction = 'pca',
new.reduction = 'integrated.cca')
merged <- JoinLayers(merged)fastMNN (R)
library(batchelor)
library(SingleCellExperiment)
# Convert Seurat to SCE
sce <- as.SingleCellExperiment(merged)
# Run fastMNN
corrected <- fastMNN(sce, batch = sce$batch, d = 30, k = 20)
# Extract corrected values
reducedDim(sce, 'MNN') <- reducedDim(corrected, 'corrected')Evaluate Integration
Goal: Assess whether integration successfully removed batch effects while preserving biological variation.
Approach: Compute mixing metrics (LISI, silhouette scores) and visualize batch versus cell-type separation before and after integration.
Mixing Metrics (R)
# LISI score (lower = more mixed)
library(lisi)
lisi_scores <- compute_lisi(Embeddings(merged, 'harmony'),
merged@meta.data, c('batch', 'cell_type'))
# Batch mixing should be high, cell type separation preserved
mean(lisi_scores$batch) # Want high
mean(lisi_scores$cell_type) # Want low (preserved)Visual Assessment
# Before integration
DimPlot(merged, reduction = 'pca', group.by = 'batch')
DimPlot(merged, reduction = 'pca', group.by = 'cell_type')
# After integration
DimPlot(merged, reduction = 'harmony', group.by = 'batch')
DimPlot(merged, reduction = 'harmony', group.by = 'cell_type')Silhouette Score (Python)
from sklearn.metrics import silhouette_score
# Batch silhouette (want low - batches mixed)
batch_sil = silhouette_score(adata.obsm['X_scVI'], adata.obs['batch'])
# Cell type silhouette (want high - types separated)
celltype_sil = silhouette_score(adata.obsm['X_scVI'], adata.obs['cell_type'])Complete Workflow
Goal: Run end-to-end multi-sample integration from raw 10X files to clustered, integrated UMAP.
Approach: Load and merge samples, preprocess jointly, integrate with Harmony, and perform downstream clustering on corrected embeddings.
library(Seurat)
library(harmony)
# Load and merge samples
samples <- list.files('data/', pattern = '*.h5', full.names = TRUE)
obj_list <- lapply(samples, Read10X_h5)
names(obj_list) <- gsub('.h5', '', basename(samples))
merged <- merge(CreateSeuratObject(obj_list[[1]], project = names(obj_list)[1]),
y = lapply(2:length(obj_list), function(i)
CreateSeuratObject(obj_list[[i]], project = names(obj_list)[i])))
# QC
merged[['percent.mt']] <- PercentageFeatureSet(merged, pattern = '^MT-')
merged <- subset(merged, nFeature_RNA > 200 & nFeature_RNA < 5000 & percent.mt < 20)
# Preprocess
merged <- NormalizeData(merged)
merged <- FindVariableFeatures(merged, nfeatures = 2000)
merged <- ScaleData(merged, vars.to.regress = 'percent.mt')
merged <- RunPCA(merged, npcs = 50)
# Integrate with Harmony
merged <- RunHarmony(merged, group.by.vars = 'orig.ident')
# Downstream analysis on integrated data
merged <- RunUMAP(merged, reduction = 'harmony', dims = 1:30)
merged <- FindNeighbors(merged, reduction = 'harmony', dims = 1:30)
merged <- FindClusters(merged, resolution = 0.5)
DimPlot(merged, group.by = c('orig.ident', 'seurat_clusters'), ncol = 2)When to Use Each Method
| Scenario | Recommended |
|---|---|
| Quick integration, most cases | Harmony |
| Large datasets (>500k cells) | scVI or Harmony |
| Strong batch effects | scVI |
| Reference mapping | Seurat anchors or scANVI |
| Preserving rare populations | fastMNN |
Related Skills
- single-cell/preprocessing - QC before integration
- single-cell/clustering - Clustering after integration
- single-cell/cell-annotation - Annotation after integration
- single-cell/multimodal-integration - Multi-omic integration (different from batch)
#!/usr/bin/env python3
'''Batch integration with Harmony in Scanpy'''
# Reference: anndata 0.10+, scanpy 1.10+, scikit-learn 1.4+, scvi-tools 1.1+ | Verify API if version differs
import scanpy as sc
import harmonypy as hm
import matplotlib.pyplot as plt
import sys
def integrate_with_harmony(h5ad_files, output_prefix='integrated'):
'''Integrate multiple scRNA-seq datasets with Harmony'''
print('Loading datasets...')
adatas = []
for f in h5ad_files:
adata = sc.read_h5ad(f)
adata.obs['batch'] = f.replace('.h5ad', '').split('/')[-1]
adatas.append(adata)
print('Concatenating...')
adata = sc.concat(adatas, label='batch', keys=[a.obs['batch'].iloc[0] for a in adatas])
print('Preprocessing...')
sc.pp.normalize_total(adata, target_sum=1e4)
sc.pp.log1p(adata)
sc.pp.highly_variable_genes(adata, n_top_genes=2000, batch_key='batch')
adata.raw = adata
adata = adata[:, adata.var.highly_variable]
sc.pp.scale(adata, max_value=10)
sc.tl.pca(adata, n_comps=50)
print('Running Harmony...')
ho = hm.run_harmony(adata.obsm['X_pca'], adata.obs, 'batch')
adata.obsm['X_pca_harmony'] = ho.Z_corr.T
print('Post-integration...')
sc.pp.neighbors(adata, use_rep='X_pca_harmony')
sc.tl.umap(adata)
sc.tl.leiden(adata, resolution=0.5)
print('Plotting...')
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
sc.pl.umap(adata, color='batch', ax=axes[0], show=False, title='By Batch')
sc.pl.umap(adata, color='leiden', ax=axes[1], show=False, title='By Cluster')
plt.tight_layout()
plt.savefig(f'{output_prefix}_umap.png', dpi=150)
adata.write(f'{output_prefix}.h5ad')
print(f'Saved: {output_prefix}.h5ad')
return adata
if __name__ == '__main__':
if len(sys.argv) > 1:
integrate_with_harmony(sys.argv[1:])
else:
print('Usage: python harmony_integration.py sample1.h5ad sample2.h5ad sample3.h5ad')
#!/usr/bin/env Rscript
# Reference: anndata 0.10+, scanpy 1.10+, scikit-learn 1.4+, scvi-tools 1.1+ | Verify API if version differs
# Batch integration with Harmony
library(Seurat)
library(harmony)
library(ggplot2)
# Example: integrate multiple 10X samples
# Assumes sample1.rds, sample2.rds, sample3.rds exist as Seurat objects
integrate_with_harmony <- function(sample_files, output_prefix = 'integrated') {
cat('Loading samples...\n')
samples <- lapply(sample_files, readRDS)
names(samples) <- gsub('\\.rds$', '', basename(sample_files))
cat('Merging samples...\n')
merged <- merge(samples[[1]], y = samples[-1], add.cell.ids = names(samples))
cat('Standard preprocessing...\n')
merged <- NormalizeData(merged)
merged <- FindVariableFeatures(merged, selection.method = 'vst', nfeatures = 2000)
merged <- ScaleData(merged)
merged <- RunPCA(merged, npcs = 50)
cat('Running Harmony integration...\n')
merged <- RunHarmony(merged, group.by.vars = 'orig.ident', dims.use = 1:30)
cat('Post-integration analysis...\n')
merged <- RunUMAP(merged, reduction = 'harmony', dims = 1:30)
merged <- FindNeighbors(merged, reduction = 'harmony', dims = 1:30)
merged <- FindClusters(merged, resolution = 0.5)
# Visualizations
p1 <- DimPlot(merged, reduction = 'umap', group.by = 'orig.ident') +
ggtitle('By Sample')
p2 <- DimPlot(merged, reduction = 'umap', label = TRUE) +
ggtitle('By Cluster')
pdf(paste0(output_prefix, '_umap.pdf'), width = 14, height = 6)
print(p1 + p2)
dev.off()
saveRDS(merged, paste0(output_prefix, '.rds'))
cat('Saved:', paste0(output_prefix, '.rds\n'))
return(merged)
}
# Run if executed directly
args <- commandArgs(trailingOnly = TRUE)
if (length(args) > 0) {
sample_files <- args
integrate_with_harmony(sample_files)
} else {
cat('Usage: Rscript harmony_integration.R sample1.rds sample2.rds sample3.rds\n')
}
Batch Integration - Usage Guide
Overview
Batch integration removes technical variation between samples, experiments, or technologies while preserving biological differences, enabling meaningful comparison across datasets.
Prerequisites
# Python
pip install scanpy harmonypy scvi-tools# R
install.packages('Seurat')
install.packages('harmony')
BiocManager::install('batchelor')Quick Start
Tell your AI agent what you want to do:
- "Integrate my samples to remove batch effects"
- "Run Harmony on my merged Seurat object"
- "Combine datasets from different experiments"
Example Prompts
Integration
"Merge my samples and run Harmony integration"
"Use scVI to integrate these batches"
"Run Seurat CCA integration on my samples"
Assessment
"Show batch mixing on the UMAP"
"Calculate integration metrics (LISI, kBET)"
"Are the batches well mixed within cell types?"
Comparison
"Compare Harmony vs scVI integration"
"Which method preserves cell type separation best?"
"Test different integration parameters"
Downstream
"Cluster the integrated data"
"Find markers using the integrated representation"
"Run differential expression between conditions"
What the Agent Will Do
1. Merge datasets with batch labels 2. Preprocess each batch appropriately 3. Run chosen integration method 4. Generate integrated low-dimensional representation 5. Visualize batch mixing 6. Cluster using integrated embeddings 7. Assess integration quality
Method Comparison
| Method | Speed | Best For |
|---|---|---|
| Harmony | Fast | Most use cases |
| scVI | Moderate | Large datasets, deep learning |
| Seurat CCA | Moderate | Conserved biology |
| fastMNN | Fast | MNN-based correction |
Evaluating Integration
Visual Assessment
- UMAP should show mixing of batches within cell types
- Cell types should cluster together across batches
Quantitative Metrics
- kBET: batch mixing within neighborhoods
- LISI: local inverse Simpson index
- Silhouette: cluster separation
Tips
- Preprocess each batch separately before merging
- Check cell type representation - ensure types are present across batches
- Use batch as covariate in DE - not integrated values
- Keep original counts for DE - use raw counts, not batch-corrected
- Validate integration - cell types should mix, not batch artifacts
- Harmony is a good default - fast and works well for most cases