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