-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkflowTest.py
More file actions
1360 lines (1152 loc) · 57 KB
/
WorkflowTest.py
File metadata and controls
1360 lines (1152 loc) · 57 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 streamlit as st
from pathlib import Path
import pandas as pd
import plotly.express as px
from streamlit_plotly_events import plotly_events
from pyopenms import IdXMLFile
from scipy.stats import ttest_ind
import numpy as np
import mygene
from collections import defaultdict
from scipy.stats import fisher_exact
from src.workflow.WorkflowManager import WorkflowManager
from src.common.common import page_setup
from src.common.results_helpers import get_abundance_data
from src.common.results_helpers import parse_idxml, build_spectra_cache
from openms_insight import Table, Heatmap, LinePlot, SequenceView
# params = page_setup()
class WorkflowTest(WorkflowManager):
def __init__(self) -> None:
super().__init__("TOPP Workflow", st.session_state["workspace"])
def upload(self) -> None:
t = st.tabs(["MS data (mzML)", "FASTA database"])
with t[0]:
self.ui.upload_widget(
key="mzML-files",
name="MS data",
file_types="mzML",
fallback=[str(f) for f in Path("example-data", "mzML").glob("*.mzML")],
)
with t[1]:
self.ui.upload_widget(
key="fasta-file",
name="Protein FASTA database",
file_types=("fasta", "fa"),
fallback=[str(f) for f in Path("example-data", "db").glob("*.fasta")],
)
@st.fragment
def configure(self) -> None:
# reactive=True so Group Selection tab updates when selection changes
self.ui.select_input_file("mzML-files", multiple=True, reactive=True)
self.ui.select_input_file("fasta-file", multiple=False)
t = st.tabs(["**Identification**", "**Rescoring**", "**Filtering**", "**Library Generation**", "**Quantification**", "**Group Selection**"])
with t[0]:
# Checkbox for decoy generation
# reactive=True ensures the parent configure() fragment re-runs when checkbox changes,
# so conditional UI (DecoyDatabase settings) updates immediately
self.ui.input_widget(
key="generate-decoys",
default=True,
name="Generate Decoy Database",
widget_type="checkbox",
help="Generate reversed decoy sequences for FDR calculation. Disable if your FASTA already contains decoys.",
reactive=True,
)
# Reload params to get current checkbox value after it was saved
self.params = self.parameter_manager.get_parameters_from_json()
# Show DecoyDatabase settings if generating decoys
if self.params.get("generate-decoys", True):
st.info("""
**Decoy Database Settings:**
* **method**: How decoy sequences are generated from target protein sequences.
*Reverse* creates decoys by reversing each sequence, while *shuffle* randomly
rearranges the amino acids. Both methods preserve the amino acid composition
of the original protein, ensuring decoys have similar properties to real sequences
for accurate false discovery rate (FDR) estimation.
""")
self.ui.input_TOPP(
"DecoyDatabase",
custom_defaults={
"decoy_string": "rev_",
"decoy_string_position": "prefix",
"method": "reverse",
},
include_parameters=["method"],
)
comet_info = """
**Identification (Comet):**
* **enzyme**: The enzyme used for peptide digestion.
* **missed_cleavages**: Number of possible cleavage sites missed by the enzyme. It has no effect if enzyme is unspecific cleavage.
* **fixed_modifications**: Fixed modifications, specified using Unimod (www.unimod.org) terms, e.g. 'Carbamidomethyl (C)' or 'Oxidation (M)'
* **variable_modifications**: Variable modifications, specified using Unimod (www.unimod.org) terms, e.g. 'Carbamidomethyl (C)' or 'Oxidation (M)'
* **instrument**: Type of instrument (high_res or low_res). Use 'high_res' for high-resolution MS2 (Orbitrap, TOF), 'low_res' for ion trap.
* **fragment_mass_tolerance**: Fragment mass tolerance for MS2 matching.
* **fragment_bin_offset**: Offset for binning MS2 spectra. Typically 0.0 for high-res, 0.4 for low-res instruments.
"""
if not self.params.get("generate-decoys", True):
comet_info += """* **PeptideIndexing:decoy_string**: String that was appended (or prefixed - see 'decoy_string_position' flag below) to the accessions
in the protein database to indicate decoy proteins.
"""
st.info(comet_info)
comet_include = [":enzyme", "missed_cleavages", "fixed_modifications", "variable_modifications",
"instrument", "fragment_mass_tolerance", "fragment_error_units", "fragment_bin_offset"]
if not self.params.get("generate-decoys", True):
# Only show decoy_string when not generating decoys
comet_include.append("PeptideIndexing:decoy_string")
self.ui.input_TOPP(
"CometAdapter",
custom_defaults={
"threads": 8,
"instrument": "high_res",
"missed_cleavages": 2,
"min_peptide_length": 6,
"max_peptide_length": 40,
"num_hits": 1,
"num_enzyme_termini": "fully",
"isotope_error": "0/1",
"precursor_charge": "2:4",
"precursor_mass_tolerance": 20.0,
"fragment_mass_tolerance": 0.02,
"fragment_bin_offset": 0.0,
"max_variable_mods_in_peptide": 3,
"minimum_peaks": 1,
"clip_nterm_methionine": "true",
"PeptideIndexing:IL_equivalent": "true",
"PeptideIndexing:unmatched_action": "warn",
"PeptideIndexing:decoy_string": "rev_",
},
include_parameters=comet_include,
exclude_parameters=["second_enzyme"],
)
with t[1]:
st.info("""
**Rescoring (Percolator):**
* **post_processing_tdc**: Use target-decoy competition to assign q-values and PEPs.
* **score_type**: Type of the peptide main score
* **subset_max_train**: Only train an SVM on a subset of <x> PSMs, and use the resulting score vector to evaluate the other PSMs. Recommended when analyzing huge numbers (>1 million) of PSMs. When set to 0, all PSMs are used for training as normal.
""")
# decoy_pattern is always derived from upstream, never shown
percolator_include = ["post_processing_tdc", "score_type", "subset_max_train"]
self.ui.input_TOPP(
"PercolatorAdapter",
custom_defaults={
"threads": 8,
"subset_max_train": 300000,
"decoy_pattern": "rev_",
"score_type": "pep",
"post_processing_tdc": "true",
},
include_parameters=percolator_include,
exclude_parameters=["out_type"],
)
with t[2]:
st.info("""
**Filtering (IDFilter):**
* **score:type_peptide**: Score used for filtering. If empty, the main score is used.
* **score:psm**: The score which should be reached by a peptide hit to be kept. (use 'NAN' to disable this filter)
""")
self.ui.input_TOPP(
"IDFilter",
custom_defaults={
"threads": 2,
"score:type_peptide": "q-value",
"score:psm": 0.10,
},
# include_parameters=["type_peptide", "score:psm"]
exclude_parameters=["type_protein"],
)
with t[3]: # Library Generation
st.info("""
**Spectral Library Generation (EasyPQP):**
Generate a spectral library from filtered PSMs for targeted proteomics (DIA/SWATH).
""")
self.ui.input_widget(
key="generate-library",
default=False,
name="Generate Spectral Library",
widget_type="checkbox",
help="Enable spectral library generation using EasyPQP.",
reactive=True,
)
self.params = self.parameter_manager.get_parameters_from_json()
if self.params.get("generate-library", False):
# FDR options
self.ui.input_widget(
key="library-use-fdr",
default=False,
name="Apply additional FDR filtering",
widget_type="checkbox",
help="If disabled (recommended), uses --nofdr since IDFilter already applied FDR.",
reactive=True,
)
self.params = self.parameter_manager.get_parameters_from_json()
if self.params.get("library-use-fdr", False):
self.ui.input_widget(
key="library-psm-fdr",
default=0.01,
name="PSM FDR Threshold",
widget_type="number",
min_value=0.001,
max_value=1.0,
step_size=0.01,
)
# Decoy options
self.ui.input_widget(
key="library-generate-decoys",
default=True,
name="Generate Decoy Library",
widget_type="checkbox",
reactive=True,
)
self.params = self.parameter_manager.get_parameters_from_json()
if self.params.get("library-generate-decoys", True):
self.ui.input_widget(
key="library-decoy-method",
default="shuffle",
name="Decoy Method",
widget_type="selectbox",
options=["shuffle", "reverse", "pseudo-reverse", "shift"],
)
with t[4]:
st.info("""
**Quantification (ProteomicsLFQ):**
* **intThreshold**: Peak intensity threshold applied in seed detection.
* **psmFDR**: FDR threshold for sub-protein level (e.g. 0.05=5%). Use -FDR_type to choose the level. Cutoff is applied at the highest level. If Bayesian inference was chosen, it is equivalent with a peptide FDR
* **proteinFDR**: Protein FDR threshold (0.05=5%).
""")
self.ui.input_TOPP(
"ProteomicsLFQ",
custom_defaults={
"threads": 12,
"targeted_only": "true",
"feature_with_id_min_score": 0.1,
"Seeding:intThreshold": 1000.0,
"psmFDR": 0.01,
"proteinFDR": 0.01,
"picked_proteinFDR": "true",
},
include_parameters=["intThreshold", "psmFDR", "proteinFDR"],
)
with t[5]:
st.markdown("### 🧪 Sample Group Assignment")
st.info(
"Enter a group name for each mzML file.\n\n"
"Examples: case, control"
)
mzml_keys = self.params.get("mzML-files")
if not mzml_keys:
st.warning("No mzML files available. Please upload mzML files first.")
return
try:
mzml_files = self.file_manager.get_files(mzml_keys)
except ValueError:
st.warning("Selected mzML files are not available.")
return
# Current mzML filenames
current_filenames = {Path(mz).name for mz in mzml_files}
# Per-file group input using input_widget (auto-saves to params.json)
for mz in mzml_files:
filename = Path(mz).name
self.ui.input_widget(
key=f"mzML-group-{filename}",
default="",
name=f"Group for {filename}",
widget_type="text",
help="e.g. case, control",
)
# Reload params to get current values
self.params = self.parameter_manager.get_parameters_from_json()
# Clean up orphaned group params from previously selected files
orphaned_keys = [
k for k in self.params.keys()
if k.startswith("mzML-group-") and k[11:] not in current_filenames
]
for key in orphaned_keys:
del self.params[key]
if orphaned_keys:
self.parameter_manager.save_parameters()
def execution(self) -> bool:
"""
Refactored TOPP workflow execution:
- Per-sample: CometAdapter -> PercolatorAdapter -> IDFilter
- Cross-sample: ProteomicsLFQ (single combined output)
"""
# ================================
# 0️⃣ Input validation
# ================================
if not self.params.get("mzML-files"):
st.error("No mzML files selected.")
return False
if not self.params.get("fasta-file"):
st.error("No FASTA file selected.")
return False
in_mzML = self.file_manager.get_files(self.params["mzML-files"])
fasta_file = self.file_manager.get_files([self.params["fasta-file"]])[0]
if len(in_mzML) < 1:
st.error("At least one mzML file is required.")
return False
fasta_path = Path(fasta_file)
self.logger.log(f"📂 Loaded {len(in_mzML)} sample(s)")
if self.params.get("generate-decoys", True):
decoy_fasta = fasta_path.with_suffix(".decoy.fasta")
# Get decoy_string from DecoyDatabase params
decoy_string = self.params.get("DecoyDatabase", {}).get("decoy_string", "rev_")
if not decoy_fasta.exists():
self.logger.log("🧬 Generating decoy database...")
st.info("Generating decoy FASTA database...")
if not self.executor.run_topp(
"DecoyDatabase",
{"in": [str(fasta_path)], "out": [str(decoy_fasta)]},
):
self.logger.log("Workflow stopped due to error")
return False
self.logger.log("✅ Decoy database ready")
st.success(f"Using decoy FASTA: {decoy_fasta.name}")
database_fasta = decoy_fasta
else:
# Get decoy_string from CometAdapter params
decoy_string = self.params.get("CometAdapter", {}).get("PeptideIndexing:decoy_string", "rev_")
self.logger.log("📄 Using existing FASTA database")
st.info(f"Using original FASTA: {fasta_path.name}")
database_fasta = fasta_path
# ================================
# 1️⃣ Directory setup
# ================================
results_dir = Path(self.workflow_dir, "results")
comet_dir = results_dir / "comet_results"
perc_dir = results_dir / "percolator_results"
filter_dir = results_dir / "filter_results"
quant_dir = results_dir / "quant_results"
for d in [comet_dir, perc_dir, filter_dir, quant_dir]:
d.mkdir(parents=True, exist_ok=True)
self.logger.log("📁 Output directories created")
# # ================================
# # 2️⃣ File path definitions (per sample)
# # ================================
comet_results = []
percolator_results = []
filter_results = []
for mz in in_mzML:
stem = Path(mz).stem
comet_results.append(str(comet_dir / f"{stem}_comet.idXML"))
percolator_results.append(str(perc_dir / f"{stem}_per.idXML"))
filter_results.append(str(filter_dir / f"{stem}_filter.idXML"))
# ================================
# 3️⃣ Per-file processing
# ================================
for i, mz in enumerate(in_mzML):
stem = Path(mz).stem
st.info(f"Processing sample: {stem}")
self.logger.log("🔬 Starting per-sample processing...")
# --- CometAdapter ---
self.logger.log("🔎 Running peptide search...")
with st.spinner(f"CometAdapter ({stem})"):
comet_extra_params = {"database": str(database_fasta)}
if self.params.get("generate-decoys", True):
# Propagate decoy_string from DecoyDatabase
comet_extra_params["PeptideIndexing:decoy_string"] = decoy_string
if not self.executor.run_topp(
"CometAdapter",
{
"in": in_mzML,
"out": comet_results,
},
comet_extra_params,
):
self.logger.log("Workflow stopped due to error")
return False
# Get fragment tolerance from CometAdapter parameters for visualization
comet_params = self.parameter_manager.get_topp_parameters("CometAdapter")
frag_tol = comet_params.get("fragment_mass_tolerance", 0.02)
frag_tol_is_ppm = comet_params.get("fragment_error_units", "Da") != "Da"
# Build visualization cache for Comet results
results_dir_path = Path(self.workflow_dir, "results")
cache_dir = results_dir_path / "insight_cache"
cache_dir.mkdir(parents=True, exist_ok=True)
# Get mzML directory
mzml_dir = Path(in_mzML[0]).parent
# Build spectra cache (once, shared by all stages)
spectra_df = None
filename_to_index = {}
for idxml_file in comet_results:
idxml_path = Path(idxml_file)
cache_id_prefix = idxml_path.stem
# Parse idXML to DataFrame
id_df, spectra_data = parse_idxml(idxml_path)
# Build spectra cache (only once)
if spectra_df is None:
filename_to_index = {Path(f).name: i for i, f in enumerate(spectra_data)}
spectra_df, filename_to_index = build_spectra_cache(mzml_dir, filename_to_index)
# Initialize Table component (caches itself)
Table(
cache_id=f"table_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
interactivity={"file": "file_index", "spectrum": "scan_id", "identification": "id_idx"},
column_definitions=[
{"field": "sequence", "title": "Sequence"},
{"field": "charge", "title": "Z", "sorter": "number"},
{"field": "mz", "title": "m/z", "sorter": "number"},
{"field": "rt", "title": "RT", "sorter": "number"},
{"field": "score", "title": "Score", "sorter": "number"},
{"field": "protein_accession", "title": "Proteins"},
],
initial_sort=[{"column": "score", "dir": "asc"}],
index_field="id_idx",
)
# Initialize Heatmap component
Heatmap(
cache_id=f"heatmap_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
x_column="rt",
y_column="mz",
intensity_column="score",
interactivity={"identification": "id_idx"},
)
# Initialize SequenceView component
seq_view = SequenceView(
cache_id=f"seqview_{cache_id_prefix}",
sequence_data=id_df.lazy().select(["id_idx", "sequence", "charge", "file_index", "scan_id"]).rename({
"id_idx": "sequence_id",
"charge": "precursor_charge",
}),
peaks_data=spectra_df.lazy(),
filters={
"identification": "sequence_id",
"file": "file_index",
"spectrum": "scan_id",
},
interactivity={"peak": "peak_id"},
cache_path=str(cache_dir),
deconvolved=False,
annotation_config={
"ion_types": ["b", "y"],
"neutral_losses": True,
"tolerance": frag_tol,
"tolerance_ppm": frag_tol_is_ppm,
},
)
# Initialize LinePlot from SequenceView
LinePlot.from_sequence_view(
seq_view,
cache_id=f"lineplot_{cache_id_prefix}",
cache_path=str(cache_dir),
title="Annotated Spectrum",
styling={
"unhighlightedColor": "#CCCCCC",
"highlightColor": "#E74C3C",
"selectedColor": "#F3A712",
},
)
self.logger.log("✅ Peptide search complete")
# --- PercolatorAdapter ---
self.logger.log("📊 Running rescoring...")
with st.spinner(f"PercolatorAdapter ({stem})"):
if not self.executor.run_topp(
"PercolatorAdapter",
{
"in": comet_results,
"out": percolator_results,
},
{"decoy_pattern": decoy_string}, # Always propagated from upstream
):
self.logger.log("Workflow stopped due to error")
return False
# Build visualization cache for Percolator results
for idxml_file in percolator_results:
idxml_path = Path(idxml_file)
cache_id_prefix = idxml_path.stem
# Parse idXML to DataFrame
id_df, spectra_data = parse_idxml(idxml_path)
# Initialize Table component (caches itself)
Table(
cache_id=f"table_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
interactivity={"file": "file_index", "spectrum": "scan_id", "identification": "id_idx"},
column_definitions=[
{"field": "sequence", "title": "Sequence"},
{"field": "charge", "title": "Z", "sorter": "number"},
{"field": "mz", "title": "m/z", "sorter": "number"},
{"field": "rt", "title": "RT", "sorter": "number"},
{"field": "score", "title": "Score", "sorter": "number"},
{"field": "protein_accession", "title": "Proteins"},
],
initial_sort=[{"column": "score", "dir": "asc"}],
index_field="id_idx",
)
# Initialize Heatmap component
Heatmap(
cache_id=f"heatmap_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
x_column="rt",
y_column="mz",
intensity_column="score",
interactivity={"identification": "id_idx"},
)
# Initialize SequenceView component
seq_view = SequenceView(
cache_id=f"seqview_{cache_id_prefix}",
sequence_data=id_df.lazy().select(["id_idx", "sequence", "charge", "file_index", "scan_id"]).rename({
"id_idx": "sequence_id",
"charge": "precursor_charge",
}),
peaks_data=spectra_df.lazy(),
filters={
"identification": "sequence_id",
"file": "file_index",
"spectrum": "scan_id",
},
interactivity={"peak": "peak_id"},
cache_path=str(cache_dir),
deconvolved=False,
annotation_config={
"ion_types": ["b", "y"],
"neutral_losses": True,
"tolerance": frag_tol,
"tolerance_ppm": frag_tol_is_ppm,
},
)
# Initialize LinePlot from SequenceView
LinePlot.from_sequence_view(
seq_view,
cache_id=f"lineplot_{cache_id_prefix}",
cache_path=str(cache_dir),
title="Annotated Spectrum",
styling={
"unhighlightedColor": "#CCCCCC",
"highlightColor": "#E74C3C",
"selectedColor": "#F3A712",
},
)
self.logger.log("✅ Rescoring complete")
# if not Path(percolator_results[i]).exists():
# st.error(f"PercolatorAdapter failed for {stem}")
# st.stop()
# --- IDFilter ---
self.logger.log("🔧 Filtering identifications...")
with st.spinner(f"IDFilter ({stem})"):
if not self.executor.run_topp(
"IDFilter",
{
"in": percolator_results,
"out": filter_results,
},
):
self.logger.log("Workflow stopped due to error")
return False
# Build visualization cache for Filter results
for idxml_file in filter_results:
idxml_path = Path(idxml_file)
cache_id_prefix = idxml_path.stem
# Parse idXML to DataFrame
id_df, spectra_data = parse_idxml(idxml_path)
# Initialize Table component (caches itself)
Table(
cache_id=f"table_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
interactivity={"file": "file_index", "spectrum": "scan_id", "identification": "id_idx"},
column_definitions=[
{"field": "sequence", "title": "Sequence"},
{"field": "charge", "title": "Z", "sorter": "number"},
{"field": "mz", "title": "m/z", "sorter": "number"},
{"field": "rt", "title": "RT", "sorter": "number"},
{"field": "score", "title": "Score", "sorter": "number"},
{"field": "protein_accession", "title": "Proteins"},
],
initial_sort=[{"column": "score", "dir": "asc"}],
index_field="id_idx",
)
# Initialize Heatmap component
Heatmap(
cache_id=f"heatmap_{cache_id_prefix}",
data=id_df.lazy(),
cache_path=str(cache_dir),
x_column="rt",
y_column="mz",
intensity_column="score",
interactivity={"identification": "id_idx"},
)
# Initialize SequenceView component
seq_view = SequenceView(
cache_id=f"seqview_{cache_id_prefix}",
sequence_data=id_df.lazy().select(["id_idx", "sequence", "charge", "file_index", "scan_id"]).rename({
"id_idx": "sequence_id",
"charge": "precursor_charge",
}),
peaks_data=spectra_df.lazy(),
filters={
"identification": "sequence_id",
"file": "file_index",
"spectrum": "scan_id",
},
interactivity={"peak": "peak_id"},
cache_path=str(cache_dir),
deconvolved=False,
annotation_config={
"ion_types": ["b", "y"],
"neutral_losses": True,
"tolerance": frag_tol,
"tolerance_ppm": frag_tol_is_ppm,
},
)
# Initialize LinePlot from SequenceView
LinePlot.from_sequence_view(
seq_view,
cache_id=f"lineplot_{cache_id_prefix}",
cache_path=str(cache_dir),
title="Annotated Spectrum",
styling={
"unhighlightedColor": "#CCCCCC",
"highlightColor": "#E74C3C",
"selectedColor": "#F3A712",
},
)
self.logger.log("✅ Filtering complete")
# if not Path(filter_results[i]).exists():
# st.error(f"IDFilter failed for {stem}")
# st.stop()
# ================================
# EasyPQP Spectral Library Generation (optional)
# ================================
if self.params.get("generate-library", False):
self.logger.log("📚 Building spectral library with EasyPQP...")
st.info("Building spectral library with EasyPQP...")
library_dir = Path(self.workflow_dir, "results", "library")
library_dir.mkdir(parents=True, exist_ok=True)
psms_files, peaks_files = [], []
for filter_idxml in filter_results:
original_stem = Path(filter_idxml).stem.replace("_filter", "")
matching_mzml = next((m for m in in_mzML if Path(m).stem == original_stem), None)
if not matching_mzml:
self.logger.log(f"Warning: No matching mzML found for {filter_idxml}")
continue
# easypqp library requires specific extensions for file recognition:
# - PSM files must contain 'psmpkl' → use .psmpkl extension
# - Peak files must contain 'peakpkl' → use .peakpkl extension
# After splitext(), stem will be just "{mzML_stem}" matching PSM base_name
psms_out = str(library_dir / f"{original_stem}.psmpkl")
peaks_out = str(library_dir / f"{original_stem}.peakpkl")
convert_cmd = [
"easypqp", "convert",
"--pepxml", filter_idxml,
"--spectra", matching_mzml,
"--psms", psms_out,
"--peaks", peaks_out
]
if self.executor.run_command(convert_cmd):
psms_files.append(psms_out)
peaks_files.append(peaks_out)
if psms_files:
# easypqp library outputs TSV format (despite common .pqp extension)
library_tsv = str(library_dir / "spectral_library.tsv")
library_cmd = ["easypqp", "library", "--out", library_tsv]
if not self.params.get("library-use-fdr", False):
# --nofdr only skips FDR recalculation, NOT threshold filtering
# Set all thresholds to 1.0 to bypass filtering for pre-filtered input
library_cmd.extend([
"--nofdr",
"--psm_fdr_threshold", "1.0",
"--peptide_fdr_threshold", "1.0",
"--protein_fdr_threshold", "1.0"
])
else:
# Apply user-specified FDR filtering
library_cmd.extend([
"--psm_fdr_threshold",
str(self.params.get("library-psm-fdr", 0.01)),
"--peptide_fdr_threshold",
str(self.params.get("library-peptide-fdr", 0.01)),
"--protein_fdr_threshold",
str(self.params.get("library-protein-fdr", 0.01))
])
for psms, peaks in zip(psms_files, peaks_files):
library_cmd.extend([psms, peaks])
if self.executor.run_command(library_cmd):
self.logger.log("✅ Spectral library created")
st.success("Spectral library created")
else:
self.logger.log("Warning: Failed to build spectral library")
else:
self.logger.log("Warning: No PSMs converted for library generation")
st.success(f"✓ {stem} identification completed")
# ================================
# 4️⃣ ProteomicsLFQ (cross-sample)
# ================================
self.logger.log("📈 Running cross-sample quantification...")
st.info("Running ProteomicsLFQ (cross-sample quantification)")
quant_mztab = str(quant_dir / "openms_quant.mzTab")
quant_cxml = str(quant_dir / "openms.consensusXML")
quant_msstats = str(quant_dir / "openms_msstats.csv")
with st.spinner("ProteomicsLFQ"):
combined_in = " ".join(in_mzML)
combined_ids = " ".join(filter_results)
self.logger.log(f"COMBINED_IN {combined_in}", 1)
self.logger.log(f"COMBINED_IN_TYPE {type(combined_in).__name__}", 1)
self.logger.log(f"FILTER_RESULTS = {filter_results}", 1)
self.logger.log(f"FILTER_RESULTS_LEN = {len(filter_results)}", 1)
# ✅ Streamlit output (debug view)
st.markdown("### 🔍 ProteomicsLFQ Input Debug")
st.write("**combined_in:**", combined_in)
st.write("**combined_in type:**", type(combined_in).__name__)
st.write("**combined_ids:**", combined_ids)
st.write("**combined_ids type:**", type(combined_ids).__name__)
if not self.executor.run_topp(
"ProteomicsLFQ",
{
"in": [in_mzML],
"ids": [filter_results],
"out": [quant_mztab],
"out_cxml": [quant_cxml],
"out_msstats": [quant_msstats],
},
{
"fasta": str(database_fasta),
"psmFDR": 0.5,
"proteinFDR": 0.5,
"threads": 12,
# Disable FAIMS/IM handling to avoid segfault in OpenMS 3.5.0
"PeptideQuantification:extract:IM_window": "0.0",
"PeptideQuantification:faims:merge_features": "false",
}
):
self.logger.log("Workflow stopped due to error")
return False
self.logger.log("✅ Quantification complete")
# ======================================================
# ⚠️ 5️⃣ GO Enrichment Analysis (INLINE IN EXECUTION)
# ======================================================
workspace_path = Path(self.workflow_dir).parent
res = get_abundance_data(workspace_path)
if res is not None:
pivot_df, _, _ = res
self.logger.log("✅ pivot_df loaded, starting GO enrichment...")
self._run_go_enrichment(pivot_df, results_dir)
else:
st.warning("GO enrichment skipped: abundance data not available.")
# ================================
# 5️⃣ Final report
# # ================================
st.success("🎉 TOPP workflow completed successfully")
st.write("📁 Results directory:")
st.code(str(results_dir))
return True
def _run_go_enrichment(self, pivot_df: pd.DataFrame, results_dir: Path):
p_cutoff = 0.05
fc_cutoff = 1.0
analysis_df = pivot_df.dropna(subset=["p-value", "log2FC"]).copy()
if analysis_df.empty:
st.error("No valid statistical data found for GO enrichment.")
self.logger.log("❗ analysis_df is empty")
else:
with st.spinner("Fetching GO terms from MyGene.info API..."):
mg = mygene.MyGeneInfo()
def get_clean_uniprot(name):
parts = str(name).split("|")
return parts[1] if len(parts) >= 2 else parts[0]
analysis_df["UniProt"] = analysis_df["ProteinName"].apply(get_clean_uniprot)
bg_ids = analysis_df["UniProt"].dropna().astype(str).unique().tolist()
fg_ids = analysis_df[
(analysis_df["p-value"] < p_cutoff) &
(analysis_df["log2FC"].abs() >= fc_cutoff)
]["UniProt"].dropna().astype(str).unique().tolist()
self.logger.log("✅ get_clean_uniprot applied")
if len(fg_ids) < 3:
st.warning(
f"Not enough significant proteins "
f"(p < {p_cutoff}, |log2FC| ≥ {fc_cutoff}). "
f"Found: {len(fg_ids)}"
)
self.logger.log("❗ Not enough significant proteins")
else:
res_list = mg.querymany(
bg_ids, scopes="uniprot", fields="go", as_dataframe=False
)
res_go = pd.DataFrame(res_list)
if "notfound" in res_go.columns:
res_go = res_go[res_go["notfound"] != True]
def extract_go_terms(go_data, go_type):
if not isinstance(go_data, dict) or go_type not in go_data:
return []
terms = go_data[go_type]
if isinstance(terms, dict):
terms = [terms]
return list({t.get("term") for t in terms if "term" in t})
for go_type in ["BP", "CC", "MF"]:
res_go[f"{go_type}_terms"] = res_go["go"].apply(
lambda x: extract_go_terms(x, go_type)
)
annotated_ids = set(res_go["query"].astype(str))
fg_set = annotated_ids.intersection(fg_ids)
bg_set = annotated_ids
self.logger.log(f"✅ fg_set bg_set are set")
def run_go(go_type):
go2fg = defaultdict(set)
go2bg = defaultdict(set)
for _, row in res_go.iterrows():
uid = str(row["query"])
for term in row[f"{go_type}_terms"]:
go2bg[term].add(uid)
if uid in fg_set:
go2fg[term].add(uid)
records = []
N_fg = len(fg_set)
N_bg = len(bg_set)
for term, fg_genes in go2fg.items():
a = len(fg_genes)
if a == 0:
continue
b = N_fg - a
c = len(go2bg[term]) - a
d = N_bg - (a + b + c)
_, p = fisher_exact([[a, b], [c, d]], alternative="greater")
records.append({
"GO_Term": term,
"Count": a,
"GeneRatio": f"{a}/{N_fg}",
"p_value": p,
})
df = pd.DataFrame(records)
if df.empty:
return None, None
df["-log10(p)"] = -np.log10(df["p_value"].replace(0, 1e-10))
df = df.sort_values("p_value").head(20)
# ✅ Plotly Figure
fig = px.bar(
df,
x="-log10(p)",
y="GO_Term",
orientation="h",
title=f"GO Enrichment ({go_type})",
)
self.logger.log(f"✅ Plotly Figure generated")
fig.update_layout(
yaxis=dict(autorange="reversed"),
height=500,
margin=dict(l=10, r=10, t=40, b=10),
)
return fig, df
go_results = {}
for go_type in ["BP", "CC", "MF"]:
fig, df_go = run_go(go_type)
if fig is not None:
go_results[go_type] = {
"fig": fig,
"df": df_go
}
self.logger.log(f"✅ go_type generated")
go_dir = results_dir / "go-terms"
go_dir.mkdir(parents=True, exist_ok=True)
import json
go_data = {}
for go_type in ["BP", "CC", "MF"]:
if go_type in go_results:
fig = go_results[go_type]["fig"]
df = go_results[go_type]["df"]
go_data[go_type] = {
"fig_json": fig.to_json(), # Figure → JSON string
"df_dict": df.to_dict(orient="records") # DataFrame → list of dicts
}
go_json_file = go_dir / "go_results.json"
with open(go_json_file, "w") as f:
json.dump(go_data, f)
st.session_state["go_results"] = go_results
st.session_state["go_ready"] = True if go_data else False
self.logger.log("✅ GO enrichment analysis complete")
@st.fragment
def results(self) -> None:
st.title("📊 Results")
comet_tab, perc_tab, filter_tab, lfq_tab = st.tabs([
"🔍 Identification",
"🔍 Rescoring",
"🔍 Filtering",
"🔍 Quantification"
])
# ================================
# 🔍 CometAdapter