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

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

相关推荐
言乐67 小时前
HTML视频审核模型
python·django·virtualenv·pygame·tornado
言乐67 小时前
Python根据无法识别搜索词找出可能输入内容模型
开发语言·python·django·virtualenv·pygame
yl45308 小时前
硫酸泄露处理生产商怎么选才够专业
大数据·人工智能·python
笨笨饿8 小时前
140_AI新手村MCP与Skills是干嘛的
开发语言·人工智能·python·stm32·单片机·嵌入式硬件·物联网
for_ever_love__9 小时前
机器学习入门——手写线性回归与梯度下降
人工智能·python·学习·机器学习·线性回归
打工仔折腾 AI9 小时前
从 Demo 到生产级 Agent:8 个关键设计机制与 Python 实现拆解
java·jvm·人工智能·后端·python·langchain·ai agent 实战
I Am a robert girl9 小时前
当传感器学会“说谎“:拆解可靠性门控的稀疏惯性动捕融合
python·姿态估计·传感器融合·惯性动捕·imu传感器·可靠性门控·可穿戴计算
李航198310 小时前
AI定制柜建模,需要详细的建模规范和标准流程
人工智能·python·计算机视觉·ai·ai编程
沙漠之主10 小时前
Python 教学设计资料:从入门到实战的完整课程方案
爬虫·python
ebiobiz12 小时前
极海 APM32 使用 Nimmake 编译指南
c++·python·单片机·嵌入式硬件·mcu