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