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