libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
cborscanmapbase.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/cbor/psm/cborscanmapbase.h
3 * \date 13/07/2025
4 * \author Olivier Langella
5 * \brief Base class to handle scan cbor map
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 "cborscanmapbase.h"
30#include "psmfilescanprocess.h"
31#include "psmcborutils.h"
32#include <QCborArray>
33
34
35namespace pappso
36{
37namespace cbor
38{
39
41 : m_PsmFileScanProcess(psm_file_scan_process)
42{
43}
44
48
49void
53
54QCborMap
56{
57 if(!keys().contains("id"))
58 {
59 throw pappso::PappsoException(QObject::tr("missing id in scan"));
60 }
61 return value("id").toMap();
62}
63
64QCborMap
66{
67 if(!keys().contains("precursor"))
68 {
69 throw pappso::PappsoException(QObject::tr("missing precursor in scan"));
70 }
71 return value("precursor").toMap();
72}
73
74QCborArray
76{
77 if(!keys().contains("psm_list"))
78 {
79 throw pappso::PappsoException(QObject::tr("missing psm_list in scan"));
80 }
81 return value("psm_list").toArray();
82}
83
84
85std::vector<double>::iterator
87 const QString &eval_value_key,
88 std::vector<double>::iterator itbegin,
89 std::vector<double>::const_iterator itend)
90{
91 std::vector<double>::iterator it = itbegin;
92 if(keys().contains("psm_list"))
93 {
94
95
96 QCborArray new_psm_arr;
97 for(QCborValue cbor_psm : value("psm_list").toArray())
98 {
99 QCborMap cbor_psm_map = cbor_psm.toMap();
100
101 if(!cbor_psm_map.keys().contains("proforma"))
102 {
104 QObject::tr("missing proforma in psm %1").arg(cbor_psm_map.keys().size()));
105 }
106 QCborMap cbor_psm_eval_newvalues;
107
108
109 if(it == itend)
110 {
112 QObject::tr("no more values to add in psm %1").arg(cbor_psm_map.keys().size()));
113 }
114 cbor_psm_eval_newvalues.insert(eval_value_key, *it);
115 it++;
116
117
118 QCborMap psm_eval = cbor_psm_map.value("eval").toMap();
119 psm_eval.remove(eval_name);
120 psm_eval.insert(eval_name, cbor_psm_eval_newvalues);
121 cbor_psm_map.remove(QString("eval"));
122 cbor_psm_map.insert(QString("eval"), psm_eval);
123
124 new_psm_arr.push_back(cbor_psm_map);
125 }
126
127 // insert(QString("psm_list"), new_psm_arr);
128 remove(QString("psm_list"));
129 insert(QString("psm_list"), new_psm_arr);
130 }
131 return it;
132}
133
134
137{
138 if(isEmpty())
139 {
140 throw pappso::PappsoException(QObject::tr("ERROR: cbor scan map is empty"));
141 }
142 if(!contains(QString("precursor")))
143 {
144 throw pappso::PappsoException(QObject::tr("ERROR: no precursor in scan"));
145 }
146 const QCborMap cbor_precursor(value("precursor").toMap());
147 if(!contains(QString("ms2")))
148 {
149 throw pappso::PappsoException(QObject::tr("ERROR: ms2 is empty in scan"));
150 }
151 const QCborMap cbor_ms2(value("ms2").toMap());
152
153 if(!contains(QString("id")))
154 {
155 throw pappso::PappsoException(QObject::tr("ERROR: id is empty in scan"));
156 }
157 const QCborMap cbor_id(value("id").toMap());
158
159 if(!cbor_id.keys().contains("index"))
160 {
161 throw pappso::PappsoException("There is no scan index");
162 }
163
164 if(!cbor_ms2.keys().contains("spectrum"))
165 {
166 throw pappso::PappsoException("There is no spectrum in ms2");
167 }
168
169 const QCborMap cbor_spectrum(cbor_ms2.value("spectrum").toMap());
170
171 if(!cbor_spectrum.keys().contains("mz"))
172 {
173 throw pappso::PappsoException("There is no cbor_spectrum mz values");
174 }
175 if(!cbor_spectrum.keys().contains("intensity"))
176 {
177 throw pappso::PappsoException("There is no cbor_spectrum intensity values");
178 }
179 pappso::MsRunId msrun_id(m_PsmFileScanProcess.m_currentPeaklistFile.name);
180 msrun_id.setSampleName(m_PsmFileScanProcess.m_currentSampleName);
181 pappso::MsRunIdCstSPtr msrun_id_sp = std::make_shared<const pappso::MsRunId>(msrun_id);
182 pappso::MassSpectrumId ms_id(msrun_id_sp);
183 ms_id.setSpectrumIndex(cbor_id.value("index").toInteger());
184
185 // native_id
186 if(cbor_id.keys().contains("native_id"))
187 {
188 ms_id.setNativeId(cbor_id.value("native_id").toString());
189 }
190
191 std::vector<DataPoint> data_point_vector;
192 std::size_t i = 0;
193 for(auto cbor_mz_value : cbor_spectrum.value("mz").toArray())
194 {
195 data_point_vector.push_back(
196 {cbor_mz_value.toDouble(), cbor_spectrum.value("intensity").toArray().at(i).toDouble()});
197 i++;
198 }
199
200
201 MassSpectrum mass_spectrum(data_point_vector);
202 pappso::PrecursorIonData precursor_ion_data;
203
204 pappso::QualifiedMassSpectrum qualified_mass_spectrum(ms_id);
205 qualified_mass_spectrum.setMassSpectrumSPtr(mass_spectrum.makeMassSpectrumSPtr());
206 qualified_mass_spectrum.setMsLevel(2);
207
208 if(cbor_precursor.keys().contains("z"))
209 {
210 precursor_ion_data.charge = cbor_precursor.value("z").toInteger();
211 }
212 if(cbor_precursor.keys().contains("mz"))
213 {
214 precursor_ion_data.mz = cbor_precursor.value("mz").toDouble();
215 }
216 if(cbor_precursor.keys().contains("intensity"))
217 {
218 precursor_ion_data.intensity = cbor_precursor.value("intensity").toDouble();
219 }
220 qualified_mass_spectrum.appendPrecursorIonData(precursor_ion_data);
221 if(cbor_ms2.keys().contains("rt"))
222 {
223 qualified_mass_spectrum.setRtInSeconds(cbor_ms2.value("rt").toDouble());
224 }
225
226
227 return qualified_mass_spectrum.makeQualifiedMassSpectrumSPtr();
228}
229void
231 PsmProteinMap &new_protein_map) const
232{
233 if(isEmpty())
234 {
235 throw pappso::PappsoException(QObject::tr("ERROR: cbor scan map is empty"));
236 }
237 if(!contains(QString("psm_list")))
238 {
239 throw pappso::PappsoException(QObject::tr("ERROR: no psm_list in scan"));
240 }
241
242 for(auto it_psm : value("psm_list").toArray())
243 {
244 QCborMap psm = it_psm.toMap();
245 if(!psm.contains(QString("protein_list")))
246 {
248 QObject::tr("ERROR: no protein_list in psm, psm_list in scan"));
249 }
250
251 for(auto it_protein : psm.value("protein_list").toArray())
252 {
253 new_protein_map.insert(
254 old_protein_map.getByAccession(it_protein.toMap().value("accession").toString()));
255 }
256 }
257}
258
259
260void
262{
263 // cbor_writer.writeCborMap(*this);
264}
265
266
267void
269{
270
271
272 struct uniquePsmResult
273 {
274 QString proforma;
275 QCborMap psm;
276 };
277
278 QCborArray old_psm_arr = value("psm_list").toArray();
279 QCborArray new_psm_arr;
280 // for(QCborValue cbor_psm : old_psm_arr) {
281
282
283 std::vector<uniquePsmResult> all_psm_list;
284 for(auto it_psm : old_psm_arr)
285 {
286 QCborMap psm_map = it_psm.toMap();
287 all_psm_list.push_back({psm_map.value("proforma").toString(), psm_map});
288 }
289 qDebug();
290 std::sort(all_psm_list.begin(), all_psm_list.end(), [](uniquePsmResult &a, uniquePsmResult &b) {
291 return a.proforma > b.proforma;
292 });
293
294 qDebug();
295 std::vector<uniquePsmResult> unique_psm_list;
296
297 for(auto it = all_psm_list.begin(); it != all_psm_list.end(); it++)
298 {
299 qDebug() << it->proforma;
300 if(unique_psm_list.size() > 0)
301 {
302 if(unique_psm_list.back().proforma == it->proforma)
303 {
304 // merge ?
305 PsmCborUtils::mergePsmProteinRefList(unique_psm_list.back().psm, it->psm);
306 }
307 else
308 {
309 unique_psm_list.push_back(*it);
310 }
311 }
312 else
313 {
314 unique_psm_list.push_back(*it);
315 }
316
317 qDebug();
318 }
319
320
321 qDebug() << unique_psm_list.size();
322 for(auto it = unique_psm_list.begin(); it != unique_psm_list.end(); it++)
323 {
324 new_psm_arr.append(it->psm);
325 qDebug();
326 }
327
328 // qWarning() << new_psm_arr.size();
329 remove(QString("psm_list"));
330 insert(QString("psm_list"), new_psm_arr);
331}
332
333} // namespace cbor
334} // namespace pappso
void setNativeId(const QString &native_id)
void setSpectrumIndex(std::size_t index)
Class to represent a mass spectrum.
MassSpectrumSPtr makeMassSpectrumSPtr() const
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition msrunid.h:54
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition msrunid.cpp:77
Class representing a fully specified mass spectrum.
void appendPrecursorIonData(const PrecursorIonData &precursor_ion_data)
void setMsLevel(uint ms_level)
Set the mass spectrum level.
QualifiedMassSpectrumSPtr makeQualifiedMassSpectrumSPtr() const
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
std::vector< double >::iterator addPsmEvalVectorDouble(const QString &eval_name, const QString &eval_value_key, std::vector< double >::iterator begin, std::vector< double >::const_iterator end)
add a new eval key and double values (from a vector) to each PSM
const PsmFileScanProcess & m_PsmFileScanProcess
CborScanMapBase(const PsmFileScanProcess &psm_file_scan_process)
void populateProteinMapUsingOldProteinMap(const PsmProteinMap &old_protein_map, PsmProteinMap &new_protein_map) const
pappso::QualifiedMassSpectrumSPtr getCurrentQualifiedMassSpectrumSPtr() const
static void mergePsmProteinRefList(QCborMap &cbor_psm_destination, const QCborMap &cbor_psm_source)
Basic PSM file reader to process scan (parallelized scan processing).
store PsmProtein in a map with accession as key
const pappso::cbor::psm::PsmProtein & getByAccession(const QString &accession) const
retrieve a PsmProtein with its accession
std::pair< std::map< QString, PsmProtein >::iterator, bool > insert(const PsmProtein &psm_protein)
standard map insert of a PsmProtein (accession is the key)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::shared_ptr< QualifiedMassSpectrum > QualifiedMassSpectrumSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition msrunid.h:46