92 lines
4.2 KiB
Python
92 lines
4.2 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
18_mitocarta_extract.py — 将 ref/Mouse.MitoCarta3.0.xls 结构化提取为可复用资产
|
||
|
||
产出(按用户指示存回 ref/mitocarta3.0/,属"参考资产结构化"而非改动原始文件):
|
||
1. Mouse_MitoCarta3.0_genes.csv 1140 个小鼠线粒体基因主表(Symbol/亚线粒体定位/通路归属/证据)
|
||
2. MitoPathways.csv 149 条 MitoPathway,层级拆分为 L1/L2/L3 + 基因数
|
||
3. MitoPathways_long.csv 基因 × 通路 长表(一个基因可属多条通路)
|
||
4. MitoPathways.gmt GMT 格式(全层级路径为 term 名),供 gseapy prerank
|
||
5. MitoPathways_L1.gmt 仅一级通路(8 大类),供粗粒度模块打分
|
||
6. README.md 来源与字段说明
|
||
"""
|
||
from pathlib import Path
|
||
|
||
import pandas as pd
|
||
|
||
SRC = Path("ref/Mouse.MitoCarta3.0.xls")
|
||
OUT = Path("ref/mitocarta3.0")
|
||
OUT.mkdir(parents=True, exist_ok=True)
|
||
|
||
xl = pd.ExcelFile(SRC)
|
||
|
||
# ---- 1. 主基因表 ----
|
||
genes = xl.parse("A Mouse MitoCarta3.0")[
|
||
["MouseGeneID", "Symbol", "HumanOrthologGeneID", "Description",
|
||
"MitoCarta3.0_Evidence", "MitoCarta3.0_SubMitoLocalization",
|
||
"MitoCarta3.0_MitoPathways"]
|
||
].rename(columns={
|
||
"MitoCarta3.0_Evidence": "Evidence",
|
||
"MitoCarta3.0_SubMitoLocalization": "SubMitoLocalization",
|
||
"MitoCarta3.0_MitoPathways": "MitoPathways",
|
||
})
|
||
genes = genes[genes["Symbol"].notna()].reset_index(drop=True)
|
||
genes.to_csv(OUT / "Mouse_MitoCarta3.0_genes.csv", index=False)
|
||
print(f"genes: {len(genes)}")
|
||
|
||
# ---- 2. 通路表 + 层级拆分 ----
|
||
pw = xl.parse("C MitoPathways")
|
||
pw["n_genes"] = pw["Genes"].str.split(", ").str.len()
|
||
hier = pw["MitoPathway Hierarchy"].str.split(" > ", expand=True)
|
||
hier.columns = [f"L{i+1}" for i in range(hier.shape[1])]
|
||
pw = pd.concat([pw, hier], axis=1)
|
||
pw.to_csv(OUT / "MitoPathways.csv", index=False)
|
||
print(f"pathways: {len(pw)}; L1 大类:")
|
||
print(pw.groupby("L1")["n_genes"].agg(["count", "sum"]).to_string())
|
||
|
||
# ---- 3. 长表(基因 × 通路) ----
|
||
rows = []
|
||
for _, r in pw.iterrows():
|
||
for g in str(r["Genes"]).split(", "):
|
||
rows.append({"Symbol": g.strip(), "MitoPathway": r["MitoPathway"],
|
||
"Hierarchy": r["MitoPathway Hierarchy"], "L1": r["L1"]})
|
||
long = pd.DataFrame(rows)
|
||
long.to_csv(OUT / "MitoPathways_long.csv", index=False)
|
||
print(f"long rows: {len(long)}; unique genes in pathways: {long['Symbol'].nunique()}")
|
||
|
||
# ---- 4/5. GMT ----
|
||
def write_gmt(df, term_col, path):
|
||
with open(path, "w", encoding="utf-8") as f:
|
||
for _, r in df.iterrows():
|
||
gl = ", ".join(sorted(set(str(r["Genes"]).split(", "))))
|
||
f.write(f"{r[term_col]}\tMitoCarta3.0\t{gl.replace(', ', chr(9))}\n")
|
||
|
||
write_gmt(pw, "MitoPathway Hierarchy", OUT / "MitoPathways.gmt")
|
||
l1 = long.groupby("L1")["Symbol"].agg(lambda s: sorted(set(s))).reset_index()
|
||
with open(OUT / "MitoPathways_L1.gmt", "w", encoding="utf-8") as f:
|
||
for _, r in l1.iterrows():
|
||
f.write(f"{r['L1']}\tMitoCarta3.0\t" + "\t".join(r["Symbol"]) + "\n")
|
||
|
||
# ---- 6. README ----
|
||
(OUT / "README.md").write_text(f"""# MitoCarta3.0(小鼠)结构化提取
|
||
|
||
来源:`ref/Mouse.MitoCarta3.0.xls`(Broad Institute MitoCarta3.0,Rath et al. 2021, NAR)。
|
||
由 `script/18_mitocarta_extract.py` 生成,勿手改。
|
||
|
||
| 文件 | 内容 |
|
||
|---|---|
|
||
| `Mouse_MitoCarta3.0_genes.csv` | {len(genes)} 个线粒体定位基因:Symbol、亚线粒体定位(Matrix/MIM/MOM/IMS)、所属 MitoPathways、证据 |
|
||
| `MitoPathways.csv` | {len(pw)} 条通路 + 层级(L1/L2/L3)+ 基因数 |
|
||
| `MitoPathways_long.csv` | 基因 × 通路长表,{long['Symbol'].nunique()} 个基因 |
|
||
| `MitoPathways.gmt` | 全层级 term 的 GMT,供 gseapy prerank |
|
||
| `MitoPathways_L1.gmt` | {len(l1)} 个一级大类 GMT,供模块打分 |
|
||
|
||
与本项目分析的直接衔接:
|
||
- 模块打分可用 L1/L2 级基因集(如 OXPHOS > Complex I)替代 P0/P1 手工整理的 ETC/CI 列表做敏感性核对;
|
||
- GSEA prerank 可用 `MitoPathways.gmt` 补充 Hallmark 覆盖不到的线粒体特异通路
|
||
(mtDNA maintenance、mito-ribosome、Fe-S cluster、mitophagy 等);
|
||
- 基因 Symbol 为小鼠原始大小写,与本项目 var_names 直接兼容(已核实分隔符为 ", ")。
|
||
""", encoding="utf-8")
|
||
print("Done ->", OUT)
|