libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
filterpass.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/filers/filterpass.cpp
3 * \date 26/04/2019
4 * \author Olivier Langella
5 * \brief collection of filters concerned by Y selection
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2019 Olivier Langella <Olivier.Langella@u-psud.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 ******************************************************************************/
27
28#include "filterpass.h"
29#include "../../trace/trace.h"
30#include <algorithm>
31#include <cmath>
35
36#include <QObject>
37
38using namespace pappso;
39
40
41FilterLowPass::FilterLowPass(double pass_y) : m_passY(pass_y)
42{
43}
47
50{
51 m_passY = other.m_passY;
52
53 return *this;
54}
55
56
57Trace &
58FilterLowPass::filter(Trace &data_points) const
59{
60 Trace new_data_points;
61 for(auto &&data_point : data_points)
62 {
63 if(data_point.y < m_passY)
64 {
65 new_data_points.push_back(data_point);
66 }
67 }
68 data_points = std::move(new_data_points);
69 return data_points;
70}
71
73{
74}
78
81{
82 m_passY = other.m_passY;
83
84 return *this;
85}
86
87
88Trace &
89FilterHighPass::filter(Trace &data_points) const
90{
91 Trace new_data_points;
92 for(auto &&data_point : data_points)
93 {
94 if(data_point.y > m_passY)
95 {
96 new_data_points.push_back(data_point);
97 }
98 }
99 data_points = std::move(new_data_points);
100 return data_points;
101}
102
103
105{
106}
107
112
115{
117
118 return *this;
119}
120
121
122Trace &
124{
125 auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
126 if(it_max == data_points.end())
127 return data_points;
128 double pass = (it_max->y * m_ratioPassY);
129 Trace new_data_points;
130 for(auto &&data_point : data_points)
131 {
132 if(data_point.y > pass)
133 {
134 new_data_points.push_back(data_point);
135 }
136 }
137 data_points = std::move(new_data_points);
138 return data_points;
139}
140
141
142FilterGreatestY::FilterGreatestY(std::size_t number_of_points) : m_numberOfPoints(number_of_points)
143{
144}
145
146
151
152
155{
157
158 return *this;
159}
160
161
162Trace &
164{
165
166 // Reverse-sort the data points (in y decreasing order) so that we get the
167 // greatest to the front of the vector and we'll then copy the first n data
168 // points to the returned vector. See that return (b < a) ?
169 if(m_numberOfPoints >= data_points.size())
170 return data_points;
171
172 std::sort(data_points.begin(), data_points.end(), [](const DataPoint &a, const DataPoint &b) {
173 return (b.y < a.y);
174 });
175
176 data_points.erase(data_points.begin() + m_numberOfPoints, data_points.end());
177
178 // And now sort the Trace conventionally, that is in x increasing order.
179 std::sort(data_points.begin(), data_points.end(), [](const DataPoint &a, const DataPoint &b) {
180 return (a.x < b.x);
181 });
182
183
184 return data_points;
185}
186
187std::size_t
192
193
195 std::size_t number_of_points_per_window)
196 : m_xWindowRange(window_range), m_numberOfPoints(number_of_points_per_window)
197{
198
199 qDebug();
200 if(m_xWindowRange < 0.5)
201 {
202 throw pappso::ExceptionOutOfRange(QObject::tr("window_range must be greater than 0.5"));
203 }
204
205 qDebug();
206}
207
208
214
217{
218 qDebug();
221
222 return *this;
223}
224
225
226Trace &
228{
229
230 std::vector<DataPoint> new_trace(data_points);
231 data_points.clear();
232
233 int window_number = 0;
234 int old_window_number = -1;
235 std::size_t number_of_peaks_in_window = 0;
236 auto itbegin = data_points.begin();
237 std::vector<DataPoint>::iterator it_min;
238
239
240 // std::sort(data_points.begin(),
241 // data_points.end(),
242 // [](const DataPoint &a, const DataPoint &b) { return (a.y > b.y);
243 // });
244
245 qDebug() << " m_xWindowRange=" << m_xWindowRange << " m_numberOfPoints=" << m_numberOfPoints;
246 for(const pappso::DataPoint &data_point : new_trace)
247 {
248 qDebug() << " data_point.x=" << data_point.x << " data_point.y=" << data_point.y;
249 window_number = trunc(data_point.x / m_xWindowRange);
250 qDebug() << window_number;
251 if(window_number != old_window_number)
252 {
253 old_window_number = window_number;
254 number_of_peaks_in_window = 0;
255 itbegin = data_points.end();
256 }
257 if(number_of_peaks_in_window < m_numberOfPoints)
258 {
259 qDebug();
260 data_points.push_back(data_point);
261 number_of_peaks_in_window++;
262 if(number_of_peaks_in_window == 1)
263 {
264 itbegin = data_points.begin() + (data_points.size() - 1);
265 }
266 }
267 else
268 {
269 qDebug();
270
271 it_min = minYDataPoint(itbegin, data_points.end());
272 if(it_min != data_points.end())
273 {
274 qDebug();
275 if(it_min->y < data_point.y)
276 {
277 qDebug();
278 *it_min = data_point;
279 // it_min->x = data_point.x;
280 // it_min->y = data_point.y;
281 }
282 }
283 }
284 }
285 qDebug();
286 // new_trace.sortX();
287 // qDebug() << new_trace.size();
288 // data_points.clear();
289 // data_points = new_trace;
290 // data_points = std::move(new_trace);
291 // qDebug() << data_points.size();
292 data_points.sortX();
293 qDebug();
294 return data_points;
295}
296
297std::size_t
302
303
307FilterFloorY::FilterFloorY([[maybe_unused]] const FilterFloorY &other)
308{
309}
310
312FilterFloorY::operator=([[maybe_unused]] const FilterFloorY &other)
313{
314 return *this;
315}
316
317
318Trace &
319FilterFloorY::filter(Trace &data_points) const
320{
321 for(auto &&dataPoint : data_points)
322 {
323 dataPoint.y = std::floor(dataPoint.y);
324 }
325 return data_points;
326}
327
328
332FilterRoundY::FilterRoundY([[maybe_unused]] const FilterRoundY &other)
333{
334}
335
337FilterRoundY::operator=([[maybe_unused]] const FilterRoundY &other)
338{
339 return *this;
340}
341
342Trace &
343FilterRoundY::filter(Trace &data_points) const
344{
345 for(auto &&dataPoint : data_points)
346 {
347 dataPoint.y = std::round(dataPoint.y);
348 }
349 return data_points;
350}
351
352
354{
355}
359Trace &
360FilterRescaleY::filter(Trace &data_points) const
361{
362 if(m_dynamic == 0)
363 return data_points;
364 auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
365 if(it_max == data_points.end())
366 return data_points;
367 double maximum = it_max->y;
368 for(auto &&dataPoint : data_points)
369 {
370 dataPoint.y = (dataPoint.y / maximum) * m_dynamic;
371 }
372 return data_points;
373}
374
377{
378 m_dynamic = other.m_dynamic;
379
380 return *this;
381}
382
383
384double
386{
387 return m_dynamic;
388}
389
390
392 std::size_t number_of_points)
393 : m_filterGreatestY(number_of_points)
394{
395}
396
402
410
411
414{
415 m_filterGreatestY.filter(spectrum);
416 return spectrum;
417}
418
419
421{
422}
426
429{
430 m_factor = other.m_factor;
431
432 return *this;
433}
434
435
436Trace &
438{
439 if(m_factor == 1)
440 return data_points;
441 for(auto &&dataPoint : data_points)
442 {
443 dataPoint.y = dataPoint.y * m_factor;
444 }
445 return data_points;
446}
447double
449{
450 return m_factor;
451}
452
453
454pappso::FilterRemoveY::FilterRemoveY(const QString &strBuildParams)
455{
456 buildFilterFromString(strBuildParams);
457}
458
459
460void
462{
463 if(strBuildParams.startsWith(QString("%1|").arg(name())))
464 {
465 QStringList params = strBuildParams.split("|").back().split(";");
466
467 m_valueToRemove = params.at(0).toDouble();
468 }
469 else
470 {
472 QString("Building of FilterRemoveY from string %1 failed").arg(strBuildParams));
473 }
474}
475
476FilterRemoveY::FilterRemoveY(double valueToRemove) : m_valueToRemove(valueToRemove)
477{
478}
479
483
484QString
486{
487 return "removeY";
488}
489
490QString
492{
493
494 QString strCode = QString("%1|%2").arg(name()).arg(m_valueToRemove);
495
496 return strCode;
497}
498
501{
503 return *this;
504}
505
506double
508{
509 return m_valueToRemove;
510}
511
512Trace &
513FilterRemoveY::filter(Trace &data_points) const
514{
515 for(auto &&dataPoint : data_points)
516 {
517 if(dataPoint.y < m_valueToRemove)
518 dataPoint.y = 0;
519 else
520 dataPoint.y = dataPoint.y - m_valueToRemove;
521 }
522 return data_points;
523}
524
525
529
534
537{
538 m_quantile = other.m_quantile;
539 return *this;
540}
541
542double
547
548Trace &
550{
551
552 if(data_points.size() == 0)
553 return data_points;
554 double value_to_temove = quantileYTrace(data_points.begin(), data_points.end(), m_quantile);
555 for(auto &&dataPoint : data_points)
556 {
557 if(dataPoint.y < value_to_temove)
558 dataPoint.y = 0;
559 else
560 dataPoint.y = dataPoint.y - value_to_temove;
561 }
562 return data_points;
563}
564
566{
567 buildFilterFromString(strBuildParams);
568}
569
570
571void
573{
574 //"passQuantileBasedRemoveY|0.6"
575 qDebug();
576 if(strBuildParams.startsWith("passQuantileBasedRemoveY|"))
577 {
578 QStringList params = strBuildParams.split("|").back().split(";", Qt::SkipEmptyParts);
579
580 QString value = params.at(0);
581 m_quantile = value.toDouble();
582 }
583 else
584 {
586 QString("building passQuantileBasedRemoveY from string %1 is not possible")
587 .arg(strBuildParams));
588 }
589 qDebug();
590}
591
592
593QString
595{
596 return "passQuantileBasedRemoveY";
597}
598
599
600QString
602{
603 QString strCode = QString("%1|%2").arg(name()).arg(m_quantile);
604
605 return strCode;
606}
excetion to use when an item type is not recognized
apply std::floor (round to lowest integer) to all Y values
Definition filterpass.h:163
FilterFloorY & operator=(const FilterFloorY &other)
Trace & filter(Trace &data_points) const override
keep N datapoints form the greatest intensities to the lowest
Definition filterpass.h:96
Trace & filter(Trace &data_points) const override
FilterGreatestY(std::size_t number_of_points=0)
constructor with the number of datapoints to keep
FilterGreatestY & operator=(const FilterGreatestY &other)
std::size_t getNumberOfPoints() const
std::size_t m_numberOfPoints
Definition filterpass.h:114
keep N datapoints form the greatest intensities to the lowest within a mass range in dalton
Definition filterpass.h:122
Trace & filter(Trace &data_points) const override
FilterGreatestYperWindow(double window_range, std::size_t number_of_points_per_window)
constructor with the number of datapoints to keep
FilterGreatestYperWindow & operator=(const FilterGreatestYperWindow &other)
std::size_t getNumberOfPoints() const
remove datapoints below a given intensity percentage (ratio) of the maximum intensity
Definition filterpass.h:76
FilterHighPassPercentage(double y_ratio)
Trace & filter(Trace &data_points) const override
FilterHighPassPercentage & operator=(const FilterHighPassPercentage &other)
remove datapoints below a given Y value (intensity)
Definition filterpass.h:58
Trace & filter(Trace &data_points) const override
FilterHighPass & operator=(const FilterHighPass &other)
FilterHighPass(double pass_y)
remove datapoints higher than a given Y value (intensity)
Definition filterpass.h:41
FilterLowPass(double pass_y)
Trace & filter(Trace &data_points) const override
FilterLowPass & operator=(const FilterLowPass &other)
removes a value found by quantile to all Y values
Definition filterpass.h:263
FilterQuantileBasedRemoveY & operator=(const FilterQuantileBasedRemoveY &other)
FilterQuantileBasedRemoveY(double quantile_threshold)
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
Trace & filter(Trace &data_points) const override
QString toString() const override
virtual QString name() const override
removes a value to all Y values
Definition filterpass.h:232
double getValue() const
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
Trace & filter(Trace &data_points) const override
FilterRemoveY & operator=(const FilterRemoveY &other)
QString name() const override
QString toString() const override
FilterRemoveY(const QString &strBuildParams)
rescales Y values into a dynamic range if the dynamic range is set to 0, this filter is ignored
Definition filterpass.h:193
Trace & filter(Trace &data_points) const override
FilterRescaleY(double dynamic)
double getDynamicRange() const
FilterRescaleY & operator=(const FilterRescaleY &other)
apply std::round (round to nearest integer) to all Y values
Definition filterpass.h:178
Trace & filter(Trace &data_points) const override
FilterRoundY & operator=(const FilterRoundY &other)
rescales Y values given a tranformation factor
Definition filterpass.h:212
FilterScaleFactorY & operator=(const FilterScaleFactorY &other)
FilterScaleFactorY(double m_factor)
Trace & filter(Trace &data_points) const override
double getScaleFactorY() const
MassSpectrumFilterGreatestItensities(std::size_t number_of_points=0)
MassSpectrum & filter(MassSpectrum &spectrum) const override
MassSpectrumFilterGreatestItensities & operator=(const MassSpectrumFilterGreatestItensities &other)
Class to represent a mass spectrum.
A simple container of DataPoint instances.
Definition trace.h:148
void sortX(Enums::SortOrder sort_order=Enums::SortOrder::ascending)
Definition trace.cpp:1039
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:174
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition trace.cpp:157
double quantileYTrace(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end, double quantile)
calculate the quantile of y value of a trace
Definition trace.cpp:249