libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
precursor.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/core/processing/cbor/mzcbor/precursor.h
3 * \date 24/11/2025
4 * \author Olivier Langella
5 * \brief PSI precursor object for mzML/mzCBOR
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2025 Olivier Langella <Olivier.Langella@universite-paris-saclay.fr>.
10 *
11 * This file is part of PAPPSOms-tools.
12 *
13 * PAPPSOms-tools is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * PAPPSOms-tools is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with PAPPSOms-tools. If not, see <http://www.gnu.org/licenses/>.
25 *
26 ******************************************************************************/
27
28
29#include "precursor.h"
31#include <cmath>
32
33
34void
36{
37 QString txt_value;
38 for(auto it_binarr = cbor_map.cbegin(); it_binarr != cbor_map.cend(); it_binarr++)
39 {
40 txt_value = it_binarr.key().toString();
41 qDebug() << txt_value;
42 if(txt_value == "cvParam")
43 {
44 cvParamMap.fromQCborArray(it_binarr.value().toArray());
45 // reader.next();
46 }
47 }
48}
49void
51{
52 QString txt_value;
53 reader.enterContainer();
54 qDebug() << txt_value;
55 while(reader.hasNext() && (!reader.isInvalid()))
56 {
57 if(reader.isString())
58 {
59 if(reader.decodeString(txt_value))
60 {
61 qDebug() << txt_value;
62 if(txt_value == "cvParam")
63 {
64 cvParamMap.fromCbor(reader);
65 // reader.next();
66 }
67 else
68 {
69 reader.next();
70 }
71 }
72 else
73 {
74 reader.next();
75 }
76 }
77 else
78 {
79 reader.next();
80 }
81 }
82 reader.leaveContainer();
83}
84
85double
87{
88 auto it = cvParamMap.find("MS:1000827");
89
90 if(it == cvParamMap.end())
91 {
93 QObject::tr("isolation window target m/z MS:1000827 not found in cvParam map"));
94 }
95
96 if(std::isnan(it->second.valueDouble))
97 {
98 return (double)it->second.valueInt;
99 }
100 return it->second.valueDouble;
101}
102
103double
105{
106 double mz = getTargetMz();
107
108 auto it = cvParamMap.find("MS:1000828");
109
110 if(it == cvParamMap.end())
111 {
113 QObject::tr("isolation window lower offset MS:1000828 not found in cvParam map"));
114 }
115
116 if(std::isnan(it->second.valueDouble))
117 {
118 return mz - (double)it->second.valueInt;
119 }
120 return mz - it->second.valueDouble;
121}
122
123QJsonObject
125{
126 QJsonObject isolationWindow;
127 isolationWindow.insert("cvParam", cvParamMap.toJsonArray());
128 return isolationWindow;
129}
130QJsonObject
132{
133 QJsonObject precursor;
134 if(!spectrumRef.isEmpty())
135 precursor.insert("spectrumRef", spectrumRef);
136 precursor.insert("isolationWindow", isolationWindow.toJsonObject());
137 return precursor;
138}
139
140double
142{
143
144 double mz = getTargetMz();
145
146 auto it = cvParamMap.find("MS:1000829");
147
148 if(it == cvParamMap.end())
149 {
151 QObject::tr("isolation window upper offset MS:1000829 not found in cvParam map"));
152 }
153
154 if(std::isnan(it->second.valueDouble))
155 {
156 return mz + (double)it->second.valueInt;
157 }
158 return mz + it->second.valueDouble;
159}
160
161void
163{
164 QString txt_value;
165 for(auto it_binarr = cbor_map.cbegin(); it_binarr != cbor_map.cend(); it_binarr++)
166 {
167 txt_value = it_binarr.key().toString();
168 qDebug() << txt_value;
169 if(txt_value == "cvParam")
170 {
171 cvParamMap.fromQCborArray(it_binarr.value().toArray());
172 // reader.next();
173 }
174 }
175}
176void
178{
179 QString txt_value;
180 reader.enterContainer();
181 qDebug() << txt_value;
182 while(reader.hasNext() && (!reader.isInvalid()))
183 {
184 if(reader.isString())
185 {
186 if(reader.decodeString(txt_value))
187 {
188 qDebug() << txt_value;
189 if(txt_value == "cvParam")
190 {
191 cvParamMap.fromCbor(reader);
192 // reader.next();
193 }
194 else
195 {
196 reader.next();
197 }
198 }
199 else
200 {
201 reader.next();
202 }
203 }
204 else
205 {
206 reader.next();
207 }
208 }
209 reader.leaveContainer();
210}
211
212
213int
215{
216
217 // <cvParam cvRef="MS" accession="MS:1000041" value="3" name="charge state" />
218
219 auto it = cvParamMap.find("MS:1000041");
220 if(it == cvParamMap.end())
221 {
223 QObject::tr("Charge state not found in SelectedIon cvParam map"));
224 }
225 qDebug() << it->first << " " << it->second.valueInt;
226 return it->second.valueInt;
227}
228
229double
231{
232
233 // <cvParam cvRef="MS" accession="MS:1000042" value="248032.25" name="peak intensity"
234 // unitAccession="MS:1000131" unitName="number of detector counts" unitCvRef="MS" />
235 // MS:1000042
236 auto it = cvParamMap.find("MS:1000042");
237 if(it == cvParamMap.end())
238 {
239 // throw pappso::ExceptionNotFound(
240 // QObject::tr("Intensity not found in SelectedIon cvParam map"));
241 *isok = false;
242 return 0;
243 }
244 qDebug() << it->first << " " << it->second.valueDouble;
245 double intensity = it->second.valueDouble;
246 *isok = true;
247 if(std::isnan(intensity))
248 {
249 return it->second.valueInt;
250 }
251 return intensity;
252}
253
254double
256{
257
258 // <cvParam cvRef="MS" accession="MS:1000744" value="506.2513" name="selected ion m/z"
259 // unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" />
260
261 auto it = cvParamMap.find("MS:1000744");
262 if(it == cvParamMap.end())
263 {
264 throw pappso::ExceptionNotFound(QObject::tr("m/z not found in SelectedIon cvParam map"));
265 }
266 qDebug() << it->first << " " << it->second.valueDouble;
267 return it->second.valueDouble;
268}
269
270
271void
273{
274
275 QString txt_value;
276 reader.enterContainer();
277 qDebug() << txt_value;
278 while(reader.hasNext() && (!reader.isInvalid()))
279 {
280 if(reader.isString())
281 {
282 if(reader.decodeString(txt_value))
283 {
284 qDebug() << txt_value;
285 if(txt_value == "cvParam")
286 {
287 // precursorListCvParamMap = CvParam::getCvParamsMapFromCbor(reader);
288 reader.next();
289 }
290 else if(txt_value == "isolationWindow")
291 {
292 isolationWindow.fromCbor(reader);
293 }
294 else if(txt_value == "selectedIonList")
295 {
296
297 reader.enterContainer();
298
299 while(reader.hasNext() && (!reader.isInvalid()))
300 {
301 if(reader.isString())
302 {
303 if(reader.decodeString(txt_value))
304 {
305 qDebug() << txt_value;
306 if(txt_value == "cvParam")
307 {
308 selectedIonListCvParamMap.fromCbor(reader);
309 }
310 else if(txt_value == "selectedIon")
311 {
312
313 reader.enterContainer(); // array
314
315 while(reader.hasNext() && (!reader.isInvalid()))
316 {
317 SelectedIon selected_ion;
318 selected_ion.fromCbor(reader);
319 selectedIonList.emplace_back(selected_ion);
320 }
321 reader.leaveContainer(); // array
322 }
323 else
324 {
325 reader.next();
326 }
327 }
328 else
329 {
330 reader.next();
331 }
332 }
333 else
334 {
335 reader.next();
336 }
337 }
338 reader.leaveContainer();
339 }
340 else if(txt_value == "activation")
341 {
342 qDebug();
343 reader.next();
344 }
345
346 else if(txt_value == "spectrumRef")
347 {
348 reader.decodeString(txt_value);
349 qDebug() << txt_value;
350 spectrumRef = txt_value;
351 }
352 else
353 {
354 reader.next();
355 }
356 }
357 else
358 {
359 reader.next();
360 }
361 }
362 else
363 {
364 reader.next();
365 }
366 }
367 reader.leaveContainer();
368}
simple override of the raw QCborStreamReader This adds convenient functions to put CBOR data into C++...
bool decodeString(QString &the_str)
decode the current cbor value as a string the point to the next value the current value is decoded as...
PSI precursor object for mzML/mzCBOR.
double getUpperMz() const
compute isolation window upper m/z given cvParam informations
double getLowerMz() const
compute isolation window lower m/z given cvParam informations
double getTargetMz() const
get isolation window target m/z given cvParam informations
Definition precursor.cpp:86
QJsonObject toJsonObject() const
write the structure to a JSON object
void fromQCborMap(const QCborMap &reader)
Definition precursor.cpp:35
void fromCbor(CborStreamReader &reader)
Definition precursor.cpp:50
IsolationWindow isolationWindow
Definition precursor.h:96
QJsonObject toJsonObject() const
write the structure to a JSON object
std::vector< SelectedIon > selectedIonList
Definition precursor.h:100
void fromCbor(CborStreamReader &reader)
void fromQCborMap(const QCborMap &reader)
void fromCbor(CborStreamReader &reader)
double getIntensity(bool *isok) const