Debian Codegraph @

docs/source/code_inc/get_precursors.inc

raw
1 .. code-block:: python
2
3 #!/usr/bin/env python
4
5 import os
6 from operator import itemgetter
7
8 import pymzml
9
10
11 def main():
12 """
13 Extract the 10 most often fragmented precursors from the BSA example file.
14
15 This can e.g. be used for defining exclusion lists for further MS runs.
16
17 usage:
18
19 ./get_precursors.py
20
21 """
22
23 example_file = os.path.join(
24 os.path.dirname(__file__), os.pardir, "tests", "data", "BSA1.mzML.gz"
25 )
26 run = pymzml.run.Reader(example_file)
27 fragmented_precursors = {}
28 for spectrum in run:
29 if spectrum.ms_level == 2:
30 selected_precursors = spectrum.selected_precursors
31 if spectrum.selected_precursors is not None:
32 for precursor_dict in selected_precursors:
33 precursor_mz = precursor_dict["mz"]
34 precursor_i = precursor_dict["i"]
35 rounded_precursor_mz = round(precursor_mz, 3)
36 if rounded_precursor_mz not in fragmented_precursors.keys():
37 fragmented_precursors[rounded_precursor_mz] = []
38 fragmented_precursors[rounded_precursor_mz].append(spectrum.ID)
39
40 precursor_info_list = []
41 for rounded_precursor_mz, spectra_list in fragmented_precursors.items():
42 precursor_info_list.append(
43 (len(spectra_list), rounded_precursor_mz, spectra_list)
44 )
45
46 for pos, (number_of_spectra, rounded_precursor_mz, spectra_list) in enumerate(
47 sorted(precursor_info_list, reverse=True)
48 ):
49 print(
50 "Found precursor: {0} in spectra: {1}".format(
51 rounded_precursor_mz, spectra_list
52 )
53 )
54 if pos > 8:
55 break
56
57
58 if __name__ == "__main__":
59 main()