libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
spectrum.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/core/processing/cbor/mzcbor/spectrum.h
3 * \date 24/11/2025
4 * \author Olivier Langella
5 * \brief PSI spectrum 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 "spectrum.h"
33#include <qjsonarray.h>
34#include <qjsonobject.h>
35#include <qtypes.h>
36#include <unistd.h>
37
38
39void
41{
42 QString txt_value;
43 // cborBinaryDataArray.clear();
44 precursorList.clear();
46 index = 0;
47 cvParamMap.clear();
48 scanListCvParamMap.clear();
49 scanList.clear();
50 id.clear();
51 qDebug();
52
53 if(reader.isInvalid())
54 {
56 QObject::tr("ERROR in Spectrum::fromCbor :\ncbor is not valid"));
57 }
58 if(reader.enterContainer())
59 {
60 while(reader.hasNext() && (!reader.isInvalid()))
61 {
62 if(reader.isString())
63 {
64 reader.decodeString(txt_value);
65 qDebug() << txt_value;
66 if(txt_value == "id")
67 {
68 reader.decodeString(txt_value);
69 qDebug() << txt_value;
70 id = txt_value;
71 }
72 else if(txt_value == "index")
73 {
74 index = reader.toUnsignedInteger();
75 reader.next();
76 }
77 else if(txt_value == "defaultArrayLength")
78 {
79 defaultArrayLength = reader.toUnsignedInteger();
80 reader.next();
81 }
82 else if(txt_value == "cvParam")
83 {
84 cvParamMap.fromCbor(reader);
85 }
86 else if(txt_value == "scanList")
87 {
88 // mpa_cborReader->next();
89 reader.enterContainer();
90
91 while(reader.hasNext() && (!reader.isInvalid()))
92 {
93 if(reader.isString())
94 {
95 if(reader.decodeString(txt_value))
96 {
97 qDebug() << txt_value;
98 if(txt_value == "cvParam")
99 {
100 scanListCvParamMap.fromCbor(reader);
101 }
102 else if(txt_value == "scan")
103 {
104 // scan array
105 reader.enterContainer();
106
107 while(reader.hasNext() && (!reader.isInvalid()))
108 {
109 // scan array
110 Scan scan;
111 scan.fromCbor(reader);
112 scanList.emplace_back(scan);
113 }
114 reader.leaveContainer();
115 }
116 }
117 else
118 {
119 reader.next();
120 }
121 }
122 else
123 {
124 reader.next();
125 }
126 }
127 reader.leaveContainer();
128 }
129 else if(txt_value == "precursorList")
130 {
131
132 reader.enterContainer();
133
134 while(reader.hasNext() && (!reader.isInvalid()))
135 {
136 if(reader.isString())
137 {
138 if(reader.decodeString(txt_value))
139 {
140 qDebug() << txt_value;
141
142 if(txt_value == "cvParam")
143 {
144 precursorListCvParamMap.fromCbor(reader);
145 }
146 else if(txt_value == "precursor")
147 {
148
149 reader
150 .enterContainer(); // array
151
152 while(reader.hasNext() && (!reader.isInvalid()))
153 {
154 // scan array
155 Precursor precursor;
156 precursor.fromCbor(reader);
157 precursorList.emplace_back(precursor);
158 }
159 reader
160 .leaveContainer(); // array
161
162 // reader.next();
163 }
164 else
165 {
166 reader.next();
167 }
168 }
169 else
170 {
171 reader.next();
172 }
173 }
174 else
175 {
176 reader.next();
177 }
178 }
179 reader.leaveContainer();
180 }
181 else if(txt_value == "binaryDataArray")
182 {
183 if(want_binary_data)
184 {
185
186 reader.enterContainer(); // start array
187
188 while(reader.hasNext())
189 {
190
191 BinaryDataArray data_array;
192 data_array.fromCbor(reader);
193 binaryDataArrayList.emplace_back(data_array);
194 }
195
196 reader.leaveContainer(); // end array
197 }
198 else
199 {
200 reader.next();
201 }
202 }
203 else
204 {
205 reader.next();
206 }
207 }
208 // reader.next();
209 }
210
211 reader.leaveContainer();
212 }
213 else
214 {
215 throw pappso::PappsoException(QObject::tr("ERROR in Spectrum::fromCbor : no container"));
216 }
217
218
219 if(reader.lastError() != QCborError::NoError)
220 {
222 QObject::tr("ERROR in Spectrum::fromCbor :\n%1").arg(reader.lastError()));
223 }
224}
225
226
227QJsonObject
229{
230 QJsonObject spectrum_json;
231 spectrum_json.insert("id", id);
232 spectrum_json.insert("index", (qint64)index);
233 spectrum_json.insert("defaultArrayLength", (qint64)defaultArrayLength);
234 spectrum_json.insert("cvParam", cvParamMap.toJsonArray());
235
236 QJsonObject scan_list;
237 // QJsonObject scan_list_cvparam;
238 scan_list.insert("cvParam", scanListCvParamMap.toJsonArray());
239 QJsonArray scan_array;
240 for(auto &scan : scanList)
241 {
242 scan_array.append(scan.toJsonObject());
243 }
244 scan_list.insert("scan", scan_array);
245 spectrum_json.insert("scanList", scan_list);
246
247 if(!precursorList.empty())
248 {
249 QJsonObject precursor_list;
250 // QJsonObject scan_list_cvparam;
251 precursor_list.insert("cvParam", precursorListCvParamMap.toJsonArray());
252 QJsonArray precursor_array;
253 for(auto &precursor : precursorList)
254 {
255 precursor_array.append(precursor.toJsonObject());
256 }
257 precursor_list.insert("precursor", precursor_array);
258 spectrum_json.insert("precursorList", precursor_list);
259 }
260
261 if(!binaryDataArrayList.empty())
262 {
263 Trace trace;
264
265 decodeTrace(trace);
266
267
268 spectrum_json.insert("spectrum", trace.toJsonObject());
269 }
270
271 return spectrum_json;
272}
273
274std::map<QString, pappso::cbor::mzcbor::CvParam>
276{
277 CvParamMap cv_params;
278 reader.enterContainer();
279 QString txt_value;
280
281 while(reader.hasNext() && (!reader.isInvalid()))
282 {
283 if(reader.isString())
284 {
285 if(reader.decodeString(txt_value))
286 {
287 qDebug() << txt_value;
288 if(txt_value == "cvParam")
289 {
290 cv_params.fromCbor(reader);
291 }
292 else
293 {
294 reader.next();
295 }
296 }
297 }
298 else
299 {
300 reader.next();
301 }
302 }
303
304 reader.leaveContainer();
305 return cv_params;
306}
307
308
309qint64
311{
312 // <cvParam cvRef="MS" accession="MS:1000285" value="17377980" name="total ion current" />
313
314 auto it = cvParamMap.find("MS:1000285");
315 if(it == cvParamMap.end())
316 {
317 throw pappso::ExceptionNotFound(QObject::tr("total ion count not found in cvParam map"));
318 }
319 qDebug() << it->first << " " << it->second.valueInt;
320 return it->second.valueInt;
321}
322
323
324uint
326{
327 // <cvParam cvRef="MS" accession="MS:1000511" value="1" name="ms level" />
328
329 auto it = cvParamMap.find("MS:1000511");
330 if(it == cvParamMap.end())
331 {
332 throw pappso::ExceptionNotFound(QObject::tr("MS level not found in cvParam map"));
333 }
334 qDebug() << it->first << " " << it->second.valueInt;
335 return it->second.valueInt;
336}
337
338double
340{
341 if(scanList.size() == 0)
342 {
343 throw pappso::ExceptionNotFound(QObject::tr("scan list is empty in spectrum"));
344 }
345 if(scanList.size() > 1)
346 {
347 throw pappso::ExceptionNotFound(QObject::tr("too many scans in spectrum"));
348 }
349 return scanList.front().getRtInSeconds();
350}
351
352void
354{
355 std::vector<pappso_double> xVector;
356 std::vector<pappso_double> yVector;
357 for(auto &binary_array : binaryDataArrayList)
358 {
359 if(binary_array.isIntensity())
360 {
361 binary_array.decodeVector(defaultArrayLength, yVector);
362 }
363 else if(binary_array.isMz())
364 {
365 binary_array.decodeVector(defaultArrayLength, xVector);
366 }
367 else
368 {
369
370 throw pappso::ExceptionNotFound(QObject::tr(
371 "error in pappso::cbor::mzcbor::Spectrum::decodeTrace: binary vector type not found"));
372 }
373 }
374
375 trace.initialize(xVector, yVector);
376}
A simple container of DataPoint instances.
Definition trace.h:152
QJsonObject toJsonObject(const QString &x_label="x", const QString &y_label="y") const
serialize a PAPPSO trace object to JSON
Definition trace.cpp:1191
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition trace.cpp:597
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...
cvParam map
unsigned int uint
Definition types.h:67
PSI scan object for mzML/mzCBOR.
PSI spectrum object for mzML/mzCBOR.
void fromCbor(CborStreamReader &reader)
void fromCbor(CborStreamReader &reader)
fill the structure reading data from a CBOR stream
void fromCbor(CborStreamReader &reader)
void fromCbor(CborStreamReader &reader)
Definition scan.cpp:57
static std::map< QString, pappso::cbor::mzcbor::CvParam > readScanCvParams(CborStreamReader &reader)
Definition spectrum.cpp:275
std::vector< Precursor > precursorList
Definition spectrum.h:81
std::vector< Scan > scanList
Definition spectrum.h:80
QJsonObject toJsonObject() const
write the structure to a JSON object This is a simplified representation for easy JSON data handling ...
Definition spectrum.cpp:228
void fromCbor(CborStreamReader &reader, bool want_binary_data)
fill the structure reading data from a CBOR stream
Definition spectrum.cpp:40
void decodeTrace(pappso::Trace &trace) const
Definition spectrum.cpp:353
std::vector< BinaryDataArray > binaryDataArrayList
Definition spectrum.h:82