robert05 commited on
Commit
2fe8356
·
verified ·
1 Parent(s): 3689349

Upload folder using huggingface_hub

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
2011_04_01_bsa_consensus_final_true_lib.tar.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2156f4d7848a698337f933d8600b47a88e1bae6e567c208aa2b62ddc50fbe6d0
3
+ size 949605
bsa_consensus_final_true_lib.mgf ADDED
The diff for this file is too large to render. See raw diff
 
bsa_consensus_final_true_lib.msp ADDED
The diff for this file is too large to render. See raw diff
 
msp_to_mgf.py CHANGED
@@ -1,29 +1,25 @@
1
  # -*- coding: utf-8 -*-
 
2
  import re
3
- from pathlib import Path
4
- from openpyxl import Workbook
5
 
6
  # =========================================================
7
- # 1) 路径配置:改这里即可
8
- # INPUT_DIR : 你截图里的 extracted 目录(存放 .msp)
9
- # OUTPUT_DIR: 输出 .mgf 的目录(会自动创建)
10
- # EXCEL_PATH: 输出统计表
11
  # =========================================================
12
- INPUT_DIR = Path("/Users/guanmumu/Desktop/Data/Human/extracted")
13
- OUTPUT_DIR = Path("/Users/guanmumu/Desktop/Data/Human/mgf")
14
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
15
 
16
- EXCEL_PATH = OUTPUT_DIR / "msp_to_mgf_summary.xlsx"
17
-
18
- # 进度打印(大文件可调)
19
- PROGRESS_EVERY_SPECTRA = 50000
20
 
21
  # =========================================================
22
- # 2) MSP 字段解析函数(保持和你单文件脚本一致)
23
  # =========================================================
24
  def parse_sequence_from_name(name_val: str) -> str:
25
  # Name: SEQUENCE/charge_...
26
- return name_val.split("/", 1)[0].strip()
27
 
28
  def parse_charge_from_name(name_val: str):
29
  m = re.search(r"/(\d+)", name_val)
@@ -35,16 +31,14 @@ def parse_mz_exact_from_comment(line: str):
35
  return float(m.group(1)) if m else None
36
 
37
  # =========================================================
38
- # 3) 转换单个 MSP -> MGF,并返回统计信息
39
- # - Spectra count: Num peaks 次数
40
- # - Unique peptides: 对 Name: 的 seq 去重(与你单文件脚本一致)
41
  # =========================================================
42
- def convert_one_msp(msp_path: Path, mgf_path: Path):
43
  spectra_count = 0
44
  unique_peptides = set()
45
 
46
- with msp_path.open("r", encoding="utf-8", errors="replace") as fin, \
47
- mgf_path.open("w", encoding="utf-8") as fout:
48
 
49
  name_val = None
50
  seq = None
@@ -71,12 +65,15 @@ def convert_one_msp(msp_path: Path, mgf_path: Path):
71
  precursor_mz = parse_mz_exact_from_comment(line)
72
 
73
  elif line.startswith("Num peaks:"):
 
74
  num_peaks = int(line.split("Num peaks:", 1)[1].strip())
 
75
  spectra_count += 1
76
 
77
- # ===== 写一个 MGF block =====
78
  fout.write("BEGIN IONS\n")
79
 
 
80
  if seq is not None and charge is not None:
81
  fout.write(f"TITLE={seq}/z{charge}\n")
82
  elif seq is not None:
@@ -85,94 +82,49 @@ def convert_one_msp(msp_path: Path, mgf_path: Path):
85
  fout.write(f"TITLE=Spectrum_{spectra_count}\n")
86
 
87
  # 你要求:PEPMASS = MW(不做换算)
 
88
  if mw is not None:
89
  fout.write(f"PEPMASS={mw:.6f}\n")
90
 
 
91
  if charge is not None:
92
  fout.write(f"CHARGE={charge}+\n")
93
 
