libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
locationsaver.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/processing/specpeptidoms/locationsaver.cpp
3 * \date 24/03/2025
4 * \author Aurélien Berthier
5 * \brief save protein subsequences for alignment
6 *
7 * C++ implementation of the SpecPeptidOMS algorithm described in :
8 * (1) Benoist, É.; Jean, G.; Rogniaux, H.; Fertin, G.; Tessier, D. SpecPeptidOMS Directly and
9 * Rapidly Aligns Mass Spectra on Whole Proteomes and Identifies Peptides That Are Not Necessarily
10 * Tryptic: Implications for Peptidomics. J. Proteome Res. 2025.
11 * https://doi.org/10.1021/acs.jproteome.4c00870.
12 */
13
14/*
15 * Copyright (c) 2025 Aurélien Berthier
16 * <aurelien.berthier@ls2n.fr>
17 *
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32#include <algorithm>
33#include "locationsaver.h"
37
38namespace pappso
39{
40namespace specpeptidoms
41{
42
43QString
45{
46 std::size_t length2;
47 if((qsizetype)(beginning + length) <= proteinPtr->size())
48 {
49 length2 = length;
50 }
51 else
52 {
53 length2 = proteinPtr->size() - beginning;
54 }
55 return proteinPtr->getSequence().sliced(proteinPtr->size() - beginning - length2, length2);
56}
57
58
60{
61 Location location_zero;
62 location_zero.beginning = 0;
63 location_zero.length = 0;
64 location_zero.proteinPtr = nullptr;
65 location_zero.score = MIN_ALIGNMENT_SCORE;
66 location_zero.tree = -1;
67
68 m_locations_heap.resize(MAX_SAVED_ALIGNMENTS, location_zero);
69 // std::make_heap(m_locations_heap.begin(), m_locations_heap.end(),
70 // LocationSaver::locationCompare); ?
71 // m_tree_scores.reserve ?
72}
73
74bool
76{
77 return loc1.score > loc2.score;
78}
79
80// TODO : check complexity if m_tree_in_heap[tree] == true
81void
83 std::size_t length,
84 int tree,
85 int score,
86 const SpOMSProtein *protein_ptr)
87{
88 try
89 {
90 m_tree_scores.at(tree) = score;
91 if(m_tree_in_heap.at(tree))
92 {
93 for(std::vector<Location>::iterator iter = m_locations_heap.begin();
94 iter != m_locations_heap.end();
95 iter++)
96 {
97 if(iter->tree == tree)
98 {
99 iter->score = score;
100 iter->length = length;
101 }
102 }
103 std::make_heap(
104 m_locations_heap.begin(), m_locations_heap.end(), LocationSaver::locationCompare);
105 }
106 else
107 {
108 if(m_locations_heap.begin()->tree >= 0)
109 {
110 m_tree_in_heap.at(m_locations_heap.begin()->tree) = false;
111 }
112 m_tree_in_heap.at(tree) = true;
113 std::pop_heap(
114 m_locations_heap.begin(), m_locations_heap.end(), LocationSaver::locationCompare);
115 m_locations_heap.pop_back();
116 m_locations_heap.push_back({beginning, length, tree, score, protein_ptr});
117 std::push_heap(
118 m_locations_heap.begin(), m_locations_heap.end(), LocationSaver::locationCompare);
119 }
120 }
121 catch(const std::exception &stderr)
122 {
124 QObject::tr("addLocation failed std::exception :\n%1").arg(stderr.what()));
125 }
126}
127
128std::vector<pappso::specpeptidoms::Location>
130{
131 std::vector<Location> locations;
132 locations.reserve(m_locations_heap.size());
133 for(std::vector<Location>::const_iterator iter = m_locations_heap.begin();
134 iter != m_locations_heap.end();
135 iter++)
136 {
137 if(iter->tree >= 0)
138 {
139 locations.push_back(*iter);
140 }
141 }
142 return locations;
143}
144
145std::size_t
147{
148 m_tree_scores.push_back(MIN_ALIGNMENT_SCORE);
149 m_tree_in_heap.push_back(false);
150 return m_tree_scores.size() - 1;
151}
152
153int
155{
156 if(m_tree_scores.size() == 0)
157 {
158 return m_locations_heap.begin()->score;
159 }
160 else
161 {
162 if(tree_id > (int)m_tree_scores.size())
163 {
164
166 QObject::tr("LocationSaver::getMinScore failed :\nout of "
167 "range access %1 with m_tree_scores.size() %2")
168 .arg(tree_id)
169 .arg(m_tree_scores.size()));
170 }
171
172 return std::max(m_tree_scores.at(tree_id), m_locations_heap.begin()->score);
173 }
174}
175
179
180void
182{
183 Location location_zero;
184 location_zero.beginning = 0;
185 location_zero.length = 0;
186 location_zero.proteinPtr = nullptr;
187 location_zero.score = MIN_ALIGNMENT_SCORE;
188 location_zero.tree = -1;
189
190 std::fill(m_locations_heap.begin(), m_locations_heap.end(), location_zero);
191 m_tree_scores.clear();
192 m_tree_in_heap.clear();
193
194
195 // int m_min_score, m_max_score;
196}
197} // namespace specpeptidoms
198} // namespace pappso
int getMinScore(int tree_id) const
Returns the minimum score for a location with the provided tree_id to be saved in the heap.
static bool locationCompare(const Location &loc1, const Location &loc2)
void addLocation(std::size_t beginning, std::size_t length, int tree, int score, const SpOMSProtein *protein_ptr)
Adds a location to the locations heap. If a saved location has the same tree_id, it will replace it....
std::vector< Location > getLocations() const
Returns a vector containing the saved locations.
std::size_t getNextTree()
Creates a new alignment tree and returns its id.
const QString & getSequence() const
const uint MAX_SAVED_ALIGNMENTS(5)
const int MIN_ALIGNMENT_SCORE(15)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::size_t length
length of ?
QString getPeptideString() const
convenient function to get peptide sequence from location
std::size_t beginning
start position on ?
const SpOMSProtein * proteinPtr
Protein accession.