libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
psmcborutils.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/cbor/psm/psmcborutils.cpp
3 * \date 16/09/2025
4 * \author Olivier Langella
5 * \brief PSM CBOR utilities
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#include <QCborArray>
28#include <cstddef>
29#include <qtypes.h>
30
31#include "psmcborutils.h"
34
35namespace pappso
36{
37namespace cbor
38{
39namespace psm
40{
41void
43 QCborMap &cbor_scan,
44 const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum,
45 const std::vector<pappso::PrecursorIonData> &alternative_precursors)
46{
47 // id
48 QCborMap cbor_scan_id;
49 cbor_scan_id.insert(QString("index"),
50 (qint64)ms2_qualified_mass_spectrum.getMassSpectrumId().getSpectrumIndex());
51 cbor_scan_id.insert(QString("native_id"),
52 ms2_qualified_mass_spectrum.getMassSpectrumId().getNativeId());
53 bool is_ok;
54 std::size_t scan =
55 ms2_qualified_mass_spectrum.getMassSpectrumId().extractScanNumberFromNativeId(&is_ok);
56 if(is_ok)
57 {
58 cbor_scan_id.insert(QString("scan"), (qint64)scan);
59 }
60 cbor_scan.insert(QString("id"), cbor_scan_id.toCborValue());
61
62
63 // precursor
64 QCborMap cbor_scan_precursor;
65 cbor_scan_precursor.insert(QString("z"), ms2_qualified_mass_spectrum.getPrecursorCharge());
66 cbor_scan_precursor.insert(QString("mz"), ms2_qualified_mass_spectrum.getPrecursorMz());
67 cbor_scan_precursor.insert(QString("mh"),
68 ms2_qualified_mass_spectrum.getPrecursorMass() + MHPLUS);
69 cbor_scan_precursor.insert(QString("mass"), ms2_qualified_mass_spectrum.getPrecursorMass());
70 cbor_scan_precursor.insert(QString("intensity"),
71 ms2_qualified_mass_spectrum.getPrecursorIntensity());
72 if(alternative_precursors.size() > 0)
73 {
74 QCborArray alternatives_cbor;
75 for(const pappso::PrecursorIonData &precursor : alternative_precursors)
76 {
77 QCborMap cbor_alt_item;
78 cbor_alt_item.insert(QString("z"), precursor.charge);
79 cbor_alt_item.insert(QString("mz"), precursor.mz);
80 cbor_alt_item.insert(QString("intensity"), precursor.intensity);
81 alternatives_cbor.append(cbor_alt_item);
82 }
83
84 cbor_scan_precursor.insert(QString("alternatives"), alternatives_cbor);
85 }
86
87 cbor_scan.insert(QString("precursor"), cbor_scan_precursor.toCborValue());
88
89
90 // ms2
91 QCborMap cbor_scan_ms2;
92 cbor_scan_ms2.insert(QString("rt"), ms2_qualified_mass_spectrum.getRtInSeconds());
93 cbor_scan.insert(QString("ms2"), cbor_scan_ms2.toCborValue());
94}
95
96void
98 QCborMap &cbor_scan,
99 const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum,
100 const std::vector<pappso::PrecursorIonData> &alternative_precursors)
101{
102 prepareCborScanWithSpectrum(cbor_scan, ms2_qualified_mass_spectrum, alternative_precursors);
103 QCborMap spectrum_cbor;
104
105 QCborArray mz_cbor;
106 QCborArray intensity_cbor;
107 for(const pappso::DataPoint &data_point :
108 *(ms2_qualified_mass_spectrum.getMassSpectrumCstSPtr().get()))
109 {
110 mz_cbor.append(data_point.x);
111 intensity_cbor.append(data_point.y);
112 }
113 spectrum_cbor.insert(QString("mz"), mz_cbor);
114 spectrum_cbor.insert(QString("intensity"), intensity_cbor);
115
116 QCborMap new_ms2_map = cbor_scan.value("ms2").toMap();
117 new_ms2_map.insert(QString("spectrum"), spectrum_cbor.toCborValue());
118
119 cbor_scan.insert(QString("ms2"), new_ms2_map);
120}
121
122
123std::vector<PsmCborUtils::PsmProteinRef>
124PsmCborUtils::getPsmProteinRefList(const QCborMap &cbor_psm)
125{
126 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list;
127
128 if(cbor_psm.contains(QString("protein_list")))
129 {
130 for(auto it : cbor_psm.value("protein_list").toArray())
131 {
132 QCborMap cbor_protein_ref = it.toMap();
133 PsmCborUtils::PsmProteinRef protein_ref;
134 protein_ref.accession = cbor_protein_ref.value("accession").toString();
135
136 for(auto ref_position : cbor_protein_ref.value("positions").toArray())
137 {
138 protein_ref.positions.push_back(ref_position.toInteger());
139 }
140 protein_ref_list.push_back(protein_ref);
141 }
142 }
143
144 return protein_ref_list;
145}
146
147
148void
150 const std::vector<PsmCborUtils::PsmProteinRef> &protein_ref_list)
151{
152 QCborArray protein_list;
153
154 for(auto it : protein_ref_list)
155 {
156 QCborMap protein_ref;
157 protein_ref.insert(QString("accession"), it.accession);
158 QCborArray positions_arr;
159 for(auto position : it.positions)
160 {
161 positions_arr.append((qint64)position);
162 }
163 protein_ref.insert(QString("positions"), positions_arr);
164 protein_list.append(protein_ref);
165 }
166
167 cbor_psm.remove(QString("protein_list"));
168 cbor_psm.insert(QString("protein_list"), protein_list);
169}
170
171
172void
173PsmCborUtils::mergePsmProteinRefList(QCborMap &cbor_psm_destination,
174 const QCborMap &cbor_psm_source)
175{
176 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list =
177 getPsmProteinRefList(cbor_psm_destination);
178 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list_source =
179 getPsmProteinRefList(cbor_psm_source);
180
181 protein_ref_list.insert(
182 protein_ref_list.end(), protein_ref_list_source.begin(), protein_ref_list_source.end());
183
184
185 std::sort(protein_ref_list.begin(),
186 protein_ref_list.end(),
188 return a.accession > b.accession;
189 });
190
191 std::vector<PsmCborUtils::PsmProteinRef> unique_protein_ref_list;
192
193 for(auto it = protein_ref_list.begin(); it != protein_ref_list.end(); it++)
194 {
195 // qDebug() << it->proforma;
196 if(unique_protein_ref_list.size() > 0)
197 {
198 if(unique_protein_ref_list.back().accession == it->accession)
199 {
200 // merge positions
201 unique_protein_ref_list.back().positions.insert(
202 unique_protein_ref_list.back().positions.end(),
203 it->positions.begin(),
204 it->positions.end());
205
206 std::sort(unique_protein_ref_list.back().positions.begin(),
207 unique_protein_ref_list.back().positions.end());
208
209 auto last = std::unique(unique_protein_ref_list.back().positions.begin(),
210 unique_protein_ref_list.back().positions.end());
211 // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate
212 unique_protein_ref_list.back().positions.erase(
213 last, unique_protein_ref_list.back().positions.end());
214 }
215 else
216 {
217 unique_protein_ref_list.push_back(*it);
218 }
219 }
220 else
221 {
222 unique_protein_ref_list.push_back(*it);
223 }
224
225 qDebug();
226 }
227
228 setPsmProteinRefList(cbor_psm_destination, unique_protein_ref_list);
229}
230
231
232} // namespace psm
233} // namespace cbor
234} // namespace pappso
std::size_t getSpectrumIndex() const
const QString & getNativeId() const
std::size_t extractScanNumberFromNativeId(bool *is_ok) const
try to find scan id in the native id string
Class representing a fully specified mass spectrum.
MassSpectrumCstSPtr getMassSpectrumCstSPtr() const
Get the MassSpectrumCstSPtr.
uint getPrecursorCharge(bool *ok=nullptr) const
get precursor charge
pappso_double getPrecursorIntensity(bool *ok=nullptr) const
get precursor intensity
double getPrecursorMass(bool *ok_p=nullptr) const
get precursor mass given the charge stats and precursor mz
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
pappso_double getPrecursorMz(bool *ok=nullptr) const
get precursor mz
pappso_double getRtInSeconds() const
Get the retention time in seconds.
static void prepareCborScanWithSpectrumAndPeakList(QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum, const std::vector< pappso::PrecursorIonData > &alternative_precursors)
static void prepareCborScanWithSpectrum(QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum, const std::vector< pappso::PrecursorIonData > &alternative_precursors)
static void setPsmProteinRefList(QCborMap &cbor_psm, const std::vector< PsmProteinRef > &protein_ref_list)
static void mergePsmProteinRefList(QCborMap &cbor_psm_destination, const QCborMap &cbor_psm_source)
static std::vector< PsmProteinRef > getPsmProteinRefList(const QCborMap &cbor_psm)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
const pappso_double MHPLUS(1.007276466879)