Batch correction for single-cell data using the Harmony algorithm with a C++ Armadillo backend.
Maintained. Last release 8 weeks ago, commits in 5 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install harmonypyharmonypy
harmonypy is a Python package for the Harmony algorithm for integrating multiple high-dimensional datasets. It uses a multithreaded C++ backend that follows the R harmony2 package step-by-step.
This animation shows Harmony aligning three single-cell RNA-seq datasets from different donors. → How to make this animation. Before Harmony, you can clearly distinguish cells from each of the three donors. After Harmony, the cells from different donors are mixed while preserving the overall shape of the data.
Installation
Install from PyPI (pre-built wheels for Linux and macOS):
pip install harmonypy
Building from source
Building from source requires a C++17 compiler and the Python development
headers (python3-dev on Debian and Ubuntu, python3-devel on Fedora);
harmonypy does not use BLAS or LAPACK. CMake is installed during the build if
it is missing, and the Armadillo headers are downloaded (which needs network
access) if Armadillo is not installed:
pip install .
Quick Start
import harmonypy as hm
import pandas as pd
# Load the principal components and metadata
pcs = pd.read_csv("data/pbmc_3500_pcs.tsv.gz", sep="\t")
meta = pd.read_csv("data/pbmc_3500_meta.tsv.gz", sep="\t")
# Run Harmony to correct for batch effects (donor)
harmony_out = hm.run_harmony(pcs, meta, "donor")
# Save corrected PCs (same shape as input)
result = pd.DataFrame(harmony_out.Z_corr, columns=pcs.columns)
result.to_csv("pbmc_3500_pcs_harmony.tsv", sep="\t", index=False)
