|
1 | 1 | """Read MS2 spectra.""" |
2 | 2 |
|
3 | 3 | from pathlib import Path |
4 | | -from typing import Generator, List |
| 4 | +from typing import Generator |
5 | 5 |
|
6 | 6 | import numpy as np |
7 | | -from pyteomics import mzml, mgf |
| 7 | +from pyteomics import mgf, mzml |
8 | 8 |
|
9 | 9 | from ms2pip.exceptions import ( |
10 | | - UnsupportedSpectrumFiletypeError, |
11 | | - InvalidSpectrumError, |
12 | 10 | EmptySpectrumError, |
| 11 | + InvalidSpectrumError, |
| 12 | + UnsupportedSpectrumFiletypeError, |
13 | 13 | ) |
14 | 14 |
|
15 | 15 |
|
16 | 16 | class Spectrum: |
17 | | - def __init__(self, title, charge, pepmass, msms, peaks) -> None: |
| 17 | + def __init__( |
| 18 | + self, title, msms, peaks, precursor_charge=None, precursor_mz=None |
| 19 | + ) -> None: |
18 | 20 | """Minimal information on observed MS2 spectrum.""" |
19 | 21 | self.title = str(title) |
20 | | - self.charge = int(charge) |
21 | | - self.pepmass = float(pepmass) |
22 | 22 | self.msms = np.array(msms, dtype=np.float32) |
23 | 23 | self.peaks = np.array(peaks, dtype=np.float32) |
| 24 | + self.precursor_charge = int(precursor_charge) if precursor_charge else None |
| 25 | + self.precursor_mz = float(precursor_mz) if precursor_mz else None |
24 | 26 |
|
25 | 27 | self.tic = np.sum(self.peaks) |
26 | 28 |
|
@@ -57,7 +59,9 @@ def remove_reporter_ions(self, label_type=None) -> None: |
57 | 59 | def remove_precursor(self, tolerance=0.02) -> None: |
58 | 60 | """Remove precursor peak.""" |
59 | 61 | for mi, mp in enumerate(self.msms): |
60 | | - if (mp >= self.pepmass - tolerance) & (mp <= self.pepmass + tolerance): |
| 62 | + if (mp >= self.precursor_mz - tolerance) & ( |
| 63 | + mp <= self.precursor_mz + tolerance |
| 64 | + ): |
61 | 65 | self.peaks[mi] = 0 |
62 | 66 |
|
63 | 67 | def tic_norm(self) -> None: |
@@ -91,10 +95,16 @@ def read_mgf(spec_file) -> Generator[Spectrum, None, None]: |
91 | 95 | spec_id = spectrum["params"]["title"] |
92 | 96 | peaks = spectrum["intensity array"] |
93 | 97 | msms = spectrum["m/z array"] |
94 | | - precursor_mz = spectrum["params"]["pepmass"][0] |
95 | | - precursor_charge = spectrum["params"]["charge"][0] |
| 98 | + try: |
| 99 | + precursor_charge = spectrum["params"]["charge"][0] |
| 100 | + except KeyError: |
| 101 | + precursor_charge = None |
| 102 | + try: |
| 103 | + precursor_mz = spectrum["params"]["pepmass"][0] |
| 104 | + except KeyError: |
| 105 | + precursor_mz = None |
96 | 106 | parsed_spectrum = Spectrum( |
97 | | - spec_id, precursor_charge, precursor_mz, msms, peaks |
| 107 | + spec_id, msms, peaks, precursor_charge, precursor_mz |
98 | 108 | ) |
99 | 109 | yield parsed_spectrum |
100 | 110 |
|
@@ -128,7 +138,7 @@ def read_mzml(spec_file) -> Generator[Spectrum, None, None]: |
128 | 138 | precursor_mz = precursor["selected ion m/z"] |
129 | 139 | precursor_charge = precursor["charge state"] |
130 | 140 | parsed_spectrum = Spectrum( |
131 | | - spec_id, precursor_charge, precursor_mz, msms, peaks |
| 141 | + spec_id, msms, peaks, precursor_charge, precursor_mz |
132 | 142 | ) |
133 | 143 | yield parsed_spectrum |
134 | 144 |
|
|
0 commit comments