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"
31
32
33void
35{
36 QString txt_value;
37 cborBinaryDataArray.clear();
38 precursorList.clear();
40 index = 0;
41 cvParamMap.clear();
42 scanListCvParamMap.clear();
43 scanCvParamMap.clear();
44 id.clear();
45 qDebug();
46
47 if(reader.isInvalid())
48 {
50 QObject::tr("ERROR in Spectrum::fromCbor :\ncbor is not valid"));
51 }
52 if(reader.enterContainer())
53 {
54 while(reader.hasNext() && (!reader.isInvalid()))
55 {
56 if(reader.isString())
57 {
58 reader.decodeString(txt_value);
59 qDebug() << txt_value;
60 if(txt_value == "id")
61 {
62 reader.decodeString(txt_value);
63 qDebug() << txt_value;
64 id = txt_value;
65 }
66 else if(txt_value == "index")
67 {
68 index = reader.toUnsignedInteger();
69 reader.next();
70 }
71 else if(txt_value == "defaultArrayLength")
72 {
73 defaultArrayLength = reader.toUnsignedInteger();
74 reader.next();
75 }
76 else if(txt_value == "cvParam")
77 {
79 }
80 else if(txt_value == "scanList")
81 {
82 // mpa_cborReader->next();
83 reader.enterContainer();
84
85 while(reader.hasNext() && (!reader.isInvalid()))
86 {
87 if(reader.isString())
88 {
89 if(reader.decodeString(txt_value))
90 {
91 qDebug() << txt_value;
92 if(txt_value == "cvParam")
93 {
95 }
96 else if(txt_value == "scan")
97 {
98 // scan array
99 reader.enterContainer();
100 while(reader.hasNext())
101 {
102 auto new_cv = readScanCvParams(reader);
103 scanCvParamMap.insert(new_cv.begin(), new_cv.end());
104 }
105 reader.leaveContainer();
106 }
107 }
108 else
109 {
110 reader.next();
111 }
112 }
113 else
114 {
115 reader.next();
116 }
117 }
118 reader.leaveContainer();
119 }
120 else if(txt_value == "precursorList")
121 {
122
123 reader.enterContainer();
124
125 while(reader.hasNext() && (!reader.isInvalid()))
126 {
127 if(reader.isString())
128 {
129 if(reader.decodeString(txt_value))
130 {
131 qDebug() << txt_value;
132
133 if(txt_value == "cvParam")
134 {
136 }
137 else if(txt_value == "precursor")
138 {
139
140 reader
141 .enterContainer(); // array
142
143 while(reader.hasNext() && (!reader.isInvalid()))
144 {
145 // scan array
146 Precursor precursor;
147 precursor.fromCbor(reader);
148 precursorList.emplace_back(precursor);
149 }
150 reader
151 .leaveContainer(); // array
152
153 // reader.next();
154 }
155 else
156 {
157 reader.next();
158 }
159 }
160 else
161 {
162 reader.next();
163 }
164 }
165 else
166 {
167 reader.next();
168 }
169 }
170 reader.leaveContainer();
171 }
172 else if(txt_value == "binaryDataArray")
173 {
174 if(want_binary_data)
175 {
176
177 reader.enterContainer(); // start array
178
179 while(reader.hasNext())
180 {
181
182 BinaryDataArray data_array;
183 data_array.fromCbor(reader);
184 binaryDataArrayList.emplace_back(data_array);
185 }
186
187 reader.leaveContainer(); // end array
188 }
189 else
190 {
191 reader.next();
192 }
193 }
194 else
195 {
196 reader.next();
197 }
198 }
199 // reader.next();
200 }
201
202 reader.leaveContainer();
203 }
204 else
205 {
206 throw pappso::PappsoException(QObject::tr("ERROR in Spectrum::fromCbor : no container"));
207 }
208
209
210 if(reader.lastError() != QCborError::NoError)
211 {
213 QObject::tr("ERROR in Spectrum::fromCbor :\n%1").arg(reader.lastError()));
214 }
215}
216
217
218std::map<QString, pappso::cbor::mzcbor::CvParam>
220{
221 std::map<QString, CvParam> cv_params;
222 reader.enterContainer();
223 QString txt_value;
224
225 while(reader.hasNext() && (!reader.isInvalid()))
226 {
227 if(reader.isString())
228 {
229 if(reader.decodeString(txt_value))
230 {
231 qDebug() << txt_value;
232 if(txt_value == "cvParam")
233 {
234 cv_params = CvParam::getCvParamsMapFromCbor(reader);
235 }
236 else
237 {
238 reader.next();
239 }
240 }
241 }
242 else
243 {
244 reader.next();
245 }
246 }
247
248 reader.leaveContainer();
249 return cv_params;
250}
251
252
253qint64
255{
256 // <cvParam cvRef="MS" accession="MS:1000285" value="17377980" name="total ion current" />
257
258 auto it = cvParamMap.find("MS:1000285");
259 if(it == cvParamMap.end())
260 {
261 throw pappso::ExceptionNotFound(QObject::tr("total ion count not found in cvParam map"));
262 }
263 qDebug() << it->first << " " << it->second.valueInt;
264 return it->second.valueInt;
265}
266
267
268uint
270{
271 // <cvParam cvRef="MS" accession="MS:1000511" value="1" name="ms level" />
272
273 auto it = cvParamMap.find("MS:1000511");
274 if(it == cvParamMap.end())
275 {
276 throw pappso::ExceptionNotFound(QObject::tr("MS level not found in cvParam map"));
277 }
278 qDebug() << it->first << " " << it->second.valueInt;
279 return it->second.valueInt;
280}
281
282double
284{
285 // <cvParam cvRef="MS" accession="MS:1000016" value="0.0048758833" name="scan start
286 // time" unitAccession="UO:0000031" unitName="minute" unitCvRef="UO" />
287
288 auto it = scanCvParamMap.find("MS:1000016");
289 if(it == scanCvParamMap.end())
290 {
291 throw pappso::ExceptionNotFound(QObject::tr("retention time not found in cvParam map"));
292 }
293 double rt_seconds = it->second.valueDouble;
294 if(it->second.unitAccession == "UO:0000031")
295 {
296 // // minutes
297 rt_seconds = rt_seconds * 60;
298 }
299
300 // <cvParam cvRef="MS" accession="MS:1000016" value="0" name="scan start time"
301 // unitAccession="UO:0000031" unitName="minute" unitCvRef="UO" />
302 return rt_seconds;
303}
304
305void
307{
308 std::vector<pappso_double> xVector;
309 std::vector<pappso_double> yVector;
310 for(auto &binary_array : binaryDataArrayList)
311 {
312 if(binary_array.isIntensity())
313 {
314 binary_array.decodeVector(defaultArrayLength, yVector);
315 }
316 else if(binary_array.isMz())
317 {
318 binary_array.decodeVector(defaultArrayLength, xVector);
319 }
320 else
321 {
322
323 throw pappso::ExceptionNotFound(QObject::tr(
324 "error in pappso::cbor::mzcbor::Spectrum::decodeTrace: binary vector type not found"));
325 }
326 }
327
328 trace.initialize(xVector, yVector);
329}
A simple container of DataPoint instances.
Definition trace.h:148
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition trace.cpp:564
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...
unsigned int uint
Definition types.h:67
PSI spectrum object for mzML/mzCBOR.
void fromCbor(CborStreamReader &reader)
static std::map< QString, pappso::cbor::mzcbor::CvParam > getCvParamsMapFromCbor(CborStreamReader &reader)
Definition cvparam.cpp:182
void fromCbor(CborStreamReader &reader)
std::map< QString, pappso::cbor::mzcbor::CvParam > cvParamMap
Definition spectrum.h:62
void decodeTrace(pappso::Trace &trace)
Definition spectrum.cpp:306
static std::map< QString, pappso::cbor::mzcbor::CvParam > readScanCvParams(CborStreamReader &reader)
Definition spectrum.cpp:219
std::vector< Precursor > precursorList
Definition spectrum.h:67
void fromCbor(CborStreamReader &reader, bool want_binary_data)
Definition spectrum.cpp:34
std::map< QString, pappso::cbor::mzcbor::CvParam > scanListCvParamMap
Definition spectrum.h:63
std::map< QString, pappso::cbor::mzcbor::CvParam > scanCvParamMap
Definition spectrum.h:64
std::vector< BinaryDataArray > binaryDataArrayList
Definition spectrum.h:68
std::map< QString, pappso::cbor::mzcbor::CvParam > precursorListCvParamMap
Definition spectrum.h:65