OpenMS
DataFilters.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
15 
16 namespace OpenMS
17 {
18  class Feature;
19  class ConsensusFeature;
26  class OPENMS_DLLAPI DataFilters
27  {
28 public:
29  DataFilters() = default;
30 
33  {
37  SIZE,
38  META_DATA
39  };
42  {
46  EXISTS
47  };
48 
50  struct OPENMS_DLLAPI DataFilter
51  {
54  DataFilter(const FilterType type, const FilterOperation op, const double val, const String& meta_name = "")
55  : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
56  {};
58  DataFilter(const FilterType type, const FilterOperation op, const String& val, const String& meta_name = "")
59  : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
60  {};
66  double value{ 0.0 };
72  bool value_is_numerical{ false };
73 
75  String toString() const;
76 
84  void fromString(const String & filter);
85 
87  bool operator==(const DataFilter & rhs) const;
88 
90  bool operator!=(const DataFilter & rhs) const;
91 
92  };
93 
95  Size size() const;
96 
102  const DataFilter & operator[](Size index) const;
103 
105  void add(const DataFilter & filter);
106 
112  void remove(Size index);
113 
119  void replace(Size index, const DataFilter & filter);
120 
122  void clear();
123 
125  void setActive(bool is_active);
126 
133  inline bool isActive() const
134  {
135  return is_active_;
136  }
137 
139  bool passes(const Feature& feature) const;
140 
142  bool passes(const ConsensusFeature& consensus_feature) const;
143 
145  inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
146  {
147  if (!is_active_) return true;
148 
149  for (Size i = 0; i < filters_.size(); i++)
150  {
151  const DataFilters::DataFilter & filter = filters_[i];
152  if (filter.field == INTENSITY)
153  {
154  switch (filter.op)
155  {
156  case GREATER_EQUAL:
157  if (spectrum[peak_index].getIntensity() < filter.value) return false;
158 
159  break;
160 
161  case EQUAL:
162  if (spectrum[peak_index].getIntensity() != filter.value) return false;
163 
164  break;
165 
166  case LESS_EQUAL:
167  if (spectrum[peak_index].getIntensity() > filter.value) return false;
168 
169  break;
170 
171  default:
172  break;
173  }
174  }
175  else if (filter.field == META_DATA)
176  {
177  const auto& f_arrays = spectrum.getFloatDataArrays();
178  //find the right meta data array
179  SignedSize f_index = -1;
180  for (Size j = 0; j < f_arrays.size(); ++j)
181  {
182  if (f_arrays[j].getName() == filter.meta_name)
183  {
184  f_index = j;
185  break;
186  }
187  }
188  //if it is present, compare it
189  if (f_index != -1)
190  {
191  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
192  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
193  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
194  }
195 
196  //if float array not found, search in integer arrays
197  const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
198  //find the right meta data array
199  SignedSize i_index = -1;
200  for (Size j = 0; j < i_arrays.size(); ++j)
201  {
202  if (i_arrays[j].getName() == filter.meta_name)
203  {
204  i_index = j;
205  break;
206  }
207  }
208  //if it is present, compare it
209  if (i_index != -1)
210  {
211  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
212  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
213  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
214  }
215 
216  //if it is not present, abort
217  if (f_index == -1 && i_index == -1) return false;
218  }
219  }
220  return true;
221  }
222 
224  inline bool passes(const MSChromatogram& chrom, Size peak_index) const
225  {
226  if (!is_active_) return true;
227 
228  for (Size i = 0; i < filters_.size(); i++)
229  {
230  const DataFilters::DataFilter& filter = filters_[i];
231  if (filter.field == INTENSITY)
232  {
233  switch (filter.op)
234  {
235  case GREATER_EQUAL:
236  if (chrom[peak_index].getIntensity() < filter.value)
237  return false;
238 
239  break;
240 
241  case EQUAL:
242  if (chrom[peak_index].getIntensity() != filter.value)
243  return false;
244 
245  break;
246 
247  case LESS_EQUAL:
248  if (chrom[peak_index].getIntensity() > filter.value)
249  return false;
250 
251  break;
252 
253  default:
254  break;
255  }
256  }
257  else if (filter.field == META_DATA)
258  {
259  const auto& f_arrays = chrom.getFloatDataArrays();
260  // find the right meta data array
261  SignedSize f_index = -1;
262  for (Size j = 0; j < f_arrays.size(); ++j)
263  {
264  if (f_arrays[j].getName() == filter.meta_name)
265  {
266  f_index = j;
267  break;
268  }
269  }
270  // if it is present, compare it
271  if (f_index != -1)
272  {
273  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
274  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
275  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
276  }
277 
278  // if float array not found, search in integer arrays
279  const typename MSSpectrum::IntegerDataArrays& i_arrays = chrom.getIntegerDataArrays();
280  // find the right meta data array
281  SignedSize i_index = -1;
282  for (Size j = 0; j < i_arrays.size(); ++j)
283  {
284  if (i_arrays[j].getName() == filter.meta_name)
285  {
286  i_index = j;
287  break;
288  }
289  }
290  // if it is present, compare it
291  if (i_index != -1)
292  {
293  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
294  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
295  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
296  }
297 
298  // if it is not present, abort
299  if (f_index == -1 && i_index == -1) return false;
300  }
301  }
302  return true;
303  }
304 
306  inline bool passes(const Mobilogram& mobilogram, Size peak_index) const
307  {
308  if (!is_active_) {
309  return true;
310  }
311 
312 
313  for (Size i = 0; i < filters_.size(); i++)
314  {
315  const DataFilters::DataFilter& filter = filters_[i];
316  if (filter.field == INTENSITY)
317  {
318  switch (filter.op)
319  {
320  case GREATER_EQUAL:
321  if (mobilogram[peak_index].getIntensity() < filter.value)
322  return false;
323 
324  break;
325 
326  case EQUAL:
327  if (mobilogram[peak_index].getIntensity() != filter.value)
328  return false;
329 
330  break;
331 
332  case LESS_EQUAL:
333  if (mobilogram[peak_index].getIntensity() > filter.value)
334  return false;
335 
336  break;
337 
338  default:
339  break;
340  }
341  }
342  else if (filter.field == META_DATA)
343  { // no metadata arrays so far...
344  return false;
345  }
346  }
347  return true;
348  }
349 
350  protected:
352  std::vector<DataFilter> filters_;
354  std::vector<Size> meta_indices_;
355 
357  bool is_active_ = false;
358 
360  bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
361  };
362 
363 } //namespace
364 
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:45
DataFilter array providing some convenience functions.
Definition: DataFilters.h:27
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:133
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:306
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:224
void setActive(bool is_active)
Enables/disables the all the filters.
FilterType
Information to filter.
Definition: DataFilters.h:33
@ INTENSITY
Filter the intensity value.
Definition: DataFilters.h:34
@ SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:37
@ QUALITY
Filter the overall quality value.
Definition: DataFilters.h:35
@ CHARGE
Filter the charge value.
Definition: DataFilters.h:36
FilterOperation
Filter operation.
Definition: DataFilters.h:42
@ GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:43
@ EQUAL
Equal to the value.
Definition: DataFilters.h:44
@ LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:45
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:145
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:352
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:354
An LC-MS feature.
Definition: Feature.h:46
The representation of a chromatogram.
Definition: MSChromatogram.h:31
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:44
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:102
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:35
The representation of a 1D ion mobilogram.
Definition: Mobilogram.h:29
A more convenient string class.
Definition: String.h:34
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:108
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
Representation of a peak/feature filter combining FilterType, FilterOperation and a value (either dou...
Definition: DataFilters.h:51
DataFilter(const FilterType type, const FilterOperation op, const String &val, const String &meta_name="")
ctor for common case of string filter
Definition: DataFilters.h:58
String toString() const
Returns a string representation of the filter.
bool operator==(const DataFilter &rhs) const
Equality operator.
DataFilter()
Definition: DataFilters.h:52
FilterType field
Field to filter.
Definition: DataFilters.h:62
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:70
bool operator!=(const DataFilter &rhs) const
Inequality operator.
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:68
DataFilter(const FilterType type, const FilterOperation op, const double val, const String &meta_name="")
ctor for common case of numerical filter
Definition: DataFilters.h:54
FilterOperation op
Filter operation.
Definition: DataFilters.h:64
double value
Value for comparison.
Definition: DataFilters.h:66