OpenMS
DataFilters.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
41 
42 namespace OpenMS
43 {
44  class Feature;
45  class ConsensusFeature;
52  class OPENMS_DLLAPI DataFilters
53  {
54 public:
55  DataFilters() = default;
56 
59  {
63  SIZE,
64  META_DATA
65  };
68  {
72  EXISTS
73  };
74 
76  struct OPENMS_DLLAPI DataFilter
77  {
80  DataFilter(const FilterType type, const FilterOperation op, const double val, const String& meta_name = "")
81  : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
82  {};
84  DataFilter(const FilterType type, const FilterOperation op, const String& val, const String& meta_name = "")
85  : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
86  {};
92  double value{ 0.0 };
98  bool value_is_numerical{ false };
99 
101  String toString() const;
102 
110  void fromString(const String & filter);
111 
113  bool operator==(const DataFilter & rhs) const;
114 
116  bool operator!=(const DataFilter & rhs) const;
117 
118  };
119 
121  Size size() const;
122 
128  const DataFilter & operator[](Size index) const;
129 
131  void add(const DataFilter & filter);
132 
138  void remove(Size index);
139 
145  void replace(Size index, const DataFilter & filter);
146 
148  void clear();
149 
151  void setActive(bool is_active);
152 
159  inline bool isActive() const
160  {
161  return is_active_;
162  }
163 
165  bool passes(const Feature& feature) const;
166 
168  bool passes(const ConsensusFeature& consensus_feature) const;
169 
171  inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
172  {
173  if (!is_active_) return true;
174 
175  for (Size i = 0; i < filters_.size(); i++)
176  {
177  const DataFilters::DataFilter & filter = filters_[i];
178  if (filter.field == INTENSITY)
179  {
180  switch (filter.op)
181  {
182  case GREATER_EQUAL:
183  if (spectrum[peak_index].getIntensity() < filter.value) return false;
184 
185  break;
186 
187  case EQUAL:
188  if (spectrum[peak_index].getIntensity() != filter.value) return false;
189 
190  break;
191 
192  case LESS_EQUAL:
193  if (spectrum[peak_index].getIntensity() > filter.value) return false;
194 
195  break;
196 
197  default:
198  break;
199  }
200  }
201  else if (filter.field == META_DATA)
202  {
203  const auto& f_arrays = spectrum.getFloatDataArrays();
204  //find the right meta data array
205  SignedSize f_index = -1;
206  for (Size j = 0; j < f_arrays.size(); ++j)
207  {
208  if (f_arrays[j].getName() == filter.meta_name)
209  {
210  f_index = j;
211  break;
212  }
213  }
214  //if it is present, compare it
215  if (f_index != -1)
216  {
217  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
218  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
219  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
220  }
221 
222  //if float array not found, search in integer arrays
223  const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
224  //find the right meta data array
225  SignedSize i_index = -1;
226  for (Size j = 0; j < i_arrays.size(); ++j)
227  {
228  if (i_arrays[j].getName() == filter.meta_name)
229  {
230  i_index = j;
231  break;
232  }
233  }
234  //if it is present, compare it
235  if (i_index != -1)
236  {
237  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
238  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
239  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
240  }
241 
242  //if it is not present, abort
243  if (f_index == -1 && i_index == -1) return false;
244  }
245  }
246  return true;
247  }
248 
250  inline bool passes(const MSChromatogram& chrom, Size peak_index) const
251  {
252  if (!is_active_) return true;
253 
254  for (Size i = 0; i < filters_.size(); i++)
255  {
256  const DataFilters::DataFilter& filter = filters_[i];
257  if (filter.field == INTENSITY)
258  {
259  switch (filter.op)
260  {
261  case GREATER_EQUAL:
262  if (chrom[peak_index].getIntensity() < filter.value)
263  return false;
264 
265  break;
266 
267  case EQUAL:
268  if (chrom[peak_index].getIntensity() != filter.value)
269  return false;
270 
271  break;
272 
273  case LESS_EQUAL:
274  if (chrom[peak_index].getIntensity() > filter.value)
275  return false;
276 
277  break;
278 
279  default:
280  break;
281  }
282  }
283  else if (filter.field == META_DATA)
284  {
285  const auto& f_arrays = chrom.getFloatDataArrays();
286  // find the right meta data array
287  SignedSize f_index = -1;
288  for (Size j = 0; j < f_arrays.size(); ++j)
289  {
290  if (f_arrays[j].getName() == filter.meta_name)
291  {
292  f_index = j;
293  break;
294  }
295  }
296  // if it is present, compare it
297  if (f_index != -1)
298  {
299  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
300  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
301  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
302  }
303 
304  // if float array not found, search in integer arrays
305  const typename MSSpectrum::IntegerDataArrays& i_arrays = chrom.getIntegerDataArrays();
306  // find the right meta data array
307  SignedSize i_index = -1;
308  for (Size j = 0; j < i_arrays.size(); ++j)
309  {
310  if (i_arrays[j].getName() == filter.meta_name)
311  {
312  i_index = j;
313  break;
314  }
315  }
316  // if it is present, compare it
317  if (i_index != -1)
318  {
319  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
320  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
321  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
322  }
323 
324  // if it is not present, abort
325  if (f_index == -1 && i_index == -1) return false;
326  }
327  }
328  return true;
329  }
330 
332  inline bool passes(const Mobilogram& mobilogram, Size peak_index) const
333  {
334  if (!is_active_) {
335  return true;
336  }
337 
338 
339  for (Size i = 0; i < filters_.size(); i++)
340  {
341  const DataFilters::DataFilter& filter = filters_[i];
342  if (filter.field == INTENSITY)
343  {
344  switch (filter.op)
345  {
346  case GREATER_EQUAL:
347  if (mobilogram[peak_index].getIntensity() < filter.value)
348  return false;
349 
350  break;
351 
352  case EQUAL:
353  if (mobilogram[peak_index].getIntensity() != filter.value)
354  return false;
355 
356  break;
357 
358  case LESS_EQUAL:
359  if (mobilogram[peak_index].getIntensity() > filter.value)
360  return false;
361 
362  break;
363 
364  default:
365  break;
366  }
367  }
368  else if (filter.field == META_DATA)
369  { // no metadata arrays so far...
370  return false;
371  }
372  }
373  return true;
374  }
375 
376  protected:
378  std::vector<DataFilter> filters_;
380  std::vector<Size> meta_indices_;
381 
383  bool is_active_ = false;
384 
386  bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
387  };
388 
389 } //namespace
390 
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:71
DataFilter array providing some convenience functions.
Definition: DataFilters.h:53
bool metaPasses_(const MetaInfoInterface &meta_interface, const DataFilters::DataFilter &filter, Size index) const
Returns if the meta value at index of meta_interface (a peak or feature) passes the filter.
bool passes(const ConsensusFeature &consensus_feature) const
Returns if the consensus_feature fulfills the current filter criteria.
bool isActive() const
Returns if the filters are enabled.
Definition: DataFilters.h:159
void add(const DataFilter &filter)
Adds a filter.
bool passes(const Mobilogram &mobilogram, Size peak_index) const
Returns if the a peak in a mobilogram at peak_index fulfills the current filter criteria.
Definition: DataFilters.h:332
void replace(Size index, const DataFilter &filter)
Replaces the filter corresponding to index.
void remove(Size index)
Removes the filter corresponding to index.
bool passes(const MSChromatogram &chrom, Size peak_index) const
Returns if the a peak in a chrom at peak_index fulfills the current filter criteria.
Definition: DataFilters.h:250
void setActive(bool is_active)
Enables/disables the all the filters.
FilterType
Information to filter.
Definition: DataFilters.h:59
@ INTENSITY
Filter the intensity value.
Definition: DataFilters.h:60
@ SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:63
@ QUALITY
Filter the overall quality value.
Definition: DataFilters.h:61
@ CHARGE
Filter the charge value.
Definition: DataFilters.h:62
FilterOperation
Filter operation.
Definition: DataFilters.h:68
@ GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:69
@ EQUAL
Equal to the value.
Definition: DataFilters.h:70
@ LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:71
bool passes(const MSSpectrum &spectrum, Size peak_index) const
Returns if the a peak in a spectrum at peak_index fulfills the current filter criteria.
Definition: DataFilters.h:171
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:378
void clear()
Removes all filters.
Size size() const
Filter count.
bool passes(const Feature &feature) const
Returns if the feature fulfills the current filter criteria.
const DataFilter & operator[](Size index) const
Filter accessor.
std::vector< Size > meta_indices_
Vector of meta indices acting as index cache.
Definition: DataFilters.h:380
An LC-MS feature.
Definition: Feature.h:72
The representation of a chromatogram.
Definition: MSChromatogram.h:57
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
const FloatDataArrays & getFloatDataArrays() const
The representation of a 1D spectrum.
Definition: MSSpectrum.h:70
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
std::vector< IntegerDataArray > IntegerDataArrays
Definition: MSSpectrum.h:128
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:61
The representation of a 1D ion mobilogram.
Definition: Mobilogram.h:55
A more convenient string class.
Definition: String.h:60
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
Representation of a peak/feature filter combining FilterType, FilterOperation and a value (either dou...
Definition: DataFilters.h:77
DataFilter(const FilterType type, const FilterOperation op, const String &val, const String &meta_name="")
ctor for common case of string filter
Definition: DataFilters.h:84
String toString() const
Returns a string representation of the filter.
bool operator==(const DataFilter &rhs) const
Equality operator.
DataFilter()
Definition: DataFilters.h:78
FilterType field
Field to filter.
Definition: DataFilters.h:88
void fromString(const String &filter)
Parses filter and sets the filter properties accordingly.
String meta_name
Name of the considered meta information (key)
Definition: DataFilters.h:96
bool operator!=(const DataFilter &rhs) const
Inequality operator.
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:94
DataFilter(const FilterType type, const FilterOperation op, const double val, const String &meta_name="")
ctor for common case of numerical filter
Definition: DataFilters.h:80
FilterOperation op
Filter operation.
Definition: DataFilters.h:90
double value
Value for comparison.
Definition: DataFilters.h:92