Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Correcting batch effects with Harmony

Cells sequenced in different batches can separate by batch instead of by cell type, and every result built on their neighbor graph inherits the split. This page uses Harmony to correct the sequencing-batch effect in 9,000 human peripheral blood mononuclear cells (PBMCs) from a rheumatoid arthritis study, and measures what the correction changes. Without correction, batch 3 forms its own island of every major cell type, and the cells of a control donor sequenced in all three batches split the same way, which shows that the split is technical. Harmony converges in 12 rounds and raises batch mixing (iLISI) from 0.060 to 0.211, 77.5% of the score that fully mixed batches reach on the same graph. Cell-type separation (cLISI, 0.998 to 0.995) and the connectivity of each cell type in the graph (0.969 to 0.960) barely change. Selecting the 1,000 most variable genes within each batch, instead of across the pooled cells, keeps 441 of the pooled genes and does not improve mixing (iLISI 0.209 with Harmony).

Harmony Korsunsky et al., 2019 corrects the principal components, not the counts. Each round clusters the cells softly in PCA space, penalizing clusters whose batch mix departs from that of all cells, then subtracts from each cell its batch’s offset in each cluster, weighted by the cell’s membership. CyteArc implements the Harmony2 algorithm Patikas et al., 2026, which the integration API reference describes in full. The corrected coordinates replace the PCA coordinates in the neighbor graph, the UMAP and every result built on the graph.

The data come from the rheumatoid arthritis study of Binvignat et al. (2024), published as a CELLxGENE collection: 108,717 PBMCs and 21,648 genes from 18 patients and 18 controls, sequenced in three batches. The donor-level comparisons tutorial uses the same file to compare patients with their matched controls.

Prepare the local source store

The code downloads the versioned H5AD file once, checks that its raw/X matrix has the published numbers of cells and genes and that the values it samples are whole, non-negative counts, and converts the counts, within a 6 GiB memory budget, to a CyteArc store that later sessions reuse. Import and export explains the conversion in detail.

Started: 2026-11-18T00:15:53.629028Z | Wall time: 1.484 s

{'count matrix': 'raw/X', 'cells': 108717, 'genes': 21648, 'integer counts': True}

Started: 2026-11-18T00:15:55.115347Z | Wall time: 1.958 s

Started: 2026-11-18T00:15:57.076048Z | Wall time: 19.435 s

A mount is a writable store that reads its counts from another store. The cell selection and the pipeline runs below go to a mount in a temporary folder, so the converted store stays unchanged for later sessions; the remote dataset mounting tutorial introduces mounts. Mounting needs the converted store to have been opened once with DataStore, and min_features_per_cell=0 keeps every cell with at least one detected gene.

{'cells': 108717, 'genes': 21648}

Started: 2026-11-18T00:16:16.513339Z | Wall time: 3.192 s

Sample cells evenly across batches and conditions

The three batches hold different numbers of cells and donors from each condition:

cellsdonors
conditionnormalrheumatoid arthritisnormalrheumatoid arthritis
batch
1190112163977
217989804353
3230801895588

Started: 2026-11-18T00:16:19.707811Z | Wall time: 0.079 s

Batch 2 holds 17,989 control cells but only 8,043 patient cells, from 5 control and 3 patient donors; batches 1 and 3 are closer to even. Every donor was sequenced in a single batch except one:

{'donors': 36, 'donors per condition': {'normal': 18, 'rheumatoid arthritis': 18}, 'donor in several batches': 'Control07_1_C_F', 'its cells per batch': {'1': 2831, '2': 2080, '3': 4455}}

Started: 2026-11-18T00:16:19.789360Z | Wall time: 0.035 s

Control donor Control07_1_C_F has 2,831, 2,080 and 4,455 cells in batches 1, 2 and 3. The sections below call this donor the bridging donor and follow its cells through the correction.

Drawing 1,500 cells from every batch and condition gives a 9,000-cell analysis in which each batch is half patients and half controls. Uneven shares, such as those of batch 2, would tie part of the difference between the conditions to the batch, and correcting the batch would then shrink that difference. A seeded generator draws the same cells in every session.

