
Run Harmony
harmony_sc.RdHarmony v2 by Patikas et al., 2026, implemented in Rust. Performs batch
correction on PCA embeddings and stores the result as a "harmony"
embedding in the object. The original Harmony by Korsunsky et al. is
harmony_v1_sc().
Usage
harmony_sc(
object,
batch_column,
additional_batch_columns = NULL,
embd_to_use = "pca",
modality = c("rna", "adt", "atac"),
harmony_params = params_sc_harmony_v2(),
seed = 42L,
.verbose = TRUE
)Arguments
- object
SingleCellsorSingleCellsSubsetclass.- batch_column
String. Column name in the object containing the primary batch labels.
- additional_batch_columns
Optional character vector. Additional batch columns to regress out. If
NULL, only the primary batch column is used.- embd_to_use
String. The embedding to correct.
"pca"(default) or another cached embedding of the modality, e.g."lsi"withmodality = "atac".- modality
String. One of
c("rna", "adt", "atac")."adt"needs aSingleCellsMultiModal,"atac"aSingleCellsMultiModalor aSingleCellsAtac.- harmony_params
List. Output of
params_sc_harmony_v2().- seed
Integer. For reproducibility.
- .verbose
Boolean or integer. Controls verbosity and returns run times.
FALSE-> quiet,TRUEor1L-> normal verbosity,2L-> detailed verbosity.
Value
The object with a "harmony" embedding added. If no PCA
embeddings are found, returns the object unchanged with a warning.
Examples
# Harmony correction of the PCA embedding
sc <- demo_single_cells(
syn_data_params = params_sc_synthetic_data(
n_cells = 600L, n_genes = 50L, n_batches = 3L
)
)
sc <- harmony_sc(sc, batch_column = "batch_index", .verbose = FALSE)
dim(get_embedding(sc, "harmony"))
#> [1] 600 10
unlink(sc@dir_data, recursive = TRUE, force = TRUE)