libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
cboroutputstream.cpp
Go to the documentation of this file.
1
2/**
3 * \file pappsomspp/masschroq/output/cboroutputstream.cpp
4 * \date 02/01/2025
5 * \author Olivier Langella
6 * \brief quantification result cbor writer for MassChroqLight
7 */
8
9/*******************************************************************************
10 * Copyright (c) 2025 Olivier Langella
11 *<Olivier.Langella@universite-paris-saclay.fr>.
12 *
13 * This file is part of MassChroQ.
14 *
15 * MassChroQ is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * MassChroQ is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with MassChroQ. If not, see <http://www.gnu.org/licenses/>.
27 *
28 ******************************************************************************/
29
30#include "cboroutputstream.h"
31#include <QDateTime>
32#include <QCborMap>
33#include <QThreadPool>
39
40namespace pappso
41{
43{
45 mpa_writer->startMap();
46}
47
51
52void
53masschroq::CborOutputStream::setIsPeakShapeOutput(bool is_shape_output, double margin_in_seconds)
54{
55 m_isPeakShapeOutput = is_shape_output;
56 m_peakShapeMarginInSeconds = margin_in_seconds;
57}
58
59void
61{
62 m_isTraceOutput = is_trace_output;
63}
64
65void
67{
68 mpa_writer->writeInformations(
69 "masschroq", masschroq::Utils::getVersion(), "quantification", operation);
70}
71
72
73void
75{
76 mpa_writer->append(QLatin1String("end"));
77 mpa_writer->startMap();
78 mpa_writer->append(QLatin1String("timestamp"));
79 mpa_writer->append(QDateTime::currentDateTime().toString(Qt::ISODate));
80 mpa_writer->endMap();
81}
82void
84{
86 mpa_writer->endMap();
87
88 delete mpa_writer;
89}
90
91void
92masschroq::CborOutputStream::writeJsonObject(const QString &name, const QJsonObject &json_object)
93{
94 mpa_writer->append(name);
95 QCborMap map;
96 map = map.fromJsonObject(json_object);
97 map.toCborValue().toCbor(*mpa_writer);
98}
99
100QCborStreamWriter &
105
106
107void
109{
110 mpa_writer->append(QLatin1String("msrun"));
111 mpa_writer->startMap(3);
112 mpa_writer->append(QLatin1String("id"));
113 mpa_writer->append(msrun.getMsRunReaderSPtr().get()->getMsRunId().get()->getXmlId());
114 mpa_writer->append(QLatin1String("filename"));
115 mpa_writer->append(msrun.getMsRunReaderSPtr().get()->getMsRunId().get()->getFileName());
116 mpa_writer->append(QLatin1String("sample"));
117 mpa_writer->append(msrun.getMsRunReaderSPtr().get()->getMsRunId().get()->getSampleName());
118 mpa_writer->endMap();
119}
120
121
122void
124 const masschroq::PeptideMeasurements &peptide_measurements,
125 const MsRunRetentionTime<QString> *msrun_retention_time_p)
126{
127 masschroq::PeptideSp peptide_sp =
128 peptide_measurements.getPeptideObservationSp().get()->getPeptideSp();
129
130 mpa_writer->append(peptide_sp.get()->getId());
131 mpa_writer->startMap();
132 // mpa_writer->append(QLatin1String("peptide_id"));
133 // mpa_writer->append(peptide_sp.get()->getId());
134 mpa_writer->append(QLatin1String("proforma"));
135 mpa_writer->append(peptide_sp.get()->getPappsoPeptideSp().get()->toProForma());
136
137
138 mpa_writer->append(QLatin1String("mods"));
139 mpa_writer->append(peptide_sp.get()->getMods());
140
141 std::size_t size_measurement = peptide_measurements.getMeasurementList().size();
142 if(size_measurement > 0)
143 {
144 mpa_writer->append(QLatin1String("rt_target"));
145 mpa_writer->append(
146 peptide_measurements.getMeasurementList().at(0).msp_xicCoord.get()->rtTarget);
147 }
148
149 mpa_writer->append(QLatin1String("xics"));
150 mpa_writer->startArray(size_measurement);
152 peptide_measurements.getMeasurementList())
153 {
154 // write(peptide_sp, measure, msrun_retention_time_p);
155 writeMeasurement(measure, msrun_retention_time_p);
156 }
157 mpa_writer->endArray();
158
159 mpa_writer->endMap();
160}
161
162void
165 const MsRunRetentionTime<QString> *msrun_retention_time_p)
166{
167
168 mpa_writer->startMap();
169 mpa_writer->append(QLatin1String("charge"));
170 mpa_writer->append(measurement.m_charge);
171
172 mpa_writer->append(QLatin1String("mz"));
173 mpa_writer->append(measurement.msp_xicCoord.get()->mzRange.getMz());
174
176 {
177 measurement.msp_xicCoord.get()->writeCborStream(*mpa_writer);
178 }
179
180 if(measurement.m_peptideNaturalIsotopeAverageSp.get() != nullptr)
181 {
182 mpa_writer->append(QLatin1String("isotope"));
183 mpa_writer->append(measurement.m_peptideNaturalIsotopeAverageSp.get()->getIsotopeNumber());
184
185 mpa_writer->append(QLatin1String("rank"));
186 mpa_writer->append(measurement.m_peptideNaturalIsotopeAverageSp.get()->getIsotopeRank());
187
188 mpa_writer->append(QLatin1String("th_ratio"));
189 mpa_writer->append(measurement.m_peptideNaturalIsotopeAverageSp.get()->getIntensityRatio());
190 }
191
192
193 mpa_writer->append(QLatin1String("quality"));
195
196 // label
197 if(measurement.mp_peptideLabel != nullptr)
198 {
199 mpa_writer->append(QLatin1String("label"));
200 mpa_writer->append(measurement.mp_peptideLabel->getLabel());
201 }
202
203 if(m_isTraceOutput && (measurement.msp_xicCoord.get()->xicSptr.get() != nullptr))
204 {
205 mpa_writer->append(QLatin1String("trace"));
206 writeTrace(*(measurement.msp_xicCoord.get()->xicSptr.get()));
207 }
208
209
210 if(measurement.m_tracePeakSp.get() != nullptr)
211 {
212
213 if(m_isPeakShapeOutput && (measurement.msp_xicCoord.get()->xicSptr.get() != nullptr))
214 {
215
216 mpa_writer->append(QLatin1String("peak_shape"));
217 mpa_writer->startMap();
218 mpa_writer->append(QLatin1String("trace"));
219
220 TraceSPtr trace_sp;
221
223 measurement.m_tracePeakSp.get()->getLeftBoundary().x - m_peakShapeMarginInSeconds,
224 measurement.m_tracePeakSp.get()->getRightBoundary().x + m_peakShapeMarginInSeconds);
225
226 trace_sp = measurement.msp_xicCoord.get()->xicSptr;
227 trace_sp.get()->filter(cut_xic);
228
229
230 writeTrace(*(trace_sp.get()));
231 mpa_writer->endMap();
232 }
233
234 mpa_writer->append(QLatin1String("peak"));
235 writeTracePeak(*(measurement.m_tracePeakSp.get()), msrun_retention_time_p);
236 }
237 mpa_writer->endMap();
238}
239
240
241void
243{
244 mpa_writer->startMap(2);
245 mpa_writer->append(QLatin1String("x"));
246 mpa_writer->startArray(trace.size());
247 for(double x : trace.xValues())
248 {
249 mpa_writer->append(x);
250 }
251 mpa_writer->endArray();
252
253
254 mpa_writer->append(QLatin1String("y"));
255 mpa_writer->startArray(trace.size());
256 for(double y : trace.yValues())
257 {
258 mpa_writer->append(y);
259 }
260 mpa_writer->endArray();
261 mpa_writer->endMap();
262}
263
264void
266 const TracePeak &peak, const MsRunRetentionTime<QString> *msrun_retention_time_p)
267{
268 mpa_writer->startMap();
269 mpa_writer->append(QLatin1String("area"));
270 mpa_writer->append(peak.getArea());
271
272 mpa_writer->append(QLatin1String("max_intensity"));
273 mpa_writer->append(peak.getMaxXicElement().y);
274
275 mpa_writer->append(QLatin1String("rt"));
276 mpa_writer->startArray();
277 mpa_writer->append(peak.getLeftBoundary().x);
278 mpa_writer->append(peak.getMaxXicElement().x);
279 mpa_writer->append(peak.getRightBoundary().x);
280 mpa_writer->endArray();
281
282 if((msrun_retention_time_p != nullptr) && msrun_retention_time_p->isAligned())
283 {
284 mpa_writer->append(QLatin1String("aligned_rt"));
285 mpa_writer->startArray();
286 mpa_writer->append(
287 msrun_retention_time_p->translateOriginal2AlignedRetentionTime(peak.getLeftBoundary().x));
288 mpa_writer->append(
289 msrun_retention_time_p->translateOriginal2AlignedRetentionTime(peak.getMaxXicElement().x));
290 mpa_writer->append(
291 msrun_retention_time_p->translateOriginal2AlignedRetentionTime(peak.getRightBoundary().x));
292 mpa_writer->endArray();
293 }
294
295 mpa_writer->endMap();
296}
297
298
299void
301 const masschroq::MbrPeptideMeasurements &mbr_peptide_measurements,
302 const MsRunRetentionTime<QString> *msrun_retention_time_p)
303{
304 masschroq::PeptideSp peptide_sp = mbr_peptide_measurements.getPeptideSp();
305
306 mpa_writer->append(peptide_sp.get()->getId());
307 mpa_writer->startMap();
308 mpa_writer->append(QLatin1String("proforma"));
309 mpa_writer->append(peptide_sp.get()->getPappsoPeptideSp().get()->toProForma());
310
311
312 mpa_writer->append(QLatin1String("mods"));
313 mpa_writer->append(peptide_sp.get()->getMods());
314
315
316 std::size_t size_measurement = mbr_peptide_measurements.getMeasurementList().size();
317 if(size_measurement > 0)
318 {
319 mpa_writer->append(QLatin1String("rt_target"));
320 mpa_writer->append(
321 mbr_peptide_measurements.getMeasurementList().at(0).msp_xicCoord.get()->rtTarget);
322 }
323
324 mpa_writer->append(QLatin1String("xics"));
325 mpa_writer->startArray(mbr_peptide_measurements.getMeasurementList().size());
327 mbr_peptide_measurements.getMeasurementList())
328 {
329 // write(peptide_sp, measure, msrun_retention_time_p);
330 writeMeasurement(measure, msrun_retention_time_p);
331 }
332 mpa_writer->endArray();
333
334 mpa_writer->endMap();
335}
336
337void
339 const masschroq::MsRunPeptideList &msrun_peptide_list,
340 bool is_mbr)
341{
342
343 quint64 size;
344 if(is_mbr)
345 {
346 size = msrun_peptide_list.getMbrPeptideMeasurementsList().size();
347
348 monitor.setStatus(QString("Writing MBR quantification results for msrun %1")
349 .arg(msrun_peptide_list.getMsRunSp()
350 .get()
352 .get()
353 ->getMsRunId()
354 .get()
355 ->getSampleName()));
356 }
357 else
358 {
359 size = msrun_peptide_list.getPeptideMeasurementsList().size();
360
361 monitor.setStatus(QString("Writing quantification results for msrun %1")
362 .arg(msrun_peptide_list.getMsRunSp()
363 .get()
365 .get()
366 ->getMsRunId()
367 .get()
368 ->getSampleName()));
369 }
370 monitor.setTotalSteps(size);
371 mpa_writer->append(msrun_peptide_list.getMsRunSp()
372 .get()
374 .get()
375 ->getMsRunId()
376 .get()
377 ->getXmlId());
378 mpa_writer->startMap();
379 writeMsRun(*(msrun_peptide_list.getMsRunSp().get()));
380
381
382 mpa_writer->append("peptide_measurements");
383 mpa_writer->startMap(size);
384 if(is_mbr)
385 {
386 for(const masschroq::MbrPeptideMeasurementsSp &measures :
387 msrun_peptide_list.getMbrPeptideMeasurementsList())
388 {
389 writeMbrPeptideMeasurements(*(measures.get()),
390 msrun_peptide_list.getMsRunRetentionTimeConstPtr());
391
392 monitor.count();
393 }
394 }
395 else
396 {
397 for(const masschroq::PeptideMeasurementsSp &measures :
398 msrun_peptide_list.getPeptideMeasurementsList())
399 {
400 writePeptideMeasurements(*(measures.get()),
401 msrun_peptide_list.getMsRunRetentionTimeConstPtr());
402
403 monitor.count();
404 }
405 }
406 monitor.setTotalSteps(0);
407 mpa_writer->endMap();
408
409 mpa_writer->endMap();
410}
411
412void
413masschroq::CborOutputStream::writeVectorDouble(const std::vector<double> &vector)
414{
415 mpa_writer->startArray(vector.size());
416 for(double x : vector)
417 {
418 mpa_writer->append(x);
419 }
420 mpa_writer->endArray();
421}
422
423void
425{
426 project_parameters.writeParameters(*mpa_writer);
427}
428
429void
431 const Trace &report_common_ms2, const MsRunRetentionTime<QString> &msrun_retention_time)
432{
433 mpa_writer->startMap(5);
434 mpa_writer->append(QLatin1String("original"));
435 writeVectorDouble(msrun_retention_time.getMs1RetentionTimeVector());
436 mpa_writer->append(QLatin1String("aligned"));
437 writeVectorDouble(msrun_retention_time.getAlignedRetentionTimeVector());
438 mpa_writer->append(QLatin1String("ms2_delta_rt"));
439 writeTrace(report_common_ms2);
440
441 Trace median(report_common_ms2);
442 msrun_retention_time.getMs2MedianFilter().filter(median);
443
444 mpa_writer->append(QLatin1String("ms2_median"));
445 writeVectorDouble(median.yValues());
446
447 msrun_retention_time.getMs2MeanFilter().filter(median);
448 mpa_writer->append(QLatin1String("ms2_mean"));
449 writeVectorDouble(median.yValues());
450
451
452 mpa_writer->endMap();
453}
454
455} // namespace pappso
virtual Trace & filter(Trace &data_points) const override
const FilterMorphoMedian & getMs2MedianFilter() const
const std::vector< double > & getAlignedRetentionTimeVector() const
get aligned retention time vector
double translateOriginal2AlignedRetentionTime(double original_retention_time) const
const std::vector< double > & getMs1RetentionTimeVector() const
get orginal retention time vector (not aligned)
const FilterMorphoMean & getMs2MeanFilter() const
void writeParameters(CalcWriterInterface &writer) const
DataPoint & getLeftBoundary()
Definition tracepeak.cpp:93
pappso_double getArea() const
DataPoint & getMaxXicElement()
Definition tracepeak.cpp:83
DataPoint & getRightBoundary()
A simple container of DataPoint instances.
Definition trace.h:152
std::vector< pappso_double > xValues() const
Definition trace.cpp:697
std::vector< pappso_double > yValues() const
Definition trace.cpp:711
virtual void setStatus(const QString &status)=0
current status of the process
virtual void setTotalSteps(std::size_t total_number_of_steps)
use it if the number of steps is known in an algorithm the total number of steps is usefull to report...
virtual void count()=0
count steps report when a step is computed in an algorithm
overrides QCborStreamWriter base class to provide convenient functions
void setIsTraceOutput(bool is_trace_output)
set trace output flag
void writeMbrPeptideMeasurements(const MbrPeptideMeasurements &peptide_measurements, const pappso::MsRunRetentionTime< QString > *msrun_retention_time_p)
void setIsPeakShapeOutput(bool is_shape_output, double margin_in_seconds)
set peak shape output flag
pappso::cbor::CborStreamWriter * mpa_writer
void writeJsonObject(const QString &name, const QJsonObject &json_object)
void writeTrace(const pappso::Trace &trace)
void writeMeasurement(const PeptideMeasurements::Measurement &measurement, const pappso::MsRunRetentionTime< QString > *msrun_retention_time_p)
void writeProjectParameters(const pappso::ProjectParameters &project_parameters)
void writeMsRunRetentionTime(const pappso::Trace &report_common_ms2, const pappso::MsRunRetentionTime< QString > &msrun_retention_time)
void writeVectorDouble(const std::vector< double > &vector)
void writeActionBegin(const QString &operation)
void writeTracePeak(const pappso::TracePeak &peak, const pappso::MsRunRetentionTime< QString > *msrun_retention_time_p)
void writeQrDataBlock(pappso::UiMonitorInterface &m_uiMonitor, const MsRunPeptideList &msrun_peptide_list, bool is_mbr)
void writePeptideMeasurements(const PeptideMeasurements &peptide_measurements, const pappso::MsRunRetentionTime< QString > *msrun_retention_time_p)
const std::vector< MbrPeptideMeasurementsSp > & getMbrPeptideMeasurementsList() const
const std::vector< PeptideMeasurementsSp > & getPeptideMeasurementsList() const
const pappso::MsRunRetentionTime< QString > * getMsRunRetentionTimeConstPtr() const
const pappso::MsRunReaderSPtr & getMsRunReaderSPtr() const
Definition msrun.cpp:63
virtual const pappso::PeptideSp & getPappsoPeptideSp() const
get the peptide sequence
const QString & getLabel() const
virtual const std::vector< Measurement > & getMeasurementList() const
const PeptideObservationSp & getPeptideObservationSp() const
const QString & getMods() const
get optional information as text
Definition peptide.cpp:94
const QString & getId() const
get peptide unique identifier
Definition peptide.cpp:82
static QString getVersion()
Definition utils.cpp:37
static QString enumToString(PeakQualityCategory peak_category)
Convenience function to return a string describing the specglob alingment type.
Definition utils.cpp:44
std::shared_ptr< PeptideMeasurements > PeptideMeasurementsSp
std::shared_ptr< MbrPeptideMeasurements > MbrPeptideMeasurementsSp
std::shared_ptr< Peptide > PeptideSp
Definition peptide.h:46
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< Trace > TraceSPtr
Definition trace.h:139
pappso_double x
Definition datapoint.h:24
pappso_double y
Definition datapoint.h:25
pappso::PeptideNaturalIsotopeAverageSp m_peptideNaturalIsotopeAverageSp