libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
psmproteinmap.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/cbor/psm/psmproteinmap.cpp
3 * \date 05/07/2025
4 * \author Olivier Langella
5 * \brief PAPPSO CBOR protein 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 <QObject>
29#include "psmproteinmap.h"
31
32
36
40
41
42std::map<QString, pappso::cbor::psm::PsmProtein> &
47
48
49std::pair<std::map<QString, pappso::cbor::psm::PsmProtein>::iterator, bool>
51{
52 return m_proteinMap.insert(
53 std::pair<QString, PsmProtein>(psm_protein.protein_sp.get()->getAccession(), psm_protein));
54}
55
56void
58{
59 // writer.append("protein_map");
60 writer.startMap(m_proteinMap.size()); // protein map
61 for(auto &protein_pair : m_proteinMap)
62 {
63 writer.append(protein_pair.first);
64
65 qDebug() << protein_pair.first;
66 writer.startMap(); // protein
67 writer.append("description");
68 writer.append(protein_pair.second.protein_sp.get()->getDescription());
69 writer.append("sequence");
70 writer.append(protein_pair.second.protein_sp.get()->getSequence());
71 writer.append("target");
72 writer.append(protein_pair.second.isTarget);
73 writer.append("contaminant");
74 writer.append(protein_pair.second.isContaminant);
75
76 if(!protein_pair.second.cborEval.isEmpty())
77 {
78 writer.append("eval");
79 writer.writeCborMap(protein_pair.second.cborEval);
80 }
81 writer.endMap(); // protein
82 }
83
84 writer.endMap(); // protein map
85}
86
87void
89{
90 PsmProtein psm_protein;
91 reader.enterContainer();
92 while(!reader.lastError() && reader.hasNext())
93 {
94 bool is_ok;
95
96 QString accession;
97 is_ok = reader.decodeString(accession);
98 qDebug() << accession;
99 if(is_ok)
100 {
101 reader.enterContainer();
102 pappso::Protein protein;
103 protein.setAccession(accession);
104 psm_protein.protein_sp = std::make_shared<pappso::Protein>(protein);
105 psm_protein.isContaminant = false;
106 psm_protein.isTarget = true;
107 auto it = m_proteinMap.insert(std::pair<QString, PsmProtein>(accession, psm_protein));
108
109 QString tag;
110 while(!reader.lastError() && reader.hasNext())
111 {
112 is_ok = reader.decodeString(tag);
113 qDebug() << tag;
114 if(is_ok)
115 {
116 if(tag == "description")
117 {
118 QString description;
119 is_ok = reader.decodeString(description);
120 qDebug() << description;
121 it.first->second.protein_sp.get()->setDescription(description);
122 }
123 else if(tag == "sequence")
124 {
125 QString sequence;
126 reader.decodeString(sequence);
127 it.first->second.protein_sp.get()->setSequence(sequence);
128 }
129 else if(tag == "target")
130 {
131 it.first->second.isTarget = reader.toBool();
132 reader.next();
133 }
134 else if(tag == "contaminant")
135 {
136 it.first->second.isContaminant = reader.toBool();
137 reader.next();
138 }
139 else if(tag == "eval")
140 {
141 reader.readCborMap(it.first->second.cborEval);
142 }
143 }
144 }
145 reader.leaveContainer();
146 }
147 else
148 {
149 // no accession
150 }
151 // reader.next();
152 // }
153 }
154 reader.leaveContainer();
155}
156
157std::size_t
159{
160 return m_proteinMap.size();
161}
162
165{
166 auto it = m_proteinMap.find(accession);
167
168 if(it == m_proteinMap.end())
169 {
170 throw pappso::ExceptionNotFound(QObject::tr("%1 accession not found").arg(accession));
171 }
172 return it->second;
173}
174
175void
177{
178 for(auto &it_prot : other_protein_map.m_proteinMap)
179 {
180 m_proteinMap.insert(std::pair<QString, PsmProtein>(it_prot.first, it_prot.second));
181 }
182}
183
184void
185pappso::cbor::psm::PsmProteinMap::reduce(const QStringList &accession_list)
186{
187 std::map<QString, PsmProtein> new_protein_map;
188
189 for(const QString &accession : accession_list)
190 {
191 new_protein_map.insert(std::pair<QString, PsmProtein>(accession, getByAccession(accession)));
192 }
193 m_proteinMap = new_protein_map;
194}
virtual void setAccession(const QString &accession)
Definition protein.cpp:132
simple override of the raw QCborStreamReader This adds convenient functions to put CBOR data into C++...
bool readCborMap(QCborMap &cbor_map)
transfer the entire current CBOR map to a QCborMap object
bool decodeString(QString &the_str)
decode the current cbor value as a string the point to the next value the current value is decoded as...
overrides QCborStreamWriter base class to provide convenient functions
void writeCborMap(const QCborMap &cbor_map)
void reduce(const QStringList &accession_list)
reduce to protein accessions listed in input
std::size_t size() const
get protein map size
void merge(const PsmProteinMap &other_protein_map)
merge with other protein map
std::map< QString, PsmProtein > m_proteinMap
const pappso::cbor::psm::PsmProtein & getByAccession(const QString &accession) const
retrieve a PsmProtein with its accession
std::map< QString, PsmProtein > & getProteinMap()
std::pair< std::map< QString, PsmProtein >::iterator, bool > insert(const PsmProtein &psm_protein)
standard map insert of a PsmProtein (accession is the key)
void readMap(CborStreamReader &reader)
reads the protein map from a cbor input stream
void writeMap(CborStreamWriter &writer) const
writes the protein map in a cbor output stream this output a CborMap structure directly in the output...
std::shared_ptr< Protein > protein_sp