Inferensys

Glossary

Harmony

An iterative clustering algorithm that projects cells into a shared embedding where it softly clusters cells by both cell type and dataset of origin to correct for batch effects in single-cell RNA sequencing data.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
SINGLE-CELL DATA INTEGRATION

What is Harmony?

Harmony is an iterative algorithm for integrating single-cell RNA sequencing datasets that projects cells into a shared, low-dimensional embedding while simultaneously clustering them by cell type and dataset of origin to correct for batch effects.

Harmony is an iterative clustering algorithm that projects cells from multiple batches into a shared embedding where it softly clusters cells by both cell type and dataset of origin to correct for batch effects in single-cell RNA sequencing data. Unlike methods that rely on identifying mutual nearest neighbors (MNN) or a single reference batch, Harmony uses a probabilistic clustering model that iteratively estimates a dataset-specific linear correction factor for each cell, pushing cells from different batches toward shared cluster centroids while preserving genuine biological variation.

The algorithm begins with a low-dimensional embedding, typically from Principal Component Analysis (PCA) , and alternates between two steps: maximum-likelihood clustering of cells using a soft k-means framework and estimating a batch-specific linear correction for each cluster. This correction is applied to the cell embeddings, and the process repeats until convergence. Harmony's performance is often evaluated using the Local Inverse Simpson's Index (LISI) , which quantifies the effective number of datasets in a cell's local neighborhood, with a high integration LISI score indicating successful mixing of batches without overcorrection that would obscure true cell-type distinctions.

ALGORITHM MECHANICS

Key Features of Harmony

Harmony is an iterative algorithm that projects single-cell RNA-seq data into a shared, batch-corrected embedding. It uses a novel soft-clustering strategy that groups cells by both cell type and dataset of origin, then applies a dataset-specific linear correction to align populations.

01

Soft k-Means Clustering

Harmony uses a soft k-means clustering approach that assigns cells to multiple clusters probabilistically rather than making hard assignments. This allows cells to belong to a cell-type cluster and a dataset-specific cluster simultaneously.

  • Penalizes clusters that are dominated by a single dataset
  • Encourages mixing of cells from different batches within each cluster
  • The soft assignment matrix is used to compute a maximum diversity clustering
02

Maximum Diversity Clustering

The algorithm applies a diversity penalty to the soft clustering objective. For each cluster, Harmony calculates the proportion of cells from each dataset and penalizes clusters where one dataset dominates.

  • Uses an entropy-based regularization term
  • The penalty strength is controlled by a parameter theta (θ)
  • High theta forces aggressive mixing; low theta preserves biological structure
  • This prevents the algorithm from simply grouping cells by batch
03

Linear Mixture Model Correction

Once cells are softly clustered, Harmony estimates a dataset-specific linear correction factor for each cluster. The correction is computed as the difference between the cluster centroid and the dataset-specific centroid within that cluster.

  • Correction is a simple shift vector applied to each cell
  • Preserves the relative distances between cells within a cluster
  • Avoids the overcorrection problems of global alignment methods
  • Correction is applied iteratively until convergence
04

Iterative Expectation-Maximization

Harmony operates in an expectation-maximization (EM) framework that alternates between two steps until convergence:

  1. E-step: Softly assign cells to clusters based on current corrected coordinates
  2. M-step: Re-estimate cluster centroids and dataset-specific correction factors
  • Typically converges in 5-10 iterations
  • The algorithm is deterministic given the same initialization
  • Output is a batch-corrected embedding matrix suitable for downstream analysis
05

Integration Metrics

Harmony's performance is evaluated using quantitative metrics that assess both batch mixing and biological preservation:

  • Local Inverse Simpson's Index (LISI): Measures the effective number of datasets in each cell's local neighborhood. A high integration LISI (iLISI) indicates good mixing
  • Average Silhouette Width (ASW): Evaluates whether cells of the same type remain close together after correction
  • kBET acceptance rate: Tests whether the local batch distribution matches the global distribution
  • Harmony consistently achieves near-1.0 kBET acceptance rates on benchmark datasets
06

Scalability and Performance

Harmony is designed for large-scale single-cell datasets with hundreds of thousands to millions of cells. Its linear-time complexity makes it suitable for atlas-scale integration projects.

  • Runtime scales linearly O(N) with the number of cells
  • Processes 100,000 cells in under 2 minutes on standard hardware
  • Implemented in both R (harmony package) and Python (harmonypy)
  • Integrates seamlessly with Seurat, Scanpy, and SingleCellExperiment workflows
  • Memory-efficient implementation uses sparse matrix operations
METHOD COMPARISON

Harmony vs. Other Batch Correction Methods

A feature-level comparison of Harmony against widely used batch correction algorithms for single-cell RNA sequencing data integration.

FeatureHarmonySeurat IntegrationMNNScanoramaBBKNN

Core Algorithm

Iterative soft k-means clustering in PCA space

CCA-based anchor identification and integration

Mutual nearest neighbor detection and correction vector estimation

Panoramic stitching via pairwise MNN matching

Batch-balanced k-nearest neighbor graph construction

Input Data

PCA embeddings

Normalized expression matrix

High-dimensional expression space

Normalized expression matrix

PCA or expression matrix

Scalability

Linear in cell number; handles 10^6 cells

Moderate; anchor finding is computationally intensive

O(N^2) pairwise distance calculations

O(N^2) pairwise comparisons across all datasets

Efficient; approximate nearest neighbors via annoy

Preserves Batch Structure

Output Type

Corrected PCA embeddings

Integrated expression matrix and corrected embeddings

Corrected expression matrix

Corrected expression matrix

Corrected kNN graph only

Requires Cell Type Labels

Handles >2 Batches Natively

Runtime for 100k Cells

< 1 min

5-15 min

10-30 min

30-60 min

< 1 min

Sensitivity to Batch Order

HARMONY ALGORITHM DEEP DIVE

Frequently Asked Questions

Clarifying the mechanics, applications, and evaluation of the Harmony algorithm for robust single-cell data integration.

The Harmony algorithm is an iterative process that projects single-cell RNA sequencing data into a shared, batch-corrected embedding by softly clustering cells using both cell-type identity and dataset-of-origin labels. It begins with a low-dimensional representation, such as from Principal Component Analysis (PCA) , and iteratively refines it. In each iteration, Harmony performs a soft k-means clustering that groups cells by their transcriptional similarity while penalizing clusters that are dominated by a single batch. It then calculates a linear, dataset-specific correction factor for each cluster and moves each cell toward the cluster centroid by its batch-specific factor. This process repeats until convergence, effectively removing the technical variation introduced by different experimental batches while preserving the true biological heterogeneity between distinct cell types and states.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.