libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
qcborspectrum.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/core/processing/cbor/mzcbor/copyreplace/qcborspectrum.cpp
3 * \date 2/10/2026
4 * \author Olivier Langella
5 * \brief Low level CBOR map to store an mzCBOR Spectrum object
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2026 Olivier Langella <Olivier.Langella@cnrs.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#include "qcborspectrum.h"
29#include <cstddef>
30#include <QCborMap>
31#include <QCborValue>
32#include <QCborArray>
33#include <QDebug>
34#include <qcborarray.h>
35#include <qcbormap.h>
36#include <qcborvalue.h>
37#include <qlogging.h>
43
44namespace pappso
45{
46namespace cbor
47{
48namespace mzcbor
49{
51{
52}
53QcborSpectrum::QcborSpectrum(const QcborSpectrum &other) : QCborMap(other)
54{
55}
56std::vector<QcborSpectrumPrecursor>
58{
59 std::vector<QcborSpectrumPrecursor> precursor_list;
60 qDebug();
61 auto itout = constFind(QCborValue("precursorList"));
62 if(itout != end())
63 {
64 auto it = itout.value().toMap().constFind(QCborValue("precursor"));
65 if(it != end())
66 {
67 for(auto it_cv = it->toArray().cbegin(); it_cv != it->toArray().cend(); it_cv++)
68 {
69 qDebug();
70 QcborSpectrumPrecursor precursor(it_cv->toMap());
71 precursor_list.push_back(precursor);
72 }
73 return precursor_list;
74 }
75 else
76 {
77 throw pappso::PappsoException(QObject::tr("ERROR: no precursor array in QcborSpectrum"));
78 }
79 }
80 else
81 {
82 throw pappso::PappsoException(QObject::tr("ERROR: no precursorList in QcborSpectrum"));
83 }
84}
85
86double
88{
89 auto itout = constFind(QCborValue("scanList"));
90 if(itout != end())
91 {
92 auto it = itout.value().toMap().constFind(QCborValue("scan"));
93 if(it != end())
94 {
95 for(auto it_cv = it->toArray().cbegin(); it_cv != it->toArray().cend(); it_cv++)
96 {
97 qDebug();
98 Scan scan;
99 scan.fromQCborMap(it_cv->toMap());
100 return scan.getRtInSeconds();
101 }
102 }
103 else
104 {
105 throw pappso::PappsoException(QObject::tr("ERROR: no scan array in QcborSpectrum"));
106 }
107 }
108 else
109 {
110 throw pappso::PappsoException(QObject::tr("ERROR: no scanList in QcborSpectrum"));
111 }
112 return 0.0;
113}
114void
115QcborSpectrum::setPrecursorList(std::vector<QcborSpectrumPrecursor> &precursor_list)
116{
117 QCborMap cbor_precursor_list;
118 // auto itout = find(QCborValue("precursorList"));
119 // qDebug();
120 // if(itout != end())
121 // {
122 qDebug();
123 cbor_precursor_list.insert(QCborValue("count"), (qint64)precursor_list.size());
124 qDebug();
125
126 QCborArray cbor_arr;
127 for(auto &it_prec : precursor_list)
128 {
129 cbor_arr.push_back(it_prec);
130 }
131 cbor_precursor_list.insert(QCborValue("precursor"), cbor_arr);
132
133 insert(QCborValue("precursorList"), cbor_precursor_list);
134 // auto it2 = itout.value().toMap().find(QCborValue("precursor"));
135 // if(it2 != end())
136 // {
137 // QCborArray cbor_arr;
138 // for(auto &it_prec : precursor_list)
139 // {
140 // cbor_arr.push_back(it_prec);
141 // }
142 // qDebug() << cbor_arr.size();
143 // // it2. = QCborValue(cbor_arr);
144 // itout.value().toMap().insert(QCborValue("precursor"), cbor_arr);
145 // }
146 // else
147 // {
148 // throw pappso::PappsoException(
149 // QObject::tr("ERROR: no precursor in precursorList QcborSpectrum"));
150 // }
151 // qDebug();
152 // }
153 // else
154 // {
155 // throw pappso::PappsoException(QObject::tr("ERROR: no precursorList in QcborSpectrum"));
156 // }
157}
158QString
160{
161 // <spectrum
162 // id="controllerType=0 controllerNumber=1 scan=1"
163 // index="0"
164 // defaultArrayLength="1552"
165 // >
166 QCborValue id("id");
167 auto it = constFind(id);
168 if(it != end())
169 {
170 qDebug() << *it;
171 return it.value().toString();
172 }
173 else
174 {
175 throw pappso::PappsoException(QObject::tr("ERROR: no id in QcborSpectrum"));
176 }
177}
178std::size_t
180{
181 QCborValue id("defaultArrayLength");
182 auto it = constFind(id);
183 if(it != end())
184 {
185 qDebug() << *it;
186 return it.value().toInteger();
187 }
188 else
189 {
191 QObject::tr("ERROR: no defaultArrayLength in QcborSpectrum %1").arg(getId()));
192 }
193}
194void
195QcborSpectrum::setId(const QString &id)
196{
197 // <spectrum
198 // id="controllerType=0 controllerNumber=1 scan=1"
199 // index="0"
200 // defaultArrayLength="1552"
201 // >
202 QCborValue native_id("id");
203 auto it = find(native_id);
204 if(it != end())
205 {
206 qDebug() << *it;
207 it.value() = QCborValue(id);
208 }
209 else
210 {
211 throw pappso::PappsoException(QObject::tr("ERROR: no id in QcborSpectrum"));
212 }
213}
214std::size_t
216{
217 QCborValue id("index");
218 auto it = constFind(id);
219 if(it != end())
220 {
221 qDebug() << *it;
222 return (std::size_t)it.value().toInteger();
223 }
224 else
225 {
226 throw pappso::PappsoException(QObject::tr("ERROR: no index in QcborSpectrum"));
227 }
228}
229void
230QcborSpectrum::setIndex(std::size_t index)
231{
232 // <spectrum
233 // id="controllerType=0 controllerNumber=1 scan=1"
234 // index="0"
235 // defaultArrayLength="1552"
236 // >
237 QCborValue id("index");
238 auto it = find(id);
239 if(it != end())
240 {
241 qDebug() << *it;
242 it.value() = QCborValue((qint64)index);
243 }
244 else
245 {
246 throw pappso::PappsoException(QObject::tr("ERROR: no index in QcborSpectrum"));
247 }
248}
249
250int
252{
253 //"cvParam":[{"cvRef":"MS","accession":"MS:1000511","name":"ms level","value":1}
254
255 QCborValue cv_param("cvParam");
256 auto it = constFind(cv_param);
257 if(it != end())
258 {
259 for(auto it_cv = it->toArray().cbegin(); it_cv != it->toArray().cend(); it_cv++)
260 {
261 auto it_accession = it_cv->toMap().constFind(QCborValue("accession"));
262 if(it_accession->toString() == "MS:1000511")
263 {
264 auto it_value = it_cv->toMap().find(QCborValue("value"));
265 return (it_value.value().toInteger());
266 }
267 }
268
270 QObject::tr("ERROR: MS level not found in QcborSpectrum %1").arg(getId()));
271 }
272 else
273 {
275 QObject::tr("ERROR: no cvparam in QcborSpectrum %1").arg(getId()));
276 }
277}
278
279void
281{
282 qDebug();
283 std::vector<pappso_double> xVector;
284 std::vector<pappso_double> yVector;
285 std::size_t length = getDefaultArrayLength();
286
287
288 auto it = constFind(QCborValue("binaryDataArray"));
289 if(it != end())
290 {
291 for(auto it_binarr = it->toArray().cbegin(); it_binarr != it->toArray().cend(); it_binarr++)
292 {
293 BinaryDataArray binary_array;
294 binary_array.fromQCborMap(it_binarr->toMap());
295 qDebug();
296 if(binary_array.isIntensity())
297 {
298 qDebug();
299 binary_array.decodeVector(length, yVector);
300 }
301 else if(binary_array.isMz())
302 {
303 qDebug();
304 binary_array.decodeVector(length, xVector);
305 }
306 else
307 {
308
310 QObject::tr("error in QcborSpectrum::decodeTrace binary vector "
311 "type not found"));
312 }
313 qDebug();
314 }
315 }
316 else
317 {
319 QObject::tr("ERROR: no binaryDataArray in QcborSpectrum %1").arg(getId()));
320 }
321 qDebug();
322 trace.initialize(xVector, yVector);
323 qDebug();
324}
325} // namespace mzcbor
326} // namespace cbor
327} // namespace pappso
PSI BinaryDataArray object for mzML/mzCBOR.
A simple container of DataPoint instances.
Definition trace.h:152
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition trace.cpp:597
void decodeTrace(pappso::Trace &trace) const
std::vector< QcborSpectrumPrecursor > getPrecursorList() const
void setPrecursorList(std::vector< QcborSpectrumPrecursor > &precursor_list)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
Low level CBOR map to store an mzCBOR Spectrum object.
Low level CBOR map to store an mzCBOR Spectrum object.
PSI scan object for mzML/mzCBOR.
void fromQCborMap(const QCborMap &cbor_map)
void decodeVector(std::size_t estimated_length, std::vector< double > &double_list) const
void fromQCborMap(const QCborMap &reader)
Definition scan.cpp:34
double getRtInSeconds() const
Definition scan.cpp:49