# Pathway Enrichment > Ejecuta e interpreta análisis de enriquecimiento de rutas y conjuntos de genes (ORA, GSEA preranked, ssGSEA/GSVA) con gseapy, g:Profiler, MSigDB, GO, KEGG, Reactome y WikiPathways. Fuente: https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/pathway-enrichment Markdown: https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/pathway-enrichment.md Repositorio: https://github.com/K-Dense-AI/scientific-agent-skills Autor: K-Dense-AI Licencia: MIT Actualizado: el mes pasado Coste de contexto: 253 tok instalada, 3.2k tok al activarse, 10.3k tok con todos los archivos del bundle Bundle: 5 archivos, 40 KB Permisos que pide: ninguno declarado ## Instalación Un skill son archivos markdown: los mismos archivos valen para cualquier agente y lo único que cambia es el directorio de destino, es decir la bandera `--agent`. Añade `-g` para instalarlo en todos los proyectos de la máquina. ```bash # Claude Code npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent claude-code # Cursor npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent cursor # Codex npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent codex # Gemini CLI npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent gemini # Windsurf npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent windsurf # Cline npx -y skills add K-Dense-AI/scientific-agent-skills --skill pathway-enrichment --agent cline ``` ## Qué hace - Ejecuta análisis de enriquecimiento de rutas y conjuntos de genes (ORA o GSEA) sobre listas de genes o datos rankeados - Ayuda a elegir entre ORA (lista con umbral) y GSEA (lista completa rankeada) según los datos disponibles - Cubre gseapy, g:Profiler, MSigDB, GO, KEGG, Reactome y WikiPathways, además del mapeo de IDs de genes - Corrige errores comunes: fondo incorrecto en ORA, umbral aplicado antes de GSEA, mezcla de organismos o símbolos - Genera dotplots, mapas de enriquecimiento y tablas listas para publicación con `scripts/run_enrichment.py` ## Cuándo usarla - Se busca qué rutas biológicas, términos GO o conjuntos de genes están sobrerrepresentados en una lista de genes - Se quiere correr GSEA o preranked GSEA sobre salidas de PyDESeq2, edgeR, limma o Scanpy - Se necesita puntuar actividad de rutas por muestra o célula (ssGSEA/GSVA) - Hay que interpretar, deduplicar o visualizar resultados de enriquecimiento ## Cuándo no - Es solo una consulta puntual y rápida en Enrichr: la skill `gget` (`gget enrichr`) es más ligera para eso ## Qué la activa - "Haz un análisis de enriquecimiento GO sobre estos genes diferencialmente expresados" - "Corre GSEA preranked con los resultados de DESeq2" - "¿Qué rutas KEGG están enriquecidas en estos hits del screen CRISPR?" - "Puntúa la actividad de esta ruta por célula con ssGSEA" ## Antes de instalar - Necesita `uv pip install gseapy gprofiler-official` y acceso a red para Enrichr, g:Profiler y descargas de MSigDB; el ORA offline puede usar un archivo GMT local. - Necesita en el PATH: python ## Archivos - SKILL.md — 12 KB - references/databases-and-gene-sets.md — 6 KB - references/gseapy.md — 8 KB - references/interpretation.md — 6 KB - scripts/run_enrichment.py — 8 KB ## SKILL.md Reproducido tal cual desde K-Dense-AI/scientific-agent-skills bajo MIT. Esta sección es el documento original y está en inglés. # 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 ## Dónde encaja - Categoría: [Investigación](https://skillsagentes.com/categorias/investigacion.md) — Investigación estructurada, búsqueda de fuentes y síntesis. - Creador: [K-Dense-AI](https://skillsagentes.com/creators/k-dense-ai.md) — 163 skills en el directorio - [Todas las skills](https://skillsagentes.com/skills.md) - [Ranking de instalaciones](https://skillsagentes.com/ranking.md) ## Otras skills del mismo repositorio - [Citation Management](https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/citation-management.md): Gestión integral de citas académicas: busca en OpenAlex, PubMed y Google Scholar, extrae metadatos precisos, valida citas y genera entradas BibTeX correctamente formateadas. - [Scientific Slides](https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/scientific-slides.md): Crea decks de diapositivas y presentaciones para charlas de investigación: PowerPoint, presentaciones de conferencia, seminarios, defensas de tesis. Da estructura, plantillas, guía de tiempos y validación visual. - [Literature Review](https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/literature-review.md): Realiza revisiones bibliográficas sistemáticas y completas usando varias bases académicas (PubMed, arXiv, bioRxiv, Semantic Scholar). Genera markdown y PDF con citas verificadas en varios estilos (APA, Nature, Vancouver). - [Infographics](https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/infographics.md): Crea infografías profesionales con Nano Banana Pro AI y refinamiento iterativo inteligente. Usa Gemini 3.6 Flash para revisar la calidad e integra investigación con Perplexity Sonar. Soporta 10 tipos, 8 estilos y paletas para daltonismo. - [Latex Posters](https://skillsagentes.com/skills/k-dense-ai/scientific-agent-skills/latex-posters.md): Crea pósteres de investigación profesionales en LaTeX con beamerposter, tikzposter o baposter, para conferencias y comunicación científica: layout, colores, columnas múltiples e integración de figuras. --- Skills Agentes · [Índice de páginas en markdown](https://skillsagentes.com/sitemap.md) · [Inicio](https://skillsagentes.com/index.md)