OpenMS  3.0.0
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-2022.
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 
40 
41 namespace OpenMS
42 {
43  class Feature;
44  class ConsensusFeature;
51  class OPENMS_DLLAPI DataFilters
52  {
53 public:
54  DataFilters();
55 
58  {
62  SIZE,
63  META_DATA
64  };
67  {
71  EXISTS
72  };
73 
75  struct OPENMS_DLLAPI DataFilter
76  {
79  DataFilter(const FilterType type, const FilterOperation op, const double val, const String& meta_name = "")
80  : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
81  {};
83  DataFilter(const FilterType type, const FilterOperation op, const String& val, const String& meta_name = "")
84  : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
85  {};
91  double value{ 0.0 };
97  bool value_is_numerical{ false };
98 
100  String toString() const;
101 
109  void fromString(const String & filter);
110 
112  bool operator==(const DataFilter & rhs) const;
113 
115  bool operator!=(const DataFilter & rhs) const;
116 
117  };
118 
120  Size size() const;
121 
127  const DataFilter & operator[](Size index) const;
128 
130  void add(const DataFilter & filter);
131 
137  void remove(Size index);
138 
144  void replace(Size index, const DataFilter & filter);
145 
147  void clear();
148 
150  void setActive(bool is_active);
151 
158  inline bool isActive() const
159  {
160  return is_active_;
161  }
162 
164  bool passes(const Feature& feature) const;
165 
167  bool passes(const ConsensusFeature& consensus_feature) const;
168 
170  inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
171  {
172  if (!is_active_) return true;
173 
174  for (Size i = 0; i < filters_.size(); i++)
175  {
176  const DataFilters::DataFilter & filter = filters_[i];
177  if (filter.field == INTENSITY)
178  {
179  switch (filter.op)
180  {
181  case GREATER_EQUAL:
182  if (spectrum[peak_index].getIntensity() < filter.value) return false;
183 
184  break;
185 
186  case EQUAL:
187  if (spectrum[peak_index].getIntensity() != filter.value) return false;
188 
189  break;
190 
191  case LESS_EQUAL:
192  if (spectrum[peak_index].getIntensity() > filter.value) return false;
193 
194  break;
195 
196  default:
197  break;
198  }
199  }
200  else if (filter.field == META_DATA)
201  {
202  const auto& f_arrays = spectrum.getFloatDataArrays();
203  //find the right meta data array
204  SignedSize f_index = -1;
205  for (Size j = 0; j < f_arrays.size(); ++j)
206  {
207  if (f_arrays[j].getName() == filter.meta_name)
208  {
209  f_index = j;
210  break;
211  }
212  }
213  //if it is present, compare it
214  if (f_index != -1)
215  {
216  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
217  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
218  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
219  }
220 
221  //if float array not found, search in integer arrays
222  const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
223  //find the right meta data array
224  SignedSize i_index = -1;
225  for (Size j = 0; j < i_arrays.size(); ++j)
226  {
227  if (i_arrays[j].getName() == filter.meta_name)
228  {
229  i_index = j;
230  break;
231  }
232  }
233  //if it is present, compare it
234  if (i_index != -1)
235  {
236  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
237  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
238  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
239  }
240 
241  //if it is not present, abort
242  if (f_index == -1 && i_index == -1) return false;
243  }
244  }
245  return true;
246  }
247 
249  inline bool passes(const MSChromatogram& chrom, Size peak_index) const
250  {
251  if (!is_active_) return true;
252 
253  for (Size i = 0; i < filters_.size(); i++)
254  {
255  const DataFilters::DataFilter& filter = filters_[i];
256  if (filter.field == INTENSITY)
257  {
258  switch (filter.op)
259  {
260  case GREATER_EQUAL:
261  if (chrom[peak_index].getIntensity() < filter.value)
262  return false;
263 
264  break;
265 
266  case EQUAL:
267  if (chrom[peak_index].getIntensity() != filter.value)
268  return false;
269 
270  break;
271 
272  case LESS_EQUAL:
273  if (chrom[peak_index].getIntensity() > filter.value)
274  return false;
275 
276  break;
277 
278  default:
279  break;
280  }
281  }
282  else if (filter.field == META_DATA)
283  {
284  const auto& f_arrays = chrom.getFloatDataArrays();
285  // find the right meta data array
286  SignedSize f_index = -1;
287  for (Size j = 0; j < f_arrays.size(); ++j)
288  {
289  if (f_arrays[j].getName() == filter.meta_name)
290  {
291  f_index = j;
292  break;
293  }
294  }
295  // if it is present, compare it
296  if (f_index != -1)
297  {
298  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
299  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
300  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
301  }
302 
303  // if float array not found, search in integer arrays
304  const typename MSSpectrum::IntegerDataArrays& i_arrays = chrom.getIntegerDataArrays();
305  // find the right meta data array
306  SignedSize i_index = -1;
307  for (Size j = 0; j < i_arrays.size(); ++j)
308  {
309  if (i_arrays[j].getName() == filter.meta_name)
310  {
311  i_index = j;
312  break;
313  }
314  }
315  // if it is present, compare it
316  if (i_index != -1)
317  {
318  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
319  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
320  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
321  }
322 
323  // if it is not present, abort
324  if (f_index == -1 && i_index == -1) return false;
325  }
326  }
327  return true;
328  }
329 
330 protected:
332  std::vector<DataFilter> filters_;
334  std::vector<Size> meta_indices_;
335 
338 
340  bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
341  };
342 
343 } //namespace
344 
OpenMS::DataFilters::DataFilter::field
FilterType field
Field to filter.
Definition: DataFilters.h:87
OpenMS::DataFilters::DataFilter::value_string
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:93
OpenMS::DataFilters::passes
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:249
OpenMS::toString
const std::string & toString(const DriftTimeUnit value)
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::DataFilters::DataFilter
Representation of a peak/feature filter combining FilterType, FilterOperation and a value (either dou...
Definition: DataFilters.h:75
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::DataFilters::DataFilter::op
FilterOperation op
Filter operation.
Definition: DataFilters.h:89
OpenMS::DataFilters::QUALITY
Filter the overall quality value.
Definition: DataFilters.h:60
OpenMS::DataFilters::DataFilter::value
double value
Value for comparison.
Definition: DataFilters.h:91
OpenMS::DataFilters::DataFilter::DataFilter
DataFilter(const FilterType type, const FilterOperation op, const double val, const String &meta_name="")
ctor for common case of numerical filter
Definition: DataFilters.h:79
OpenMS::DataFilters::is_active_
bool is_active_
Determines if the filters are activated.
Definition: DataFilters.h:337
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::DataFilters::FilterType
FilterType
Information to filter.
Definition: DataFilters.h:57
OpenMS::DataFilters::DataFilter::DataFilter
DataFilter(const FilterType type, const FilterOperation op, const String &val, const String &meta_name="")
ctor for common case of string filter
Definition: DataFilters.h:83
OpenMS::StringUtils::remove
static String & remove(String &this_s, char what)
Definition: StringUtilsSimple.h:595
OpenMS::DataFilters::INTENSITY
Filter the intensity value.
Definition: DataFilters.h:59
OpenMS::DataFilters::FilterOperation
FilterOperation
Filter operation.
Definition: DataFilters.h:66
OpenMS::MetaInfoInterface
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:60
OpenMS::DataFilters::passes
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:170
OpenMS::ConsensusFeature
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
OpenMS::MSChromatogram::getIntegerDataArrays
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
OpenMS::MSSpectrum::IntegerDataArrays
std::vector< IntegerDataArray > IntegerDataArrays
Definition: MSSpectrum.h:122
OpenMS::DataFilters::EQUAL
Equal to the value.
Definition: DataFilters.h:69
OpenMS::MSSpectrum::getFloatDataArrays
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
OpenMS::MSChromatogram::getFloatDataArrays
const FloatDataArrays & getFloatDataArrays() const
OpenMS::DataFilters::DataFilter::meta_name
String meta_name
Name of the considered meta information (key)
Definition: DataFilters.h:95
OpenMS::SignedSize
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
OpenMS::DataFilters
DataFilter array providing some convenience functions.
Definition: DataFilters.h:51
OpenMS::Internal::operator==
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
MSChromatogram.h
OpenMS::MSChromatogram
The representation of a chromatogram.
Definition: MSChromatogram.h:53
String.h
OpenMS::DataFilters::DataFilter::DataFilter
DataFilter()
Definition: DataFilters.h:77
OpenMS::DataFilters::isActive
bool isActive() const
Returns if the filters are enabled.
Definition: DataFilters.h:158
OpenMS::DataFilters::GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:68
OpenMS::DataFilters::CHARGE
Filter the charge value.
Definition: DataFilters.h:61
OpenMS::DataFilters::SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:62
OpenMS::MSSpectrum::getIntegerDataArrays
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
OpenMS::DataFilters::meta_indices_
std::vector< Size > meta_indices_
Vector of meta indices acting as index cache.
Definition: DataFilters.h:334
OpenMS::DataFilters::filters_
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:332
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
OpenMS::DataFilters::LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:70
MSSpectrum.h