OpenMS
Loading...
Searching...
No Matches
MzPAF.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
16#include <OpenMS/OpenMSConfig.h>
17
18#include <iosfwd>
19#include <optional>
20#include <utility>
21#include <vector>
22
23namespace OpenMS
24{
25
26 //--------------------------------------------------------------------------
27 // Data Structures
28 //--------------------------------------------------------------------------
29
37 enum class MzPAFIonSeries
38 {
39 A,
40 B,
41 C,
42 X,
43 Y,
44 Z,
45 PRECURSOR,
46 IMMONIUM,
47 INTERNAL,
48 REPORTER,
49 FORMULA,
50 NAMED,
51 UNKNOWN,
52 // The satellite series are appended after UNKNOWN on purpose. They were added later, and
53 // the enumerator values are exposed arithmetically to Python (nb::is_arithmetic), so the
54 // existing values have to stay put. Do not "tidy" this by moving UNKNOWN back to the end.
55 D,
56 V,
57 W
58 };
59
65 enum class MzPAFDeltaUnit
66 {
67 DALTON,
68 PPM
69 };
70
78 struct OPENMS_DLLAPI MzPAFNeutralLoss
79 {
81
82 bool operator==(const MzPAFNeutralLoss& other) const;
83 bool operator!=(const MzPAFNeutralLoss& other) const { return !(*this == other); }
84 };
85
94 struct OPENMS_DLLAPI MzPAFMassDelta
95 {
96 double value = 0.0;
97 MzPAFDeltaUnit unit = MzPAFDeltaUnit::DALTON;
98
99 bool operator==(const MzPAFMassDelta& other) const;
100 bool operator!=(const MzPAFMassDelta& other) const { return !(*this == other); }
101 };
102
128 struct OPENMS_DLLAPI MzPAFAnnotation
129 {
130 std::optional<int> analyte_index;
131 MzPAFIonSeries ion_series = MzPAFIonSeries::UNKNOWN;
132 std::optional<int> ordinal;
133 std::optional<char> immonium_residue;
134 std::optional<std::pair<int, int>> internal_range;
135 std::optional<std::string> reporter_name;
136 std::optional<EmpiricalFormula> formula;
137 std::optional<std::string> named_compound;
138 std::vector<MzPAFNeutralLoss> neutral_losses;
139 std::optional<int> isotope_offset;
140 std::optional<EmpiricalFormula> adduct;
141 std::optional<int> charge;
142 std::optional<MzPAFMassDelta> mass_delta;
143 std::optional<double> confidence;
144 std::optional<std::string> embedded_sequence;
145 std::optional<char> satellite_subtype;
146
148 bool isValid() const;
149
151 bool operator==(const MzPAFAnnotation& other) const;
152 bool operator!=(const MzPAFAnnotation& other) const { return !(*this == other); }
153 };
154
156 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const MzPAFAnnotation& ann);
157
166 struct OPENMS_DLLAPI MzPAFPeakAnnotations
167 {
168 std::vector<MzPAFAnnotation> annotations;
169
171 bool empty() const { return annotations.empty(); }
172
174 size_t size() const { return annotations.size(); }
175
177 bool operator==(const MzPAFPeakAnnotations& other) const;
178 bool operator!=(const MzPAFPeakAnnotations& other) const { return !(*this == other); }
179 };
180
181 //--------------------------------------------------------------------------
182 // Error Handling
183 //--------------------------------------------------------------------------
184
204
211 OPENMS_DLLAPI const char* mzPAFErrorCodeToString(MzPAFErrorCode code);
212
218 class OPENMS_DLLAPI MzPAFParseError : public Exception::ParseError
219 {
220 public:
222 const char* file,
223 int line,
224 const char* function,
225 MzPAFErrorCode error_code,
226 size_t error_position,
227 const std::string& input,
228 const std::string& message
229 ) noexcept;
230
231 MzPAFErrorCode getErrorCode() const noexcept { return code_; }
232 size_t getPosition() const noexcept { return position_; }
233 std::string getFormattedMessage() const;
234
235 private:
237 size_t position_;
238 std::string context_before_;
239 std::string context_after_;
240
241 void extractContext_(const std::string& input, size_t pos);
242 };
243
244 //--------------------------------------------------------------------------
245 // Parser API
246 //--------------------------------------------------------------------------
247
279 class OPENMS_DLLAPI MzPAF
280 {
281 public:
282 //--------------------------------------------------------------------------
283 // Parsing
284 //--------------------------------------------------------------------------
285
296 static MzPAFAnnotation parse(const std::string& input);
297
305 static MzPAFPeakAnnotations parseMultiple(const std::string& input);
306
313 static std::optional<MzPAFAnnotation> tryParse(const std::string& input);
314
321 static std::optional<MzPAFPeakAnnotations> tryParseMultiple(const std::string& input);
322
323 //--------------------------------------------------------------------------
324 // Serialization
325 //--------------------------------------------------------------------------
326
337 static std::string toString(const MzPAFAnnotation& ann);
338
345 static std::string toString(const MzPAFPeakAnnotations& anns);
346
347 //--------------------------------------------------------------------------
348 // PeakAnnotation Integration
349 //--------------------------------------------------------------------------
350
362 const MzPAFAnnotation& mzpaf, double mz, double intensity);
363
373 const PeptideHit::PeakAnnotation& peak_annotation);
374
375 //--------------------------------------------------------------------------
376 // Utilities
377 //--------------------------------------------------------------------------
378
387 static bool isMzPAFFormat(const std::string& annotation);
388
402 static std::optional<double> calculateTheoreticalMZ(
403 const MzPAFAnnotation& ann, const AASequence& sequence);
404
418
426 static char ionSeriesToChar(MzPAFIonSeries series);
427
435 static bool charToIonSeries(char c, MzPAFIonSeries& series);
436
437 private:
438 MzPAF() = delete; // Static class, no instantiation
439 };
440
441} // namespace OpenMS
Definition AASequence.h:96
Representation of an empirical formula.
Definition EmpiricalFormula.h:62
Parse Error exception.
Definition Exception.h:596
Parse error for mzPAF notation.
Definition MzPAF.h:219
std::string context_before_
Definition MzPAF.h:238
MzPAFErrorCode getErrorCode() const noexcept
Definition MzPAF.h:231
size_t position_
Definition MzPAF.h:237
size_t getPosition() const noexcept
Definition MzPAF.h:232
void extractContext_(const std::string &input, size_t pos)
std::string getFormattedMessage() const
std::string context_after_
Definition MzPAF.h:239
MzPAFErrorCode code_
Definition MzPAF.h:236
MzPAFParseError(const char *file, int line, const char *function, MzPAFErrorCode error_code, size_t error_position, const std::string &input, const std::string &message) noexcept
Parser and writer for mzPAF (Peak Annotation Format) notation.
Definition MzPAF.h:280
static std::optional< MzPAFAnnotation > tryParse(const std::string &input)
Try to parse an mzPAF string (non-throwing)
static std::string toString(const MzPAFPeakAnnotations &anns)
Convert multiple annotations to mzPAF string.
static bool charToIonSeries(char c, MzPAFIonSeries &series)
Parse ion series from character.
static MzPAFAnnotation parse(const std::string &input)
Parse an mzPAF string into a single annotation.
static PeptideHit::PeakAnnotation toPeakAnnotation(const MzPAFAnnotation &mzpaf, double mz, double intensity)
Create a PeptideHit::PeakAnnotation from mzPAF data.
static std::optional< double > calculateTheoreticalMZ(const MzPAFAnnotation &ann, const AASequence &sequence)
Calculate theoretical m/z for an annotation.
static MzPAFPeakAnnotations parseMultiple(const std::string &input)
Parse an mzPAF string with potentially multiple annotations.
static MzPAFPeakAnnotations fromPeakAnnotation(const PeptideHit::PeakAnnotation &peak_annotation)
Parse mzPAF annotations from a PeptideHit::PeakAnnotation.
static std::optional< MzPAFPeakAnnotations > tryParseMultiple(const std::string &input)
Try to parse multiple annotations (non-throwing)
static bool isMzPAFFormat(const std::string &annotation)
Check if a string appears to be in mzPAF format.
MzPAF()=delete
static bool isPeptideFragmentIon(MzPAFIonSeries series)
Check if ion series is a peptide fragment ion (a, b, c, d, v, w, x, y, z)
static char ionSeriesToChar(MzPAFIonSeries series)
Get the ion series character for an annotation.
static std::string toString(const MzPAFAnnotation &ann)
Convert an annotation to mzPAF string.
MzPAFIonSeries
Ion series types for mzPAF peak annotations.
Definition MzPAF.h:38
MzPAFErrorCode
Error codes for mzPAF parsing errors.
Definition MzPAF.h:191
MzPAFDeltaUnit
Unit for mass delta values in mzPAF annotations.
Definition MzPAF.h:66
@ X
x-ion (C-terminal)
@ C
c-ion (N-terminal, ETD)
@ INTERNAL
Internal fragment (m)
@ Z
z-ion (C-terminal, ETD)
@ V
v-ion (C-terminal satellite ion, complete side-chain loss)
@ Y
y-ion (C-terminal)
@ W
w-ion (C-terminal satellite ion, partial side-chain loss)
@ UNKNOWN
Unknown or unrecognized ion type.
@ NAMED
Named compound (_)
@ A
a-ion (N-terminal, loses CO)
@ REPORTER
Reporter ion (r)
@ IMMONIUM
Immonium ion (I)
@ B
b-ion (N-terminal)
@ FORMULA
Chemical formula ion (f)
@ PRECURSOR
Precursor ion (p)
@ D
d-ion (N-terminal satellite ion, partial side-chain loss)
@ INVALID_NUMBER
Invalid numeric value.
@ UNEXPECTED_CHARACTER
Unexpected character encountered.
@ EMPTY_INPUT
Empty input string.
@ INVALID_ION_SERIES
Invalid ion series character.
@ INVALID_CONFIDENCE
Invalid confidence score.
@ UNEXPECTED_END_OF_INPUT
Unexpected end of input.
@ INVALID_DELTA
Invalid mass delta specification.
@ UNCLOSED_BRACKET
Opening bracket without matching close.
@ INTERNAL_ERROR
Internal parser error.
@ INVALID_CHARGE
Invalid charge specification.
@ INVALID_FORMULA
Invalid chemical formula.
@ DALTON
Mass delta in Daltons (Da)
@ PPM
Mass delta in parts per million.
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
const char * mzPAFErrorCodeToString(MzPAFErrorCode code)
Convert error code to human-readable string.
A single mzPAF peak annotation.
Definition MzPAF.h:129
std::optional< std::string > named_compound
Name for named compound ions (_[name])
Definition MzPAF.h:137
std::optional< std::string > reporter_name
Name for reporter ions (r[TMT127N])
Definition MzPAF.h:135
std::optional< char > immonium_residue
Residue for immonium ions (Y in IY)
Definition MzPAF.h:133
bool isValid() const
Check if this annotation has minimal valid data.
std::vector< MzPAFNeutralLoss > neutral_losses
Neutral losses (-H2O, -NH3, etc.)
Definition MzPAF.h:138
std::optional< int > charge
Charge state (^2, ^3)
Definition MzPAF.h:141
std::optional< char > satellite_subtype
Optional 'a' or 'b' subtype, valid only for d- and w-ions.
Definition MzPAF.h:145
std::optional< double > confidence
Confidence score (*0.75)
Definition MzPAF.h:143
std::optional< MzPAFMassDelta > mass_delta
Mass delta (/0.001, /-1.4ppm)
Definition MzPAF.h:142
std::optional< int > ordinal
Position/ordinal (4 in y4)
Definition MzPAF.h:132
bool operator!=(const MzPAFAnnotation &other) const
Definition MzPAF.h:152
std::optional< EmpiricalFormula > formula
Chemical formula for formula ions (f{C16H22O})
Definition MzPAF.h:136
std::optional< int > analyte_index
Analyte index (0@, 1@, etc.) for multi-analyte spectra.
Definition MzPAF.h:130
std::optional< EmpiricalFormula > adduct
Adduct ion (+Na, +K, etc.)
Definition MzPAF.h:140
std::optional< std::string > embedded_sequence
Embedded ProForma sequence string ({LC[Carbamidomethyl]})
Definition MzPAF.h:144
std::optional< std::pair< int, int > > internal_range
Range for internal fragments (m3:6 -> {3,6})
Definition MzPAF.h:134
std::optional< int > isotope_offset
Isotope offset (+1i, +2i for M+1, M+2)
Definition MzPAF.h:139
bool operator==(const MzPAFAnnotation &other) const
Equality comparison.
Mass delta in an mzPAF annotation.
Definition MzPAF.h:95
bool operator!=(const MzPAFMassDelta &other) const
Definition MzPAF.h:100
bool operator==(const MzPAFMassDelta &other) const
Neutral loss in an mzPAF annotation.
Definition MzPAF.h:79
bool operator==(const MzPAFNeutralLoss &other) const
bool operator!=(const MzPAFNeutralLoss &other) const
Definition MzPAF.h:83
EmpiricalFormula formula
Parsed chemical formula of the loss.
Definition MzPAF.h:80
Multiple mzPAF annotations for a single peak.
Definition MzPAF.h:167
size_t size() const
Get number of annotations.
Definition MzPAF.h:174
bool empty() const
Check if there are any annotations.
Definition MzPAF.h:171
std::vector< MzPAFAnnotation > annotations
List of alternative annotations.
Definition MzPAF.h:168
bool operator!=(const MzPAFPeakAnnotations &other) const
Definition MzPAF.h:178
bool operator==(const MzPAFPeakAnnotations &other) const
Equality comparison.
Contains annotations of a peak.
Definition PeptideHit.h:87