libpappsomspp
Library for mass spectrometry
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>
32 #include "../../massspectrum/massspectrum.h"
33 #include "../../exception/exceptionoutofrange.h"
34 #include "../../exception/exceptionnotrecognized.h"
35 
36 using namespace pappso;
37 
38 
39 FilterLowPass::FilterLowPass(double pass_y) : m_passY(pass_y)
40 {
41 }
43  : m_passY(other.m_passY)
44 {
45 }
46 
49 {
50  m_passY = other.m_passY;
51 
52  return *this;
53 }
54 
55 
56 Trace &
57 FilterLowPass::filter(Trace &data_points) const
58 {
59  Trace new_data_points;
60  for(auto &&data_point : data_points)
61  {
62  if(data_point.y < m_passY)
63  {
64  new_data_points.push_back(data_point);
65  }
66  }
67  data_points = std::move(new_data_points);
68  return data_points;
69 }
70 
71 FilterHighPass::FilterHighPass(double pass_y) : m_passY(pass_y)
72 {
73 }
75  : m_passY(other.m_passY)
76 {
77 }
78 
81 {
82  m_passY = other.m_passY;
83 
84  return *this;
85 }
86 
87 
88 Trace &
89 FilterHighPass::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  : m_ratioPassY(ratio_pass_y)
106 {
107 }
108 
110  const FilterHighPassPercentage &other)
111  : m_ratioPassY(other.m_ratioPassY)
112 {
113 }
114 
117 {
118  m_ratioPassY = other.m_ratioPassY;
119 
120  return *this;
121 }
122 
123 
124 Trace &
126 {
127  auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
128  if(it_max == data_points.end())
129  return data_points;
130  double pass = (it_max->y * m_ratioPassY);
131  Trace new_data_points;
132  for(auto &&data_point : data_points)
133  {
134  if(data_point.y > pass)
135  {
136  new_data_points.push_back(data_point);
137  }
138  }
139  data_points = std::move(new_data_points);
140  return data_points;
141 }
142 
143 
144 FilterGreatestY::FilterGreatestY(std::size_t number_of_points)
145  : m_numberOfPoints(number_of_points)
146 {
147 }
148 
149 
151  : m_numberOfPoints(other.m_numberOfPoints)
152 {
153 }
154 
155 
158 {
160 
161  return *this;
162 }
163 
164 
165 Trace &
166 FilterGreatestY::filter(Trace &data_points) const
167 {
168 
169  // Reverse-sort the data points (in y decreasing order) so that we get the
170  // greatest to the front of the vector and we'll then copy the first n data
171  // points to the returned vector. See that return (b < a) ?
172  if(m_numberOfPoints >= data_points.size())
173  return data_points;
174 
175  std::sort(data_points.begin(),
176  data_points.end(),
177  [](const DataPoint &a, const DataPoint &b) { return (b.y < a.y); });
178 
179  data_points.erase(data_points.begin() + m_numberOfPoints, data_points.end());
180 
181  // And now sort the Trace conventionally, that is in x increasing order.
182  std::sort(data_points.begin(),
183  data_points.end(),
184  [](const DataPoint &a, const DataPoint &b) { return (a.x < b.x); });
185 
186 
187  return data_points;
188 }
189 
190 std::size_t
192 {
193  return m_numberOfPoints;
194 }
195 
196 
198  double window_range, std::size_t number_of_points_per_window)
199  : m_xWindowRange(window_range), m_numberOfPoints(number_of_points_per_window)
200 {
201 
202  qDebug();
203  if(m_xWindowRange < 0.5)
204  {
206  QObject::tr("window_range must be greater than 0.5"));
207  }
208 
209  qDebug();
210 }
211 
212 
214  const FilterGreatestYperWindow &other)
215  : m_xWindowRange(other.m_xWindowRange),
216  m_numberOfPoints(other.m_numberOfPoints)
217 {
218  qDebug();
219 }
220 
223 {
224  qDebug();
227 
228  return *this;
229 }
230 
231 
232 Trace &
234 {
235 
236  std::vector<DataPoint> new_trace(data_points);
237  data_points.clear();
238 
239  int window_number = 0;
240  int old_window_number = -1;
241  std::size_t number_of_peaks_in_window = 0;
242  auto itbegin = data_points.begin();
243  std::vector<DataPoint>::iterator it_min;
244 
245 
246  // std::sort(data_points.begin(),
247  // data_points.end(),
248  // [](const DataPoint &a, const DataPoint &b) { return (a.y > b.y);
249  // });
250 
251  qDebug() << " m_xWindowRange=" << m_xWindowRange
252  << " m_numberOfPoints=" << m_numberOfPoints;
253  for(const pappso::DataPoint &data_point : new_trace)
254  {
255  qDebug() << " data_point.x=" << data_point.x
256  << " data_point.y=" << data_point.y;
257  window_number = trunc(data_point.x / m_xWindowRange);
258  qDebug() << window_number;
259  if(window_number != old_window_number)
260  {
261  old_window_number = window_number;
262  number_of_peaks_in_window = 0;
263  itbegin = data_points.end();
264  }
265  if(number_of_peaks_in_window < m_numberOfPoints)
266  {
267  qDebug();
268  data_points.push_back(data_point);
269  number_of_peaks_in_window++;
270  if(number_of_peaks_in_window == 1)
271  {
272  itbegin = data_points.begin() + (data_points.size() - 1);
273  }
274  }
275  else
276  {
277  qDebug();
278 
279  it_min = minYDataPoint(itbegin, data_points.end());
280  if(it_min != data_points.end())
281  {
282  qDebug();
283  if(it_min->y < data_point.y)
284  {
285  qDebug();
286  *it_min = data_point;
287  // it_min->x = data_point.x;
288  // it_min->y = data_point.y;
289  }
290  }
291  }
292  }
293  qDebug();
294  // new_trace.sortX();
295  // qDebug() << new_trace.size();
296  // data_points.clear();
297  // data_points = new_trace;
298  // data_points = std::move(new_trace);
299  // qDebug() << data_points.size();
300  data_points.sortX();
301  qDebug();
302  return data_points;
303 }
304 
305 std::size_t
307 {
308  return m_numberOfPoints;
309 }
310 
311 
313 {
314 }
315 FilterFloorY::FilterFloorY([[maybe_unused]] const FilterFloorY &other)
316 {
317 }
318 
319 FilterFloorY &
320 FilterFloorY::operator=([[maybe_unused]] const FilterFloorY &other)
321 {
322  return *this;
323 }
324 
325 
326 Trace &
327 FilterFloorY::filter(Trace &data_points) const
328 {
329  for(auto &&dataPoint : data_points)
330  {
331  dataPoint.y = std::floor(dataPoint.y);
332  }
333  return data_points;
334 }
335 
336 
338 {
339 }
340 FilterRoundY::FilterRoundY([[maybe_unused]] const FilterRoundY &other)
341 {
342 }
343 
344 FilterRoundY &
345 FilterRoundY::operator=([[maybe_unused]] const FilterRoundY &other)
346 {
347  return *this;
348 }
349 
350 Trace &
351 FilterRoundY::filter(Trace &data_points) const
352 {
353  for(auto &&dataPoint : data_points)
354  {
355  dataPoint.y = std::round(dataPoint.y);
356  }
357  return data_points;
358 }
359 
360 
361 FilterRescaleY::FilterRescaleY(double dynamic) : m_dynamic(dynamic)
362 {
363 }
365  : m_dynamic(other.m_dynamic)
366 {
367 }
368 Trace &
369 FilterRescaleY::filter(Trace &data_points) const
370 {
371  if(m_dynamic == 0)
372  return data_points;
373  auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
374  if(it_max == data_points.end())
375  return data_points;
376  double maximum = it_max->y;
377  for(auto &&dataPoint : data_points)
378  {
379  dataPoint.y = (dataPoint.y / maximum) * m_dynamic;
380  }
381  return data_points;
382 }
383 
386 {
387  m_dynamic = other.m_dynamic;
388 
389  return *this;
390 }
391 
392 
393 double
395 {
396  return m_dynamic;
397 }
398 
399 
401  std::size_t number_of_points)
402  : m_filterGreatestY(number_of_points)
403 {
404 }
405 
408  : m_filterGreatestY(other.m_filterGreatestY)
409 {
410 }
411 
415 {
417 
418  return *this;
419 }
420 
421 
422 MassSpectrum &
424 {
425  m_filterGreatestY.filter(spectrum);
426  return spectrum;
427 }
428 
429 
430 FilterScaleFactorY::FilterScaleFactorY(double dynamic) : m_factor(dynamic)
431 {
432 }
434  : m_factor(other.m_factor)
435 {
436 }
437 
440 {
441  m_factor = other.m_factor;
442 
443  return *this;
444 }
445 
446 
447 Trace &
449 {
450  if(m_factor == 1)
451  return data_points;
452  for(auto &&dataPoint : data_points)
453  {
454  dataPoint.y = dataPoint.y * m_factor;
455  }
456  return data_points;
457 }
458 double
460 {
461  return m_factor;
462 }
463 
464 FilterRemoveY::FilterRemoveY(double valueToRemove)
465  : m_valueToRemove(valueToRemove)
466 {
467 }
468 
470  : m_valueToRemove(other.m_valueToRemove)
471 {
472 }
473 
476 {
478  return *this;
479 }
480 
481 double
483 {
484  return m_valueToRemove;
485 }
486 
487 Trace &
488 FilterRemoveY::filter(Trace &data_points) const
489 {
490  for(auto &&dataPoint : data_points)
491  {
492  if(dataPoint.y < m_valueToRemove)
493  dataPoint.y = 0;
494  else
495  dataPoint.y = dataPoint.y - m_valueToRemove;
496  }
497  return data_points;
498 }
499 
500 
502  : m_quantile(quantile)
503 {
504 }
505 
507  const FilterQuantileBasedRemoveY &other)
508  : m_quantile(other.m_quantile)
509 {
510 }
511 
514 {
515  m_quantile = other.m_quantile;
516  return *this;
517 }
518 
519 double
521 {
522  return m_quantile;
523 }
524 
525 Trace &
527 {
528 
529  if(data_points.size() == 0)
530  return data_points;
531  double value_to_temove =
532  quantileYTrace(data_points.begin(), data_points.end(), m_quantile);
533  for(auto &&dataPoint : data_points)
534  {
535  if(dataPoint.y < value_to_temove)
536  dataPoint.y = 0;
537  else
538  dataPoint.y = dataPoint.y - value_to_temove;
539  }
540  return data_points;
541 }
542 
544  const QString &strBuildParams)
545 {
546  buildFilterFromString(strBuildParams);
547 }
548 
549 
550 void
552  const QString &strBuildParams)
553 {
554  //"passQuantileBasedRemoveY|0.6"
555  qDebug();
556  if(strBuildParams.startsWith("passQuantileBasedRemoveY|"))
557  {
558  QStringList params =
559  strBuildParams.split("|").back().split(";", Qt::SkipEmptyParts);
560 
561  QString value = params.at(0);
562  m_quantile = value.toDouble();
563  }
564  else
565  {
567  QString(
568  "building passQuantileBasedRemoveY from string %1 is not possible")
569  .arg(strBuildParams));
570  }
571  qDebug();
572 }
573 
574 
575 QString
577 {
578  return "passQuantileBasedRemoveY";
579 }
580 
581 
582 QString
584 {
585  QString strCode = QString("%1|%2").arg(name()).arg(m_quantile);
586 
587  return strCode;
588 }
excetion to use when an item type is not recognized
apply std::floor (round to lowest integer) to all Y values
Definition: filterpass.h:166
FilterFloorY & operator=(const FilterFloorY &other)
Definition: filterpass.cpp:320
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:327
keep N datapoints form the greatest intensities to the lowest
Definition: filterpass.h:96
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:166
FilterGreatestY(std::size_t number_of_points=0)
constructor with the number of datapoints to keep
Definition: filterpass.cpp:144
FilterGreatestY & operator=(const FilterGreatestY &other)
Definition: filterpass.cpp:157
std::size_t getNumberOfPoints() const
Definition: filterpass.cpp:191
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
Definition: filterpass.cpp:233
FilterGreatestYperWindow(double window_range, std::size_t number_of_points_per_window)
constructor with the number of datapoints to keep
Definition: filterpass.cpp:197
FilterGreatestYperWindow & operator=(const FilterGreatestYperWindow &other)
Definition: filterpass.cpp:222
std::size_t getNumberOfPoints() const
Definition: filterpass.cpp:306
remove datapoints below a given intensity percentage (ratio) of the maximum intensity
Definition: filterpass.h:76
FilterHighPassPercentage(double y_ratio)
Definition: filterpass.cpp:104
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:125
FilterHighPassPercentage & operator=(const FilterHighPassPercentage &other)
Definition: filterpass.cpp:116
remove datapoints below a given Y value (intensity)
Definition: filterpass.h:58
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:89
FilterHighPass & operator=(const FilterHighPass &other)
Definition: filterpass.cpp:80
FilterHighPass(double pass_y)
Definition: filterpass.cpp:71
remove datapoints higher than a given Y value (intensity)
Definition: filterpass.h:41
FilterLowPass(double pass_y)
Definition: filterpass.cpp:39
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:57
FilterLowPass & operator=(const FilterLowPass &other)
Definition: filterpass.cpp:48
removes a value found by quantile to all Y values
Definition: filterpass.h:258
FilterQuantileBasedRemoveY & operator=(const FilterQuantileBasedRemoveY &other)
Definition: filterpass.cpp:513
FilterQuantileBasedRemoveY(double quantile_threshold)
Definition: filterpass.cpp:501
void buildFilterFromString(const QString &strBuildParams) override
build this filter using a string
Definition: filterpass.cpp:551
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:526
QString toString() const override
Definition: filterpass.cpp:583
virtual QString name() const override
Definition: filterpass.cpp:576
removes a value to all Y values
Definition: filterpass.h:235
FilterRemoveY(double valueToRemove)
Definition: filterpass.cpp:464
double getValue() const
Definition: filterpass.cpp:482
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:488
FilterRemoveY & operator=(const FilterRemoveY &other)
Definition: filterpass.cpp:475
rescales Y values into a dynamic range if the dynamic range is set to 0, this filter is ignored
Definition: filterpass.h:196
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:369
FilterRescaleY(double dynamic)
Definition: filterpass.cpp:361
double getDynamicRange() const
Definition: filterpass.cpp:394
FilterRescaleY & operator=(const FilterRescaleY &other)
Definition: filterpass.cpp:385
apply std::round (round to nearest integer) to all Y values
Definition: filterpass.h:181
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:351
FilterRoundY & operator=(const FilterRoundY &other)
Definition: filterpass.cpp:345
rescales Y values given a tranformation factor
Definition: filterpass.h:215
FilterScaleFactorY & operator=(const FilterScaleFactorY &other)
Definition: filterpass.cpp:439
FilterScaleFactorY(double m_factor)
Definition: filterpass.cpp:430
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:448
double getScaleFactorY() const
Definition: filterpass.cpp:459
MassSpectrumFilterGreatestItensities(std::size_t number_of_points=0)
Definition: filterpass.cpp:400
MassSpectrum & filter(MassSpectrum &spectrum) const override
Definition: filterpass.cpp:423
MassSpectrumFilterGreatestItensities & operator=(const MassSpectrumFilterGreatestItensities &other)
Definition: filterpass.cpp:413
Class to represent a mass spectrum.
Definition: massspectrum.h:71
A simple container of DataPoint instances.
Definition: trace.h:148
void sortX()
Definition: trace.cpp:905
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:139
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:117
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:224