conditionnormalrheumatoid arthritis
batch
115001500
215001500
315001500

Started: 2026-11-18T00:16:19.825851Z | Wall time: 0.242 s

Run matched uncorrected and Harmony pipelines

ds.pipeline.rna runs CyteArc’s RNA recipe on the cells of the balanced_sample column and returns a pipeline run: a frozen record of the cells, settings and results of every stage, which later calls index by output name, such as run["neighbors"]. Both runs keep the recipe’s defaults for gene selection, normalization, PCA, the neighbor graph and the UMAP. filtering=False analyzes the sampled cells as they are, and clustering, cell-cycle scoring, doublet detection and marker ranking are off, because the comparison needs only the graph and the layout. snapshot_columns freezes each cell’s batch, condition, broad cell type and donor into the run for the plots and scores below.

{'analysis cells': 9000}

Started: 2026-11-18T00:16:20.069529Z | Wall time: 38.555 s

The second run adds harmony={"batch_columns": ["batch"]}, which inserts a Harmony stage between PCA and the neighbor graph and keeps every other setting of the first run. The run’s report records how many rounds Harmony ran and whether it converged.

{'analysis cells': 9000, 'rounds': 12, 'converged': True}

Started: 2026-11-18T00:16:58.626800Z | Wall time: 14.628 s

Harmony converged in 12 rounds, and both runs analyzed the same 9,000 cells.

Compare the layouts before and after correction

Each figure colors one run’s UMAP by sequencing batch and by rough_annot, the broad cell types of the published annotation.

Two UMAP panels of the 9,000 uncorrected cells, colored by sequencing batch (left) and by published broad cell type (right). Batches 1 and 2 overlap in every island, while batch 3 forms separate islands of monocytes at the top right, B cells at the center right, CD4 T cells at the lower right and NK and CD8 T cells at the bottom. The batch 3 CD4 T-cell island is dotted with cells that the annotation labels as monocytes.

Started: 2026-11-18T00:17:13.256123Z | Wall time: 0.457 s

Without correction, batches 1 and 2 share each island, and batch 3 forms separate islands of monocytes, B cells, CD4 T cells, and NK and CD8 T cells. Every cell type splits the same way, which points to a technical difference between the batches rather than a population present only in batch 3.

Two UMAP panels of the 9,000 cells after Harmony, colored by sequencing batch (left) and by published broad cell type (right). Every island holds cells from all three batches, but batch 3 is nearly absent from the left edge of the CD4 T-cell island and fills the lower lobe of the monocyte island. B cells sit at the top, CD4 T cells on the left, NK and CD8 T cells below them and monocytes on the right; cells labeled as monocytes remain scattered inside the CD4 T-cell island.

Started: 2026-11-18T00:17:13.714740Z | Wall time: 0.363 s

After Harmony, every island holds cells from all three batches, and the cell types keep their own regions. The mixing is incomplete: batch 3 is nearly absent from the left edge of the CD4 T-cell island and gathers along the lower edge of the monocyte island, a remainder that the scores below measure.

Check the batch split within one donor

Apart from the bridging donor, each batch holds different donors, so the split could reflect the people in batch 3 rather than the batch itself. The bridging donor separates the two explanations, because all of its cells come from one person. The figure draws only its 678 sampled cells on each layout.

Two UMAP panels showing only the 704 sampled cells of control donor Control07_1_C_F, colored by sequencing batch, on the uncorrected layout (left) and the Harmony layout (right). Without correction, the donor's batch 3 cells form separate groups beside its overlapping batch 1 and 2 cells. After Harmony, all three batches fall in the same groups, but the donor's batch 3 cells still gather toward the right of the T-cell group and at the lower edge of the monocyte group, while its batch 1 and 2 cells fill the left of the T-cell group and the upper part of the monocyte group.

Started: 2026-11-18T00:17:14.080061Z | Wall time: 0.350 s

