PyDESeq2
Overview
PyDESeq2 is a Python implementation of DESeq2 for differential expression analysis with bulk RNA-seq data. It supports complete workflows from data loading through result interpretation, including single-factor and multi-factor designs, Wald tests with multiple-testing correction, optional apeGLM shrinkage, and integration with pandas and AnnData.
When to Use This Skill
Use this skill when:
- Analyzing bulk RNA-seq count data for differential expression
- Comparing gene expression between experimental conditions (e.g., treated vs control)
- Performing multi-factor designs accounting for batch effects or covariates
- Converting R-based DESeq2 workflows to Python
- Integrating differential expression analysis into Python-based pipelines
- Users mention "DESeq2", "differential expression", "RNA-seq analysis", or "PyDESeq2"
Installation and Requirements
uv pip install "pydeseq2>=0.5,<0.6"
System requirements (pydeseq2 0.5.x): Python ≥3.11; numpy ≥2.0, pandas ≥2.2, scipy ≥1.12, scikit-learn ≥1.4, anndata ≥0.11, formulaic ≥1.0.2 (parses the ~ design formula), matplotlib ≥3.9. These are pulled in automatically as dependencies.
API note (0.4+): parallelism is configured through an inference object, not a bare n_cpus= kwarg:
from pydeseq2.default_inference import DefaultInference
inference = DefaultInference(n_cpus=8)
dds = DeseqDataSet(counts=counts_df, metadata=metadata, design="~condition", inference=inference)
ds = DeseqStats(dds, contrast=["condition", "treated", "control"], inference=inference)
Core Workflow
- Prepare data — load counts as samples × genes (transpose with
.T if loaded genes × samples); filter low-count genes (e.g., total reads < 10); drop samples with missing metadata.
- Specify the design — Wilkinson formula (
"~condition", "~batch + condition"); put adjustment variables before the variable of interest.
- Fit —
DeseqDataSet(...).deseq2() runs the full pipeline (size factors → dispersions → LFCs → Cook's outliers).
- Test —
DeseqStats(dds, contrast=[var, test, ref]).summary(); read results_df.
- (Optional) shrink —
ds.lfc_shrink() for visualization/ranking only; p-values stay unshrunken.
- Interpret/export — filter on
padj < 0.05, plot volcano/MA, save CSV/pickle.
Minimal skeleton:
from pydeseq2.dds import DeseqDataSet
from pydeseq2.ds import DeseqStats
dds = DeseqDataSet(counts=counts_df, metadata=metadata, design="~condition")
dds.deseq2()
ds = DeseqStats(dds, contrast=["condition", "treated", "control"])
ds.summary()
significant = ds.results_df[ds.results_df.padj < 0.05]
Command-Line Script
This skill includes a complete standalone script for standard analyses:
python scripts/run_deseq2_analysis.py \
--counts counts.csv \
--metadata metadata.csv \
--design "~batch + condition" \
--contrast condition treated control \
--output results/ \
--min-counts 10 --alpha 0.05 --n-cpus 4 --plots
It handles data loading/validation, gene+sample filtering, the full DESeq2 pipeline, statistical testing with customizable parameters, result export (CSV, pickle), and optional volcano/MA plots. Refer users to scripts/run_deseq2_analysis.py for batch-processing multiple datasets.
Routing Guidance
- Running a standard analysis (load → fit → test → export), or any specific design (two-group, multi-comparison, batch, covariate) →
references/pipeline_steps.md.
- Interpreting results, ranking genes, plotting volcano/MA, or quality metrics →
references/interpretation_and_plots.md.
- Hitting an error (index mismatch, all-zero counts, "not full rank", no significant genes) → Troubleshooting in
references/interpretation_and_plots.md.
- Need exact class/method parameters or object attributes →
references/api_reference.md.
- Complex experimental designs or in-depth workflow →
references/workflow_guide.md.
Key Reminders
- Data orientation matters: counts usually load genes × samples but need samples × genes — transpose with
.T if needed.
- Sample filtering: remove samples with missing metadata before analysis.
- Gene filtering: drop low-count genes (e.g., < 10 total reads) to improve power.
- Design formula order: adjustment variables before the variable of interest (
"~batch + condition").
- LFC shrinkage timing: shrink after testing, for visualization/ranking only — p-values stay unshrunken.
- Significance: use
padj < 0.05 (Benjamini-Hochberg FDR), not raw p-values.
- Contrast format:
[variable, test_level, reference_level].
- Save intermediates: pickle the DeseqDataSet to avoid re-running the expensive fit.
Reference Index
references/pipeline_steps.md — Quick-start, the six pipeline steps with full code (data prep, design, fitting, testing, shrinkage, export), and four common experimental designs.
references/interpretation_and_plots.md — Filtering/ranking significant genes, quality metrics, volcano and MA plots, and a troubleshooting guide.
references/api_reference.md — Complete PyDESeq2 class/method/parameter and data-structure documentation.
references/workflow_guide.md — In-depth complete workflows, data-loading patterns, multi-factor designs, and best practices.
Additional Resources
1---2name: alterlab-pydeseq23description: Run differential gene expression analysis on bulk RNA-seq count matrices with PyDESeq2, the Python port of DESeq2 — size-factor normalization, dispersion estimation, Wald tests, FDR (Benjamini-Hochberg) correction, and volcano/MA plots. Use when identifying differentially expressed genes between conditions from raw bulk RNA-seq counts. Part of the AlterLab Academic Skills suite.4license: MIT5---67# PyDESeq289## Overview1011PyDESeq2 is a Python implementation of DESeq2 for differential expression analysis with bulk RNA-seq data. It supports complete workflows from data loading through result interpretation, including single-factor and multi-factor designs, Wald tests with multiple-testing correction, optional apeGLM shrinkage, and integration with pandas and AnnData.1213## When to Use This Skill1415Use this skill when:16- Analyzing bulk RNA-seq count data for differential expression17- Comparing gene expression between experimental conditions (e.g., treated vs control)18- Performing multi-factor designs accounting for batch effects or covariates19- Converting R-based DESeq2 workflows to Python20- Integrating differential expression analysis into Python-based pipelines21- Users mention "DESeq2", "differential expression", "RNA-seq analysis", or "PyDESeq2"2223## Installation and Requirements2425```bash26uv pip install "pydeseq2>=0.5,<0.6"27```2829**System requirements (pydeseq2 0.5.x):** Python ≥3.11; numpy ≥2.0, pandas ≥2.2, scipy ≥1.12, scikit-learn ≥1.4, anndata ≥0.11, formulaic ≥1.0.2 (parses the `~` design formula), matplotlib ≥3.9. These are pulled in automatically as dependencies.3031**API note (0.4+):** parallelism is configured through an `inference` object, not a bare `n_cpus=` kwarg:3233```python34from pydeseq2.default_inference import DefaultInference35inference = DefaultInference(n_cpus=8)36dds = DeseqDataSet(counts=counts_df, metadata=metadata, design="~condition", inference=inference)37ds = DeseqStats(dds, contrast=["condition", "treated", "control"], inference=inference)38```3940## Core Workflow41421. **Prepare data** — load counts as **samples × genes** (transpose with `.T` if loaded genes × samples); filter low-count genes (e.g., total reads < 10); drop samples with missing metadata.432. **Specify the design** — Wilkinson formula (`"~condition"`, `"~batch + condition"`); put adjustment variables before the variable of interest.443. **Fit** — `DeseqDataSet(...).deseq2()` runs the full pipeline (size factors → dispersions → LFCs → Cook's outliers).454. **Test** — `DeseqStats(dds, contrast=[var, test, ref]).summary()`; read `results_df`.465. **(Optional) shrink** — `ds.lfc_shrink()` for visualization/ranking only; p-values stay unshrunken.476. **Interpret/export** — filter on `padj < 0.05`, plot volcano/MA, save CSV/pickle.4849Minimal skeleton:50```python51from pydeseq2.dds import DeseqDataSet52from pydeseq2.ds import DeseqStats5354dds = DeseqDataSet(counts=counts_df, metadata=metadata, design="~condition")55dds.deseq2()56ds = DeseqStats(dds, contrast=["condition", "treated", "control"])57ds.summary()58significant = ds.results_df[ds.results_df.padj < 0.05]59```6061## Command-Line Script6263This skill includes a complete standalone script for standard analyses:6465```bash66python scripts/run_deseq2_analysis.py \67 --counts counts.csv \68 --metadata metadata.csv \69 --design "~batch + condition" \70 --contrast condition treated control \71 --output results/ \72 --min-counts 10 --alpha 0.05 --n-cpus 4 --plots73```7475It handles data loading/validation, gene+sample filtering, the full DESeq2 pipeline, statistical testing with customizable parameters, result export (CSV, pickle), and optional volcano/MA plots. Refer users to `scripts/run_deseq2_analysis.py` for batch-processing multiple datasets.7677## Routing Guidance7879- **Running a standard analysis (load → fit → test → export), or any specific design (two-group, multi-comparison, batch, covariate)** → `references/pipeline_steps.md`.80- **Interpreting results, ranking genes, plotting volcano/MA, or quality metrics** → `references/interpretation_and_plots.md`.81- **Hitting an error** (index mismatch, all-zero counts, "not full rank", no significant genes) → Troubleshooting in `references/interpretation_and_plots.md`.82- **Need exact class/method parameters or object attributes** → `references/api_reference.md`.83- **Complex experimental designs or in-depth workflow** → `references/workflow_guide.md`.8485## Key Reminders86871. **Data orientation matters:** counts usually load genes × samples but need samples × genes — transpose with `.T` if needed.882. **Sample filtering:** remove samples with missing metadata before analysis.893. **Gene filtering:** drop low-count genes (e.g., < 10 total reads) to improve power.904. **Design formula order:** adjustment variables before the variable of interest (`"~batch + condition"`).915. **LFC shrinkage timing:** shrink after testing, for visualization/ranking only — p-values stay unshrunken.926. **Significance:** use `padj < 0.05` (Benjamini-Hochberg FDR), not raw p-values.937. **Contrast format:** `[variable, test_level, reference_level]`.948. **Save intermediates:** pickle the DeseqDataSet to avoid re-running the expensive fit.9596## Reference Index9798- **`references/pipeline_steps.md`** — Quick-start, the six pipeline steps with full code (data prep, design, fitting, testing, shrinkage, export), and four common experimental designs.99- **`references/interpretation_and_plots.md`** — Filtering/ranking significant genes, quality metrics, volcano and MA plots, and a troubleshooting guide.100- **`references/api_reference.md`** — Complete PyDESeq2 class/method/parameter and data-structure documentation.101- **`references/workflow_guide.md`** — In-depth complete workflows, data-loading patterns, multi-factor designs, and best practices.102103## Additional Resources104105- **Official Documentation:** https://pydeseq2.readthedocs.io106- **GitHub Repository:** https://github.com/owkin/PyDESeq2107- **Publication:** Muzellec et al. (2023) Bioinformatics, DOI: 10.1093/bioinformatics/btad547108- **Original DESeq2 (R):** Love et al. (2014) Genome Biology, DOI: 10.1186/s13059-014-0550-8