94
- # 自定义字段
95
  if seq is not None:
96
  fout.write(f"SEQ={seq}\n")
97
 
98
- # 可选:记录 Mz_exact(不影响你要求)
99
  if precursor_mz is not None:
100
  fout.write(f"PRECURSOR_MZ={precursor_mz:.6f}\n")
101
 
102
- # 峰:每行 "m/z intensity"
103
  for _ in range(num_peaks):
104
  peak_line = next(fin).strip()
105
  if not peak_line:
106
  continue
107
  parts = peak_line.split()
108
  if len(parts) >= 2:
109
- fout.write(f"{parts[0]} {parts[1]}\n")
 
 
110
 
111
  fout.write("END IONS\n\n")
112
 
113
- # reset 当前谱状态(与你单文件脚本一致)
114
  name_val = None
115
  seq = None
116
  charge = None
117
  mw = None
118
  precursor_mz = None
119
 
120
- if spectra_count % PROGRESS_EVERY_SPECTRA == 0:
121
- print(f" ... {msp_path.name}: spectra={spectra_count}")
122
-
123
- return spectra_count, len(unique_peptides)
124
-
125
- # =========================================================
126
- # 4) 写 Excel(3列:文件名 / Spectra count / Unique peptides
127
- # =========================================================
128
- def write_excel(excel_path: Path, rows):
129
- wb = Workbook()
130
- ws = wb.active
131
- ws.title = "summary"
132
-
133
- for r in rows:
134
- ws.append(list(r))
135
-
136
- ws.column_dimensions["A"].width = 55
137
- ws.column_dimensions["B"].width = 18
138
- ws.column_dimensions["C"].width = 20
139
-
140
- wb.save(excel_path)
141
-
142
- # =========================================================
143
- # 5) 批量处理
144
- # =========================================================
145
- def main():
146
- if not INPUT_DIR.exists():
147
- raise FileNotFoundError(f"INPUT_DIR not found: {INPUT_DIR}")
148
-
149
- msp_files = sorted(INPUT_DIR.glob("*.msp"))
150
- if not msp_files:
151
- print(f"No .msp files found in: {INPUT_DIR}")
152
- return
153
-
154
- print(f"Input dir : {INPUT_DIR}")
155
- print(f"Output dir: {OUTPUT_DIR}")
156
- print(f"Found {len(msp_files)} MSP files.")
157
-
158
- rows = [("file_name", "spectra_count", "unique_peptides")]
159
-
160
- for idx, msp_path in enumerate(msp_files, 1):
161
- mgf_path = OUTPUT_DIR / (msp_path.stem + ".mgf")
162
-
163
- print(f"\n[{idx}/{len(msp_files)}] Converting: {msp_path.name}")
164
- spectra_count, unique_pep = convert_one_msp(msp_path, mgf_path)
165
-
166
- print(f" -> MGF: {mgf_path.name}")
167
- print(f" Spectra count : {spectra_count}")
168
- print(f" Unique peptides : {unique_pep}")
169
-
170
- rows.append((msp_path.name, spectra_count, unique_pep))
171
-
172
- write_excel(EXCEL_PATH, rows)
173
-
174
- print("\n================= DONE =================")
175
- print(f"Excel summary: {EXCEL_PATH}")
176
 
177
  if __name__ == "__main__":
178
  main()
 
1
  # -*- coding: utf-8 -*-
2
+ import os
3
  import re
 
 
4
 
5
  # =========================================================
6
+ # 1) 读取路径与保存路径(请你按自己的电脑路径修改)
 
 
 
7
  # =========================================================
8
+ # 示例(macOS):
9
+ MSP_PATH = "/Users/guanmumu/Desktop/Data/BSA/bsa_consensus_final_true_lib.msp"
10
+ MGF_PATH = "/Users/guanmumu/Desktop/Data/BSA/bsa_consensus_final_true_lib.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)
 
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
 
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:
 
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()