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