Scanpy 富集分析实战:gseapy 从基因列表到通路解读

差异表达分析跑完了,拿到一列 DEG------然后呢?

基因名本身没有意义,通路才有意义。富集分析就是把你的一列基因翻译成生物学家能理解的"故事"。

Python 生态里做富集分析最好用的工具是 gseapy,今天完整拆解。

1. 安装与基本用法

复制代码
pip install gseapy pandas matplotlib
ini 复制代码
import gseapy as gp
import pandas as pd
​
# 方法1:直接传基因列表(Over-Representation Analysis, ORA)
gene_list = ['CD79A', 'MS4A1', 'CD19', 'CD22', 'PAX5',
             'EBF1', 'BANK1', 'FCRL5', 'TCL1A', 'BLNK']
​
enr = gp.enrichr(
    gene_list=gene_list,
    organism='Human',
    gene_sets=['GO_Biological_Process_2023', 'KEGG_2021_Human'],
    outdir='enrichr_results',
    cutoff=0.5
)
​
# 查看结果
print(enr.res2d.head(10))

2. GSEA 分析(需要排序列表)

GSEA 比 ORA 更严谨------不需要硬性阈值切分基因。

ini 复制代码
# 从 Scanpy 提取排好序的基因列表
import scanpy as sc
import numpy as np
​
adata = sc.datasets.pbmc68k_reduced()
sc.tl.rank_genes_groups(adata, groupby='louvain', method='wilcoxon')
​
# 提取某个 cluster 的 DEG 并按 logFC 排序
df = sc.get.rank_genes_groups_df(adata, group='1')  # B cells
df = df.dropna(subset=['logfoldchanges', 'pvals_adj'])
df = df.sort_values('logfoldchanges', ascending=False)
​
# 构造 GSEA 需的 ranked gene list
rnk = pd.Series(
    df['logfoldchanges'].values,
    index=df['names'].values
)
​
# GSEA
gs = gp.prerank(
    rnk=rnk,
    gene_sets=['GO_Biological_Process_2023', 'KEGG_2021_Human'],
    outdir='gsea_results',
    permutation_num=1000,
    seed=42,
    min_size=10,
    max_size=500
)
​
# 经典 GSEA 山脊图
from gseapy.plot import gseaplot
terms = gs.res2d.Term[:3]
for term in terms:
    gseaplot(gs.res2d, term=term, ofname=f'gsea_{term[:20]}.pdf')

3. 多个 Cluster 批量富集

ini 复制代码
# 批量分析每个 cluster 的 Top DEG
all_results = []
​
for cluster in adata.obs['louvain'].unique():
    df = sc.get.rank_genes_groups_df(adata, group=cluster)
    top_genes = df[df['pvals_adj'] < 0.05].head(200)['names'].tolist()
​
    if len(top_genes) < 5:
        continue
​
    enr = gp.enrichr(
        gene_list=top_genes,
        organism='Human',
        gene_sets=['GO_Biological_Process_2023'],
        outdir=f'enrichr_cluster_{cluster}',
        cutoff=0.5,
        no_plot=True
    )
​
    result = enr.res2d.copy()
    result['cluster'] = cluster
    all_results.append(result)
​
# 合并所有结果
combined = pd.concat(all_results, ignore_index=True)
combined.to_csv('enrichment_all_clusters.csv', index=False)

4. 可视化

ini 复制代码
import matplotlib.pyplot as plt
​
# ── 气泡图 ──
from gseapy import dotplot
dotplot(enr.res2d, title='GO Enrichment', cutoff=0.05,
        top_term=15, figsize=(6, 8))
plt.savefig('enrichment_dotplot.pdf', dpi=300, bbox_inches='tight')
​
# ── 条形图 ──
from gseapy import barplot
barplot(enr.res2d, title='KEGG Pathway', top_term=10,
        figsize=(6, 6), color='steelblue')
plt.savefig('enrichment_barplot.pdf', dpi=300, bbox_inches='tight')
​
# ── 多 Cluster 通路热图 ──
# 把各 cluster 的 top 通路整理成矩阵
pivot = combined.pivot_table(
    index='Term', columns='cluster',
    values='Adjusted P-value', aggfunc='min'
)
pivot = -np.log10(pivot + 1e-10)  # 取 -log10(pvalue)
​
import seaborn as sns
fig, ax = plt.subplots(figsize=(12, 8))
sns.heatmap(pivot.head(20), cmap='Reds', annot=True, fmt='.1f',
            linewidths=0.5, ax=ax)
ax.set_title('Enrichment Heatmap by Cluster')
plt.savefig('enrichment_heatmap.pdf', dpi=300, bbox_inches='tight')

5. 常用基因集数据库

数据库 适用场景 gseapy 名称
GO BP 通用生物过程 GO_Biological_Process_2023
GO CC 细胞组分 GO_Cellular_Component_2023
GO MF 分子功能 GO_Molecular_Function_2023
KEGG 代谢通路 KEGG_2021_Human
Reactome 信号通路 Reactome_2022
MSigDB 癌症 hallmark MSigDB_Hallmark_2020
CellMarker 细胞标记 Cell_Marker_2022

富集分析是从"基因列表"到"生物学故事"的桥梁。但通路解读本身需要领域知识------不是所有显著通路都跟你研究相关,需要人工筛选和判断。

相关推荐
特立独行的猫A31 分钟前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
吃我一窝jio3 小时前
Python与Go线程模型深度对比
java·开发语言·python
zwd20054 小时前
Manim 怎么放大画面的某一部分?
python·动画·可视化·manim·数学动画
屿华AI5 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
RobinDevNotes5 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
如夏倾城6 小时前
VRRP(虚拟路由器冗余协议Virtual Router Redundancy Protocol)
网络·python·网络协议·安全·智能路由器·负载均衡·信息与通信
IvanCodes7 小时前
LangChain(04):Runnable 与 LCEL 的执行与组合机制
python·langchain·agent
特创数字科技7 小时前
纯本地离线运行的文档脱敏工具(技术分享)
前端·驱动开发·python
东莞市云毅网络有限公司8 小时前
用 Playwright 搭一套问答平台引用监测的最小骨架
python·数据清洗·rag·企业知识库·文档解析
Thanks_ks8 小时前
【第 010 讲】全面掌握 Python 变量与常量:变量的定义与赋值 | 标识符与命名规范 | 变量的底层机制 | 常量与魔术数字
python·内存·变量·标识符·常量·赋值·命名规范