Without correction, the bridging donor’s batch 3 cells sit apart from its batch 1 and 2 cells in every cell type, as the batches do across all donors. After Harmony, its three batches share each island, but its batch 3 cells still sit to one side of the T-cell and monocyte islands.

Each pipeline stage saves its result as an artifact, and run["neighbors"] returns an artifact reference: a handle to the run’s saved neighbor table. ds.artifacts.load opens the table’s arrays, and the next cell uses them to measure the split for every cell: the fraction of the cell’s 11 nearest neighbors that come from its own batch. With three equal batches, fully mixed neighborhoods would share a cell’s batch about a third of the time.

neighbors per cellall 9,000 cellsControl07_1_C_F (678 cells)
Uncorrected110.690.73
Harmony110.420.45

Started: 2026-11-18T00:17:14.432476Z | Wall time: 0.059 s

Without correction, 69% of a cell’s neighbors share its batch on average, and 73% for the bridging donor’s cells; after Harmony, 42% and 45%. The bridging donor’s cells move the same way as the whole sample, which ties the split to the batch rather than to the donors. Both values after Harmony stay above the third that full mixing would give, which matches the uneven batch 3 shares within the CD4 T-cell and monocyte islands.

Score batch mixing and cell-type separation

Three scores from the scIB benchmark of integration methods Luecken et al., 2021 summarize each graph, each scaled so that higher is better:

  • iLISI scores batch mixing: the effective number of batches among a cell’s 11 neighbors, weighted by distance so that the closest few count most, as a median over cells, rescaled so that 0 means one batch and 1 means all three batches in equal shares.

  • cLISI scores cell-type separation: the same calculation on rough_annot, inverted so that 1 means each cell’s neighbors all carry a single type.

  • Graph connectivity checks that each cell type stays in one piece: for each type, the fraction of its cells in the largest connected piece of the graph drawn among that type’s cells, averaged over types.

iLISI (batch)cLISI (rough_annot)graph connectivity (rough_annot)
Uncorrected0.0600.9980.969
Harmony0.2110.9950.960

Started: 2026-11-18T00:17:14.493369Z | Wall time: 0.985 s

Harmony raises iLISI more than threefold, from 0.060 to 0.211, while cLISI moves from 0.998 to 0.995 and graph connectivity from 0.969 to 0.960: the batches mix more, and the cell types stay separate and connected.

iLISI stays below 1 even for fully mixed batches, because the closest few neighbors that dominate each cell’s score rarely hold all three batches in equal shares. The next cell sets the ceiling for these graphs: it shuffles the batch labels among cells of the same type, which keeps each type’s batch shares and removes any other batch structure, and scores both graphs with the shuffled labels.

iLISIiLISI, shuffled batchesobserved / shuffled
Uncorrected0.0600.2740.219
Harmony0.2110.2730.775

Started: 2026-11-18T00:17:15.480651Z | Wall time: 0.092 s

Shuffled batches reach an iLISI of 0.274 on the uncorrected graph and 0.273 on the Harmony graph. Measured against those ceilings, the uncorrected graph reaches 21.9% of full mixing and the Harmony graph 77.5%.

Select variable genes within each batch

Both runs above use one pooled gene selection: the 1,000 genes that vary most across all 9,000 cells. A gene whose level differs between batches varies across the pooled cells even when it varies little within each batch, so a pooled selection can favor genes that separate the batches. With batch="batch", features.hvgs ranks the genes within each batch and selects the genes that the most batches rank among their top 1,000, as the highly variable genes page describes. Each gene’s batch_count records how many batches rank it among their top 1,000.

pooled genes 1000 batch-aware genes 1000 shared genes 441 Name: gene count, dtype: int64

Started: 2026-11-18T00:17:15.574748Z | Wall time: 3.173 s

all genespooled genesbatch-aware genes
batches ranking the gene in their top 1,000
0191372660
12080492569
2373195373
3584758

Started: 2026-11-18T00:17:18.749302Z | Wall time: 0.009 s

