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 
35 using namespace pappso;
36 
37 
38 FilterLowPass::FilterLowPass(double pass_y) : m_passY(pass_y)
39 {
40 }
42  : m_passY(other.m_passY)
43 {
44 }
45 
48 {
49  m_passY = other.m_passY;
50 
51  return *this;
52 }
53 
54 
55 Trace &
56 FilterLowPass::filter(Trace &data_points) const
57 {
58  Trace new_data_points;
59  for(auto &&data_point : data_points)
60  {
61  if(data_point.y < m_passY)
62  {
63  new_data_points.push_back(data_point);
64  }
65  }
66  data_points = std::move(new_data_points);
67  return data_points;
68 }
69 
70 FilterHighPass::FilterHighPass(double pass_y) : m_passY(pass_y)
71 {
72 }
74  : m_passY(other.m_passY)
75 {
76 }
77 
80 {
81  m_passY = other.m_passY;
82 
83  return *this;
84 }
85 
86 
87 Trace &
88 FilterHighPass::filter(Trace &data_points) const
89 {
90  Trace new_data_points;
91  for(auto &&data_point : data_points)
92  {
93  if(data_point.y > m_passY)
94  {
95  new_data_points.push_back(data_point);
96  }
97  }
98  data_points = std::move(new_data_points);
99  return data_points;
100 }
101 
102 
104  : m_ratioPassY(ratio_pass_y)
105 {
106 }
107 
109  const FilterHighPassPercentage &other)
110  : m_ratioPassY(other.m_ratioPassY)
111 {
112 }
113 
116 {
117  m_ratioPassY = other.m_ratioPassY;
118 
119  return *this;
120 }
121 
122 
123 Trace &
125 {
126  auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
127  if(it_max == data_points.end())
128  return data_points;
129  double pass = (it_max->y * m_ratioPassY);
130  Trace new_data_points;
131  for(auto &&data_point : data_points)
132  {
133  if(data_point.y > pass)
134  {
135  new_data_points.push_back(data_point);
136  }
137  }
138  data_points = std::move(new_data_points);
139  return data_points;
140 }
141 
142 
143 FilterGreatestY::FilterGreatestY(std::size_t number_of_points)
144  : m_numberOfPoints(number_of_points)
145 {
146 }
147 
148 
150  : m_numberOfPoints(other.m_numberOfPoints)
151 {
152 }
153 
154 
157 {
159 
160  return *this;
161 }
162 
163 
164 Trace &
165 FilterGreatestY::filter(Trace &data_points) const
166 {
167 
168  // Reverse-sort the data points (in y decreasing order) so that we get the
169  // greatest to the front of the vector and we'll then copy the first n data
170  // points to the returned vector. See that return (b < a) ?
171  if(m_numberOfPoints >= data_points.size())
172  return data_points;
173 
174  std::sort(data_points.begin(),
175  data_points.end(),
176  [](const DataPoint &a, const DataPoint &b) { return (b.y < a.y); });
177 
178  data_points.erase(data_points.begin() + m_numberOfPoints, data_points.end());
179 
180  // And now sort the Trace conventionally, that is in x increasing order.
181  std::sort(data_points.begin(),
182  data_points.end(),
183  [](const DataPoint &a, const DataPoint &b) { return (a.x < b.x); });
184 
185 
186  return data_points;
187 }
188 
189 std::size_t
191 {
192  return m_numberOfPoints;
193 }
194 
195 
197  double window_range, std::size_t number_of_points_per_window)
198  : m_xWindowRange(window_range), m_numberOfPoints(number_of_points_per_window)
199 {
200 
201  qDebug();
202  if(m_xWindowRange < 0.5)
203  {
205  QObject::tr("window_range must be greater than 0.5"));
206  }
207 
208  qDebug();
209 }
210 
211 
213  const FilterGreatestYperWindow &other)
214  : m_xWindowRange(other.m_xWindowRange),
215  m_numberOfPoints(other.m_numberOfPoints)
216 {
217  qDebug();
218 }
219 
220 
223 {
225 
226  return *this;
227 }
228 
229 
230 Trace &
232 {
233 
234  Trace new_trace;
235 
236  int window_number = 0;
237  int old_window_number = -1;
238  std::size_t number_of_peaks_in_window = 0;
239  auto itbegin = new_trace.begin();
240 
241 
242  // std::sort(data_points.begin(),
243  // data_points.end(),
244  // [](const DataPoint &a, const DataPoint &b) { return (a.y > b.y);
245  // });
246 
247  qDebug() << " m_xWindowRange=" << m_xWindowRange
248  << " m_numberOfPoints=" << m_numberOfPoints;
249  for(auto &data_point : data_points)
250  {
251  qDebug() << " data_point.x=" << data_point.x
252  << " data_point.y=" << data_point.y;
253  window_number = trunc(data_point.x / m_xWindowRange);
254  qDebug() << window_number;
255  if(window_number != old_window_number)
256  {
257  old_window_number = window_number;
258  number_of_peaks_in_window = 0;
259  itbegin = new_trace.end();
260  }
261  if(number_of_peaks_in_window < m_numberOfPoints)
262  {
263  qDebug();
264  new_trace.push_back(data_point);
265  number_of_peaks_in_window++;
266  if(number_of_peaks_in_window == 1)
267  {
268  itbegin = new_trace.begin() + (new_trace.size() - 1);
269  }
270  }
271  else
272  {
273  qDebug();
274  auto it_min = minYDataPoint(itbegin, new_trace.end());
275  qDebug();
276  if(it_min != new_trace.end())
277  {
278  qDebug();
279  if(it_min->y < data_point.y)
280  {
281  qDebug();
282  it_min->x = data_point.x;
283  it_min->y = data_point.y;
284  }
285  }
286  }
287  }
288  qDebug();
289  new_trace.sortX();
290  // qDebug() << new_trace.size();
291  data_points = std::move(new_trace);
292  // qDebug() << data_points.size();
293  qDebug();
294  return data_points;
295 }
296 
297 std::size_t
299 {
300  return m_numberOfPoints;
301 }
302 
303 
305 {
306 }
307 FilterFloorY::FilterFloorY([[maybe_unused]] const FilterFloorY &other)
308 {
309 }
310 
311 FilterFloorY &
312 FilterFloorY::operator=([[maybe_unused]] const FilterFloorY &other)
313 {
314  return *this;
315 }
316 
317 
318 Trace &
319 FilterFloorY::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 
330 {
331 }
332 FilterRoundY::FilterRoundY([[maybe_unused]] const FilterRoundY &other)
333 {
334 }
335 
336 FilterRoundY &
337 FilterRoundY::operator=([[maybe_unused]] const FilterRoundY &other)
338 {
339  return *this;
340 }
341 
342 Trace &
343 FilterRoundY::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 
353 FilterRescaleY::FilterRescaleY(double dynamic) : m_dynamic(dynamic)
354 {
355 }
357  : m_dynamic(other.m_dynamic)
358 {
359 }
360 Trace &
361 FilterRescaleY::filter(Trace &data_points) const
362 {
363  if(m_dynamic == 0)
364  return data_points;
365  auto it_max = maxYDataPoint(data_points.begin(), data_points.end());
366  if(it_max == data_points.end())
367  return data_points;
368  double maximum = it_max->y;
369  for(auto &&dataPoint : data_points)
370  {
371  dataPoint.y = (dataPoint.y / maximum) * m_dynamic;
372  }
373  return data_points;
374 }
375 
378 {
379  m_dynamic = other.m_dynamic;
380 
381  return *this;
382 }
383 
384 
385 double
387 {
388  return m_dynamic;
389 }
390 
391 
393  std::size_t number_of_points)
394  : m_filterGreatestY(number_of_points)
395 {
396 }
397 
400  : m_filterGreatestY(other.m_filterGreatestY)
401 {
402 }
403 
407 {
409 
410  return *this;
411 }
412 
413 
414 MassSpectrum &
416 {
417  m_filterGreatestY.filter(spectrum);
418  return spectrum;
419 }
420 
421 
422 FilterScaleFactorY::FilterScaleFactorY(double dynamic) : m_factor(dynamic)
423 {
424 }
426  : m_factor(other.m_factor)
427 {
428 }
429 
432 {
433  m_factor = other.m_factor;
434 
435  return *this;
436 }
437 
438 
439 Trace &
441 {
442  if(m_factor == 1)
443  return data_points;
444  for(auto &&dataPoint : data_points)
445  {
446  dataPoint.y = dataPoint.y * m_factor;
447  }
448  return data_points;
449 }
450 double
452 {
453  return m_factor;
454 }
pappso::FilterHighPassPercentage::operator=
FilterHighPassPercentage & operator=(const FilterHighPassPercentage &other)
Definition: filterpass.cpp:115
pappso::FilterScaleFactorY::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:440
pappso::MassSpectrumFilterGreatestItensities::m_filterGreatestY
FilterGreatestY m_filterGreatestY
Definition: filterpass.h:168
pappso::FilterRescaleY::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:361
pappso::FilterHighPass::FilterHighPass
FilterHighPass(double pass_y)
Definition: filterpass.cpp:70
pappso::FilterGreatestY
keep N datapoints form the greatest intensities to the lowest
Definition: filterpass.h:114
pappso::FilterGreatestYperWindow::m_numberOfPoints
std::size_t m_numberOfPoints
Definition: filterpass.h:161
pappso::FilterGreatestYperWindow::operator=
FilterGreatestYperWindow & operator=(const FilterGreatestYperWindow &other)
Definition: filterpass.cpp:222
pappso::FilterRescaleY::m_dynamic
double m_dynamic
Definition: filterpass.h:216
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
Definition: aa.cpp:39
pappso::MassSpectrumFilterGreatestItensities::filter
MassSpectrum & filter(MassSpectrum &spectrum) const override
Definition: filterpass.cpp:415
pappso::FilterGreatestYperWindow::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:231
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition: massspectrum.h:92
pappso::FilterLowPass::operator=
FilterLowPass & operator=(const FilterLowPass &other)
Definition: filterpass.cpp:47
pappso::FilterHighPassPercentage
remove datapoints below a given intensity percentage (ratio) of the maximum intensity
Definition: filterpass.h:94
pappso::MassSpectrumFilterGreatestItensities
Definition: filterpass.h:166
pappso::PeptideIonNter::a
@ a
pappso::FilterGreatestYperWindow
keep N datapoints form the greatest intensities to the lowest within a mass range in dalton
Definition: filterpass.h:140
pappso::FilterRescaleY::FilterRescaleY
FilterRescaleY(double dynamic)
Definition: filterpass.cpp:353
pappso::MassSpectrumFilterGreatestItensities::operator=
MassSpectrumFilterGreatestItensities & operator=(const MassSpectrumFilterGreatestItensities &other)
Definition: filterpass.cpp:406
pappso::FilterHighPass
remove datapoints below a given Y value (intensity)
Definition: filterpass.h:76
pappso::DataPoint
Definition: datapoint.h:21
pappso::FilterGreatestYperWindow::getNumberOfPoints
std::size_t getNumberOfPoints() const
Definition: filterpass.cpp:298
pappso::FilterRoundY
apply std::round (round to nearest integer) to all Y values
Definition: filterpass.h:199
filterpass.h
pappso::FilterFloorY::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:319
pappso::FilterGreatestY::getNumberOfPoints
std::size_t getNumberOfPoints() const
Definition: filterpass.cpp:190
pappso::FilterGreatestYperWindow::FilterGreatestYperWindow
FilterGreatestYperWindow(double window_range, std::size_t number_of_points_per_window)
constructor with the number of datapoints to keep
Definition: filterpass.cpp:196
pappso::FilterGreatestY::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:165
pappso::FilterGreatestY::operator=
FilterGreatestY & operator=(const FilterGreatestY &other)
Definition: filterpass.cpp:156
pappso::FilterRoundY::operator=
FilterRoundY & operator=(const FilterRoundY &other)
Definition: filterpass.cpp:337
pappso::ExceptionOutOfRange
Definition: exceptionoutofrange.h:53
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:132
pappso::FilterLowPass
remove datapoints higher than a given Y value (intensity)
Definition: filterpass.h:59
pappso::FilterFloorY::operator=
FilterFloorY & operator=(const FilterFloorY &other)
Definition: filterpass.cpp:312
pappso::FilterScaleFactorY::m_factor
double m_factor
Definition: filterpass.h:235
pappso::maxYDataPoint
std::vector< DataPoint >::const_iterator maxYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:138
pappso::FilterHighPass::m_passY
double m_passY
Definition: filterpass.h:78
pappso::MassSpectrumFilterGreatestItensities::MassSpectrumFilterGreatestItensities
MassSpectrumFilterGreatestItensities(std::size_t number_of_points=0)
Definition: filterpass.cpp:392
pappso::FilterRoundY::FilterRoundY
FilterRoundY()
Definition: filterpass.cpp:329
pappso::Trace::sortX
void sortX()
Definition: trace.cpp:748
pappso::FilterGreatestYperWindow::m_xWindowRange
double m_xWindowRange
Definition: filterpass.h:160
pappso::FilterRescaleY
rescales Y values into a dynamic range if the dynamic range is set to 0, this filter is ignored
Definition: filterpass.h:214
pappso::FilterHighPass::operator=
FilterHighPass & operator=(const FilterHighPass &other)
Definition: filterpass.cpp:79
pappso::FilterScaleFactorY::FilterScaleFactorY
FilterScaleFactorY(double m_factor)
Definition: filterpass.cpp:422
pappso::FilterScaleFactorY
rescales Y values given a tranformation factor
Definition: filterpass.h:233
pappso::FilterLowPass::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:56
pappso::FilterHighPassPercentage::m_ratioPassY
double m_ratioPassY
Definition: filterpass.h:107
pappso::PeptideIonNter::b
@ b
pappso::FilterScaleFactorY::operator=
FilterScaleFactorY & operator=(const FilterScaleFactorY &other)
Definition: filterpass.cpp:431
pappso::FilterHighPassPercentage::FilterHighPassPercentage
FilterHighPassPercentage(double y_ratio)
Definition: filterpass.cpp:103
pappso::FilterRoundY::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:343
pappso::FilterGreatestY::m_numberOfPoints
std::size_t m_numberOfPoints
Definition: filterpass.h:132
pappso::FilterScaleFactorY::getScaleFactorY
double getScaleFactorY() const
Definition: filterpass.cpp:451
pappso::FilterFloorY::FilterFloorY
FilterFloorY()
Definition: filterpass.cpp:304
pappso::minYDataPoint
std::vector< DataPoint >::const_iterator minYDataPoint(std::vector< DataPoint >::const_iterator begin, std::vector< DataPoint >::const_iterator end)
Definition: trace.cpp:116
pappso::FilterRescaleY::getDynamicRange
double getDynamicRange() const
Definition: filterpass.cpp:386
pappso::FilterLowPass::FilterLowPass
FilterLowPass(double pass_y)
Definition: filterpass.cpp:38
pappso::FilterHighPass::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:88
pappso::FilterRescaleY::operator=
FilterRescaleY & operator=(const FilterRescaleY &other)
Definition: filterpass.cpp:377
pappso::FilterGreatestY::FilterGreatestY
FilterGreatestY(std::size_t number_of_points=0)
constructor with the number of datapoints to keep
Definition: filterpass.cpp:143
pappso::FilterHighPassPercentage::filter
Trace & filter(Trace &data_points) const override
Definition: filterpass.cpp:124
pappso::FilterLowPass::m_passY
double m_passY
Definition: filterpass.h:79
pappso::FilterFloorY
apply std::floor (round to lowest integer) to all Y values
Definition: filterpass.h:184