libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
filterresample.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/filers/filterresample.cpp
3 * \date 28/04/2019
4 * \author Olivier Langella
5 * \brief collection of filters concerned by X 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 "filterresample.h"
30#include <QDebug>
31
32namespace pappso
33{
34
35
39
44
45
46Trace &
48{
49 auto begin_it = findFirstEqualOrGreaterX(spectrum.begin(), spectrum.end(), m_value);
50 spectrum.erase(begin_it, spectrum.end());
51 return spectrum;
52}
53
57
62
63
64double
69
72{
73 m_value = other.m_value;
74
75 return *this;
76}
77
78Trace &
80{
81 // qDebug() << " spectrum.size()=" << spectrum.size();
82
83 auto last_it = findFirstGreaterX(spectrum.begin(), spectrum.end(), m_value);
84 spectrum.erase(spectrum.begin(), last_it);
85
86 // qDebug() << " spectrum.size()=" << spectrum.size();
87
88 return spectrum;
89}
90
92 : m_minX(min_x), m_maxX(max_x)
93{
94}
95
100
101
104{
105 m_minX = other.m_minX;
106 m_maxX = other.m_maxX;
107
108 return *this;
109}
110
111
112Trace &
114{
115
116 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
117 // << " m_min_x=" << m_min_x;
118 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
119 // << " m_max_x=" << m_max_x;
120 auto begin_it = findFirstEqualOrGreaterX(spectrum.begin(), spectrum.end(), m_minX);
121 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
122 // << " begin_it->x=" << begin_it->x;
123 auto end_it = findFirstGreaterX(begin_it, spectrum.end(), m_maxX);
124 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
125 // << " end_it->x=" << end_it->x;
126 spectrum.erase(begin_it, end_it);
127
128 // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
129 // << " spectrum.size()=" << spectrum.size();
130 return spectrum;
131}
132
133
135 : m_minX(min_x), m_maxX(max_x)
136{
137}
138
143
144
147{
148 if(&other == this)
149 return *this;
150
151 m_minX = other.m_minX;
152 m_maxX = other.m_maxX;
153
154 return *this;
155}
156
157
158Trace &
160{
161 // qDebug() << "The range to keep:" << m_minX << "-" << m_maxX;
162
163 auto begin_it = findFirstEqualOrGreaterX(spectrum.begin(), spectrum.end(), m_minX);
164
165 // qDebug() << "Found begin iterator (for m_minX) having:" << begin_it->x
166 //<< "x (m/z) value";
167
168 auto end_it = findFirstGreaterX(begin_it, spectrum.end(), m_maxX);
169
170 if(end_it == spectrum.end())
171 {
172 // qDebug() << "The end iterator (for m_maxX) is the end(). The prev "
173 //"iterator has"
174 //<< std::prev(end_it)->x << " x(m / z) value.";
175 }
176 else
177 {
178 // qDebug() << "Found end iterator (for m_maxX) having:" << end_it->x
179 //<< "x (m/z) value";
180 }
181
182 // qDebug() << "Only keeping range" << begin_it->x << "-"
183 //<< std::prev(end_it)->x;
184
185 spectrum.erase(end_it, spectrum.end());
186 spectrum.erase(spectrum.begin(), begin_it);
187
188 return spectrum;
189}
190
191
193 const MzRange &mz_range)
194 : m_filterRange(mz_range.lower(), mz_range.upper())
195{
196}
197
203
206{
207 // qDebug() << m_filterRange.filter(spectrum);
208 m_filterRange.filter(spectrum);
209 return spectrum;
210}
211
212
214 const MzRange &mz_range)
215 : m_filterRange(mz_range.lower(), mz_range.upper())
216{
217}
218
224
227{
228 m_filterRange.filter(spectrum);
229 return spectrum;
230}
231
232
236
237
239 IntegrationScopeBaseSPtr integration_scope_sp, Enums::DataKind data_kind)
240{
241 m_integrationScopeSpecs.push_back(IntegrationScopeSpec(integration_scope_sp, data_kind));
242
243 QPointF point;
244 if(!m_integrationScopeSpecs.front().integrationScopeSPtr->getPoint(point))
245 qFatal("Failed to get point.");
246 m_lowestMz = point.y();
247
248 double height;
249 if(!m_integrationScopeSpecs.front().integrationScopeSPtr->getHeight(height))
250 qFatal("Failed to get height.");
251 m_greatestMz = m_lowestMz + height;
252}
253
254
256 const IntegrationScopeSpecVector &integration_scope_specs)
257{
258 // qDebug();
259
260 m_integrationScopeSpecs.assign(integration_scope_specs.begin(), integration_scope_specs.end());
261
262 for(auto &&item : m_integrationScopeSpecs)
263 {
264 if(item.integrationScopeSPtr->isRectangle())
265 {
266 // qDebug() << "item is Rectangle.";
267
268 QPointF point;
269 if(!item.integrationScopeSPtr->getPoint(point))
270 qFatal("Failed to get point.");
271 m_lowestMz = std::min(m_lowestMz, point.y());
272
273 double height;
274 if(!item.integrationScopeSPtr->getHeight(height))
275 qFatal("Failed to get height.");
276 m_greatestMz = std::max(m_greatestMz, m_lowestMz + height);
277 }
278 else
279 {
280 if(!item.integrationScopeSPtr->isRhomboid())
281 qFatal(
282 "If integration scope is not Rectangle, then it must be "
283 "Rhomboid.");
284
285 // qDebug() << "item is Rhomboid.";
286
287 QPointF point;
288
289 // Because the mz scale is on the coordinate, we ask for
290 // bottom-most and top-most points of the rhomboid polygon.
291
292 if(!item.integrationScopeSPtr->getBottomMostPoint(point))
293 qFatal("Failed to get point.");
294 m_lowestMz = std::min(m_lowestMz, point.y());
295
296 if(!item.integrationScopeSPtr->getTopMostPoint(point))
297 qFatal("Failed to get point.");
298 m_greatestMz = std::max(m_greatestMz, point.y());
299 }
300 }
301}
302
303
306{
307 // qDebug();
308
310 other.m_integrationScopeSpecs.end());
311
312 for(auto &&item : m_integrationScopeSpecs)
313 {
314 QPointF point;
315 if(!item.integrationScopeSPtr->getPoint(point))
316 qFatal("Failed to get point.");
317 m_lowestMz = std::min(m_lowestMz, point.y());
318
319 double height;
320 if(!item.integrationScopeSPtr->getHeight(height))
321 qFatal("Failed to get height.");
322 m_greatestMz = std::max(m_greatestMz, m_lowestMz + height);
323 }
324}
325
326
327void
329 const IntegrationScopeSpec &integration_scope_spec)
330{
331
332 m_integrationScopeSpecs.push_back(integration_scope_spec);
333
334 QPointF point;
335 if(!m_integrationScopeSpecs.back().integrationScopeSPtr->getPoint(point))
336 qFatal("Failed to get point.");
337 m_lowestMz = std::min(m_lowestMz, point.y());
338
339 double height;
340 if(!m_integrationScopeSpecs.back().integrationScopeSPtr->getHeight(height))
341 qFatal("Failed to get height.");
342 m_greatestMz = std::max(m_greatestMz, m_lowestMz + height);
343}
344
345
348{
349 if(this == &other)
350 return *this;
351
353 other.m_integrationScopeSpecs.end());
354
355 m_lowestMz = other.m_lowestMz;
357
358 return *this;
359}
360
361
362Trace &
364{
365 qFatal("Programming error.");
366 return trace;
367}
368
369
370Trace &
371FilterResampleKeepPointInPolygon::filter(Trace &trace, double dt_value, double rt_value) const
372{
373 // Each time a new integration scope spec is added, the lowest and greatest
374 // m/z values are computed. We use these values to remove from the spectrum
375 // all the points that are outside of that lowest-gratest range.
376
377 // Find the iterator to the most front of the DataPoint vector (mass
378 // spectrum).
379
380 // Note that the m_lowestMz and m_greatestMz are set during construction of
381 // this FilterResampleKeepPointInPolygon filter using the
382 // integration scope specs.
383
384 // qDebug() << "The lowest and greatest m/z values:" << m_lowestMz << "and"
385 //<< m_greatestMz;
386
387 // Start by filtering away all the data points outside of the
388 // [m_lowestMz--m_greatestMz] range.
389
391
392 trace = the_filter.filter(trace);
393
394 // Now iterate in all the data points remaining in the trace and for each
395 // point craft a "virtual" point using the dt|rt value and the m/z value of
396 // the data point (data_point.x).
397
398 auto begin_it = trace.begin();
399 auto end_it = trace.end();
400
401 // qDebug() << "Iterating in the m/z range:" << begin_it->x << "-"
402 //<< std::prev(end_it)->x;
403
404 // Start at the end of the range. The iter is outside of the requested range,
405 // in fact, as shown using iter-- below.
406 auto iter = end_it;
407
408 while(iter > begin_it)
409 {
410 // Immediately go to the last data point of the desired iteration range of
411 // the trace that we need to filter. Remember that end() is not pointing
412 // to any vector item, but past the last one.
413 iter--;
414
415 // qDebug() << "Iterating in trace data point with m/z value:" << iter->x;
416
417 // Now that we have the m/z value, we can check it, in combination with
418 // the value in the selection polygon spec (to make a point) against the
419 // various selection polygon specs in our member vector.
420
421 double checked_value;
422
423 for(auto &&spec : m_integrationScopeSpecs)
424 {
425 // qDebug() << "Iterating in selection polygon spec:" <<
426 // spec.toString();
427
428 if(spec.dataKind == Enums::DataKind::dt)
429 {
430 if(dt_value == -1)
431 qFatal("Programming error.");
432
433 checked_value = dt_value;
434
435 // qDebug() << "The data kind: dt.";
436 }
437 else if(spec.dataKind == Enums::DataKind::rt)
438 {
439 checked_value = rt_value;
440
441 // qDebug() << "The data kind: rt.";
442 }
443 else
444 qFatal("Programming error.");
445
446 // First version doing the whole computation on the basis of the
447 // selection polygon's all faces.
448 //
449 if(!spec.integrationScopeSPtr->contains(QPointF(checked_value, iter->x)))
450 iter = trace.erase(iter);
451 }
452 // End of
453 // for(auto &&spec : m_selectionPolygonSpecs)
454 }
455 // End of
456 // while(iter > begin_it)
457
458 return trace;
459}
460
461} // namespace pappso
FilterResampleKeepGreater & operator=(const FilterResampleKeepGreater &other)
Trace & filter(Trace &trace) const override
void newSelectionPolygonSpec(const IntegrationScopeSpec &integration_scope_spec)
IntegrationScopeSpecVector m_integrationScopeSpecs
FilterResampleKeepPointInPolygon & operator=(const FilterResampleKeepPointInPolygon &other)
Trace & filter(Trace &trace) const override
Trace & filter(Trace &trace) const override
FilterResampleKeepXRange & operator=(const FilterResampleKeepXRange &other)
FilterResampleKeepXRange(double min_x=0, double max_x=0)
Trace & filter(Trace &trace) const override
FilterResampleRemoveXRange(double min_x, double max_x)
FilterResampleRemoveXRange & operator=(const FilterResampleRemoveXRange &other)
Trace & filter(Trace &trace) const override
MassSpectrumFilterResampleKeepMzRange(const MzRange &mz_range)
MassSpectrum & filter(MassSpectrum &spectrum) const override
const FilterResampleKeepXRange m_filterRange
const FilterResampleRemoveXRange m_filterRange
MassSpectrumFilterResampleRemoveMzRange(const MzRange &mz_range)
MassSpectrum & filter(MassSpectrum &spectrum) const override
Class to represent a mass spectrum.
A simple container of DataPoint instances.
Definition trace.h:152
@ dt
Drift time.
Definition types.h:252
@ rt
Retention time.
Definition types.h:251
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
std::vector< DataPoint >::iterator findFirstEqualOrGreaterX(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &value)
find the first element in which X is equal or greater than the value searched important : it implies ...
Definition trace.cpp:65
std::vector< DataPoint >::iterator findFirstGreaterX(std::vector< DataPoint >::iterator begin, std::vector< DataPoint >::iterator end, const double &value)
find the first element in which X is greater than the value searched important : it implies that Trac...
Definition trace.cpp:93
std::shared_ptr< IntegrationScopeBase > IntegrationScopeBaseSPtr
std::vector< IntegrationScopeSpec > IntegrationScopeSpecVector