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