Alterlab pydeseq2

From AlterLab-IEU/AlterLab-Academic-Skills · View interactive version

Alterlab pydeseq2 is an AI agent skill from the GitHub repository AlterLab-IEU/AlterLab-Academic-Skills. 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 con

How to use this skill

Skill file

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:

Does NOT Trigger

| Scenario | Use Instead |

|----------|-------------|

| Single-cell differential expression or cluster marker genes | alterlab-scanpy (markers) or alterlab-scvi-tools (model-based DE) |

| Turning FASTQ into the count matrix (salmon/kallisto/STAR quantification, tximport) | alterlab-rnaseq-quant |

| Generic regression / GLM / mixed models on non-count data | alterlab-statsmodels |

| Gene regulatory network inference (which TFs drive which target genes) from expression | alterlab-arboreto |

| Somatic or germline variant calling from sequencing reads | alterlab-nf-core-sarek |

| Microbiome feature-table differential abundance | alterlab-qiime2-amplicon |

Hand-off rule. When the request matches a row above, open your answer by naming that skill by its exact id (for example alterlab-scanpy) and saying in one line why it fits better, then stop applying this skill's procedure. Name it even when you also need to ask for missing input. If only part of the request is out of scope, name the skill for that part and do only the in-scope part. A brief general pointer is fine; the named skill does the work.

Installation and Requirements

uv pip install "pydeseq2>=0.5,<0.6"

System requirements (pydeseq2 0.5.x, current 0.5.4): Python ≥3.11; numpy ≥2.0, pandas ≥2.2,

scipy ≥1.12, scikit-learn ≥1.4, anndata ≥0.11, formulaic ≥1.0.2 and formulaic-contrasts ≥0.2

(parse the ~ design formula and build contrast vectors), 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

  1. 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.
  2. Specify the design — Wilkinson formula ("~condition", "~batch + condition"); put adjustment variables before the variable of interest.
  3. Fit — DeseqDataSet(...).deseq2() runs the full pipeline (size factors → dispersions → LFCs → Cook's outliers).
  4. Test — DeseqStats(dds, contrast=[var, test, ref]).summary(); read results_df.
  5. (Optional) shrink — ds.lfc_shrink() for visualization/ranking only; p-values stay unshrunken.
  6. 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

Key Reminders

  1. Data orientation matters: counts usually load genes × samples but need samples × genes — transpose with .T if needed.
  2. Sample filtering: remove samples with missing metadata before analysis.
  3. Gene filtering: drop low-count genes (e.g., < 10 total reads) to improve power.
  4. Design formula order: adjustment variables before the variable of interest ("~batch + condition").
  5. LFC shrinkage timing: shrink after testing, for visualization/ranking only — p-values stay unshrunken.
  6. Significance: use padj < 0.05 (Benjamini-Hochberg FDR), not raw p-values.
  7. Contrast format: [variable, test_level, reference_level]. contrast also accepts a raw

numpy contrast vector over the design matrix columns for comparisons a three-element list

cannot express (e.g. interaction terms, averaging several levels).

  1. Save intermediates: pickle the DeseqDataSet to avoid re-running the expensive fit.
  2. Test against a fold-change threshold, not just zero: `DeseqStats(..., lfc_null=1.0,

alt_hypothesis="greaterAbs")` asks "is |LFC| > 1?" inside the model. That is the statistically

correct way to demand an effect size — filtering a lfc_null=0 result on `abs(log2FoldChange)

> 1` afterwards does not control the FDR for that claim.

  1. Zero-heavy or sparse counts: DeseqDataSet(..., size_factors_fit_type="poscounts") uses

the positive-counts estimator instead of the median-of-ratios default, which fails when no gene

is detected in every sample. control_genes= restricts size-factor estimation to spike-ins or

housekeeping genes.

Reference Index

Additional Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.