libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
massspectrum.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/massspectrum/massspectrum.cpp
3 * \date 15/3/2015
4 * \author Olivier Langella
5 * \brief basic mass spectrum
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2015 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
10 *
11 * This file is part of the PAPPSOms++ library.
12 *
13 * PAPPSOms++ 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++ 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++. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * Contributors:
27 * Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and
28 *implementation
29 ******************************************************************************/
30
31#include <cmath>
32#include <numeric>
33#include <limits>
34#include <vector>
35#include <map>
36
37#include <QDebug>
38
45
49
50
51int massSpectrumMetaTypeId = qRegisterMetaType<pappso::MassSpectrum>("pappso::MassSpectrum");
52int massSpectrumPtrMetaTypeId = qRegisterMetaType<pappso::MassSpectrum *>("pappso::MassSpectrum *");
53
54
55namespace pappso
56{
57
58
62
63
64MassSpectrum::MassSpectrum(std::vector<std::pair<pappso_double, pappso_double>> &data_point_vector)
65 : Trace::Trace(data_point_vector)
66{
67}
68
69MassSpectrum::MassSpectrum(std::vector<DataPoint> &data_point_vector)
70 : Trace::Trace(data_point_vector)
71{
72}
73
75{
76}
77
79{
80}
81
82
83MassSpectrum::MassSpectrum(Trace &&other) : Trace(std::move(other))
84{
85}
86
87
89{
90 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()";
91}
92
93
94MassSpectrum::MassSpectrum(MassSpectrum &&other) : Trace(std::move(other))
95{
96 // Specify std::move so that && reference is passed to the Trace constructor
97 // that takes std::vector<DataPoint> && as rvalue reference.
98
99 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
100 //<< "Moving MassSpectrum::MassSpectrum(MassSpectrum &&)";
101}
102
103
107
108
111{
112 // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()";
113
114 assign(other.begin(), other.end());
115 return *this;
116}
117
118
121{
122 vector<DataPoint>::operator=(std::move(other));
123 return *this;
124}
125
126
129{
130 return std::make_shared<MassSpectrum>(*this);
131}
132
133
136{
137 return std::make_shared<const MassSpectrum>(*this);
138}
139
140
141//! Compute the total ion current of this mass spectrum
142/*!
143 * The sum of all the separate ion currents carried by the ions of different
144 * m/z contributing to a complete mass massSpectrum or in a specified m/z
145 * range of a mass massSpectrum. MS:1000285
146 *
147 * \return <pappso_double> The total ion current.
148 */
151{
152 return Trace::sumY();
153}
154
155
156//! Compute the total ion current of this mass spectrum
157/*!
158 * Convenience function that returns totalIonCurrent();
159 */
162{
163 return totalIonCurrent();
164}
165
166
168MassSpectrum::tic(double mzStart, double mzEnd)
169{
170 return Trace::sumY(mzStart, mzEnd);
171}
172
173
174//! Find the DataPoint instance having the greatest intensity (y) value.
175/*!
176 * \return <const DataPoint &> The data point having the maximum intensity (y)
177 * value of the whole mass spectrum.
178 */
179const DataPoint &
184
185
186//! Find the DataPoint instance having the smallest intensity (y) value.
187/*!
188 * \return <const DataPoint &> The data point having the minimum intensity (y)
189 * value of the whole mass spectrum.
190 */
191const DataPoint &
196
197
198//! Sort the DataPoint instances of this spectrum.
199/*!
200 * The DataPoint instances are sorted according to the x value (the m/z
201 * value) and in increasing order.
202 */
203void
208
209
210//! Tells if \c this MassSpectrum is equal to \p massSpectrum.
211/*!
212 * To compare \c this to \p massSpectrum, a tolerance is applied to both the x
213 * and y values, that is defined using \p precision.
214 *
215 * \param massSpectrum Mass spectrum to compare to \c this.
216 *
217 * \param precision Precision to be used to perform the comparison of the x
218 * and y values of the data points in \c this and \massSpectrum mass spectra.
219 */
220bool
221MassSpectrum::equals(const MassSpectrum &other, PrecisionPtr precision) const
222{
223 if(size() != other.size())
224 {
225 qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
226 << "The other mass spectrum size is not equal to *this size"
227 << "*this size:" << size() << "trace size:" << other.size();
228
229 return false;
230 }
231
233
234 auto trace_it = other.begin();
235
236 for(auto &&data_point : *this)
237 {
238 qDebug() << "first:" << data_point.x << "," << data_point.y << " second:" << trace_it->x
239 << "," << trace_it->y;
240
241 if(!MzRange(data_point.x, precision).contains(trace_it->x))
242 {
243 qDebug() << "x:" << data_point.x << " != " << trace_it->x;
244 return false;
245 }
246
247 if(!MzRange(data_point.y, precint).contains(trace_it->y))
248 {
249 qDebug() << "y:" << data_point.y << " != " << trace_it->y;
250 return false;
251 }
252
253 trace_it++;
254 }
255
256 return true;
257}
258
259
262{
263 MassSpectrum massSpectrum;
264
265 std::vector<DataPoint>::const_iterator it = begin();
266 std::vector<DataPoint>::const_iterator itEnd = end();
267
268 std::vector<DataPoint>::const_reverse_iterator itRev = rbegin();
269 std::vector<DataPoint>::const_reverse_iterator itRevEnd = rend();
270
271 pappso_double lower = range.lower();
272 pappso_double upper = range.upper();
273
274 while((it != itEnd) && (it->x <= itRev->x) && (itRev != itRevEnd))
275 {
276 pappso_double sumX = it->x + itRev->x;
277
278 if(sumX < lower)
279 {
280 ++it;
281 }
282 else if(sumX > upper)
283 {
284 ++itRev;
285 }
286 else
287 {
288 massSpectrum.push_back(*it);
289 massSpectrum.push_back(*itRev);
290
291 std::vector<DataPoint>::const_reverse_iterator itRevIn = itRev;
292 ++itRevIn;
293
294 // FIXME Attention buggy code FR 20180626.
295 sumX = it->x + itRevIn->x;
296 while((sumX > lower) && (it->x <= itRevIn->x) && (itRevIn != itRevEnd))
297 {
298 sumX = it->x + itRevIn->x;
299 // trace.push_back(*it);
300 massSpectrum.push_back(*itRevIn);
301 ++itRevIn;
302 }
303 ++it;
304 }
305 }
306
307 // Sort all the data points in increasing order by x
308 std::sort(massSpectrum.begin(), massSpectrum.end(), [](const DataPoint &a, const DataPoint &b) {
309 return (a.x < b.x);
310 });
311
312 // Remove all the but the first element of a series of elements that are
313 // considered equal. Sort of deduplication.
314 std::vector<DataPoint>::iterator itEndFix = std::unique(
315 massSpectrum.begin(), massSpectrum.end(), [](const DataPoint &a, const DataPoint &b) {
316 // Return true if both elements should be considered equal.
317 return (a.x == b.x) && (a.y == b.y);
318 });
319
320 massSpectrum.resize(std::distance(massSpectrum.begin(), itEndFix));
321
322 return massSpectrum;
323}
324
325
326void
328{
329
330 qDebug() << size();
331 for(std::size_t i = 0; i < size(); i++)
332 {
333 qDebug() << "(" << this->operator[](i).x << "," << this->operator[](i).y << ")";
334 }
335}
336
337
338QDataStream &
339operator<<(QDataStream &outstream, const MassSpectrum &massSpectrum)
340{
341 quint32 vector_size = massSpectrum.size();
342 outstream << vector_size;
343 for(auto &&peak : massSpectrum)
344 {
345 outstream << peak;
346 }
347
348 return outstream;
349}
350
351
352QDataStream &
353operator>>(QDataStream &instream, MassSpectrum &massSpectrum)
354{
355 DataPoint peak;
356
357 if(!instream.atEnd())
358 {
359 quint32 vector_size;
360 instream >> vector_size;
361 qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
362 << " vector_size=" << vector_size;
363
364 for(quint32 i = 0; i < vector_size; i++)
365 {
366
367 if(instream.status() != QDataStream::Ok)
368 {
369 throw PappsoException(QString("error in QDataStream unserialize operator>> of "
370 "massSpectrum :\nread datastream failed status=%1 "
371 "massSpectrum "
372 "i=%2 on size=%3")
373 .arg(instream.status())
374 .arg(i)
375 .arg(vector_size));
376 }
377 instream >> peak;
378 massSpectrum.push_back(peak);
379 }
380 if(instream.status() != QDataStream::Ok)
381 {
382 throw PappsoException(
383 QString("error in QDataStream unserialize operator>> of massSpectrum "
384 ":\nread datastream failed status=%1")
385 .arg(instream.status()));
386 }
387 }
388
389 return instream;
390}
391
392
393MassSpectrum &
398
399} // namespace pappso
generic interface to apply a filter on a MassSpectrum This is the same as FilterInterface,...
Class to represent a mass spectrum.
void sortMz()
Sort the DataPoint instances of this spectrum.
void debugPrintValues() const
MassSpectrumCstSPtr makeMassSpectrumCstSPtr() const
bool equals(const MassSpectrum &other, PrecisionPtr precision) const
Tells if this MassSpectrum is equal to massSpectrum.
MassSpectrumSPtr makeMassSpectrumSPtr() const
pappso_double tic() const
Compute the total ion current of this mass spectrum.
const DataPoint & minIntensityDataPoint() const
Find the DataPoint instance having the smallest intensity (y) value.
virtual MassSpectrum & massSpectrumFilter(const MassSpectrumFilterInterface &filter) final
apply a filter on this MassSpectrum
MassSpectrum filterSum(const MzRange &mass_range) const
virtual MassSpectrum & operator=(const MassSpectrum &other)
pappso_double totalIonCurrent() const
Compute the total ion current of this mass spectrum.
const DataPoint & maxIntensityDataPoint() const
Find the DataPoint instance having the greatest intensity (y) value.
pappso_double lower() const
Definition mzrange.h:71
pappso_double upper() const
Definition mzrange.h:77
bool contains(pappso_double) const
Definition mzrange.cpp:115
static PrecisionPtr getPpmInstance(pappso_double value)
get a ppm precision pointer
pappso_double sumY() const
Definition trace.cpp:1010
const DataPoint & maxYDataPoint() const
Definition trace.cpp:966
virtual Trace & filter(const FilterInterface &filter) final
apply a filter on this trace
Definition trace.cpp:1185
void sortX(Enums::SortOrder sort_order=Enums::SortOrder::ascending)
Definition trace.cpp:1071
const DataPoint & minYDataPoint() const
Definition trace.cpp:950
int massSpectrumPtrMetaTypeId
int massSpectrumMetaTypeId
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
QDataStream & operator<<(QDataStream &outstream, const MassSpectrum &massSpectrum)
QDataStream & operator>>(QDataStream &instream, MassSpectrum &massSpectrum)
double pappso_double
A type definition for doubles.
Definition types.h:60
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
const PrecisionBase * PrecisionPtr
Definition precision.h:122
std::shared_ptr< MassSpectrum > MassSpectrumSPtr