Skip to content
Eliezyer edited this page Mar 28, 2026 · 8 revisions

Generalized Contrastive PCA

Generalized contrastive PCA (gcPCA) is a dimensionality reduction method for contrasting datasets. It identifies low-dimensional patterns that are enriched in one experimental condition relative to another. Unlike PCA, which operates on a single dataset, gcPCA enables direct comparisons across conditions.

This repository provides open-source implementations of gcPCA in Python, MATLAB, and R, along with variants designed for different data types.

For full methodological details, see our paper:
Identifying patterns differing between high-dimensional datasets with generalized contrastive PCA
PLOS Computational Biology
https://doi.org/10.1371/journal.pcbi.1012747


Key Features

  • Hyperparameter-free — No manual tuning required
  • Symmetric comparison — Both conditions treated equally
  • Sparse solutions — Improved interpretability
  • Multi-language support — Python, MATLAB, and R

Installation

Python

Create an environment and install:

conda create --name gcPCA python>=3.9
conda activate gcPCA
pip install generalized_contrastive_PCA

Or install from this repository

conda env create -f environment.yml

R

install.packages("remotes")
remotes::install_github(
  "SjulsonLab/generalized_contrastive_PCA",
  subdir = "R_package"
)

MATLAB

MATLAB implementation is available in:

matlab/gcPCA

Just add the folder to the path.


Quick Example

Python

from generalized_contrastive_PCA import gcPCA

model = gcPCA(method="v4")
model.fit(Ra, Rb)

R

library(gcpca)

fit <- gcPCA(Ra, Rb, method = "v4")
pred <- predict(fit)

MATLAB

[B, S, X] = gcPCA(Ra, Rb, 4);

Citing gcPCA

If gcPCA is useful in your work, please cite:

de Oliveira, E. F., Garg, P., Hjerling-Leffler, J., Batista-Brito, R., & Sjulson, L. (2025).
Identifying patterns differing between high-dimensional datasets with generalized contrastive PCA.
PLOS Computational Biology.
https://doi.org/10.1371/journal.pcbi.1012747


Support

If you encounter issues or have suggestions for improvement, please open a GitHub issue.

You can also contact the authors of the gcPCA manuscript for questions or feedback.

If you find this project helpful, consider giving the repository a ⭐

Clone this wiki locally