Skip to content

Turn any AI agent into an AI Scientist. The #1 Agent Skills library for science, used by 250,000+ scientists worldwide. 177 ready-to-use validated skills plus 100+ scientific databases covering biology, chemistry, medicine, and drug discovery. Compatible with Cursor, Claude Code, Codex, Pi, Antigravity, and the open Agent Skills standard.

Add pathway-enrichment skill

Timothy Kassisa3fafab537b1
diff --git a/README.md b/README.md
index d9e1d1ff..1428cb75 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE.md)
[![Version](https://img.shields.io/badge/Version-2.43.0-blue.svg)](pyproject.toml)
-[![Skills](https://img.shields.io/badge/Skills-140-brightgreen.svg)](#-whats-included)
+[![Skills](https://img.shields.io/badge/Skills-141-brightgreen.svg)](#-whats-included)
[![Databases](https://img.shields.io/badge/Databases-100%2B-orange.svg)](#-whats-included)
[![Agent Skills](https://img.shields.io/badge/Standard-Agent_Skills-blueviolet.svg)](https://agentskills.io/)
[![Security Scan](https://github.com/K-Dense-AI/scientific-agent-skills/actions/workflows/security-scan.yml/badge.svg)](https://github.com/K-Dense-AI/scientific-agent-skills/actions/workflows/security-scan.yml)
@@ -17,11 +17,11 @@
> **🔔 Claude Scientific Skills is now Scientific Agent Skills.** Same skills, broader compatibility — now works with any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, not just Claude.
-> **New: [K-Dense BYOK](https://github.com/K-Dense-AI/k-dense-byok)** — A free, open-source AI co-scientist that runs on your desktop, powered by Scientific Agent Skills. Bring your own API keys, pick from 40+ models, and get a full research workspace with web search, file handling, 100+ scientific databases, and access to all 140 skills in this repo. Your data stays on your computer, and you can optionally scale to cloud compute via [Modal](https://modal.com/) for heavy workloads. [Get started here.](https://github.com/K-Dense-AI/k-dense-byok)
+> **New: [K-Dense BYOK](https://github.com/K-Dense-AI/k-dense-byok)** — A free, open-source AI co-scientist that runs on your desktop, powered by Scientific Agent Skills. Bring your own API keys, pick from 40+ models, and get a full research workspace with web search, file handling, 100+ scientific databases, and access to all 141 skills in this repo. Your data stays on your computer, and you can optionally scale to cloud compute via [Modal](https://modal.com/) for heavy workloads. [Get started here.](https://github.com/K-Dense-AI/k-dense-byok)
> **Stay up to date:** Follow K-Dense on [X](https://x.com/k_dense_ai), [LinkedIn](https://www.linkedin.com/company/k-dense-inc), and [YouTube](https://www.youtube.com/@K-Dense-Inc) for new skills, release announcements, walkthroughs, research workflow demos, and examples you can use with your own AI agent.
-A comprehensive collection of **140 ready-to-use scientific and research skills** (covering cancer genomics, drug-target binding, molecular dynamics, RNA velocity, geospatial science, time series forecasting, scientific ML resource discovery via Hugging Science, 78+ scientific databases, and more) for any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, created by [K-Dense](https://k-dense.ai). Works with **Cursor, Claude Code, Codex, Google Antigravity, and more**. Transform your AI agent into a research assistant capable of executing complex multi-step scientific workflows across biology, chemistry, medicine, and beyond.
+A comprehensive collection of **141 ready-to-use scientific and research skills** (covering cancer genomics, drug-target binding, molecular dynamics, RNA velocity, geospatial science, time series forecasting, scientific ML resource discovery via Hugging Science, 78+ scientific databases, and more) for any AI agent that supports the open [Agent Skills](https://agentskills.io/) standard, created by [K-Dense](https://k-dense.ai). Works with **Cursor, Claude Code, Codex, Google Antigravity, and more**. Transform your AI agent into a research assistant capable of executing complex multi-step scientific workflows across biology, chemistry, medicine, and beyond.
> ⭐ **Help make AI for science easier to discover:** If Scientific Agent Skills saves you time, teaches your agent a workflow, or helps your lab move faster, please [star this repository](https://github.com/K-Dense-AI/scientific-agent-skills). A star is a public signal that these open, reusable research skills are worth maintaining: it helps scientists, engineers, and open-source contributors find the project, shows which agent-skill standards are gaining real adoption, and gives us a clear reason to keep expanding the collection for the community.
@@ -54,7 +54,7 @@ These skills enable your AI agent to seamlessly work with specialized scientific
## 📦 What's Included
-This repository provides **140 scientific and research skills** organized into the following categories:
+This repository provides **141 scientific and research skills** organized into the following categories:
- **100+ Scientific & Financial Databases** - A unified database-lookup skill provides direct access to 78 public databases (PubChem, ChEMBL, UniProt, COSMIC, ClinicalTrials.gov, FRED, USPTO, and more), plus dedicated skills for DepMap, Imaging Data Commons, PrimeKG, U.S. Treasury Fiscal Data, and Hugging Science (curated catalog of scientific datasets, models, and demos across 17 scientific domains on Hugging Face). Multi-database packages like BioServices (~40 bioinformatics services), BioPython (38 NCBI sub-databases via Entrez), and gget (20+ genomics databases) add further coverage
- **70+ Optimized Python Package Skills** - Explicitly defined skills for RDKit, Scanpy, PyTorch Lightning, scikit-learn, BioPython, pyzotero, BioServices, PennyLane, Qiskit, Molecular Dynamics (OpenMM/MDAnalysis), scVelo, TimesFM, and others — with curated documentation, examples, and best practices. Note: the agent can write code using *any* Python package, not just these; these skills simply provide stronger, more reliable performance for the packages listed
@@ -99,7 +99,7 @@ Each skill includes:
- **Multi-Step Workflows** - Execute complex pipelines with a single prompt
### 🎯 **Comprehensive Coverage**
-- **140 Skills** - Extensive coverage across all major scientific domains
+- **141 Skills** - Extensive coverage across all major scientific domains
- **100+ Databases** - Unified access to 78+ databases via database-lookup, plus dedicated data access skills and multi-database packages like BioServices, BioPython, and gget
- **70+ Optimized Python Package Skills** - RDKit, Scanpy, PyTorch Lightning, scikit-learn, BioServices, PennyLane, Qiskit, Molecular Dynamics (OpenMM/MDAnalysis), scVelo, TimesFM, and others (the agent can use any Python package; these are the pre-documented, higher-performing paths)
@@ -390,17 +390,18 @@ networks, and search GEO for similar patterns.
## 📚 Available Skills
-This repository contains **140 scientific and research skills** organized across multiple domains. Each skill provides comprehensive documentation, code examples, and best practices for working with scientific libraries, databases, and tools.
+This repository contains **141 scientific and research skills** organized across multiple domains. Each skill provides comprehensive documentation, code examples, and best practices for working with scientific libraries, databases, and tools.
### Skill Categories
> **Note:** The Python package and integration skills listed below are *explicitly defined* skills — curated with documentation, examples, and best practices for stronger, more reliable performance. They are not a ceiling: the agent can install and use *any* Python package or call *any* API, even without a dedicated skill. The skills listed simply make common workflows faster and more dependable.
-#### 🧬 **Bioinformatics & Genomics** (21 skills)
+#### 🧬 **Bioinformatics & Genomics** (22 skills)
- Sequence analysis: BioPython, pysam, scikit-bio, BioServices
- Single-cell analysis: Scanpy, AnnData, scvi-tools, scVelo (RNA velocity), Arboreto, Cellxgene Census
- Genomic tools: gget, geniml, gtars, deepTools, FlowIO, Polars-Bio, Zarr, TileDB-VCF
- Differential expression: PyDESeq2
+- Functional enrichment: Pathway Enrichment (ORA, GSEA/preranked, ssGSEA via gseapy + g:Profiler; GO, KEGG, Reactome, WikiPathways, MSigDB)
- Phylogenetics: ETE Toolkit, Phylogenetics (MAFFT, IQ-TREE 2, FastTree)
#### 🧪 **Cheminformatics & Drug Discovery** (10 skills)
@@ -711,7 +712,7 @@ Recommended practice:
title = {Scientific Agent Skills: A Comprehensive Collection of Scientific Tools for AI Agents},
year = {2026},
url = {https://github.com/K-Dense-AI/scientific-agent-skills},
- note = {140 skills covering databases, packages, integrations, and analysis tools}
+ note = {141 skills covering databases, packages, integrations, and analysis tools}
}
```
diff --git a/docs/skills.md b/docs/skills.md
index 3f618126..16d637ff 100644
--- a/docs/skills.md
+++ b/docs/skills.md
@@ -50,6 +50,7 @@
- **Polars-Bio** - High-performance genomic interval operations and bioinformatics file I/O on Polars DataFrames. Provides overlap, nearest, merge, coverage, complement, and subtract operations for BED/VCF/BAM/GFF intervals. Streaming and cloud-native architecture for large-scale genomic analyses, serving as a faster alternative to bioframe
- **pysam** - Read, write, and manipulate genomic data files (SAM/BAM/CRAM alignments, VCF/BCF variants, FASTA/FASTQ sequences) with pileup analysis, coverage calculations, and bioinformatics workflows
- **PyDESeq2** - Python implementation of the DESeq2 differential gene expression analysis method for bulk RNA-seq data. Provides statistical methods for determining differential expression between experimental conditions using negative binomial generalized linear models. Key features include: size factor estimation for library size normalization, dispersion estimation and shrinkage, hypothesis testing with Wald test or likelihood ratio test, multiple testing correction (Benjamini-Hochberg FDR), results filtering and ranking, and integration with pandas DataFrames. Handles complex experimental designs, batch effects, and replicates. Produces fold-change estimates, p-values, and adjusted p-values for each gene. Use cases: identifying differentially expressed genes between conditions, RNA-seq experiment analysis, biomarker discovery, and gene expression studies requiring rigorous statistical analysis
+- **Pathway Enrichment** - Pathway and gene-set enrichment analysis on gene lists or ranked gene data, with result interpretation. Supports over-representation analysis (ORA via Enrichr/Fisher's exact/hypergeometric), preranked and standard Gene Set Enrichment Analysis (GSEA), and single-sample scoring (ssGSEA/GSVA) using gseapy and the official g:Profiler client. Covers gene-set libraries (GO Biological Process/Molecular Function/Cellular Component, KEGG, Reactome, WikiPathways, and MSigDB collections including Hallmark, C2 canonical pathways, C5 ontology, and C7 immune signatures), gene-ID mapping (Ensembl/Entrez to symbols via Biomart, g:Convert, or mygene) and organism handling, choice of the statistical background universe, multiple-testing correction (Benjamini-Hochberg FDR vs g:Profiler g:SCS vs Bonferroni), redundancy reduction (enrichment maps, leading-edge genes, term clustering), and publication-ready tables plus dotplots/bar plots/GSEA running-score plots. Includes a CLI helper (run_enrichment.py) that runs ORA or preranked GSEA end-to-end — automatically building the ranking metric from a DESeq2 results table — and writes a results table and dotplot. Cross-references PyDESeq2 and Scanpy upstream (sources of differentially expressed genes and cluster markers) and database-lookup/gget for gene-ID mapping and Reactome/KEGG/STRING APIs. Use cases: functional interpretation of differentially expressed genes, CRISPR-screen hits, and single-cell cluster markers; GO/KEGG/Reactome/WikiPathways enrichment; preranked GSEA from DESeq2 statistics; pathway activity scoring per sample or cell; and building defensible, reproducible enrichment analyses that avoid common pitfalls (gene-ID/organism mismatch, wrong background, thresholding before GSEA)
- **Scanpy** - Comprehensive Python toolkit for single-cell RNA-seq data analysis built on AnnData (scanpy 1.12.x; Python 3.12+). Provides end-to-end workflows for preprocessing (quality control, scrublet doublet detection, normalization, log transformation), dimensionality reduction (PCA, UMAP, t-SNE), Leiden clustering, marker gene identification, pseudobulk aggregation via `get.aggregate()`, trajectory inference (PAGA, diffusion maps), and visualization. Key features include: efficient handling of large datasets using sparse matrices and experimental Dask out-of-core support, integration with scvi-tools for advanced analysis, batch correction methods (ComBat), and publication-quality plotting. Optional GPU acceleration via rapids-singlecell. Use cases: single-cell RNA-seq analysis, cell-type identification, exploratory cluster markers, pseudobulk DE workflows (with pydeseq2), trajectory analysis, and comprehensive single-cell genomics workflows
- **scVelo** - RNA velocity analysis for estimating cell state transitions from unspliced/spliced mRNA dynamics. Infers trajectory directions, computes latent time, and identifies driver genes in single-cell RNA-seq data. Complements Scanpy/scVI-tools for trajectory inference, enabling the study of cellular differentiation dynamics and lineage decisions at single-cell resolution
- **scvi-tools** - Probabilistic deep learning models for single-cell omics analysis. PyTorch-based framework providing variational autoencoders (VAEs) for dimensionality reduction, batch correction, differential expression, and data integration across modalities. Includes 25+ models: scVI/scANVI (RNA-seq integration and cell type annotation), totalVI (CITE-seq protein+RNA), MultiVI (multiome RNA+ATAC integration), PeakVI (ATAC-seq analysis), DestVI/Stereoscope/Tangram (spatial transcriptomics deconvolution), MethylVI (methylation), CytoVI (flow/mass cytometry), VeloVI (RNA velocity), contrastiveVI (perturbation studies), and Solo (doublet detection). Supports seamless integration with Scanpy/AnnData ecosystem, GPU acceleration, reference mapping (scArches), and probabilistic differential expression with uncertainty quantification
diff --git a/skills/pathway-enrichment/SKILL.md b/skills/pathway-enrichment/SKILL.md
new file mode 100644
index 00000000..683b5131
--- /dev/null
+++ b/skills/pathway-enrichment/SKILL.md
@@ -0,0 +1,194 @@
+---
+name: pathway-enrichment
+description: Run pathway and gene-set enrichment analysis on gene lists or ranked gene data, then interpret the results. Use whenever the user has a set of genes (differentially expressed genes from PyDESeq2/Scanpy, CRISPR-screen hits, cluster marker genes, proteomics hits) and wants to know which biological pathways, GO terms, or gene sets are over-represented or enriched. Covers over-representation analysis (ORA / Enrichr / Fisher / hypergeometric), ranked Gene Set Enrichment Analysis (GSEA / preranked), single-sample scoring (ssGSEA/GSVA), and functional profiling via gseapy, g:Profiler, Enrichr libraries, MSigDB, GO, KEGG, Reactome, and WikiPathways — plus gene-ID mapping, choosing the right background universe, multiple-testing correction, redundancy reduction, dotplots/enrichment maps, and publication-ready tables. Use this for "pathway analysis", "enrichment analysis", "GO enrichment", "KEGG/Reactome pathways", "GSEA", "over-representation", "functional annotation", or "what pathways are my genes in".
+license: MIT
+metadata:
+ version: "1.0"
+ skill-author: K-Dense Inc.
+---
+
+# Pathway Enrichment
+
+## Overview
+
+Enrichment analysis answers "what biology is over-represented in my genes?" It is the standard last step after differential expression, a screen, or clustering. There are two core methods, and choosing correctly is the single most important decision:
+
+- **ORA (over-representation analysis)** — take a *thresholded* gene list (e.g., padj < 0.05) and test which gene sets it overlaps more than chance, using Fisher's exact / hypergeometric tests. Tools: Enrichr, g:Profiler.
+- **GSEA (gene set enrichment analysis)** — take the *whole ranked list* of genes (no threshold) and test whether each gene set is concentrated toward the top or bottom. Preranked GSEA uses a per-gene score (e.g., the DESeq2 `stat`). Better when effects are broad and subtle.
+
+This skill orchestrates these analyses, the gene-set databases behind them, and the interpretation pitfalls that make results wrong or unpublishable.
+
+## When to Use This Skill
+
+Use this skill when the user wants to:
+- Find enriched GO terms / KEGG / Reactome / WikiPathways / MSigDB Hallmark sets in a gene list.
+- Run GSEA / preranked GSEA on DESeq2, edgeR, limma, or Scanpy `rank_genes_groups` output.
+- Score pathway activity per sample/cell (ssGSEA, GSVA).
+- Interpret, deduplicate, and visualize enrichment results, or build a publication table/figure.
+- Decide between ORA and GSEA, pick gene-set libraries, choose a background, or fix gene-ID problems.
+
+For quick one-off Enrichr lookups the `gget` skill (`gget enrichr`) is lighter weight; for raw pathway/interaction APIs (Reactome, KEGG, STRING) see the `database-lookup` skill. Use **this** skill for full, defensible enrichment workflows.
+
+## Choosing the Right Method
+
+| Situation | Method | Tool / entry point |
+|-----------|--------|--------------------|
+| You have a discrete hit list (DE genes, screen hits, cluster markers) | **ORA** | `gp.enrichr(...)` or g:Profiler |
+| You have a full ranked list (every tested gene + a score) | **Preranked GSEA** | `gp.prerank(...)` |
+| You have an expression matrix + class labels | **GSEA** | `gp.gsea(...)` |
+| You want a pathway score per sample/cell | **ssGSEA / GSVA** | `gp.ssgsea(...)`, `gp.gsva(...)` |
+| You need a custom background or 500+ organisms | **ORA with custom domain** | g:Profiler (`domain_scope='custom'`) |
+| You want TF / signaling *activity* (PROGENy, DoRothEA) | activity inference | see `references/databases-and-gene-sets.md` (decoupler) |
+
+When in doubt: a thresholded list → ORA; a ranked table with scores → GSEA. Never threshold a list and then feed it to GSEA — that discards the ranking GSEA depends on.
+
+## Setup
+
+```bash
+uv pip install gseapy gprofiler-official
+# gseapy pulls pandas, numpy, scipy, matplotlib. Network access is needed for
+# Enrichr, g:Profiler, and MSigDB downloads. For fully offline ORA, use a local
+# GMT file with gp.enrich() (see references/gseapy.md).
+```
+
+Verify and list available gene-set libraries (names change over time — never hardcode blindly):
+
+```python
+import gseapy as gp
+names = gp.get_library_name(organism="human") # 200+ Enrichr libraries
+print([n for n in names if "Reactome" in n or "KEGG" in n or "Hallmark" in n])
+```
+
+## Quick Start
+
+### ORA on a hit list (gseapy + Enrichr)
+
+```python
+import gseapy as gp
+
+# Enrichr libraries expect HGNC gene SYMBOLS (human: UPPERCASE). Map IDs first if needed.
+genes = [g.strip() for g in open("deg_symbols.txt") if g.strip()]
+
+enr = gp.enrichr(
+ gene_list=genes,
+ gene_sets=["MSigDB_Hallmark_2020", "GO_Biological_Process_2023",
+ "KEGG_2021_Human", "Reactome_2022"],
+ organism="human",
+ outdir=None, # in-memory; set a path to also write tables/plots
+)
+res = enr.results
+sig = res[res["Adjusted P-value"] < 0.05].sort_values("Adjusted P-value")
+print(sig[["Gene_set", "Term", "Overlap", "Adjusted P-value", "Combined Score", "Genes"]].head(20))
+```
+
+### Preranked GSEA from DESeq2 results
+
+```python
+import gseapy as gp
+import pandas as pd
+
+res = pd.read_csv("deseq2_results.csv", index_col=0) # index = gene symbols
+# Rank by the test statistic (sign = direction, magnitude = evidence). This is
+# more stable than ranking by log2FoldChange, which is noisy for low-count genes.
+rnk = res["stat"].dropna().sort_values(ascending=False)
+rnk.index = rnk.index.str.upper()
+rnk = rnk[~rnk.index.duplicated(keep="first")]
+
+pre = gp.prerank(
+ rnk=rnk,
+ gene_sets=["MSigDB_Hallmark_2020", "GO_Biological_Process_2023"],
+ min_size=15, max_size=500, # drop tiny/huge sets (noisy or generic)
+ permutation_num=1000, seed=123, # seed = reproducible p-values
+ threads=4, outdir=None,
+)
+out = pre.res2d.sort_values("FDR q-val")
+print(out[["Term", "ES", "NES", "NOM p-val", "FDR q-val", "Lead_genes"]].head(20))
+```
+
+If you have no `stat` column, build the rank from `sign(log2FoldChange) * -log10(pvalue)`.
+
+## Core Workflow
+
+For a defensible analysis, work through these steps. The middle steps (ID type, background) are where results most often silently go wrong.
+
+### Step 1 — Pin down inputs and pick the method
+Confirm: which genes, what organism, is there a per-gene score (→ GSEA) or just a list (→ ORA), and what comparison they represent (direction matters for interpretation).
+
+### Step 2 — Get gene IDs into the right namespace
+Enrichr/MSigDB libraries are keyed by **gene symbols** (human UPPERCASE, mouse Title-case). If you have Ensembl/Entrez IDs, convert first. See `references/databases-and-gene-sets.md` for `gp.Biomart`, g:Profiler `g:Convert`, and `mygene`. A silent ID mismatch is the #1 cause of "nothing is significant".
+
+### Step 3 — Choose gene-set libraries to match the question
+Hallmark (broad themes) → GO:BP (mechanism) → KEGG/Reactome/WikiPathways (curated pathways) → C7 (immune), etc. Don't run 50 libraries; pick 2–4 that fit the biology. Catalog and selection guidance: `references/databases-and-gene-sets.md`.
+
+### Step 4 — Set the background universe (ORA only)
+The background must be the genes that *could* have been detected in your assay (e.g., all expressed/tested genes), not the whole genome. The wrong background inflates significance. Enrichr uses a fixed background; when background matters, use g:Profiler with `domain_scope='custom'` + your `background`, or `gp.enrich()` with an explicit background. Rationale in `references/interpretation.md`.
+
+### Step 5 — Run the analysis
+Use the Quick Start patterns or the bundled `scripts/run_enrichment.py`. For GSEA always set a `seed` and report `permutation_num`.
+
+### Step 6 — Filter on adjusted p-values
+Use `Adjusted P-value` (ORA, Benjamini–Hochberg) or `FDR q-val` (GSEA), not raw p-values. Typical cutoff 0.05; also check the overlap/gene count so a "hit" isn't 1 gene out of a 2000-gene set.
+
+### Step 7 — Visualize
+Dotplots, bar plots, enrichment maps, and GSEA running-score plots are built into gseapy (`gp.dotplot`, `gp.barplot`, `gp.enrichment_map`, `gp.gseaplot`). See `references/gseapy.md`.
+
+### Step 8 — Reduce redundancy and interpret
+GO especially returns many near-duplicate terms. Collapse with an enrichment map (term–term similarity), leading-edge overlap, or parent terms, and report representative terms. Interpretation framework and a publication-table format are in `references/interpretation.md`.
+
+## Helper Script
+
+`scripts/run_enrichment.py` runs ORA or GSEA end-to-end and writes a results table plus a dotplot, handling the boilerplate (symbol cleanup, dedup, NA removal, rank construction from a DESeq2 table, per-library FDR filtering).
+
+```bash
+# ORA from a hit list (one gene symbol per line)
+python scripts/run_enrichment.py ora \
+ --genes deg_symbols.txt \
+ --libraries MSigDB_Hallmark_2020 GO_Biological_Process_2023 KEGG_2021_Human \
+ --organism human --outdir results/
+
+# Preranked GSEA from a DESeq2 results CSV (auto-builds the rank from `stat`)
+python scripts/run_enrichment.py gsea \
+ --deseq2 deseq2_results.csv \
+ --libraries MSigDB_Hallmark_2020 GO_Biological_Process_2023 \
+ --organism human --outdir results/ --seed 123
+
+# Preranked GSEA from an explicit 2-column rank file (gene,score)
+python scripts/run_enrichment.py gsea --rnk ranked_genes.csv --outdir results/
+```
+
+Run `python scripts/run_enrichment.py --help` for all options (background file, FDR cutoff, min/max set size, permutations).
+
+## Common Pitfalls
+
+These cause most wrong or irreproducible results:
+
+1. **Gene-ID / organism mismatch** — symbols vs Ensembl, human vs mouse casing. Map IDs and set `organism` correctly, or matches silently drop to ~zero.
+2. **Wrong background (ORA)** — using the whole genome instead of the tested/expressed gene set inflates p-values. Set a custom background when it matters.
+3. **Thresholding before GSEA** — GSEA needs the *full* ranked list; only ORA uses a cut list.
+4. **Ranking GSEA by log2FoldChange alone** — unstable for low-count genes; prefer `stat` or `sign(LFC) * -log10(p)`.
+5. **Multiple-testing across libraries** — FDR is computed *within* a library; running many libraries multiplies tests. Report per-library FDR and stay conservative.
+6. **Redundant GO terms** — don't report 40 variants of the same term; collapse and show representatives.
+7. **Significance ≠ relevance** — check the overlap count and gene-set size; tiny sets reach significance trivially.
+8. **List too short/long for ORA** — <10 genes is underpowered; >2000 loses specificity (consider GSEA instead).
+9. **No reproducibility metadata** — Enrichr/GO libraries are versioned and drift over time. Record library names+date and set a GSEA `seed`.
+
+## Integration with Other Skills
+
+- **Upstream (where genes come from):** `pydeseq2` (DE genes + `stat` for GSEA), `scanpy` (`rank_genes_groups` markers / scores), `depmap`/`pytdc` (screen hits), proteomics skills (`pyopenms`, `matchms`).
+- **Databases / IDs:** `database-lookup` (Reactome, KEGG, STRING, Gene Ontology APIs), `gget` (`gget enrichr` quick path, `gget info` for ID mapping), `bioservices`.
+- **Downstream:** `scientific-visualization` (custom figures), `networkx` (enrichment-map graphs), `scientific-writing` / `literature-review` (interpret and cite), `statistical-analysis` (multiple-testing details).
+
+## Reference Files
+
+Read the relevant file when you need depth:
+
+- `references/gseapy.md` — full gseapy API: `enrichr`, offline `enrich`, `prerank`, `gsea`, `ssgsea`, `gsva`, `Msigdb`, `Biomart`, `get_library_name`/`read_gmt`, every plot, result-column meanings, GMT/offline usage, and troubleshooting (rate limits, empty results).
+- `references/databases-and-gene-sets.md` — GO, KEGG, Reactome, WikiPathways, MSigDB collections, Enrichr library naming, g:Profiler sources, organism handling, gene-ID conversion, library selection by question, and pointers to Reactome/STRING APIs and decoupler activity inference.
+- `references/interpretation.md` — ORA vs GSEA statistics, background-universe choice, multiple-testing methods (BH vs g:SCS vs Bonferroni), leading-edge genes, redundancy reduction, effect vs significance, a publication-table template, and reproducibility checklist.
+
+## Resources
+
+- gseapy docs: https://gseapy.readthedocs.io/ · repo: https://github.com/zqfang/GSEApy
+- g:Profiler: https://biit.cs.ut.ee/gprofiler/ · Python client: https://pypi.org/project/gprofiler-official/
+- Enrichr: https://maayanlab.cloud/Enrichr/ · MSigDB: https://www.gsea-msigdb.org/gsea/msigdb/
+- GSEA method: Subramanian et al. (2005) PNAS, DOI: 10.1073/pnas.0506580102
diff --git a/skills/pathway-enrichment/references/databases-and-gene-sets.md b/skills/pathway-enrichment/references/databases-and-gene-sets.md
new file mode 100644
index 00000000..3a423f27
--- /dev/null
+++ b/skills/pathway-enrichment/references/databases-and-gene-sets.md
@@ -0,0 +1,140 @@
+# Databases, Gene Sets, and Gene-ID Mapping
+
+## Contents
+- [Picking libraries by question](#picking-libraries-by-question)
+- [The main gene-set databases](#the-main-gene-set-databases)
+- [MSigDB collections](#msigdb-collections)
+- [g:Profiler (alternative ORA, custom background, 500+ organisms)](#gprofiler)
+- [Gene-ID types and conversion](#gene-id-types-and-conversion)
+- [Organism handling](#organism-handling)
+- [Pathway/interaction APIs (Reactome, KEGG, STRING)](#pathwayinteraction-apis)
+- [Activity inference (decoupler: PROGENy, DoRothEA/CollecTRI)](#activity-inference)
+
+## Picking libraries by question
+
+Match the database to the biological question instead of running everything:
+
+| Question | Best gene sets |
+|----------|----------------|
+| "What are the broad themes?" | MSigDB **Hallmark** (50 curated, low redundancy) |
+| "What mechanism/process?" | **GO Biological Process** |
+| "Which curated pathways?" | **Reactome**, **KEGG**, **WikiPathways** |
+| "Molecular function / localization?" | GO MF / GO CC |
+| "Immune signatures?" | MSigDB **C7** (ImmuneSigDB) |
+| "Oncogenic / perturbation?" | MSigDB **C6** (oncogenic), **C2:CGP** |
+| "TF targets / regulons?" | MSigDB **C3**, ChEA, or decoupler (below) |
+| "Disease/phenotype association?" | g:Profiler HP, DisGeNET, GWAS Catalog |
+
+Start narrow (Hallmark + one of GO:BP / Reactome). Add libraries only if the
+question needs them — each extra library multiplies the testing burden.
+
+## The main gene-set databases
+
+- **GO (Gene Ontology)** — three namespaces: Biological Process (BP), Molecular
+ Function (MF), Cellular Component (CC). Hierarchical → highly redundant; collapse
+ terms after testing (see `interpretation.md`).
+- **KEGG** — manually curated metabolic & signaling pathways. Compact, well known.
+- **Reactome** — large, expert-curated, hierarchical human pathway set; good
+ granularity. APIs in `database-lookup`.
+- **WikiPathways** — community-curated pathways; complements KEGG/Reactome.
+- **MSigDB** — collections of collections (Hallmark, curated, GO, immune, etc.);
+ the standard source of GMT files for GSEA.
+
+## MSigDB collections
+
+| Collection | Contents |
+|-----------|----------|
+| **H** (`h.all`) | Hallmark — 50 refined, non-redundant signatures (best default for GSEA) |
+| **C2:CP** | Canonical Pathways: `c2.cp.kegg_medicus`, `c2.cp.reactome`, `c2.cp.wikipathways`, `c2.cp.biocarta` |
+| **C2:CGP** | Chemical & genetic perturbations |
+| **C3** | Regulatory targets (TFT, miRNA) |
+| **C5** | Ontology: `c5.go.bp`, `c5.go.mf`, `c5.go.cc`, `c5.hpo` |
+| **C6** | Oncogenic signatures |
+| **C7** | ImmuneSigDB |
+| **C8** | Cell-type signatures |
+
+Fetch via gseapy: `gp.Msigdb().get_gmt(category="h.all", dbver="2024.1.Hs")`
+(use `dbver="…Mm"` for mouse symbols). See `gseapy.md`.
+
+## g:Profiler
+
+The official client (`gprofiler-official`) is the best path when you need a
+**custom background**, **many organisms** (~500), or g:Profiler's `g:SCS`
+multiple-testing correction. It performs ORA over GO, KEGG, Reactome,
+WikiPathways, miRTarBase, CORUM, HP, and more in one call.
+
+```python
+from gprofiler import GProfiler
+
+gp = GProfiler(return_dataframe=True)
+res = gp.profile(
+ organism="hsapiens", # mmusculus, dmelanogaster, ...
+ query=gene_list, # symbols, Ensembl, Entrez — auto-detected
+ sources=["GO:BP", "KEGG", "REAC", "WP"], # restrict sources
+ user_threshold=0.05,
+ significance_threshold_method="g_SCS", # default; or "fdr" / "bonferroni"
+ domain_scope="custom", # use a custom statistical background
+ background=expressed_genes, # the tested/expressed universe
+ no_iea=False, # True = drop electronic GO annotations
+)
+# columns: source, native, name, p_value, term_size, query_size,
+# intersection_size, effective_domain_size, intersections
+```
+
+`gp.convert(organism="hsapiens", query=ids, target_namespace="ENTREZGENE")` maps
+IDs; `gp.orth(...)` maps orthologs across organisms.
+
+## Gene-ID types and conversion
+
+Enrichr and MSigDB libraries are keyed by **gene symbols**. Convert other ID
+types before ORA/GSEA, or matches silently drop.
+
+| You have | Convert with |
+|----------|--------------|
+| Ensembl gene IDs (`ENSG…`) | `gp.Biomart`, g:Profiler `g:Convert`, or `mygene` |
+| Entrez IDs | `mygene`, g:Profiler |
+| Mouse symbols → human | g:Profiler `g:Orth`, `mygene` (then run human libraries) |
+
+`mygene` example:
+```python
+import mygene
+mg = mygene.MyGeneInfo()
+hits = mg.querymany(ensembl_ids, scopes="ensembl.gene",
+ fields="symbol", species="human", as_dataframe=True)
+symbols = hits["symbol"].dropna().tolist()
+```
+Strip Ensembl version suffixes first (`ENSG00000141510.16` → `ENSG00000141510`).
+The `gget` skill (`gget info`) is another quick ID-mapping path.
+
+## Organism handling
+
+- Human symbols are UPPERCASE (`TP53`); mouse symbols are Title-case (`Trp53`).
+- Set `organism=` for `gp.enrichr` (Enrichr) and use the matching MSigDB `dbver`
+ (`…Hs` vs `…Mm`) or g:Profiler `organism=` code.
+- Don't run human libraries on mouse symbols — convert or map orthologs first.
+
+## Pathway/interaction APIs
+
+For raw pathway content or network context (not enrichment statistics), use the
+`database-lookup` skill, which wraps:
+- **Reactome** content + Analysis Service (submit a gene list, get pathway
+ over-representation).
+- **KEGG** pathways/compounds.
+- **STRING** — protein–protein interactions plus its own functional-enrichment
+ endpoint for a submitted gene set; pairs well with `networkx` for network views.
+- **Gene Ontology / QuickGO** term metadata.
+
+## Activity inference
+
+When the goal is **pathway or TF activity** (a continuous score per sample/cell)
+rather than over-representation of a list, use `decoupler`. It runs multiple
+enrichment/activity methods (ORA, GSEA, univariate linear models, etc.) against
+curated priors:
+- **PROGENy** — 14 signaling pathway responsive signatures.
+- **DoRothEA / CollecTRI** — TF→target regulons for TF-activity inference.
+- **MSigDB** priors via its OmniPath integration.
+
+decoupler integrates natively with AnnData/Scanpy (per-cell activities) and with
+per-sample pseudobulk matrices. APIs evolve between major versions — check the
+current decoupler docs (https://decoupler-py.readthedocs.io/) for exact function
+names before writing code.
diff --git a/skills/pathway-enrichment/references/gseapy.md b/skills/pathway-enrichment/references/gseapy.md
new file mode 100644
index 00000000..f974905b
--- /dev/null
+++ b/skills/pathway-enrichment/references/gseapy.md
@@ -0,0 +1,189 @@
+# gseapy Reference
+
+gseapy (v1.1.x, Python/Rust) wraps GSEA, preranked GSEA, ssGSEA, GSVA, and the
+Enrichr API behind a pandas-friendly interface. License: BSD-3-Clause.
+
+## Contents
+- [Module map](#module-map)
+- [ORA: enrichr (online) and enrich (offline)](#ora)
+- [Preranked GSEA](#preranked-gsea)
+- [Standard GSEA (matrix + classes)](#standard-gsea)
+- [ssGSEA and GSVA](#ssgsea-and-gsva)
+- [Gene sets: libraries, MSigDB, GMT](#gene-sets)
+- [Gene-ID mapping with Biomart](#biomart)
+- [Plotting](#plotting)
+- [Result columns](#result-columns)
+- [Troubleshooting](#troubleshooting)
+
+## Module map
+
+```python
+import gseapy as gp
+gp.enrichr # online ORA via Enrichr API
+gp.enrich # offline ORA against a local GMT / dict
+gp.prerank # preranked GSEA (per-gene score)
+gp.gsea # standard GSEA (expression matrix + class labels)
+gp.ssgsea # single-sample GSEA (per-sample scores)
+gp.gsva # GSVA (per-sample scores)
+gp.Msigdb # download MSigDB collections
+gp.Biomart # gene/ID conversion
+gp.get_library_name(organism="human") # list Enrichr libraries
+gp.get_library("KEGG_2021_Human") # fetch a library as a dict
+gp.read_gmt("sets.gmt") # load a local GMT as a dict
+# plots: gp.dotplot, gp.barplot, gp.ringplot, gp.enrichment_map,
+# gp.gseaplot, gp.gseaplot2, gp.heatmap
+```
+
+## ORA
+
+### enrichr (online)
+```python
+enr = gp.enrichr(
+ gene_list=genes, # list, Series, DataFrame, or txt path (symbols)
+ gene_sets=["MSigDB_Hallmark_2020", "KEGG_2021_Human"], # names, GMT, or dict
+ organism="human", # human|mouse|fly|yeast|worm|fish
+ background=None, # list or count; default is the library background
+ outdir=None, # None = in-memory only
+)
+enr.results # DataFrame: all terms across all libraries (Gene_set column)
+```
+Key result columns: `Gene_set`, `Term`, `Overlap` (k/K), `P-value`,
+`Adjusted P-value` (BH within library), `Odds Ratio`, `Combined Score`, `Genes`.
+
+`background` note: Enrichr's online API largely ignores arbitrary custom
+backgrounds (it has fixed per-library backgrounds). For a true custom background
+use `gp.enrich()` (below) or g:Profiler. See `interpretation.md`.
+
+### enrich (offline, custom background)
+```python
+gene_sets = gp.read_gmt("c2.cp.reactome.v2024.1.Hs.symbols.gmt") # dict
+enr = gp.enrich(
+ gene_list=genes,
+ gene_sets=gene_sets,
+ background=expressed_genes, # REQUIRED here; the tested/expressed universe
+ outdir=None,
+)
+```
+Use this when reviewers will ask about the background, or when offline.
+
+## Preranked GSEA
+
+```python
+pre = gp.prerank(
+ rnk=rnk, # Series indexed by gene, or 2-col DataFrame/.rnk path
+ gene_sets=["MSigDB_Hallmark_2020"],
+ min_size=15, max_size=500, # filter sets by size
+ permutation_num=1000, # >=1000 for publication
+ weight=1.0, # weighted KS (classic = 0)
+ seed=123, threads=4, outdir=None,
+)
+pre.res2d # DataFrame of results (see Result columns)
+pre.results # dict keyed by term with ES curve, lead genes, etc.
+```
+`rnk` must be sorted high→low and have no duplicate gene IDs. Rank by the DESeq2
+`stat`, or `sign(log2FoldChange) * -log10(pvalue)`; avoid log2FC alone.
+
+## Standard GSEA
+
+When you have the expression matrix and class labels (rather than a precomputed
+rank), GSEA computes the ranking internally per the chosen metric.
+```python
+gsea = gp.gsea(
+ data=expr_df, # genes x samples (DataFrame or GCT path)
+ gene_sets="MSigDB_Hallmark_2020",
+ cls=["A","A","B","B"], # class vector or .cls path
+ permutation_type="phenotype", # or "gene_set" for few samples
+ method="signal_to_noise", # ranking metric
+ permutation_num=1000, seed=123, threads=4, outdir=None,
+)
+gsea.res2d
+```
+With < ~7 samples per group, use `permutation_type="gene_set"`.
+
+## ssGSEA and GSVA
+
+Per-sample pathway scores (no class labels) — useful as features for ML or for
+heatmaps of pathway activity across samples/cells.
+```python
+ss = gp.ssgsea(data=expr_df, gene_sets="MSigDB_Hallmark_2020",
+ sample_norm_method="rank", outdir=None, threads=4)
+ss.res2d # long-form NES per (Term, Name)
+scores = ss.res2d.pivot(index="Term", columns="Name", values="NES") # terms x samples
+
+gsva = gp.gsva(data=expr_df, gene_sets="MSigDB_Hallmark_2020", outdir=None)
+```
+
+## Gene sets
+
+### List / fetch Enrichr libraries
+```python
+gp.get_library_name(organism="human") # names drift; check, don't hardcode
+lib = gp.get_library("Reactome_2022") # dict: {term: [genes]}
+```
+Common human libraries: `MSigDB_Hallmark_2020`, `GO_Biological_Process_2023`,
+`GO_Molecular_Function_2023`, `GO_Cellular_Component_2023`, `KEGG_2021_Human`,
+`Reactome_2022`, `WikiPathway_2023_Human`, `MSigDB_Oncogenic_Signatures`.
+
+### MSigDB collections
+```python
+msig = gp.Msigdb()
+print(msig.list_dbver()) # available MSigDB versions
+cats = msig.list_category(dbver="2024.1.Hs")
+hallmark = msig.get_gmt(category="h.all", dbver="2024.1.Hs") # dict for prerank/gsea
+```
+Useful categories: `h.all` (Hallmark), `c2.cp.kegg_medicus`, `c2.cp.reactome`,
+`c2.cp.wikipathways`, `c5.go.bp`, `c7.immunesigdb`.
+
+### Local GMT
+```python
+gene_sets = gp.read_gmt("my_sets.gmt") # then pass to enrich/prerank/gsea
+```
+
+## Biomart
+
+```python
+bm = gp.Biomart()
+# Ensembl gene IDs -> HGNC symbols
+conv = bm.query(dataset="hsapiens_gene_ensembl",
+ attributes=["ensembl_gene_id", "external_gene_name"],
+ filters={"ensembl_gene_id": ensembl_ids})
+```
+For mouse→human ortholog mapping or many IDs, g:Profiler `g:Convert`/`g:Orth`
+or the `mygene` package are often easier (see `databases-and-gene-sets.md`).
+
+## Plotting
+
+```python
+gp.dotplot(enr.results, column="Adjusted P-value", size=5, top_term=15,
+ title="ORA", cmap="viridis_r", ofname="dot.png")
+gp.barplot(enr.results, column="Adjusted P-value", top_term=15, ofname="bar.png")
+gp.dotplot(pre.res2d, column="FDR q-val", title="GSEA", ofname="gsea_dot.png") # GSEA
+gp.gseaplot(term=pre.res2d.Term.iloc[0], ofname="running.png",
+ **pre.results[pre.res2d.Term.iloc[0]]) # running-ES curve
+gp.enrichment_map(pre.res2d) # nodes=terms, edges=gene overlap (returns graph)
+```
+`dotplot`/`barplot` return a Matplotlib `Axes`; `get_figure().savefig(...)` to save.
+
+## Result columns
+
+Enrichr (ORA): `Gene_set`, `Term`, `Overlap`, `P-value`, `Adjusted P-value`,
+`Old P-value`, `Old Adjusted P-value`, `Odds Ratio`, `Combined Score`, `Genes`.
+
+GSEA/prerank (`res2d`): `Name`, `Term`, `ES` (enrichment score), `NES`
+(normalized ES — compare across sets), `NOM p-val`, `FDR q-val`, `FWER p-val`,
+`Tag %`, `Gene %`, `Lead_genes` (leading-edge genes driving the signal).
+
+Rank by `NES` for direction/magnitude; filter by `FDR q-val`. Positive NES =
+enriched at the top of the rank (e.g., up in your test condition).
+
+## Troubleshooting
+
+- **Empty / near-empty results** → almost always a gene-ID or organism mismatch.
+ Check overlap: `set(genes) & set(gp.get_library(lib).keys()...)`; confirm symbols
+ and `organism`.
+- **HTTP errors / timeouts from Enrichr or MSigDB** → transient; retry, reduce the
+ number of libraries, or switch to offline `gp.enrich()` with a local GMT.
+- **`prerank` complains about duplicates / non-numeric** → dedupe the index and
+ coerce scores to float; drop NaN before sorting.
+- **Too few genes match a set** → raise `min_size` caution; tiny overlaps are noise.
+- **Different results between runs (GSEA)** → set `seed` and report `permutation_num`.
diff --git a/skills/pathway-enrichment/references/interpretation.md b/skills/pathway-enrichment/references/interpretation.md
new file mode 100644
index 00000000..085d6544
--- /dev/null
+++ b/skills/pathway-enrichment/references/interpretation.md
@@ -0,0 +1,118 @@
+# Interpreting Enrichment Results
+
+## Contents
+- [ORA vs GSEA: the statistics](#ora-vs-gsea-the-statistics)
+- [The background universe (ORA)](#the-background-universe-ora)
+- [Multiple-testing correction](#multiple-testing-correction)
+- [Reading GSEA output](#reading-gsea-output)
+- [Reducing redundant terms](#reducing-redundant-terms)
+- [Significance vs relevance](#significance-vs-relevance)
+- [Reproducibility checklist](#reproducibility-checklist)
+- [Publication table template](#publication-table-template)
+- [Common misinterpretations](#common-misinterpretations)
+
+## ORA vs GSEA: the statistics
+
+**ORA** asks: among my *k* hits (out of a background of *N* genes), are more in
+gene set *S* (size *K*) than expected by chance? This is a hypergeometric /
+Fisher's exact test. It depends entirely on the threshold used to define hits and
+on the background *N*. Good when there is a clear, strong hit list.
+
+**GSEA** asks: walking down the *fully ranked* list of all tested genes, is gene
+set *S* concentrated near the top (or bottom)? It uses a weighted Kolmogorov–
+Smirnov-like running sum; significance comes from permutations. No arbitrary
+threshold; sensitive to coordinated, modest shifts across many genes. Better when
+effects are broad/subtle or when a hit list would be very short or very long.
+
+Rule of thumb: a discrete hit list → ORA; a ranked table with per-gene scores →
+GSEA. They answer different questions and can legitimately disagree.
+
+## The background universe (ORA)
+
+The background (the "domain" / universe) is the set of genes that *could* have
+appeared as a hit. For RNA-seq that is the set of **expressed/tested genes**, not
+all ~20,000 protein-coding genes. Using too large a background makes ordinary
+housekeeping categories look significant — the most common way ORA results
+mislead.
+
+- Enrichr's online API uses fixed per-library backgrounds and largely ignores a
+ custom one. If the background matters for your claim, use **g:Profiler**
+ (`domain_scope='custom'`, `background=...`) or **gseapy `gp.enrich()`** with an
+ explicit `background`.
+- The background should use the same ID namespace as the query and the library.
+
+## Multiple-testing correction
+
+- **Benjamini–Hochberg (FDR)** — default for Enrichr/gseapy (`Adjusted P-value`,
+ `FDR q-val`). Controls expected false-discovery proportion. Use `< 0.05`.
+- **g:SCS** — g:Profiler's default; accounts for the correlated structure of GO
+ and overlapping terms; generally stricter and more appropriate than BH for
+ ontology hierarchies.
+- **Bonferroni** — very conservative; only when you have few, independent tests.
+
+FDR is computed *within a library/run*. Running many libraries multiplies the
+total tests, so report per-library FDR and avoid cherry-picking the one library
+that produced a hit.
+
+## Reading GSEA output
+
+- **NES (normalized enrichment score)** — the headline metric; normalized for set
+ size so it is comparable across sets. Sign = direction (positive = enriched at
+ the top of your ranking, e.g., up in the test condition).
+- **FDR q-val** — significance; filter on this (`< 0.05`, or `< 0.25` for
+ exploratory hypothesis generation, the GSEA convention).
+- **Leading-edge genes** (`Lead_genes`) — the subset of genes that drive the
+ signal (those before the running-sum peak). Report these; they are the concrete
+ biology and are useful for overlap/redundancy analysis.
+
+## Reducing redundant terms
+
+GO and large pathway sets return many overlapping terms describing the same
+biology. Don't list 40 near-duplicates. Options:
+- **Enrichment map** — graph with terms as nodes and edges weighted by gene
+ overlap (Jaccard/overlap coefficient); cluster it and label clusters. gseapy:
+ `gp.enrichment_map(...)`; render with `networkx` (see the networkx skill).
+- **Leading-edge / gene overlap clustering** — group terms sharing most genes;
+ keep one representative per group.
+- **Parent terms / semantic similarity** — collapse child GO terms to a parent;
+ REVIGO-style reduction by semantic similarity.
+- Report a representative term per cluster plus the count of related terms.
+
+## Significance vs relevance
+
+- Check the **overlap count**, not just the p-value. "Term enriched, padj=0.01"
+ with 2 genes out of a 1500-gene set is rarely meaningful.
+- Watch **gene-set size**: tiny sets reach significance with few genes; huge,
+ generic sets ("metabolic process") are uninformative — the `min_size`/`max_size`
+ filters (15–500) exist for this reason.
+- A very short ORA input (<10 genes) is underpowered; a very long one (>2000)
+ loses specificity — prefer GSEA in both extremes.
+
+## Reproducibility checklist
+
+- Record exact **library names and versions/date** (Enrichr/GO libraries drift).
+- Record the **background** used (or state the default).
+- For GSEA, record `permutation_num`, `seed`, `min_size`, `max_size`, weight, and
+ the **ranking metric** (e.g., DESeq2 `stat`).
+- State the **organism** and **gene-ID namespace**.
+- Save the full results table, not just the filtered top hits.
+
+## Publication table template
+
+Report a compact, reviewer-friendly table:
+
+| Term | Source | Direction (NES / Odds Ratio) | Overlap / Set size | FDR | Key genes |
+|------|--------|------------------------------|--------------------|-----|-----------|
+| Interferon alpha response | Hallmark | NES +2.1 | 38/97 | 1e-4 | STAT1, IRF7, ISG15 |
+
+For ORA use Odds Ratio + Overlap (k/K); for GSEA use NES + leading-edge size.
+Note method, library version, background, and correction in the legend.
+
+## Common misinterpretations
+
+- "Enriched pathway X" does **not** mean pathway X is activated — ORA is
+ direction-agnostic unless you split up/down lists; GSEA NES sign gives direction.
+- Overlapping significant GO terms are **not** independent findings.
+- Absence of enrichment ≠ absence of biology (power, annotation gaps, wrong
+ background, or ID mismatch can all hide real signal).
+- Don't compare raw ES across gene sets — use NES.
diff --git a/skills/pathway-enrichment/scripts/run_enrichment.py b/skills/pathway-enrichment/scripts/run_enrichment.py
new file mode 100644
index 00000000..07bf9e4b
--- /dev/null
+++ b/skills/pathway-enrichment/scripts/run_enrichment.py
@@ -0,0 +1,231 @@
+#!/usr/bin/env python3
+"""Run over-representation (ORA) or preranked GSEA with gseapy.
+
+Handles the boilerplate that every enrichment run repeats: symbol cleanup,
+deduplication, NA removal, building a ranking metric from a DESeq2 table,
+per-library FDR filtering, and a dotplot. Outputs a combined results CSV and a
+dotplot PNG into --outdir.
+
+Examples
+--------
+# ORA from a hit list (one gene symbol per line, or a CSV whose first column is genes)
+python run_enrichment.py ora \
+ --genes deg_symbols.txt \
+ --libraries MSigDB_Hallmark_2020 GO_Biological_Process_2023 KEGG_2021_Human \
+ --organism human --outdir results/
+
+# Preranked GSEA from a DESeq2 results CSV (auto-builds rank from `stat`)
+python run_enrichment.py gsea \
+ --deseq2 deseq2_results.csv \
+ --libraries MSigDB_Hallmark_2020 GO_Biological_Process_2023 \
+ --organism human --outdir results/ --seed 123
+
+# Preranked GSEA from an explicit ranked file with columns: gene,score
+python run_enrichment.py gsea --rnk ranked_genes.csv --outdir results/
+
+Requires: gseapy, pandas, matplotlib (uv pip install gseapy).
+Network access is needed for Enrichr / library downloads.
+"""
+from __future__ import annotations
+
+import argparse
+import sys
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+
+try:
+ import gseapy as gp
+except ImportError:
+ print("Error: gseapy not installed. Install with: uv pip install gseapy")
+ sys.exit(1)
+
+DEFAULT_LIBRARIES = [
+ "MSigDB_Hallmark_2020",
+ "GO_Biological_Process_2023",
+ "KEGG_2021_Human",
+ "Reactome_2022",
+]
+
+
+def _clean_symbols(genes, organism: str):
+ """Normalize gene symbols (human -> UPPER, mouse -> Title) and dedupe."""
+ out, seen = [], set()
+ for g in genes:
+ g = str(g).strip()
+ if not g or g.lower() in {"nan", "none"}:
+ continue
+ if organism == "human":
+ g = g.upper()
+ elif organism == "mouse":
+ g = g.capitalize()
+ if g not in seen:
+ seen.add(g)
+ out.append(g)
+ return out
+
+
+def _read_gene_list(path: Path):
+ """Read a gene list: one per line, or the first column of a CSV/TSV."""
+ if path.suffix.lower() in {".csv", ".tsv"}:
+ sep = "\t" if path.suffix.lower() == ".tsv" else ","
+ df = pd.read_csv(path, sep=sep)
+ return df.iloc[:, 0].tolist()
+ return [line.strip() for line in path.read_text().splitlines() if line.strip()]
+
+
+def _build_rank_from_deseq2(path: Path, organism: str) -> pd.Series:
+ """Build a ranking metric from a DESeq2-style results table.
+
+ Prefers the Wald `stat`; otherwise sign(log2FoldChange) * -log10(pvalue).
+ """
+ df = pd.read_csv(path, index_col=0)
+ cols = {c.lower(): c for c in df.columns}
+ if "stat" in cols:
+ rnk = df[cols["stat"]].dropna()
+ elif "log2foldchange" in cols and "pvalue" in cols:
+ lfc = df[cols["log2foldchange"]]
+ pval = df[cols["pvalue"]].clip(lower=1e-300)
+ rnk = (np.sign(lfc) * -np.log10(pval)).dropna()
+ else:
+ sys.exit(
+ "DESeq2 table needs a 'stat' column, or 'log2FoldChange' + 'pvalue'. "
+ f"Found: {list(df.columns)}"
+ )
+ rnk.index = _clean_index(rnk.index, organism)
+ rnk = rnk[~rnk.index.duplicated(keep="first")]
+ return rnk.sort_values(ascending=False)
+
+
+def _read_rnk(path: Path, organism: str) -> pd.Series:
+ """Read an explicit ranked file: columns gene,score (header optional)."""
+ df = pd.read_csv(path, header=None)
+ if df.shape[1] < 2:
+ sys.exit("Ranked file must have two columns: gene,score")
+ # Drop a header row if the score column is not numeric.
+ if not pd.api.types.is_numeric_dtype(pd.to_numeric(df[1], errors="coerce")):
+ df = df.iloc[1:]
+ rnk = pd.Series(
+ pd.to_numeric(df[1].values, errors="coerce"),
+ index=df[0].astype(str).values,
+ ).dropna()
+ rnk.index = _clean_index(rnk.index, organism)
+ rnk = rnk[~rnk.index.duplicated(keep="first")]
+ return rnk.sort_values(ascending=False)
+
+
+def _clean_index(index, organism: str):
+ idx = pd.Index([str(g).strip() for g in index])
+ if organism == "human":
+ idx = idx.str.upper()
+ elif organism == "mouse":
+ idx = idx.str.capitalize()
+ return idx
+
+
+def _dotplot(df: pd.DataFrame, column: str, title: str, outpath: Path):
+ try:
+ ax = gp.dotplot(df, column=column, title=title, top_term=15, cutoff=1.0)
+ fig = ax.get_figure()
+ fig.savefig(outpath, dpi=200, bbox_inches="tight")
+ print(f" dotplot -> {outpath}")
+ except Exception as exc: # plotting is best-effort, never fatal
+ print(f" (dotplot skipped: {exc})")
+
+
+def run_ora(args):
+ genes = _clean_symbols(_read_gene_list(Path(args.genes)), args.organism)
+ if len(genes) < 5:
+ print(f"WARNING: only {len(genes)} genes after cleanup; ORA is underpowered.")
+ background = None
+ if args.background:
+ background = _clean_symbols(_read_gene_list(Path(args.background)), args.organism)
+
+ enr = gp.enrichr(
+ gene_list=genes,
+ gene_sets=args.libraries,
+ organism=args.organism,
+ background=background,
+ outdir=None,
+ )
+ res = enr.results.copy()
+ sig = res[res["Adjusted P-value"] < args.fdr].sort_values("Adjusted P-value")
+ print(f"{len(sig)}/{len(res)} terms with Adjusted P-value < {args.fdr}")
+ return res, sig, "Adjusted P-value"
+
+
+def run_gsea(args):
+ if args.deseq2:
+ rnk = _build_rank_from_deseq2(Path(args.deseq2), args.organism)
+ elif args.rnk:
+ rnk = _read_rnk(Path(args.rnk), args.organism)
+ else:
+ sys.exit("GSEA needs --deseq2 or --rnk")
+ print(f"Ranked {len(rnk)} genes (top: {rnk.index[0]}={rnk.iloc[0]:.2f}, "
+ f"bottom: {rnk.index[-1]}={rnk.iloc[-1]:.2f})")
+
+ pre = gp.prerank(
+ rnk=rnk,
+ gene_sets=args.libraries,
+ min_size=args.min_size,
+ max_size=args.max_size,
+ permutation_num=args.permutations,
+ seed=args.seed,
+ threads=args.threads,
+ outdir=None,
+ )
+ res = pre.res2d.copy()
+ res["FDR q-val"] = pd.to_numeric(res["FDR q-val"], errors="coerce")
+ sig = res[res["FDR q-val"] < args.fdr].sort_values("FDR q-val")
+ print(f"{len(sig)}/{len(res)} gene sets with FDR q-val < {args.fdr}")
+ return res, sig, "FDR q-val"
+
+
+def main():
+ p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ sub = p.add_subparsers(dest="method", required=True)
+
+ common = argparse.ArgumentParser(add_help=False)
+ common.add_argument("--libraries", nargs="+", default=DEFAULT_LIBRARIES,
+ help="Enrichr library names, GMT paths, or MSigDB names.")
+ common.add_argument("--organism", default="human",
+ help="human, mouse, fly, yeast, worm, fish (default: human).")
+ common.add_argument("--outdir", default="enrichment_results", help="Output directory.")
+ common.add_argument("--fdr", type=float, default=0.05, help="Adjusted-p/FDR cutoff.")
+
+ ora = sub.add_parser("ora", parents=[common], help="Over-representation analysis.")
+ ora.add_argument("--genes", required=True, help="Hit list: one symbol per line or CSV first column.")
+ ora.add_argument("--background", help="Optional background gene list file.")
+
+ gsea = sub.add_parser("gsea", parents=[common], help="Preranked GSEA.")
+ gsea.add_argument("--deseq2", help="DESeq2 results CSV (index=genes; uses `stat`).")
+ gsea.add_argument("--rnk", help="Ranked file with columns: gene,score.")
+ gsea.add_argument("--min-size", type=int, default=15, dest="min_size")
+ gsea.add_argument("--max-size", type=int, default=500, dest="max_size")
+ gsea.add_argument("--permutations", type=int, default=1000)
+ gsea.add_argument("--seed", type=int, default=123)
+ gsea.add_argument("--threads", type=int, default=4)
+
+ args = p.parse_args()
+ outdir = Path(args.outdir)
+ outdir.mkdir(parents=True, exist_ok=True)
+
+ if args.method == "ora":
+ res, sig, col = run_ora(args)
+ title = "ORA"
+ else:
+ res, sig, col = run_gsea(args)
+ title = "GSEA (preranked)"
+
+ res_path = outdir / f"{args.method}_results.csv"
+ sig_path = outdir / f"{args.method}_significant.csv"
+ res.to_csv(res_path, index=False)
+ sig.to_csv(sig_path, index=False)
+ print(f"all terms -> {res_path}")
+ print(f"significant -> {sig_path}")
+ _dotplot(sig if len(sig) else res, col, title, outdir / f"{args.method}_dotplot.png")
+
+
+if __name__ == "__main__":
+ main()