OpenMS
DataFilters.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- 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 
32  bool operator==(const DataFilters&) const = default;
33 
36  {
40  SIZE,
41  META_DATA
42  };
45  {
49  EXISTS
50  };
51 
53  struct OPENMS_DLLAPI DataFilter
54  {
57  DataFilter(const FilterType type, const FilterOperation op, const double val, const String& meta_name = "")
58  : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
59  {};
61  DataFilter(const FilterType type, const FilterOperation op, const String& val, const String& meta_name = "")
62  : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
63  {};
69  double value{ 0.0 };
75  bool value_is_numerical{ false };
76 
78  String toString() const;
79 
87  void fromString(const String & filter);
88 
90  bool operator==(const DataFilter & rhs) const;
91 
93  bool operator!=(const DataFilter & rhs) const;
94 
95  };
96 
98  Size size() const;
99 
105  const DataFilter & operator[](Size index) const;
106 
108  void add(const DataFilter & filter);
109 
115  void remove(Size index);
116 
122  void replace(Size index, const DataFilter & filter);
123 
125  void clear();
126 
128  void setActive(bool is_active);
129 
136  inline bool isActive() const
137  {
138  return is_active_;
139  }
140 
142  bool passes(const Feature& feature) const;
143 
145  bool passes(const ConsensusFeature& consensus_feature) const;
146 
148  inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
149  {
150  if (!is_active_) return true;
151 
152  for (Size i = 0; i < filters_.size(); i++)
153  {
154  const DataFilters::DataFilter & filter = filters_[i];
155  if (filter.field == INTENSITY)
156  {
157  switch (filter.op)
158  {
159  case GREATER_EQUAL:
160  if (spectrum[peak_index].getIntensity() < filter.value) return false;
161 
162  break;
163 
164  case EQUAL:
165  if (spectrum[peak_index].getIntensity() != filter.value) return false;
166 
167  break;
168 
169  case LESS_EQUAL:
170  if (spectrum[peak_index].getIntensity() > filter.value) return false;
171 
172  break;
173 
174  default:
175  break;
176  }
177  }
178  else if (filter.field == META_DATA)
179  {
180  const auto& f_arrays = spectrum.getFloatDataArrays();
181  //find the right meta data array
182  SignedSize f_index = -1;
183  for (Size j = 0; j < f_arrays.size(); ++j)
184  {
185  if (f_arrays[j].getName() == filter.meta_name)
186  {
187  f_index = j;
188  break;
189  }
190  }
191  //if it is present, compare it
192  if (f_index != -1)
193  {
194  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
195  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
196  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
197  }
198 
199  //if float array not found, search in integer arrays
200  const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
201  //find the right meta data array
202  SignedSize i_index = -1;
203  for (Size j = 0; j < i_arrays.size(); ++j)
204  {
205  if (i_arrays[j].getName() == filter.meta_name)
206  {
207  i_index = j;
208  break;
209  }
210  }
211  //if it is present, compare it
212  if (i_index != -1)
213  {
214  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
215  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
216  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
217  }
218 
219  //if it is not present, abort
220  if (f_index == -1 && i_index == -1) return false;
221  }
222  }
223  return true;
224  }
225 
227  inline bool passes(const MSChromatogram& chrom, Size peak_index) const
228  {
229  if (!is_active_) return true;
230 
231  for (Size i = 0; i < filters_.size(); i++)
232  {
233  const DataFilters::DataFilter& filter = filters_[i];
234  if (filter.field == INTENSITY)
235  {
236  switch (filter.op)
237  {
238  case GREATER_EQUAL:
239  if (chrom[peak_index].getIntensity() < filter.value)
240  return false;
241 
242  break;
243 
244  case EQUAL:
245  if (chrom[peak_index].getIntensity() != filter.value)
246  return false;
247 
248  break;
249 
250  case LESS_EQUAL:
251  if (chrom[peak_index].getIntensity() > filter.value)
252  return false;
253 
254  break;
255 
256  default:
257  break;
258  }
259  }
260  else if (filter.field == META_DATA)
261  {
262  const auto& f_arrays = chrom.getFloatDataArrays();
263  // find the right meta data array
264  SignedSize f_index = -1;
265  for (Size j = 0; j < f_arrays.size(); ++j)
266  {
267  if (f_arrays[j].getName() == filter.meta_name)
268  {
269  f_index = j;
270  break;
271  }
272  }
273  // if it is present, compare it
274  if (f_index != -1)
275  {
276  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
277  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
278  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
279  }
280 
281  // if float array not found, search in integer arrays
282  const typename MSSpectrum::IntegerDataArrays& i_arrays = chrom.getIntegerDataArrays();
283  // find the right meta data array
284  SignedSize i_index = -1;
285  for (Size j = 0; j < i_arrays.size(); ++j)
286  {
287  if (i_arrays[j].getName() == filter.meta_name)
288  {
289  i_index = j;
290  break;
291  }
292  }
293  // if it is present, compare it
294  if (i_index != -1)
295  {
296  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
297  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
298  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
299  }
300 
301  // if it is not present, abort
302  if (f_index == -1 && i_index == -1) return false;
303  }
304  }
305  return true;
306  }
307 
309  inline bool passes(const Mobilogram& mobilogram, Size peak_index) const
310  {
311  if (!is_active_) {
312  return true;
313  }
314 
315 
316  for (Size i = 0; i < filters_.size(); i++)
317  {
318  const DataFilters::DataFilter& filter = filters_[i];
319  if (filter.field == INTENSITY)
320  {
321  switch (filter.op)
322  {
323  case GREATER_EQUAL:
324  if (mobilogram[peak_index].getIntensity() < filter.value)
325  return false;
326 
327  break;
328 
329  case EQUAL:
330  if (mobilogram[peak_index].getIntensity() != filter.value)
331  return false;
332 
333  break;
334 
335  case LESS_EQUAL:
336  if (mobilogram[peak_index].getIntensity() > filter.value)
337  return false;
338 
339  break;
340 
341  default:
342  break;
343  }
344  }
345  else if (filter.field == META_DATA)
346  { // no metadata arrays so far...
347  return false;
348  }
349  }
350  return true;
351  }
352 
353  protected:
355  std::vector<DataFilter> filters_;
357  std::vector<Size> meta_indices_;
358 
360  bool is_active_ = false;
361 
363  bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
364  };
365 
366 } //namespace
367 
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:136
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:309
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:227
bool operator==(const DataFilters &) const =default
Equality operator.
void setActive(bool is_active)
Enables/disables the all the filters.
FilterType
Information to filter.
Definition: DataFilters.h:36
@ INTENSITY
Filter the intensity value.
Definition: DataFilters.h:37
@ SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:40
@ QUALITY
Filter the overall quality value.
Definition: DataFilters.h:38
@ CHARGE
Filter the charge value.
Definition: DataFilters.h:39
FilterOperation
Filter operation.
Definition: DataFilters.h:45
@ GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:46
@ EQUAL
Equal to the value.
Definition: DataFilters.h:47
@ LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:48
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:148
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:355
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:357
An LC-MS feature.
Definition: Feature.h:46
The representation of a chromatogram.
Definition: MSChromatogram.h:30
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:32
A more convenient string class.
Definition: String.h:34
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:104
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Representation of a peak/feature filter combining FilterType, FilterOperation and a value (either dou...
Definition: DataFilters.h:54
DataFilter(const FilterType type, const FilterOperation op, const String &val, const String &meta_name="")
ctor for common case of string filter
Definition: DataFilters.h:61
String toString() const
Returns a string representation of the filter.
bool operator==(const DataFilter &rhs) const
Equality operator.
DataFilter()
Definition: DataFilters.h:55
FilterType field
Field to filter.
Definition: DataFilters.h:65
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:73
bool operator!=(const DataFilter &rhs) const
Inequality operator.
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:71
DataFilter(const FilterType type, const FilterOperation op, const double val, const String &meta_name="")
ctor for common case of numerical filter
Definition: DataFilters.h:57
FilterOperation op
Filter operation.
Definition: DataFilters.h:67
double value
Value for comparison.
Definition: DataFilters.h:69