libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
cvparam.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/core/processing/cbor/mzcbor/cvparam.cpp
3 * \date 23/11/2025
4 * \author Olivier Langella
5 * \brief PSI cvParam 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#include "cvparam.h"
30#include <cmath>
31#include <QJsonObject>
32#include <QDebug>
33
34
35void
37{
38 QString txt_value;
39 for(auto it_binarr = cbor_map.cbegin(); it_binarr != cbor_map.cend(); it_binarr++)
40 {
41 txt_value = it_binarr.key().toString();
42 qDebug() << txt_value;
43
44 if(txt_value == "cvRef")
45 {
46 cvRef = it_binarr.value().toString();
47 }
48 else if(txt_value == "accession")
49 {
50 accession = it_binarr.value().toString();
51 }
52 else if(txt_value == "name")
53 {
54 name = it_binarr.value().toString();
55 }
56 else if(txt_value == "value")
57 {
58 if(it_binarr.value().isDouble())
59 {
60 valueDouble = it_binarr.value().toDouble();
61 }
62 else if(it_binarr.value().isInteger())
63 {
64 valueInt = it_binarr.value().toInteger();
65 }
66 else if(it_binarr.value().isString())
67 {
68 valueStr = it_binarr.value().toString();
69 }
70 else
71 {
73 QObject::tr("cvParam value type not known for accession %1").arg(accession));
74 }
75 }
76 else if(txt_value == "unitAccession")
77 {
78 unitAccession = it_binarr.value().toString();
79 }
80
81 else if(txt_value == "unitCvRef")
82 {
83 unitCvRef = it_binarr.value().toString();
84 }
85 else if(txt_value == "unitName")
86 {
87 unitName = it_binarr.value().toString();
88 }
89 }
90}
91void
93{
94 qDebug();
95 if(!reader.isMap())
96 {
97 throw pappso::PappsoException(QObject::tr("this is not a cvParam : no map"));
98 }
99 reader.enterContainer();
100 QString attribute_cvparam;
101 while(reader.hasNext())
102 {
103 reader.decodeString(attribute_cvparam);
104 qDebug() << attribute_cvparam;
105 if(attribute_cvparam == "cvRef")
106 {
107 reader.decodeString(cvRef);
108 }
109 else if(attribute_cvparam == "accession")
110 {
111 reader.decodeString(accession);
112 }
113 else if(attribute_cvparam == "name")
114 {
115 reader.decodeString(name);
116 }
117 else if(attribute_cvparam == "value")
118 {
119 cborType = reader.type();
120 if(reader.isDouble())
121 {
122 valueDouble = reader.toDouble();
123 reader.next();
124 }
125 else if(reader.isUnsignedInteger())
126 {
127 valueInt = reader.toUnsignedInteger();
128 reader.next();
129 }
130 else if(reader.isInteger())
131 {
132 valueInt = reader.toInteger();
133 reader.next();
134 }
135 else if(reader.type() == QCborStreamReader::Type::String)
136 {
137 if(reader.decodeString(attribute_cvparam))
138 {
139 valueStr = attribute_cvparam;
140 }
141 else
142 {
144 QObject::tr("cvParam value string failed for accession %1").arg(accession));
145 }
146 }
147 else
148 {
150 QObject::tr("cvParam value type not known for accession %1").arg(accession));
151 }
152 }
153 else if(attribute_cvparam == "unitAccession")
154 {
156 }
157
158 else if(attribute_cvparam == "unitCvRef")
159 {
160 reader.decodeString(unitCvRef);
161 }
162 else if(attribute_cvparam == "unitName")
163 {
164 reader.decodeString(unitName);
165 }
166 else
167 {
168 reader.next();
169 }
170 }
171
172 reader.leaveContainer();
173 qDebug();
174}
175
176void
178{
179 // qDebug();
180 for(auto &the_attribute : reader.attributes())
181 {
182 if(the_attribute.name() == "cvRef")
183 {
184 cvRef = the_attribute.value().toString();
185 }
186 else if(the_attribute.name() == "accession")
187 {
188 accession = the_attribute.value().toString();
189 }
190 else if(the_attribute.name() == "name")
191 {
192 name = the_attribute.value().toString();
193 }
194 else if(the_attribute.name() == "value")
195 {
196 valueStr = the_attribute.value().toString();
197 }
198 else if(the_attribute.name() == "unitCvRef")
199 {
200 unitCvRef = the_attribute.value().toString();
201 }
202 else if(the_attribute.name() == "unitAccession")
203 {
204 unitAccession = the_attribute.value().toString();
205 }
206 else if(the_attribute.name() == "unitName")
207 {
208 unitName = the_attribute.value().toString();
209 }
210 }
211
212 reader.readNextStartElement();
213 // qDebug();
214}
215
216void
218{
219
220 writer.startMap();
221 writer.append("cvRef");
222 writer.append(cvRef);
223
224 writer.append("accession");
225 writer.append(accession);
226 writer.append("name");
227 writer.append(name);
228
229 writer.append("value");
230 xmlValueToCbor(writer, valueStr);
231
232 if(!unitAccession.isEmpty())
233 {
234 writer.append("unitCvRef");
235 writer.append(unitCvRef);
236 writer.append("unitAccession");
237 writer.append(unitAccession);
238 writer.append("unitName");
239 writer.append(unitName);
240 }
241 writer.endMap();
242}
243
244
245QJsonObject
247{
248 QJsonObject cv_param;
249 cv_param.insert("cvRef", cvRef);
250 cv_param.insert("accession", accession);
251 cv_param.insert("name", name);
252
253
254 if(std::isnan(valueDouble))
255 {
256 if(valueInt == std::numeric_limits<qint64>::max())
257 {
258 cv_param.insert("value", valueStr);
259 }
260 else
261 {
262 cv_param.insert("value", valueInt);
263 }
264 }
265 else
266 {
267 cv_param.insert("value", valueDouble);
268 }
269
270 if(!unitAccession.isEmpty())
271 {
272 cv_param.insert("unitCvRef", unitCvRef);
273 cv_param.insert("unitAccession", unitAccession);
274 cv_param.insert("unitName", unitName);
275 }
276 return cv_param;
277}
278
279void
281{
282 valueInt = 0;
283 valueDouble = 0;
284 valueStr = value_str;
285}
286
287void
289{
290 // <cvParam cvRef="MS" accession="MS:1000514" value="" name="m/z array"
291 writer.writeStartElement("cvParam");
292
293 writer.writeAttribute("cvRef", cvRef);
294 writer.writeAttribute("accession", accession);
295
296
297 if(cborType == QCborStreamReader::Type::Double)
298 {
299 valueStr = QString::number(valueDouble, 'g', 15);
300 }
301 else if(cborType == QCborStreamReader::Type::UnsignedInteger)
302 {
303 valueStr = QString("%1").arg(valueInt);
304 }
305 else if(cborType == QCborStreamReader::Type::NegativeInteger)
306 {
307 valueStr = QString("%1").arg(valueInt);
308 }
309 else if(cborType == QCborStreamReader::Type::String)
310 {
311 }
312 writer.writeAttribute("value", valueStr);
313
314 writer.writeAttribute("name", name);
315 // unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" />
316 if(!unitAccession.isEmpty())
317 {
318 writer.writeAttribute("unitAccession", unitAccession);
319 writer.writeAttribute("unitName", unitName);
320 writer.writeAttribute("unitCvRef", unitCvRef);
321 }
322
323 writer.writeEndElement(); // cvParam
324}
325
326std::uint8_t
328{
329 bool ok(false);
330 int i = valueStr.toInt(&ok);
331 if(ok)
332 {
333 return (std::uint8_t)i;
334 }
335 else
336 {
338 QObject::tr("cvParam value string is not an integer %1 %2").arg(accession).arg(valueStr));
339 }
340}
341
342qint64
344{
345 bool ok(false);
346 qint64 i = valueStr.toLongLong(&ok);
347 if(ok)
348 {
349 return i;
350 }
351 else
352 {
354 QObject::tr("cvParam value string is not an integer %1 %2").arg(accession).arg(valueStr));
355 }
356}
357
358double
360{
361 bool ok(false);
362 double i = valueStr.toDouble(&ok);
363 if(ok)
364 {
365 return i;
366 }
367 else
368 {
370 QObject::tr("cvParam value string is not a double %1 %2").arg(accession).arg(valueStr));
371 }
372}
373
374
375void
377 const QStringView &value_str)
378{
379 bool ok(false);
380 double d = value_str.toDouble(&ok);
381 if(ok)
382 {
383 if(value_str.contains('.'))
384 {
385 cborType = QCborStreamReader::Type::Double;
386 writer.append(d);
387 }
388 else
389 {
390 qint64 bigint = value_str.toLongLong(&ok);
391 if(ok)
392 {
393 cborType = QCborStreamReader::Type::NegativeInteger;
394 writer.append(bigint);
395 }
396 }
397 }
398 else
399 {
400 cborType = QCborStreamReader::Type::String;
401 writer.append(value_str);
402 }
403}
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...
overrides QCborStreamWriter base class to provide convenient functions
PSI cvParam object for mzML/mzCBOR.
void fromCbor(CborStreamReader &reader)
Definition cvparam.cpp:92
QJsonObject toJsonObject() const
write the structure to a JSON object
Definition cvparam.cpp:246
QCborStreamReader::Type cborType
Definition cvparam.h:84
void setValue(const QString &value_str)
Definition cvparam.cpp:280
void xmlValueToCbor(CborStreamWriter &writer, const QStringView &value_str)
Definition cvparam.cpp:376
void fromQCborMap(const QCborMap &reader)
Definition cvparam.cpp:36
void fromMzml(QXmlStreamReader &reader)
reads the XML attributes of the cvParam element Inside the cvParam XML element, reads the attributes ...
Definition cvparam.cpp:177
void toCbor(CborStreamWriter &writer)
Definition cvparam.cpp:217
qint64 getExpectedQint64() const
Definition cvparam.cpp:343
void toMzml(QXmlStreamWriter &writer)
Definition cvparam.cpp:288
std::uint8_t getExpectedUint8() const
Definition cvparam.cpp:327
double getExpectedDouble() const
Definition cvparam.cpp:359