-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdataset_utils.py
More file actions
1285 lines (1108 loc) · 50.8 KB
/
dataset_utils.py
File metadata and controls
1285 lines (1108 loc) · 50.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import ppx
import re
import shutil
import subprocess
import pandas as pd
from tqdm import tqdm
from pyteomics import fasta, mgf
from dataset_config import Config, DataDownloadConfig
from dotenv import load_dotenv
load_dotenv()
DATA_DIR = os.environ['DATA_DIR']
WORK_DIR = os.environ['WORK_DIR']
ROOT = os.environ['ROOT']
FRAGPIPE_DIR = os.environ['FRAGPIPE_DIR']
# Path to msconvert apptainer container
MSCONVERT_PATH = os.path.join(WORK_DIR, "benchmarking", "pwiz-skyline-i-agree-to-the-vendor-licenses_latest.sif")
# Path to MSFragger DB split script (for running DB search with large DB)
SPLIT_SCRIPT_PATH = os.path.join(FRAGPIPE_DIR, "FragPipe/21.1-Java-11/tools/msfragger_pep_split.py")
# Path to MSFragger executable file (jar)
MSFRAGGER_PATH = os.path.join(DATA_DIR, "easybuild", "build", "MSFragger-4.0", "MSFragger-4.0.jar")
MSFRAGGER_BASE_PARAMS = os.path.join(WORK_DIR, "benchmarking", "configs", "default_closed.params")
# Path to MSBooster executable file (jar)
MSBOOSTER_PATH = os.path.join(FRAGPIPE_DIR, "MSBooster", "1.2.31-Java-11", "MSBooster-1.2.31.jar")
DIANN_PATH = os.path.join(FRAGPIPE_DIR, "FragPipe/21.1-Java-11/tools/diann/1.8.2_beta_8/linux/diann-1.8.1.8")
KOINA_URL = "https://koina.wilhelmlab.org:443/v2/models/"
MSBOOSTER_BASE_PARAMS = os.path.join(WORK_DIR, "benchmarking", "params_rescore", "msbooster_base.params")
# Add MSGF+ path
MSGF_PATH = os.path.join(DATA_DIR, "tools", "MSGFPlus.jar")
# Add Comet path
COMET_PATH = os.path.join(DATA_DIR, "tools", "comet.linux.exe")
COMET_BASE_PARAMS = os.path.join(WORK_DIR, "benchmarking", "configs", "comet.params")
# Spectrum params order for saving labeled mgf files
MGF_KEY_ORDER = ["title", "pepmass", "rtinseconds", "charge", "scans", "seq"]
# Path to the file with datasets properties (tags)
DATASET_TAGS_PATH = os.path.join(ROOT, "denovo_benchmarks", "dataset_tags.tsv")
PROTEOMES_DIR = os.path.join(ROOT, "proteomes")
RAW_DATA_DIR = os.path.join(ROOT, "raw")
MZML_DATA_DIR = os.path.join(ROOT, "mzml")
RESCORED_DATA_DIR = os.path.join(ROOT, "rescored")
DATASET_STORAGE_DIR = os.path.join(DATA_DIR, "benchmarking", "datasets")
def get_files_list(dset_name: str, config: Config):
"""
Select raw spectra files for the dataset based on
selection rules defined in the download_config such as:
- file extension;
- number of files;
- inclusion/exclusion keywords in the filename.
Args:
dset_name (str): Dataset name.
config (Config): Config with dataset
selection criteria, including spectral dataset ID,
file extension for download and for DB search,
the inclusion/exclusion keywords,
or file download links, and the maximum number of files.
Returns:
dict: A dictionary where keys are file names (without extensions)
and values are their corresponding paths
to the raw or mzML spectra file within the dataset folder.
"""
def check_file(file_path, ext, links=None):
"""Check if file matches criteria."""
if links is not None:
# If downloading links provided, check if filename is in the list
if file_path not in links:
return False
if not file_path.lower().endswith(ext.lower()):
return False
if any(keyword not in file_path for keyword in config.download.keywords):
return False
if any(keyword in file_path for keyword in config.download.exclude_keywords):
return False
return True
dset_id = config.download.dset_id
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
raw_files_dir = os.path.join(RAW_DATA_DIR, dset_id)
download_ext = config.download.ext
db_search_ext = config.db_search.ext
# Step 1: Check for existing mzML files
if os.path.exists(mzml_files_dir):
links = config.download.links
links = [os.path.basename(link) for link in links] if links else None
links = [link[:-len(download_ext)] + db_search_ext for link in links] if links else None
mzml_files = [
os.path.join(mzml_files_dir, f)
for f in os.listdir(mzml_files_dir)
if check_file(f, ext=db_search_ext)
]
if len(mzml_files) >= config.download.n_files:
files_list = {
os.path.basename(file_path)[:-len(db_search_ext)]: file_path # Remove ".mzML"
for file_path in mzml_files[:config.download.n_files]
}
return files_list
# Step 2: Check for existing raw files
if os.path.exists(raw_files_dir):
links = config.download.links
links = [os.path.basename(link) for link in links] if links else None
raw_files = [
os.path.join(raw_files_dir, f)
for f in os.listdir(raw_files_dir)
if check_file(f, ext=download_ext, links=links)
]
if len(raw_files) >= config.download.n_files:
files_list = {
os.path.basename(file_path)[:-len(download_ext)]: file_path
for file_path in raw_files[:config.download.n_files]
}
return files_list
# Step 3: Connect to Pride/Massive repository
if "PXD" in dset_id or "MSV" in dset_id:
proj = ppx.find_project(
dset_id,
local=raw_files_dir,
)
files_list = [
file_path
for file_path
in proj.remote_files()
if check_file(file_path, ext=download_ext)
]
else:
# If load via wget, file_path is just fname.ext
files_list = [
os.path.basename(file_link)
for file_link
in config.download.links
# if check_file(file_link, ext=download_ext)
]
files_list = files_list[:config.download.n_files]
files_list = {
os.path.basename(file_path)[:-len(download_ext)]: file_path
for file_path
in files_list
}
return files_list
def download_files(download_config, files_list):
"""
Download files from the `files_list` to a local folder.
Args:
download_config (DataDownloadConfig): config with dataset details.
files_list (dict): Dictionary where keys are filenames (without extensions)
and values are full file paths.
"""
dset_id = download_config.dset_id
dset_dir = os.path.join(RAW_DATA_DIR, dset_id)
print(f"Loading dataset {dset_id} to the folder {dset_dir}")
if "PXD" in dset_id or "MSV" in dset_id:
proj = ppx.find_project(
dset_id,
local=dset_dir,
)
print("Local files:", proj.local_files())
# select files to download
fnames = list(files_list.values())
if download_config.ext == ".wiff":
fnames += [fname + ".scan" for fname in fnames]
proj.download(fnames)
print("Loaded files:\n", proj.local_files()) # TODO: remove
else:
# if load via wget, need to take download_links from downloag_config for each file
file_links = {
os.path.basename(file_link)[:-len(download_config.ext)]: file_link
for file_link
in download_config.links
}
print("Local files:", os.listdir(dset_dir))
for fname, file_path in files_list.items():
if not os.path.exists(os.path.join(dset_dir, file_path)):
cmd = [
"wget",
"-P",
dset_dir,
file_links[fname]
]
subprocess.run(" ".join(cmd), shell=True, check=True)
print("Loaded files:", os.listdir(dset_dir))
def convert_raw(dset_id, files_list, target_dir, target_ext=".mzml"):
os.makedirs(target_dir, exist_ok=True)
dset_dir = os.path.join(RAW_DATA_DIR, dset_id)
raw_file_pathes = {
fname: os.path.join(dset_dir, file_path)
for fname, file_path
in files_list.items()
}
print("Files:\n", list(raw_file_pathes.values()))
print(f"Converting to {target_ext}. Storing to {target_dir}")
ext_flag = "--mgf" if target_ext == ".mgf" else "--mzML"
filter_ms_level = 2 if target_ext == ".mgf" else 1
for fname, file_path in tqdm(raw_file_pathes.items()):
# Skip the conversion:
# - for .mzML: if the target file filename.mzML already exists in files_dir,
if target_ext == ".mzml":
out_fname = fname + ".mzML"
target_file = os.path.join(target_dir, out_fname)
if os.path.exists(target_file):
print(f"Skipping {fname}.mzML, already exists in {target_dir}")
continue
# - for .mgf: if any file matching the pattern filename_{i}.mgf
# exists in files_dir
elif target_ext == ".mgf":
out_fname = fname + ".mgf"
target_file = os.path.join(target_dir, out_fname)
if os.path.exists(target_file):
print(f"Skipping {fname}.mgf, already exists in {target_dir}")
continue
out_fname = fname + target_ext
cmd = [
"apptainer",
"exec",
"--cleanenv",
MSCONVERT_PATH,
"wine msconvert",
ext_flag,
"-z",
"-o",
target_dir,
"--outfile",
out_fname,
"--filter",
'"peakPicking vendor"',
"--filter",
'"precursorRefine"',
"--filter",
f'"msLevel {filter_ms_level}-"',
"--filter",
'"titleMaker <RunId>.<ScanNumber>.<ScanNumber>.<ChargeState>"'
# '"titleMaker <RunId>.<Index>.<ChargeState>.<MsLevel>"'
]
cmd += [file_path]
subprocess.run(" ".join(cmd), shell=True, check=True)
print(os.listdir(target_dir))
def generate_decoys_fasta(dset_name, db_file, contam_only=False):
"""
Add decoys and common contaminants to the reference database
with FragPipe Philosopher.
Only run if there is no existing database with decoys.
"""
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
db_path = os.path.join(PROTEOMES_DIR, db_file)
def check_fasta_filename(fname):
db_fname = db_file.split("/")[-1]
if contam_only:
return (f"contam-{db_fname}" in fname) and ("decoys-" not in fname)
return f"decoys-contam-{db_fname}" in fname
# Check if database with decoys already exists
existing_db_w_decoys = [
fname for fname in os.listdir(mzml_files_dir)
if fname.endswith(".fas") and check_fasta_filename(fname)
]
print("Existing databases with decoys:\n", existing_db_w_decoys)
if existing_db_w_decoys:
db_w_decoys_path = existing_db_w_decoys[0]
else:
cmd = [
"cd",
mzml_files_dir,
"&&",
"philosopher workspace --init",
"&&",
"philosopher database",
"--custom",
db_path,
"--contam",
]
if contam_only:
cmd += ["--nodecoys"]
subprocess.run(" ".join(cmd), shell=True, check=True)
db_w_decoys_path = [
fname for fname in os.listdir(mzml_files_dir)
if fname.endswith(".fas") and check_fasta_filename(fname)
][0]
db_w_decoys_path = os.path.join(mzml_files_dir, db_w_decoys_path)
return db_w_decoys_path
def get_extended_db_path(db_path):
db_path = db_path.split("/")
db_path[-1] = "extended-" + db_path[-1]
return "/".join(db_path)
def get_extended_db_w_decoys_path(db_w_decoys_path):
db_w_decoys_path = db_w_decoys_path.split("/")
mzml_files_dir = db_w_decoys_path[:-1]
db_w_decoys = db_w_decoys_path[-1]
db_w_decoys = db_w_decoys.split("-")
db_w_decoys.insert(3, "extended")
db_w_decoys = "-".join(db_w_decoys)
db_w_decoys_path = mzml_files_dir + [db_w_decoys]
return "/".join(db_w_decoys_path)
def append_pool_peptides(add_pool_path, db_path):
with open(add_pool_path, 'r') as f_in, open(db_path, 'a') as f_out:
skip = True
for line in f_in:
if line.startswith('>'): # a header
skip = line.startswith('>QC')
if not skip: # write the header
f_out.write(line)
elif not skip: # write the following sequence
f_out.write(line)
print(f"Pool peptides from '{add_pool_path}' appended to '{db_path}'.")
def prepare_synthetic_fasta(dset_name, files_list, pool_proteomes_dir, PT_pools_df):
files_db_w_decoys = {}
for fname in files_list:
print("File:", fname)
sample_idx = PT_pools_df[PT_pools_df["sample"] == fname].index[0]
pool_fasta = PT_pools_df.loc[sample_idx, "fasta"]
print(sample_idx, pool_fasta)
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
db_path = os.path.join(mzml_files_dir, pool_fasta)
if os.path.exists(db_path):
print("Original DB already exists:", db_path)
else:
# if DB doesn't exist yet, copy pool database to the mzml_files_dir
source_db_path = os.path.join(pool_proteomes_dir, pool_fasta)
cmd = f"cp {source_db_path} {db_path}"
subprocess.run(cmd, shell=True, check=True)
print("Original DB", db_path)
# Generate extended database and extended decoy database
db_extended_path = get_extended_db_path(db_path)
if os.path.exists(db_extended_path):
print("Extended DB already exists:", db_extended_path)
else:
# if extentded DB doesn't exist yet, create it (add 10 pools to DB)
cmd = f"cat {db_path} >> {db_extended_path}"
print(f"Extended DB: {db_extended_path}.")
subprocess.run(cmd, shell=True, check=True)
pool_fastas_df = PT_pools_df["fasta"].drop_duplicates().reset_index(drop=True).to_frame()
idx = pool_fastas_df[pool_fastas_df["fasta"] == pool_fasta].index[0]
add_pool_idxs = slice((idx + 1) % len(pool_fastas_df), (idx + 11) % len(pool_fastas_df))
add_pool_fastas = pool_fastas_df["fasta"].iloc[add_pool_idxs].values.tolist()
for add_pool_fasta in add_pool_fastas:
add_pool_path = os.path.join(pool_proteomes_dir, add_pool_fasta)
append_pool_peptides(add_pool_path, db_extended_path)
# Add decoys to extended database
db_w_decoys_extended_path = generate_decoys_fasta(
dset_name=dset_name, db_file=db_extended_path, contam_only=False
)
print("Extended DB with decoys and contaminants:", db_w_decoys_extended_path)
files_db_w_decoys[fname] = db_w_decoys_extended_path
return files_db_w_decoys
def run_msfragger_search(dset_name, db_search_config):
"""Run MSFragger database search."""
# Search for mzml files in the directory
search_ext = db_search_config.ext
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
mzml_files = [f for f in os.listdir(mzml_files_dir) if os.path.splitext(f)[1].lower() == search_ext]
mzml_files = {os.path.splitext(f)[0]: os.path.join(mzml_files_dir, f) for f in mzml_files}
# Generate decoys database
if db_search_config.pool_proteomes_dir is not None: # TODO: mb use a different check for synthetic peptides?
# For synthetic peptides, generate separate databased for each file
pool_proteomes_dir = os.path.join(PROTEOMES_DIR, db_search_config.pool_proteomes_dir)
PT_pools_name = f"{dset_name}.csv"
PT_pools_path = os.path.join(PROTEOMES_DIR, PT_pools_name)
PT_pools_df = pd.read_csv(PT_pools_path)
files_db_w_decoys = prepare_synthetic_fasta(dset_name, mzml_files, pool_proteomes_dir, PT_pools_df)
else:
db_w_decoys_path = generate_decoys_fasta(dset_name, db_search_config.database_path)
files_db_w_decoys = {fname: db_w_decoys_path for fname in mzml_files}
# Combine mzml_files with the same database
db_w_decoys_files = {}
for fname, db_w_decoys_path in files_db_w_decoys.items():
if db_w_decoys_path not in db_w_decoys_files:
db_w_decoys_files[db_w_decoys_path] = []
db_w_decoys_files[db_w_decoys_path].append(mzml_files[fname])
# Iterate over each database and run the msfragger command
for db_w_decoys_path, mzml_files in db_w_decoys_files.items():
print("\nDB with decoys:\n", db_w_decoys_path)
print("Files:\n", mzml_files)
options = [
"--database_name",
db_w_decoys_path,
"--decoy_prefix",
"rev_",
"--output_format",
"pepxml_pin", # .pin outputs for MSBooster
]
if db_search_config.ext == ".d":
options += [
"--write_uncalibrated_mgf", "1",
"--write_calibrated_mzml", "1",
]
# Parse additional search params from config if provided
for arg in [*db_search_config.search_params.items()]:
options += list(map(str, arg))
cmd = [
"java",
"-Xmx160G",
"-jar",
MSFRAGGER_PATH,
*options,
*mzml_files,
]
subprocess.run(" ".join(cmd), shell=True, check=True)
if search_ext == ".d":
# Use uncalibrated mzml files to get "proxy" mzml files
for file_path in mzml_files:
fname = os.path.splitext(file_path)[0]
src_fname = fname + "_uncalibrated.mzML"
dst_fname = fname + ".mzML"
shutil.move(src_fname, dst_fname)
print(f"Created mzML: {dst_fname}")
print("DB search results (.pepXML, .pin):\n", os.listdir(mzml_files_dir))
def generate_msfragger_params(db_w_decoys_path, config, template_path, output_path):
"""
Generates a MSFragger parameter file from a given configuration and template.
Args:
config (dict): The configuration dictionary for the dataset.
template_path (str): Path to the default template .params file.
output_path (str): Path to save the modified .params file.
"""
def replace_param_value(line, new_value):
if "#" in line:
param, desc = line.split("#")
else:
param = line
desc = ""
param_key, param_value = param.split(" = ", 1)
if "variable_mod" in param_key:
new_value = new_value.replace("_", " ")
param = f"{param_key} = {new_value}"
return f"{param} # {desc}"
# Step 1: Load the default template params file
with open(template_path, 'r') as template_file:
template_data = template_file.readlines()
# Step 2: Modify template based on the provided config
search_params = {key.lstrip("-"): value for key, value in config.search_params.items()}
search_params["database_name"] = db_w_decoys_path
modified_params = []
for line in template_data:
line = line.strip()
for key, value in search_params.items():
# Search for the parameter in the template file and replace if exists
if key in line and not line.startswith("#"):
line = replace_param_value(line, value)
break # No need to check other keys once matched
modified_params.append(line)
modified_params.append("\n")
# Step 3: Add missing parameters from config (not in the template)
existing_params = {line.split('=')[0].strip() for line in modified_params if '=' in line}
for key, value in search_params.items():
if key not in existing_params:
if "variable_mod" in key:
value = value.replace("_", " ")
modified_params.append(f"{key} = {value}")
# Step 4: Save the modified params file
with open(output_path, 'w') as output_file:
output_file.write("\n".join(modified_params))
print(f"Generated MSFragger params file at: {output_path}")
def run_msfragger_search_split(dset_name, db_search_config):
# TODO: does not support search with multiple per-file databases (synthetic peptides)
"""Run MSFragger database search with split database."""
# Generate decoys database
db_w_decoys_path = generate_decoys_fasta(dset_name, db_search_config.database_path)
# Search for mzml files in the directory
search_ext = db_search_config.ext
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
mzml_files = [f for f in os.listdir(mzml_files_dir) if os.path.splitext(f)[1].lower() == search_ext]
mzml_files = [os.path.join(mzml_files_dir, f) for f in mzml_files]
# Create MSFragger params file from config
params_file = os.path.join(mzml_files_dir, "closed.params")
generate_msfragger_params(
db_w_decoys_path,
db_search_config,
MSFRAGGER_BASE_PARAMS,
params_file
)
n_db_splits = db_search_config.n_db_splits
# Iterate over each mzML file and run the msfragger command
for mzml_file in mzml_files:
output_file = os.path.basename(mzml_file).replace("mzML", "pepXML")
if output_file in os.listdir(mzml_files_dir):
print(f"{output_file} already exists.")
continue
print(f"PROCESSING {mzml_file}")
cmd = [
"python",
SPLIT_SCRIPT_PATH,
f"{n_db_splits}",
'"java -jar -Dfile.encoding=UTF-8 -Xmx160G"',
MSFRAGGER_PATH,
params_file,
mzml_file,
]
subprocess.run(" ".join(cmd), shell=True, check=True)
print(f"PROCESSED {mzml_file}\n\n")
# Print results
print("DB search results (.pepXML, .pin):\n", os.listdir(mzml_files_dir))
def get_psm_rescoring_features(dset_name, rescoring_config):
"""Create PSMs rescoring features with MSBooster."""
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
rescored_files_dir = os.path.join(RESCORED_DATA_DIR, dset_name)
# select all the .mzml files from mzml_files_dir (# only select with fnames in files_list?)
mzml_files = [f for f in os.listdir(mzml_files_dir) if os.path.splitext(f)[1].lower() == ".mzml"]
mzml_files = [os.path.join(mzml_files_dir, f) for f in mzml_files]
print(".mzML files available for rescoring:\n", mzml_files)
# select .pin files with fnames in files_list
pin_files = [f for f in os.listdir(mzml_files_dir) if os.path.splitext(f)[1].lower() == ".pin"]
pin_files = [os.path.join(mzml_files_dir, f) for f in pin_files]
print(".pin files available for rescoring:\n", pin_files)
file_prefix = "rescore" # TODO: move outside?
options = [
"--DiaNN",
DIANN_PATH,
"--KoinaURL",
KOINA_URL,
"--editedPin",
file_prefix,
"--paramsList",
MSBOOSTER_BASE_PARAMS,
"--mzmlDirectory",
*mzml_files,
"--pinPepXMLDirectory",
*pin_files,
"--outputDirectory",
rescored_files_dir,
]
for arg in [*rescoring_config.feat_pred_params.items()]:
options += list(map(str, arg))
cmd = [
"java",
"-Xmx64G",
"-jar",
MSBOOSTER_PATH,
*options,
]
subprocess.run(" ".join(cmd), shell=True, check=True)
print("Created PSMs features (_rescore.pin):\n", os.listdir(mzml_files_dir))
def run_psm_rescoring(dset_name, rescoring_config, rescored_files_dir, rescore_file_prefix="rescore"):
"""Run Percolator for PSMs rescoring (using MSBooster features)."""
# TODO: move outside (to constants?)
num_threads = 3
test_fdr = 0.01
train_fdr = 0.01
input_file = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.pin")
weights_file = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.percolator.weights.csv")
target_psms = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.percolator.psms.txt")
decoy_psms = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.percolator.decoy.psms.txt")
target_peptides = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.percolator.peptides.txt")
decoy_peptides = os.path.join(rescored_files_dir, f"{rescore_file_prefix}.percolator.decoy.peptides.txt")
cmd = f"percolator --weights {weights_file} \
--num-threads {num_threads} \
--subset-max-train 500000 \
--post-processing-tdc \
--testFDR {test_fdr} \
--trainFDR {train_fdr} \
--results-psms {target_psms} \
--decoy-results-psms {decoy_psms} \
--results-peptides {target_peptides} \
--decoy-results-peptides {decoy_peptides} \
{input_file}"
subprocess.run(cmd, shell=True, check=True)
print(
"PSMs rescoring results (.percolator.psms.txt):\n",
os.listdir(rescored_files_dir)
)
def get_filename(psm_id: str):
"""Assumes that there are no `.` in the file name."""
return psm_id.split(".")[0]
def format_peptide_notation(sequence: str):
"""TODO: PTMs may need conversion to ProForma notation."""
# remove cleavage sites
if (
re.match(r"[A-Z-_].*.[A-Z-_]", sequence) is not None
): # check is not mandatory
sequence = sequence[2:-2]
return sequence
def collect_dataset_tags(config):
if os.path.exists(DATASET_TAGS_PATH):
tags_df = pd.read_csv(DATASET_TAGS_PATH, sep="\t").to_dict('records')
else:
tags_df = []
# add reference proteome information (for each dataset)
dset_tags = {"proteome": config.db_search.database_path}
# add dataset property tags
dset_tags.update({tag.name: 1 for tag in config.tags})
print(f"Dataset {config.name} tags:\n", dset_tags)
tags_df.append({"dataset": config.name, **dset_tags})
tags_df = pd.DataFrame(tags_df).fillna(0)
tags_df = tags_df.drop_duplicates(subset="dataset", keep="last")
tags_df.to_csv(DATASET_TAGS_PATH, index=False, sep="\t")
print(f"Written to {DATASET_TAGS_PATH}")
def prepare_msgf_fasta(dset_name, db_file=None, db_w_decoys_file=None):
"""Prepare target and decoy databases for MSGF+."""
if db_w_decoys_file is None:
print("Prepare database with reversed decoys.")
db_w_decoys_file = generate_decoys_fasta(dset_name, db_file)
print("Database with decoys:\n", db_w_decoys_file)
print("Split into separate target & decoys databases")
target_db_file = db_w_decoys_file.replace("-decoys", "").split(".")[0] + ".fasta"
decoys_db_file = db_w_decoys_file.replace("-contam", "").split(".")[0] + ".fasta"
print("Target:", target_db_file)
print("Decoys:", decoys_db_file)
cmd = [
"awk",
"'/^>/ {is_decoy = ($0 ~ /^>rev_/)} {if (is_decoy) print > \"" + decoys_db_file + "\"; else print > \"" + target_db_file + "\"}'",
db_w_decoys_file
]
subprocess.run(" ".join(cmd), shell=True, check=True)
return target_db_file, decoys_db_file
def write_modification_file(dset_name, db_search_config, mods_file="MSGF_Mods.txt"):
ptm_psims_names = {
-17.0265: "Ammonia-loss", # or Gln->pyro-Glu
0.984: "Deamidated",
4.0251: "Label:2H(4)", # SILAC
6.0201: "Label:13C(6)", # SILAC
8.0142: "Label:13C(6)15N(2)", # SILAC
10.0083: "Label:13C(6)15N(4)", # SILAC
12.0: "Thiazolidine", # not PSI-MS, Formaldehyde adduct # UNIMOD:1009
14.0157: "Methyl",
15.9949: "Oxidation",
21.9819: "Cation:Na", # Sodium adduct
26.0156: "Delta:H(2)C(2)", # Acetaldehyde +26
27.9949: "Formyl",
28.0313: "Dimethyl",
42.0106: "Acetyl",
42.0470: "Trimethyl",
44.9851: "Nitro", # Oxidation to nitro
52.9115: "Cation:Fe[III]", # not PSI-MS, Replacement of 3 protons by iron
53.9193: "Cation:Fe[II]", # not PSI-MS, Replacement of 2 protons by iron
55.9197: "Cation:Ni[II]", # not PSI-MS, Replacement of 2 protons by nickel
56.0262: "Propionyl", # Propionate labeling reagent light form (N-term & K)
56.0626: "Diethyl",
57.0215: "Carbamidomethyl",
68.0262: "Crotonyl", # not PSI-MS, Crotonylation
70.0419: "Crotonaldehyde", # Butyryl
79.9663: "Phospho",
86.0004: "Malonyl", # Malonylation
86.0368: "Hydroxyisobutyryl", # not PSI-MS, 2-hydroxyisobutyrylation
100.016: "Succinyl",
114.0317: "Glutaryl", # not PSI-MS, Glutarylation
114.0429: "GG", # Ubiquitinylation residue
226.0776: "Biotin",
229.1629: "TMT6plex", # not PSI-MS, Sixplex Tandem Mass Tag
}
term_residues_notation = {
"n": "N-term",
"c": "C-term",
}
"""Write MSGF+ modification file."""
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
mods_file_path = os.path.join(mzml_files_dir, mods_file)
search_params = db_search_config.search_params
fixed_ptm_strs = {"C": "C2H3N1O1,C,fix,any,Carbamidomethyl"}
# Parse fixed modifications
for key, mass in search_params.items():
if key.startswith("--add_"):
residue = key.split("_")[1]
mass = round(float(mass), 4)
if mass == 0.0:
del fixed_ptm_strs[residue]
else:
fixed_ptm_strs[residue] = f"{mass},{residue},fix,any,{ptm_psims_names.get(mass, 'Unknown')}"
with open(mods_file_path, "w") as f:
for fixed_ptm_str in fixed_ptm_strs.values():
f.write(fixed_ptm_str + "\n")
config_ptm_strs = []
for k in search_params:
if "variable_mod_" in k:
config_ptm_str = search_params[k]
config_ptm_strs.append(config_ptm_str)
with open(mods_file_path, "a") as f:
for config_ptm_str in config_ptm_strs:
mass, residues, n = config_ptm_str.split("_")
mass = round(float(mass), 4)
if mass == 0:
# No modification
continue
# Split residues if they include both N/C-term and amino acids
term_residues = re.findall("[cn].", residues)
aa_residues = re.sub("[cn].", "", residues)
if term_residues:
for term_residue in term_residues:
term, residue = list(term_residue) # len(term_residue) == 2
position = term_residues_notation[term]
residue = "*" if residue == "^" else residue
f.write(",".join([str(mass), residue, "opt", position, ptm_psims_names[mass]]) + "\n")
if aa_residues:
position = "any"
f.write(",".join([str(mass), aa_residues, "opt", position, ptm_psims_names[mass]]) + "\n")
return mods_file_path
def run_msgf_search(dset_name, db_search_config):
"""Run MSGF+ database search."""
msgf_enzyme_id = {
"nonspecific": 0, # unspecific cleavage
"trypsin": 1, # Trypsin
"chymotrypsin": 2, # Chymotrypsin
"lysc": 3, # Lys-C
"lysn": 4, # Lys-N
"gluc": 5, # Glu-C, glutamyl endopeptidase
"argc": 6, # Arg-C
"aspn": 7, # Asp-N
# "": 8, # alphaLP
# "": 9, no cleavage
}
msgf_instrument_id = {
"LCQ": 0, # Linear Quadrupole Ion Trap
"LTQ": 0, # Linear Trap Quadrupole
"Orbitrap": 1, # Orbitrap Elite, Fusion, Lumos
"FTICR": 1, # Fourier Transform Ion Cyclotron Resonance
"Lumos": 1, # a high-end version of the Orbitrap Fusion
"TOF": 2, # Bruker, Sciex TripleTOF, Agilent TOF
"QExactive": 3, # Hybrid Quadrupole-Orbitrap (QE, QE Plus / HF)
}
msgf_fragmentation_id = {
"not_given": 0, # as written in the spectrum or CID if no info
"CID": 1,
"ETD": 2,
"HCD": 3,
"UVPD": 4,
}
# mass tol params are passed in the MSFragger expected format
# default mass tolerance units are ppm (1)
# for fragment mass, units are set to Da (0)
def format_mass_tolerance(mass_lower, mass_upper, units="ppm"):
mass_lower = abs(mass_lower)
mass_tol_str = f"{mass_lower}{units},{mass_upper}{units}"
return mass_tol_str
# Fragment mass tolerance -- can not be specified.
# Although you can specify the precursor tolerance with -t
# you cannot specify the fragment ion tolerance.
# -- the tolerance is determined automatically by the training algorithm
# Source: https://github.com/MSGFPlus/msgfplus/issues/17#issuecomment-348549563
def get_precursor_mass_tol(db_search_config):
mass_lower = db_search_config.search_params.get("--precursor_mass_lower", -20)
mass_upper = db_search_config.search_params.get("--precursor_mass_upper", 20)
return format_mass_tolerance(mass_lower, mass_upper, units="ppm")
def get_instrument_id(db_search_config):
# 0: Low-res LCQ/LTQ
# 1: Orbitrap/FTICR/Lumos
# 2: TOF
# 3: Q-Exactive
instrument_id = db_search_config.instrument
return msgf_instrument_id.get(instrument_id, 0)
def get_fragmentation_method(db_search_config):
# 0: As written in the spectrum or CID if no info
# 1: CID
# 2: ETD
# 3: HCD
# 4: UVPD
fragmentation_method = db_search_config.fragmentation
return msgf_fragmentation_id.get(fragmentation_method, 0)
def get_enzyme_id(db_search_config):
return msgf_enzyme_id[
db_search_config.search_params.get("--search_enzyme_name_1", "trypsin")
]
search_ext = db_search_config.ext
mzml_files_dir = os.path.join(MZML_DATA_DIR, dset_name)
mzml_files = [f for f in os.listdir(mzml_files_dir) if os.path.splitext(f)[1].lower() == search_ext]
mzml_files = {os.path.splitext(f)[0]: os.path.join(mzml_files_dir, f) for f in mzml_files}
target_res_dir = os.path.join(mzml_files_dir, "target_res")
decoys_res_dir = os.path.join(mzml_files_dir, "decoys_res")
os.makedirs(target_res_dir, exist_ok=True)
os.makedirs(decoys_res_dir, exist_ok=True)
# Write MSGF+ modification file
mods_file_path = write_modification_file(dset_name, db_search_config, mods_file="MSGF_Mods.txt")
# Define MSGF+ search params
options = [
"-decoy", "rev_",
"-t", format_mass_tolerance(
db_search_config.search_params.get("--precursor_mass_lower", -20),
db_search_config.search_params.get("--precursor_mass_upper", 20),
"ppm"
),
"-tda", 0,
"-m", get_fragmentation_method(db_search_config), # Fragmentation Method; Default: 0
"-inst", get_instrument_id(db_search_config), # Instrument ID; Default: 0
"-e", msgf_enzyme_id[db_search_config.search_params.get("--search_enzyme_name_1", "trypsin")],
"-ntt", db_search_config.search_params.get("--num_enzyme_termini", 2), # Number of Tolerable Termini [0/1/2]; Default: 2
"-minLength", db_search_config.search_params.get("--digest_min_length", 7),
"-maxLength", db_search_config.search_params.get("--digest_max_length", 40),
"-addFeatures", 1, # add features for Percolator to the output
"-numMods", db_search_config.search_params.get("--max_variable_mods_per_peptide", 3),
"-mod", mods_file_path,
]
if get_enzyme_id(db_search_config) != 0: # not nonspecific cleavage
options += ["-maxMissedCleavages", db_search_config.search_params.get("--allowed_missed_cleavage_1", 2)]
options = list(map(str, options))
if db_search_config.pool_proteomes_dir is not None:
# For synthetic peptides, generate separate databased for each file
pool_proteomes_dir = os.path.join(PROTEOMES_DIR, db_search_config.pool_proteomes_dir)
PT_pools_name = f"{dset_name}.csv"
PT_pools_path = os.path.join(PROTEOMES_DIR, PT_pools_name)
PT_pools_df = pd.read_csv(PT_pools_path)
files_db_w_decoys = prepare_synthetic_fasta(dset_name, mzml_files, pool_proteomes_dir, PT_pools_df)
else:
# Generate a single database with decoys
db_w_decoys_file = generate_decoys_fasta(dset_name, db_search_config.database_path)
files_db_w_decoys = {fname: db_w_decoys_file for fname in mzml_files}
# Combine mzML files with the same database
db_w_decoys_files = {}
for fname, db_w_decoys_path in files_db_w_decoys.items():
if db_w_decoys_path not in db_w_decoys_files:
db_w_decoys_files[db_w_decoys_path] = []
db_w_decoys_files[db_w_decoys_path].append(mzml_files[fname])
# Iterate over each database and run MSGF+ search
for db_w_decoys_path, mzml_files in db_w_decoys_files.items():
print("\nDB with decoys:\n", db_w_decoys_path)
print("Files:\n", mzml_files)
# Prepare target and decoy databases
target_db_file, decoys_db_file = prepare_msgf_fasta(dset_name, db_w_decoys_file=db_w_decoys_path)
print("Run MSGF+ for target database:")
for fname in mzml_files:
res_fname = os.path.splitext(fname)[0] + ".mzid"
cmd = [
"java",
"-Xmx160G",
"-jar",
MSGF_PATH,
*options,
"-d", target_db_file, # DatabaseFile
"-s", fname,
"&&",
"mv", res_fname, target_res_dir,
]
subprocess.run(" ".join(cmd), shell=True, check=True)
# TODO: add mv result mzid to target_res_dir
print("Target DB search results:\n", os.listdir(target_res_dir), "\n")
print("Run MSGF+ for decoys database:")
for fname in mzml_files:
res_fname = os.path.splitext(fname)[0] + ".mzid"
cmd = [
"java",
"-Xmx160G",
"-jar",
MSGF_PATH,
*options,
"-d", decoys_db_file, # DatabaseFile
"-s", fname,
"&&",
"mv", res_fname, decoys_res_dir,
]
subprocess.run(" ".join(cmd), shell=True, check=True)
# TODO: add mv result mzid to target_res_dir
print("Decoys DB search results:\n", os.listdir(decoys_res_dir), "\n")
# Collect target and decoy results into features file
msgf2pin_enzymes = [
"no_enzyme",
"trypsin",
"chymotrypsin",
"lys-c",
"lys-n",
"glu-c",
"arg-c",
"asp-n",
]
res_features_dir = os.path.join(mzml_files_dir, "msgf_features")
os.makedirs(res_features_dir, exist_ok=True)
for fname in os.listdir(target_res_dir):
if fname.endswith(".mzid"):
feats_fname = fname.replace(".mzid", ".pin")
cmd = [
"msgf2pin",
"-o", os.path.join(res_features_dir, feats_fname),
"-e", msgf2pin_enzymes[get_enzyme_id(db_search_config)],
os.path.join(target_res_dir, fname),
os.path.join(decoys_res_dir, fname),
]
subprocess.run(" ".join(cmd), shell=True, check=True)
# Cleanup temporary files created by MSGF+
tmp_file_patterns = [".canno", ".cnlcp", ".csarr", ".cseq"]
for pattern in tmp_file_patterns:
for tmp_file in tqdm([f for f in os.listdir(mzml_files_dir) if f.endswith(pattern)]):
os.remove(os.path.join(mzml_files_dir, tmp_file))
print(f"Removed temporary file: {tmp_file}")