libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
psmcbor2json.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/cbor/psm/psmcbor2json.cpp
3 * \date 19/07/2025
4 * \author Olivier Langella
5 * \brief convert CBOR PSM to JSON
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 "psmcbor2json.h"
30#include <QJsonDocument>
31#include <QJsonObject>
32#include <QJsonValue>
34
36{
37 mp_jsonOutput = json_output_p;
38
39 *mp_jsonOutput << "{" << Qt::endl;
40}
41
42
46
47void
53
54void
56{
57 if(!m_targetFastaFiles.isEmpty())
58 {
59 mp_jsonOutput->appendKey("target_fasta_files");
61 }
62 if(!m_decoyFastaFiles.isEmpty())
63 {
64 mp_jsonOutput->appendKey("decoy_fasta_files");
66 }
67}
68
69void
76
77void
83
84
85void
91
92void
94{
95
96 mp_jsonOutput->appendKey("sample_list");
97 mp_jsonOutput->startArray();
98}
99
100void
106
107void
109{
110 mp_jsonOutput->appendKey("protein_map");
111 mp_jsonOutput->startMap();
112 if(!mpa_cborReader->enterContainer())
113 {
114 throw pappso::PappsoException(QObject::tr("enterContainer in protein map failed"));
115 }
116 QString accession;
117 while(!mpa_cborReader->lastError() && mpa_cborReader->hasNext())
118 {
119 mpa_cborReader->decodeString(accession);
120 mp_jsonOutput->appendKey(accession);
121
122 QCborMap cbor_protein;
123 mpa_cborReader->readCborMap(cbor_protein);
124 mp_jsonOutput->writeCborMap(cbor_protein);
125 }
126
127 mpa_cborReader->leaveContainer();
128 mp_jsonOutput->endMap();
129}
130
131
132void
134{
135 qDebug();
136 m_cborScanId.clear();
137 if(!mpa_cborReader->enterContainer())
138 {
139 throw pappso::PappsoException(QObject::tr("enterContainer in scan failed"));
140 }
141 mp_jsonOutput->startMap();
142 //"id": {
143 //"index": 1976
144 //},
145 qDebug() << "scan begin";
146
147 if(!getExpectedString())
148 {
149 throw pappso::PappsoException(QObject::tr("getExpectedString in scan failed"));
150 }
151 qDebug() << m_expectedString;
152
154 if(m_expectedString == "id")
155 {
156 if(!mpa_cborReader->readCborMap(m_cborScanId))
157 {
158 throw pappso::PappsoException(QObject::tr("id element in scan is not a cbor map"));
159 }
160
161 mp_jsonOutput->writeCborMap(m_cborScanId);
162 }
163 else
164 {
166 QObject::tr("ERROR: expecting id element in scan not %1").arg(m_expectedString));
167 }
168 //"precursor": {
169 //"z": 2,
170 //"mz": 1120.529471
171 //},
172
174 m_cborScanPrecursor.clear();
175 qDebug() << m_expectedString;
177 if(m_expectedString == "precursor")
178 {
179 if(!mpa_cborReader->readCborMap(m_cborScanPrecursor))
180 {
181 throw pappso::PappsoException(QObject::tr("precursor element in scan is not a cbor map"));
182 }
183 mp_jsonOutput->writeCborMap(m_cborScanPrecursor);
184 }
185 //"ms2": {PSM CBOR format documentation
186 //"rt": 12648.87,
187 //"mz" :[1,2,3,4],
188 //"intensity" : [1,2,3,4]
189 //},
190
192 qDebug() << m_expectedString;
193 m_cborScanMs2.clear();
195 if(m_expectedString == "ms2")
196 {
197 if(!mpa_cborReader->readCborMap(m_cborScanMs2))
198 {
200 QObject::tr("ms2 element in scan is not a cbor map %1 %2:\n%3")
202 .arg(m_cborScanId.value("index").toInteger())
203 .arg(mpa_cborReader->lastError().toString()));
204 }
205 mp_jsonOutput->writeCborMap(m_cborScanMs2);
206 }
207 //"psm_list": [
208 scanStarted(monitor);
209
211 qDebug() << m_expectedString;
213 if(m_expectedString == "psm_list")
214 {
215 mpa_cborReader->enterContainer();
216 mp_jsonOutput->startArray();
217 while(!mpa_cborReader->lastError() && mpa_cborReader->hasNext())
218 {
219 readPsm(monitor);
220 }
221 mpa_cborReader->leaveContainer();
222 mp_jsonOutput->endArray();
223 }
224
225 mpa_cborReader->leaveContainer();
226 mp_jsonOutput->endMap();
227 qDebug() << "scan end";
228 scanFinished(monitor);
229 qDebug();
230}
231
232
233void
235{
236 //"name": "tandem2017_nopatch_20120906_balliau_extract_1_A01_urnb-1",
237 qDebug();
238 mpa_cborReader->enterContainer();
239 mp_jsonOutput->startMap();
241
242 qDebug() << m_expectedString;
244 if(m_expectedString == "name")
245 {
246 if(!mpa_cborReader->decodeString(m_currentSampleName))
247 {
248 throw pappso::PappsoException("sample name is not a string");
249 }
251 }
252 else
253 {
254 throw pappso::PappsoException("ERROR: expecting name element in sample");
255 }
256 //"identification_file_list": [{ "name":
257 //"/home/langella/data1/tandem/tandem2017_nopatch_20120906_balliau_extract_1_A01_urnb-1.xml",
258 //}],
259
261
262 qDebug() << m_expectedString;
264 if(m_expectedString == "identification_file_list")
265 {
266 bool is_ok;
267 mpa_cborReader->enterContainer();
268
269 while(!mpa_cborReader->lastError() && mpa_cborReader->hasNext())
270 {
272 }
273 mpa_cborReader->leaveContainer();
274
276 }
277 //"peaklist_file": {"name": "tandem2017_nopatch_20120906_balliau_extract_1_A01_urnb-1.mzml"
278 //},
279
280 if(m_expectedString == "peaklist_file")
281 {
282 bool is_ok;
284 }
285 else
286 {
287 throw pappso::PappsoException("ERROR: expecting peaklist_file element in sample");
288 }
289 //"scan_list": [
290 sampleStarted(monitor);
293 if(m_expectedString == "scan_list")
294 {
295 mpa_cborReader->enterContainer();
296 mp_jsonOutput->startArray();
297 while(!mpa_cborReader->lastError() && mpa_cborReader->hasNext())
298 {
299 readScan(monitor);
300 }
301 mpa_cborReader->leaveContainer();
302 mp_jsonOutput->endArray();
303 }
304 else
305 {
306 throw pappso::PappsoException("ERROR: expecting scan_list element in sample");
307 }
308 mpa_cborReader->leaveContainer();
309 mp_jsonOutput->endMap();
310
311 sampleFinished(monitor);
312}
313
314void
316{
317 bool is_ok;
318 mpa_cborReader->enterContainer();
319 mp_jsonOutput->startMap();
322 // "proforma": "AQEEM[+15.99491]AQVAK",
323 if(m_expectedString == "proforma")
324 {
325 if(!mpa_cborReader->decodeString(m_currentPsmProforma))
326 {
327 throw pappso::PappsoException("ERROR: proforma element in psm-scan is not a string");
328 }
330 }
331 else
332 {
333 throw pappso::PappsoException("ERROR: expecting proforma element in psm-scan");
334 }
335 //"protein_list" : [
336 //{
337 //"accession": "GRMZM2G083841_P01",
338 //"position": [15,236]
339 //}
340 //],
341
345 qDebug() << m_expectedString;
346 if(m_expectedString == "protein_list")
347 {
348 mpa_cborReader->enterContainer();
349 mp_jsonOutput->startArray();
350 QCborMap cbor_protein;
351 while(!mpa_cborReader->lastError() && mpa_cborReader->hasNext())
352 {
353 mpa_cborReader->readCborMap(cbor_protein);
354 mp_jsonOutput->writeCborMap(cbor_protein);
355 }
356 mpa_cborReader->leaveContainer();
357 mp_jsonOutput->endArray();
358 }
359 else
360 {
361 throw pappso::PappsoException("ERROR: expecting protein_list element in psm-scan");
362 }
363 //"eval": {
364 qDebug();
365 m_cborScanPsmEval.clear();
366 if(!getExpectedString())
367 {
369 QObject::tr("ERROR: expecting eval element in psm-scan %1").arg(m_currentPsmProforma));
370 }
372 if(m_expectedString == "eval")
373 {
374 is_ok = mpa_cborReader->readCborMap(m_cborScanPsmEval);
375 if(!is_ok)
376 {
377 throw pappso::PappsoException("ERROR: eval element in psm-scan is not well formed");
378 }
379 mp_jsonOutput->writeCborMap(m_cborScanPsmEval);
380 }
381
382 qDebug() << m_expectedString;
383
384
385 mpa_cborReader->leaveContainer();
386 mp_jsonOutput->endMap();
387 qDebug();
388 psmReady(monitor);
389}
void informationsReady(pappso::UiMonitorInterface &monitor) override
void readSample(pappso::UiMonitorInterface &monitor) override
pappso::cbor::JsonStreamWriter * mp_jsonOutput
void readProteinMap(pappso::UiMonitorInterface &monitor) override
void fastaFilesReady(pappso::UiMonitorInterface &monitor) override
void logReady(pappso::UiMonitorInterface &monitor) override
void sampleListStarted(pappso::UiMonitorInterface &monitor) override
void parameterMapReady(pappso::UiMonitorInterface &monitor) override
void sampleListFinished(pappso::UiMonitorInterface &monitor) override
void readScan(pappso::UiMonitorInterface &monitor) override
void readPsm(pappso::UiMonitorInterface &monitor) override
PsmCbor2Json(pappso::cbor::JsonStreamWriter *json_output_p)
std::vector< PsmProteinRef > m_currentPsmProteinRefList
virtual void scanStarted(pappso::UiMonitorInterface &monitor)
virtual void sampleStarted(pappso::UiMonitorInterface &monitor)
virtual void scanFinished(pappso::UiMonitorInterface &monitor)
virtual void psmReady(pappso::UiMonitorInterface &monitor)
std::vector< PsmFile > m_currentIdentificationFileList
virtual void sampleFinished(pappso::UiMonitorInterface &monitor)