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 <catch2/internal/catch_polyfills.hpp>
31#include <cmath>
32#include <qjsonobject.h>
33#include <qlogging.h>
34
35
36void
38{
39 qDebug();
40 if(!reader.isMap())
41 {
42 throw pappso::PappsoException(QObject::tr("this is not a cvParam : no map"));
43 }
44 reader.enterContainer();
45 QString attribute_cvparam;
46 while(reader.hasNext())
47 {
48 reader.decodeString(attribute_cvparam);
49 qDebug() << attribute_cvparam;
50 if(attribute_cvparam == "cvRef")
51 {
52 reader.decodeString(cvRef);
53 }
54 else if(attribute_cvparam == "accession")
55 {
56 reader.decodeString(accession);
57 }
58 else if(attribute_cvparam == "name")
59 {
60 reader.decodeString(name);
61 }
62 else if(attribute_cvparam == "value")
63 {
64 cborType = reader.type();
65 if(reader.isDouble())
66 {
67 valueDouble = reader.toDouble();
68 reader.next();
69 }
70 else if(reader.isUnsignedInteger())
71 {
72 valueInt = reader.toUnsignedInteger();
73 reader.next();
74 }
75 else if(reader.isInteger())
76 {
77 valueInt = reader.toInteger();
78 reader.next();
79 }
80 else if(reader.type() == QCborStreamReader::Type::String)
81 {
82 if(reader.decodeString(attribute_cvparam))
83 {
84 valueStr = attribute_cvparam;
85 }
86 else
87 {
89 QObject::tr("cvParam value string failed for accession %1").arg(accession));
90 }
91 }
92 else
93 {
95 QObject::tr("cvParam value type not known for accession %1").arg(accession));
96 }
97 }
98 else if(attribute_cvparam == "unitAccession")
99 {
101 }
102
103 else if(attribute_cvparam == "unitCvRef")
104 {
105 reader.decodeString(unitCvRef);
106 }
107 else if(attribute_cvparam == "unitName")
108 {
109 reader.decodeString(unitName);
110 }
111 else
112 {
113 reader.next();
114 }
115 }
116
117 reader.leaveContainer();
118 qDebug();
119}
120
121void
123{
124 // qDebug();
125 for(auto &the_attribute : reader.attributes())
126 {
127 if(the_attribute.name() == "cvRef")
128 {
129 cvRef = the_attribute.value().toString();
130 }
131 else if(the_attribute.name() == "accession")
132 {
133 accession = the_attribute.value().toString();
134 }
135 else if(the_attribute.name() == "name")
136 {
137 name = the_attribute.value().toString();
138 }
139 else if(the_attribute.name() == "value")
140 {
141 valueStr = the_attribute.value().toString();
142 }
143 else if(the_attribute.name() == "unitCvRef")
144 {
145 unitCvRef = the_attribute.value().toString();
146 }
147 else if(the_attribute.name() == "unitAccession")
148 {
149 unitAccession = the_attribute.value().toString();
150 }
151 else if(the_attribute.name() == "unitName")
152 {
153 unitName = the_attribute.value().toString();
154 }
155 }
156
157 reader.readNextStartElement();
158 // qDebug();
159}
160
161void
163{
164
165 writer.startMap();
166 writer.append("cvRef");
167 writer.append(cvRef);
168
169 writer.append("accession");
170 writer.append(accession);
171 writer.append("name");
172 writer.append(name);
173
174 writer.append("value");
175 xmlValueToCbor(writer, valueStr);
176
177 if(!unitAccession.isEmpty())
178 {
179 writer.append("unitCvRef");
180 writer.append(unitCvRef);
181 writer.append("unitAccession");
182 writer.append(unitAccession);
183 writer.append("unitName");
184 writer.append(unitName);
185 }
186 writer.endMap();
187}
188
189
190QJsonObject
192{
193 QJsonObject cv_param;
194 cv_param.insert("cvRef", cvRef);
195 cv_param.insert("accession", accession);
196 cv_param.insert("name", name);
197
198
199 if(std::isnan(valueDouble))
200 {
201 if(valueInt == std::numeric_limits<qint64>::max())
202 {
203 cv_param.insert("value", valueStr);
204 }
205 else
206 {
207 cv_param.insert("value", valueInt);
208 }
209 }
210 else
211 {
212 cv_param.insert("value", valueDouble);
213 }
214
215 if(!unitAccession.isEmpty())
216 {
217 cv_param.insert("unitCvRef", unitCvRef);
218 cv_param.insert("unitAccession", unitAccession);
219 cv_param.insert("unitName", unitName);
220 }
221 return cv_param;
222}
223
224void
226{
227 valueInt = 0;
228 valueDouble = 0;
229 valueStr = value_str;
230}
231
232void
234{
235 // <cvParam cvRef="MS" accession="MS:1000514" value="" name="m/z array"
236 writer.writeStartElement("cvParam");
237
238 writer.writeAttribute("cvRef", cvRef);
239 writer.writeAttribute("accession", accession);
240
241
242 if(cborType == QCborStreamReader::Type::Double)
243 {
244 valueStr = QString::number(valueDouble, 'g', 15);
245 }
246 else if(cborType == QCborStreamReader::Type::UnsignedInteger)
247 {
248 valueStr = QString("%1").arg(valueInt);
249 }
250 else if(cborType == QCborStreamReader::Type::NegativeInteger)
251 {
252 valueStr = QString("%1").arg(valueInt);
253 }
254 else if(cborType == QCborStreamReader::Type::String)
255 {
256 }
257 writer.writeAttribute("value", valueStr);
258
259 writer.writeAttribute("name", name);
260 // unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" />
261 if(!unitAccession.isEmpty())
262 {
263 writer.writeAttribute("unitAccession", unitAccession);
264 writer.writeAttribute("unitName", unitName);
265 writer.writeAttribute("unitCvRef", unitCvRef);
266 }
267
268 writer.writeEndElement(); // cvParam
269}
270
271std::uint8_t
273{
274 bool ok(false);
275 int i = valueStr.toInt(&ok);
276 if(ok)
277 {
278 return (std::uint8_t)i;
279 }
280 else
281 {
283 QObject::tr("cvParam value string is not an integer %1 %2").arg(accession).arg(valueStr));
284 }
285}
286
287qint64
289{
290 bool ok(false);
291 qint64 i = valueStr.toLongLong(&ok);
292 if(ok)
293 {
294 return i;
295 }
296 else
297 {
299 QObject::tr("cvParam value string is not an integer %1 %2").arg(accession).arg(valueStr));
300 }
301}
302
303double
305{
306 bool ok(false);
307 double i = valueStr.toDouble(&ok);
308 if(ok)
309 {
310 return i;
311 }
312 else
313 {
315 QObject::tr("cvParam value string is not a double %1 %2").arg(accession).arg(valueStr));
316 }
317}
318
319
320void
322 const QStringView &value_str)
323{
324 bool ok(false);
325 double d = value_str.toDouble(&ok);
326 if(ok)
327 {
328 if(value_str.contains('.'))
329 {
330 cborType = QCborStreamReader::Type::Double;
331 writer.append(d);
332 }
333 else
334 {
335 qint64 bigint = value_str.toLongLong(&ok);
336 if(ok)
337 {
338 cborType = QCborStreamReader::Type::NegativeInteger;
339 writer.append(bigint);
340 }
341 }
342 }
343 else
344 {
345 cborType = QCborStreamReader::Type::String;
346 writer.append(value_str);
347 }
348}
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:37
QJsonObject toJsonObject() const
write the structure to a JSON object
Definition cvparam.cpp:191
QCborStreamReader::Type cborType
Definition cvparam.h:83
void setValue(const QString &value_str)
Definition cvparam.cpp:225
void xmlValueToCbor(CborStreamWriter &writer, const QStringView &value_str)
Definition cvparam.cpp:321
void fromMzml(QXmlStreamReader &reader)
reads the XML attributes of the cvParam element Inside the cvParam XML element, reads the attributes ...
Definition cvparam.cpp:122
void toCbor(CborStreamWriter &writer)
Definition cvparam.cpp:162
qint64 getExpectedQint64() const
Definition cvparam.cpp:288
void toMzml(QXmlStreamWriter &writer)
Definition cvparam.cpp:233
std::uint8_t getExpectedUint8() const
Definition cvparam.cpp:272
double getExpectedDouble() const
Definition cvparam.cpp:304