libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
spectrum.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/core/processing/cbor/mzcbor/spectrum.h
3 * \date 24/11/2025
4 * \author Olivier Langella
5 * \brief PSI spectrum 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
29#include "spectrum.h"
31
32
33void
35{
36 QString txt_value;
37 cborBinaryDataArray.clear();
38 precursorList.clear();
40 index = 0;
41 cvParamMap.clear();
42 scanListCvParamMap.clear();
43 scanCvParamMap.clear();
44
45 if(reader.enterContainer())
46 {
47 while(reader.hasNext() && (!reader.isInvalid()))
48 {
49 if(reader.isString())
50 {
51 reader.decodeString(txt_value);
52 qDebug() << txt_value;
53 if(txt_value == "id")
54 {
55 reader.decodeString(txt_value);
56 qDebug() << txt_value;
57 id = txt_value;
58 }
59 else if(txt_value == "index")
60 {
61 index = reader.toUnsignedInteger();
62 reader.next();
63 }
64 else if(txt_value == "defaultArrayLength")
65 {
66 defaultArrayLength = reader.toUnsignedInteger();
67 reader.next();
68 }
69 else if(txt_value == "cvParam")
70 {
72 }
73 else if(txt_value == "scanList")
74 {
75 // mpa_cborReader->next();
76 reader.enterContainer();
77
78 while(reader.hasNext() && (!reader.isInvalid()))
79 {
80 if(reader.isString())
81 {
82 if(reader.decodeString(txt_value))
83 {
84 qDebug() << txt_value;
85 if(txt_value == "cvParam")
86 {
88 }
89 else if(txt_value == "scan")
90 {
91 // scan array
92 reader.enterContainer();
93 while(reader.hasNext())
94 {
95 auto new_cv = readScanCvParams(reader);
96 scanCvParamMap.insert(new_cv.begin(), new_cv.end());
97 }
98 reader.leaveContainer();
99 }
100 }
101 else
102 {
103 reader.next();
104 }
105 }
106 else
107 {
108 reader.next();
109 }
110 }
111 reader.leaveContainer();
112 }
113 else if(txt_value == "precursorList")
114 {
115
116 reader.enterContainer();
117
118 while(reader.hasNext() && (!reader.isInvalid()))
119 {
120 if(reader.isString())
121 {
122 if(reader.decodeString(txt_value))
123 {
124 qDebug() << txt_value;
125
126 if(txt_value == "cvParam")
127 {
129 }
130 else if(txt_value == "precursor")
131 {
132
133 reader
134 .enterContainer(); // array
135
136 while(reader.hasNext() && (!reader.isInvalid()))
137 {
138 // scan array
139 Precursor precursor;
140 precursor.fromCbor(reader);
141 precursorList.emplace_back(precursor);
142 }
143 reader
144 .leaveContainer(); // array
145
146 // reader.next();
147 }
148 else
149 {
150 reader.next();
151 }
152 }
153 else
154 {
155 reader.next();
156 }
157 }
158 else
159 {
160 reader.next();
161 }
162 }
163 reader.leaveContainer();
164 }
165 else if(txt_value == "binaryDataArray")
166 {
167 if(want_binary_data)
168 {
169
170 reader.enterContainer(); // start array
171
172 while(reader.hasNext())
173 {
174
175 BinaryDataArray data_array;
176 data_array.fromCbor(reader);
177 binaryDataArrayList.emplace_back(data_array);
178 }
179
180 reader.leaveContainer(); // end array
181 }
182 else
183 {
184 reader.next();
185 }
186 }
187 else
188 {
189 reader.next();
190 }
191 }
192 // reader.next();
193 }
194
195 reader.leaveContainer();
196 }
197}
198
199
200std::map<QString, pappso::cbor::mzcbor::CvParam>
202{
203 std::map<QString, CvParam> cv_params;
204 reader.enterContainer();
205 QString txt_value;
206
207 while(reader.hasNext() && (!reader.isInvalid()))
208 {
209 if(reader.isString())
210 {
211 if(reader.decodeString(txt_value))
212 {
213 qDebug() << txt_value;
214 if(txt_value == "cvParam")
215 {
216 cv_params = CvParam::getCvParamsMapFromCbor(reader);
217 }
218 else
219 {
220 reader.next();
221 }
222 }
223 }
224 else
225 {
226 reader.next();
227 }
228 }
229
230 reader.leaveContainer();
231 return cv_params;
232}
233
234
235uint
237{
238 // <cvParam cvRef="MS" accession="MS:1000511" value="1" name="ms level" />
239
240 auto it = cvParamMap.find("MS:1000511");
241 if(it == cvParamMap.end())
242 {
243 throw pappso::ExceptionNotFound(QObject::tr("MS level not found in cvParam map"));
244 }
245 qDebug() << it->first << " " << it->second.valueInt;
246 return it->second.valueInt;
247}
248
249double
251{
252 // <cvParam cvRef="MS" accession="MS:1000016" value="0.0048758833" name="scan start
253 // time" unitAccession="UO:0000031" unitName="minute" unitCvRef="UO" />
254
255 auto it = scanCvParamMap.find("MS:1000016");
256 if(it == scanCvParamMap.end())
257 {
258 throw pappso::ExceptionNotFound(QObject::tr("retention time not found in cvParam map"));
259 }
260 double rt_seconds = it->second.valueDouble;
261 if(it->second.unitAccession == "UO:0000031")
262 {
263 // // minutes
264 rt_seconds = rt_seconds * 60;
265 }
266 return rt_seconds;
267}
268
269void
271{
272 std::vector<pappso_double> xVector;
273 std::vector<pappso_double> yVector;
274 for(auto &binary_array : binaryDataArrayList)
275 {
276 if(binary_array.isIntensity())
277 {
278 binary_array.decodeVector(defaultArrayLength, yVector);
279 }
280 else if(binary_array.isMz())
281 {
282 binary_array.decodeVector(defaultArrayLength, xVector);
283 }
284 else
285 {
286
287 throw pappso::ExceptionNotFound(QObject::tr(
288 "error in pappso::cbor::mzcbor::Spectrum::decodeTrace: binary vector type not found"));
289 }
290 }
291
292 trace.initialize(xVector, yVector);
293}
A simple container of DataPoint instances.
Definition trace.h:148
size_t initialize(const std::vector< pappso_double > &xVector, const std::vector< pappso_double > &yVector)
Definition trace.cpp:564
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...
unsigned int uint
Definition types.h:68
PSI spectrum object for mzML/mzCBOR.
void fromCbor(CborStreamReader &reader)
static std::map< QString, pappso::cbor::mzcbor::CvParam > getCvParamsMapFromCbor(CborStreamReader &reader)
Definition cvparam.cpp:182
void fromCbor(CborStreamReader &reader)
std::map< QString, pappso::cbor::mzcbor::CvParam > cvParamMap
Definition spectrum.h:61
void decodeTrace(pappso::Trace &trace)
Definition spectrum.cpp:270
static std::map< QString, pappso::cbor::mzcbor::CvParam > readScanCvParams(CborStreamReader &reader)
Definition spectrum.cpp:201
std::vector< Precursor > precursorList
Definition spectrum.h:66
void fromCbor(CborStreamReader &reader, bool want_binary_data)
Definition spectrum.cpp:34
std::map< QString, pappso::cbor::mzcbor::CvParam > scanListCvParamMap
Definition spectrum.h:62
std::map< QString, pappso::cbor::mzcbor::CvParam > scanCvParamMap
Definition spectrum.h:63
std::vector< BinaryDataArray > binaryDataArrayList
Definition spectrum.h:67
std::map< QString, pappso::cbor::mzcbor::CvParam > precursorListCvParamMap
Definition spectrum.h:64