The two selections share 441 genes. Of the 58 genes that all three batches rank among their top 1,000, the pooled selection keeps 47, and of the 373 that two batches rank there, 195. It fills 266 places with genes that no batch ranks among its top 1,000, which rank as variable only when the batches are pooled. The batch-aware selection takes all 58 and all 373, then fills its remaining 569 places with the best-ranked of the 2,080 genes that only one batch ranks among its top 1,000. The pooled selection holds 492 of those 2,080.

hvg={"batch": "batch"} gives a run this selection. A different gene set changes the normalized values, the PCA, the Harmony correction and the graph, so the run below repeats the whole analysis on the same cells.

Two UMAP panels of the 9,000 cells after Harmony with batch-aware gene selection, colored by sequencing batch (left) and by published broad cell type (right). B cells form the top island, CD4 T cells a large island below them, NK and CD8 T cells a strand underneath, and monocytes a separate island at the bottom right. The three batches overlap in every island, but batch 3 gathers at the lower edge of the monocyte island and at the tip of the NK and CD8 T-cell strand; cells labeled as monocytes remain scattered inside the CD4 T-cell island.

Started: 2026-11-18T00:17:18.760131Z | Wall time: 17.829 s

iLISI (batch)cLISI (rough_annot)graph connectivity (rough_annot)
Uncorrected0.0600.9980.969
Harmony, pooled genes0.2110.9950.960
Harmony, batch-aware genes0.2090.9990.914

Started: 2026-11-18T00:17:36.591697Z | Wall time: 0.436 s

With batch-aware genes, the layout separates the same cell types as the pooled Harmony layout, and batch 3 again gathers at one edge of the monocyte island, here its lower right edge. iLISI falls from 0.211 to 0.209 and graph connectivity from 0.960 to 0.914, while cLISI rises from 0.995 to 0.999. On these cells, selecting genes within each batch does not improve mixing, and the cell types stay separate with either selection. The lower connectivity means that, averaged over cell types, a smaller share of each type’s cells sits in the type’s largest connected piece of the graph.

Batch-aware selection fills its places with the genes that several batches share before any gene that only one batch ranks among its top 1,000. When one batch holds a cell state that the others lack, only that batch ranks the state’s marker genes among its top 1,000, so the markers compete for the places left over and can miss the selection, and scores on broad cell types would not show the loss.

The reference mapping and label transfer tutorial takes the other route to combining datasets: it maps new cells onto a fixed reference instead of correcting a joint analysis.

References
  1. Korsunsky, I., Millard, N., Fan, J., Slowikowski, K., Zhang, F., Wei, K., Baglaenko, Y., Brenner, M., Loh, P., & Raychaudhuri, S. (2019). Fast, sensitive and accurate integration of single-cell data with Harmony. Nature Methods, 16(12), 1289–1296. 10.1038/s41592-019-0619-0
  2. Patikas, N., Yao, H., Madhu, R., Raychaudhuri, S., Hemberg, M., & Korsunsky, I. (2026). Integration of large, complex single-cell datasets with Harmony2. bioRxiv. 10.64898/2026.03.16.711825
  3. Binvignat, M., Miao, B. Y., Wibrand, C., Yang, M. M., Rychkov, D., Flynn, E., Nititham, J., Tamaki, W., Khan, U., Carvidi, A., Krueger, M., Niemi, E., Sun, Y., Fragiadakis, G. K., Sellam, J., Mariotti-Ferrandiz, E., Klatzmann, D., Gross, A. J., Ye, C. J., … Sirota, M. (2024). Single-cell RNA-Seq analysis reveals cell subsets and gene signatures associated with rheumatoid arthritis disease activity. JCI Insight, 9(16). 10.1172/jci.insight.178499
  4. Luecken, M. D., Büttner, M., Chaichoompu, K., Danese, A., Interlandi, M., Mueller, M. F., Strobl, D. C., Zappia, L., Dugas, M., Colomé-Tatché, M., & Theis, F. J. (2021). Benchmarking atlas-level data integration in single-cell genomics. Nature Methods, 19(1), 41–50. 10.1038/s41592-021-01336-8