Delete msp_to_mgf_副本.py
Browse files- msp_to_mgf_副本.py +0 -130
msp_to_mgf_副本.py
DELETED
|
@@ -1,130 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
import os
|
| 3 |
-
import re
|
| 4 |
-
|
| 5 |
-
# =========================================================
|
| 6 |
-
# 1) 读取路径与保存路径(请你按自己的电脑路径修改)
|
| 7 |
-
# =========================================================
|
| 8 |
-
# 示例(macOS):
|
| 9 |
-
MSP_PATH = "/Users/guanmumu/Desktop/Data/Human/extracted/human_hair_selected_with_GVPs_passed.msp"
|
| 10 |
-
MGF_PATH = "/Users/guanmumu/Desktop/Data/human_hair_selected_with_GVPs_passed.mgf"
|
| 11 |
-
|
| 12 |
-
# 如果你想让脚本“放哪儿都能跑”,可以用脚本所在目录:
|
| 13 |
-
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
-
# MSP_PATH = os.path.join(BASE_DIR, "bsa_consensus_final_true_lib.msp")
|
| 15 |
-
# MGF_PATH = os.path.join(BASE_DIR, "bsa_consensus_final_true_lib.pepmass_is_mw.mgf")
|
| 16 |
-
|
| 17 |
-
# =========================================================
|
| 18 |
-
# 2) MSP 字段解析函数
|
| 19 |
-
# =========================================================
|
| 20 |
-
def parse_sequence_from_name(name_val: str) -> str:
|
| 21 |
-
# Name: SEQUENCE/charge_...
|
| 22 |
-
return name_val.split("/")[0].strip()
|
| 23 |
-
|
| 24 |
-
def parse_charge_from_name(name_val: str):
|
| 25 |
-
m = re.search(r"/(\d+)", name_val)
|
| 26 |
-
return int(m.group(1)) if m else None
|
| 27 |
-
|
| 28 |
-
def parse_mz_exact_from_comment(line: str):
|
| 29 |
-
# Comment: ... Mz_exact=536.5844 ...
|
| 30 |
-
m = re.search(r"\bMz_exact=([0-9]*\.?[0-9]+)", line)
|
| 31 |
-
return float(m.group(1)) if m else None
|
| 32 |
-
|
| 33 |
-
# =========================================================
|
| 34 |
-
# 3) 主逻辑:流式读取 MSP,写 MGF
|
| 35 |
-
# =========================================================
|
| 36 |
-
def main():
|
| 37 |
-
spectra_count = 0
|
| 38 |
-
unique_peptides = set()
|
| 39 |
-
|
| 40 |
-
with open(MSP_PATH, "r", encoding="utf-8", errors="replace") as fin, \
|
| 41 |
-
open(MGF_PATH, "w", encoding="utf-8") as fout:
|
| 42 |
-
|
| 43 |
-
name_val = None
|
| 44 |
-
seq = None
|
| 45 |
-
charge = None
|
| 46 |
-
mw = None # 你要求用作 PEPMASS
|
| 47 |
-
precursor_mz = None # 仅用于辅助记录(Mz_exact)
|
| 48 |
-
|
| 49 |
-
for line in fin:
|
| 50 |
-
line = line.rstrip("\n")
|
| 51 |
-
|
| 52 |
-
if line.startswith("Name:"):
|
| 53 |
-
name_val = line.split("Name:", 1)[1].strip()
|
| 54 |
-
seq = parse_sequence_from_name(name_val)
|
| 55 |
-
charge = parse_charge_from_name(name_val)
|
| 56 |
-
unique_peptides.add(seq)
|
| 57 |
-
|
| 58 |
-
elif line.startswith("MW:"):
|
| 59 |
-
try:
|
| 60 |
-
mw = float(line.split("MW:", 1)[1].strip())
|
| 61 |
-
except Exception:
|
| 62 |
-
mw = None
|
| 63 |
-
|
| 64 |
-
elif line.startswith("Comment:"):
|
| 65 |
-
precursor_mz = parse_mz_exact_from_comment(line)
|
| 66 |
-
|
| 67 |
-
elif line.startswith("Num peaks:"):
|
| 68 |
-
# 进入峰列表块
|
| 69 |
-
num_peaks = int(line.split("Num peaks:", 1)[1].strip())
|
| 70 |
-
|
| 71 |
-
spectra_count += 1
|
| 72 |
-
|
| 73 |
-
# ===================== 写一个 MGF block =====================
|
| 74 |
-
fout.write("BEGIN IONS\n")
|
| 75 |
-
|
| 76 |
-
# TITLE:建议写得可追溯
|
| 77 |
-
if seq is not None and charge is not None:
|
| 78 |
-
fout.write(f"TITLE={seq}/z{charge}\n")
|
| 79 |
-
elif seq is not None:
|
| 80 |
-
fout.write(f"TITLE={seq}\n")
|
| 81 |
-
else:
|
| 82 |
-
fout.write(f"TITLE=Spectrum_{spectra_count}\n")
|
| 83 |
-
|
| 84 |
-
# 你要求:PEPMASS = MW(不做换算)
|
| 85 |
-
# 注意:如果 mw 缺失,会跳过 PEPMASS 行
|
| 86 |
-
if mw is not None:
|
| 87 |
-
fout.write(f"PEPMASS={mw:.6f}\n")
|
| 88 |
-
|
| 89 |
-
# 电荷
|
| 90 |
-
if charge is not None:
|
| 91 |
-
fout.write(f"CHARGE={charge}+\n")
|
| 92 |
-
|
| 93 |
-
# 你要的 sequence 列:这里写在 MGF 自定义字段中
|
| 94 |
-
if seq is not None:
|
| 95 |
-
fout.write(f"SEQ={seq}\n")
|
| 96 |
-
|
| 97 |
-
# 辅助字段:把 Comment 里 Mz_exact 也记录下来(不影响你要求)
|
| 98 |
-
if precursor_mz is not None:
|
| 99 |
-
fout.write(f"PRECURSOR_MZ={precursor_mz:.6f}\n")
|
| 100 |
-
|
| 101 |
-
# 写峰:每行 "m/z intensity"
|
| 102 |
-
for _ in range(num_peaks):
|
| 103 |
-
peak_line = next(fin).strip()
|
| 104 |
-
if not peak_line:
|
| 105 |
-
continue
|
| 106 |
-
parts = peak_line.split()
|
| 107 |
-
if len(parts) >= 2:
|
| 108 |
-
mz = parts[0]
|
| 109 |
-
inten = parts[1]
|
| 110 |
-
fout.write(f"{mz} {inten}\n")
|
| 111 |
-
|
| 112 |
-
fout.write("END IONS\n\n")
|
| 113 |
-
|
| 114 |
-
# reset 当前谱状态
|
| 115 |
-
name_val = None
|
| 116 |
-
seq = None
|
| 117 |
-
charge = None
|
| 118 |
-
mw = None
|
| 119 |
-
precursor_mz = None
|
| 120 |
-
|
| 121 |
-
# =========================================================
|
| 122 |
-
# 4) 最终统计输出
|
| 123 |
-
# =========================================================
|
| 124 |
-
print(f"[DONE] MSP input : {MSP_PATH}")
|
| 125 |
-
print(f"[DONE] MGF output: {MGF_PATH}")
|
| 126 |
-
print(f"Spectra count : {spectra_count}")
|
| 127 |
-
print(f"Unique peptides : {len(unique_peptides)}")
|
| 128 |
-
|
| 129 |
-
if __name__ == "__main__":
|
| 130 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|