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
28#include "psmcborutils.h"
29
30namespace pappso
31{
32namespace cbor
33{
34namespace psm
35{
36void
38 QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum)
39{
40 // id
41 QCborMap cbor_scan_id;
42 cbor_scan_id.insert(QString("index"),
43 (qint64)ms2_qualified_mass_spectrum.getMassSpectrumId().getSpectrumIndex());
44 cbor_scan_id.insert(QString("native_id"),
45 ms2_qualified_mass_spectrum.getMassSpectrumId().getNativeId());
46 cbor_scan.insert(QString("id"), cbor_scan_id.toCborValue());
47
48
49 // precursor
50 QCborMap cbor_scan_precursor;
51 cbor_scan_precursor.insert(QString("z"), ms2_qualified_mass_spectrum.getPrecursorCharge());
52 cbor_scan_precursor.insert(QString("mz"), ms2_qualified_mass_spectrum.getPrecursorMz());
53 cbor_scan_precursor.insert(QString("mh"), ms2_qualified_mass_spectrum.getPrecursorMass());
54 cbor_scan_precursor.insert(QString("intensity"),
55 ms2_qualified_mass_spectrum.getPrecursorIntensity());
56 cbor_scan.insert(QString("precursor"), cbor_scan_precursor.toCborValue());
57
58
59 // ms2
60 QCborMap cbor_scan_ms2;
61 cbor_scan_ms2.insert(QString("rt"), ms2_qualified_mass_spectrum.getRtInSeconds());
62 cbor_scan.insert(QString("ms2"), cbor_scan_ms2.toCborValue());
63}
64
65void
67 QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum)
68{
69 prepareCborScanWithSpectrum(cbor_scan, ms2_qualified_mass_spectrum);
70 QCborMap spectrum_cbor;
71
72 QCborArray mz_cbor;
73 QCborArray intensity_cbor;
74 for(const pappso::DataPoint &data_point :
75 *(ms2_qualified_mass_spectrum.getMassSpectrumCstSPtr().get()))
76 {
77 mz_cbor.append(data_point.x);
78 intensity_cbor.append(data_point.y);
79 }
80 spectrum_cbor.insert(QString("mz"), mz_cbor);
81 spectrum_cbor.insert(QString("intensity"), intensity_cbor);
82
83 QCborMap new_ms2_map = cbor_scan.value("ms2").toMap();
84 new_ms2_map.insert(QString("spectrum"), spectrum_cbor.toCborValue());
85
86 cbor_scan.insert(QString("ms2"), new_ms2_map);
87}
88
89
90std::vector<PsmCborUtils::PsmProteinRef>
91PsmCborUtils::getPsmProteinRefList(const QCborMap &cbor_psm)
92{
93 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list;
94
95 if(cbor_psm.contains(QString("protein_list")))
96 {
97 for(auto it : cbor_psm.value("protein_list").toArray())
98 {
99 QCborMap cbor_protein_ref = it.toMap();
100 PsmCborUtils::PsmProteinRef protein_ref;
101 protein_ref.accession = cbor_protein_ref.value("accession").toString();
102
103 for(auto ref_position : cbor_protein_ref.value("positions").toArray())
104 {
105 protein_ref.positions.push_back(ref_position.toInteger());
106 }
107 protein_ref_list.push_back(protein_ref);
108 }
109 }
110
111 return protein_ref_list;
112}
113
114
115void
117 const std::vector<PsmCborUtils::PsmProteinRef> &protein_ref_list)
118{
119 QCborArray protein_list;
120
121 for(auto it : protein_ref_list)
122 {
123 QCborMap protein_ref;
124 protein_ref.insert(QString("accession"), it.accession);
125 QCborArray positions_arr;
126 for(auto position : it.positions)
127 {
128 positions_arr.append((qint64)position);
129 }
130 protein_ref.insert(QString("positions"), positions_arr);
131 protein_list.append(protein_ref);
132 }
133
134 cbor_psm.remove(QString("protein_list"));
135 cbor_psm.insert(QString("protein_list"), protein_list);
136}
137
138
139void
140PsmCborUtils::mergePsmProteinRefList(QCborMap &cbor_psm_destination,
141 const QCborMap &cbor_psm_source)
142{
143 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list =
144 getPsmProteinRefList(cbor_psm_destination);
145 std::vector<PsmCborUtils::PsmProteinRef> protein_ref_list_source =
146 getPsmProteinRefList(cbor_psm_source);
147
148 protein_ref_list.insert(
149 protein_ref_list.end(), protein_ref_list_source.begin(), protein_ref_list_source.end());
150
151
152 std::sort(protein_ref_list.begin(),
153 protein_ref_list.end(),
155 return a.accession > b.accession;
156 });
157
158 std::vector<PsmCborUtils::PsmProteinRef> unique_protein_ref_list;
159
160 for(auto it = protein_ref_list.begin(); it != protein_ref_list.end(); it++)
161 {
162 // qDebug() << it->proforma;
163 if(unique_protein_ref_list.size() > 0)
164 {
165 if(unique_protein_ref_list.back().accession == it->accession)
166 {
167 // merge positions
168 unique_protein_ref_list.back().positions.insert(
169 unique_protein_ref_list.back().positions.end(),
170 it->positions.begin(),
171 it->positions.end());
172
173 std::sort(unique_protein_ref_list.back().positions.begin(),
174 unique_protein_ref_list.back().positions.end());
175
176 auto last = std::unique(unique_protein_ref_list.back().positions.begin(),
177 unique_protein_ref_list.back().positions.end());
178 // v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate
179 unique_protein_ref_list.back().positions.erase(
180 last, unique_protein_ref_list.back().positions.end());
181 }
182 else
183 {
184 unique_protein_ref_list.push_back(*it);
185 }
186 }
187 else
188 {
189 unique_protein_ref_list.push_back(*it);
190 }
191
192 qDebug();
193 }
194
195 setPsmProteinRefList(cbor_psm_destination, unique_protein_ref_list);
196}
197
198
199} // namespace psm
200} // namespace cbor
201} // namespace pappso
std::size_t getSpectrumIndex() const
const QString & getNativeId() const
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 prepareCborScanWithSpectrum(QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum)
static void prepareCborScanWithSpectrumAndPeakList(QCborMap &cbor_scan, const pappso::QualifiedMassSpectrum &ms2_qualified_mass_spectrum)
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