OpenMS  2.7.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-2021.
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 
39 
40 namespace OpenMS
41 {
42  class Feature;
43  class ConsensusFeature;
50  class OPENMS_DLLAPI DataFilters
51  {
52 public:
54 
57  {
61  SIZE,
62  META_DATA
63  };
66  {
70  EXISTS
71  };
72 
74  struct OPENMS_DLLAPI DataFilter
75  {
78  DataFilter(const FilterType type, const FilterOperation op, const double val, const String& meta_name = "")
79  : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
80  {};
82  DataFilter(const FilterType type, const FilterOperation op, const String& val, const String& meta_name = "")
83  : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
84  {};
90  double value{ 0.0 };
96  bool value_is_numerical{ false };
97 
99  String toString() const;
100 
108  void fromString(const String & filter);
109 
111  bool operator==(const DataFilter & rhs) const;
112 
114  bool operator!=(const DataFilter & rhs) const;
115 
116  };
117 
119  Size size() const;
120 
126  const DataFilter & operator[](Size index) const;
127 
129  void add(const DataFilter & filter);
130 
136  void remove(Size index);
137 
143  void replace(Size index, const DataFilter & filter);
144 
146  void clear();
147 
149  void setActive(bool is_active);
150 
157  inline bool isActive() const
158  {
159  return is_active_;
160  }
161 
163  bool passes(const Feature& feature) const;
164 
166  bool passes(const ConsensusFeature& consensus_feature) const;
167 
169  inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
170  {
171  if (!is_active_) return true;
172 
173  for (Size i = 0; i < filters_.size(); i++)
174  {
175  const DataFilters::DataFilter & filter = filters_[i];
176  if (filter.field == INTENSITY)
177  {
178  switch (filter.op)
179  {
180  case GREATER_EQUAL:
181  if (spectrum[peak_index].getIntensity() < filter.value) return false;
182 
183  break;
184 
185  case EQUAL:
186  if (spectrum[peak_index].getIntensity() != filter.value) return false;
187 
188  break;
189 
190  case LESS_EQUAL:
191  if (spectrum[peak_index].getIntensity() > filter.value) return false;
192 
193  break;
194 
195  default:
196  break;
197  }
198  }
199  else if (filter.field == META_DATA)
200  {
201  const typename MSSpectrum::FloatDataArrays & f_arrays = spectrum.getFloatDataArrays();
202  //find the right meta data array
203  SignedSize f_index = -1;
204  for (Size j = 0; j < f_arrays.size(); ++j)
205  {
206  if (f_arrays[j].getName() == filter.meta_name)
207  {
208  f_index = j;
209  break;
210  }
211  }
212  //if it is present, compare it
213  if (f_index != -1)
214  {
215  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
216  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
217  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
218  }
219 
220  //if float array not found, search in integer arrays
221  const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
222  //find the right meta data array
223  SignedSize i_index = -1;
224  for (Size j = 0; j < i_arrays.size(); ++j)
225  {
226  if (i_arrays[j].getName() == filter.meta_name)
227  {
228  i_index = j;
229  break;
230  }
231  }
232  //if it is present, compare it
233  if (i_index != -1)
234  {
235  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
236  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
237  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
238  }
239 
240  //if it is not present, abort
241  if (f_index == -1 && i_index == -1) return false;
242  }
243  }
244  return true;
245  }
246 
247 protected:
249  std::vector<DataFilter> filters_;
251  std::vector<Size> meta_indices_;
252 
255 
257  bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
258 
259  };
260 
261 } //namespace
262 
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:71
DataFilter array providing some convenience functions.
Definition: DataFilters.h:51
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:157
void add(const DataFilter &filter)
Adds a filter.
void replace(Size index, const DataFilter &filter)
Replaces the filter corresponding to index.
void remove(Size index)
Removes the filter corresponding to index.
bool is_active_
Determines if the filters are activated.
Definition: DataFilters.h:254
void setActive(bool is_active)
Enables/disables the all the filters.
FilterType
Information to filter.
Definition: DataFilters.h:57
@ INTENSITY
Filter the intensity value.
Definition: DataFilters.h:58
@ SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:61
@ QUALITY
Filter the overall quality value.
Definition: DataFilters.h:59
@ CHARGE
Filter the charge value.
Definition: DataFilters.h:60
FilterOperation
Filter operation.
Definition: DataFilters.h:66
@ GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:67
@ EQUAL
Equal to the value.
Definition: DataFilters.h:68
@ LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:69
bool passes(const MSSpectrum &spectrum, Size peak_index) const
Returns if the peak fulfills the current filter criteria.
Definition: DataFilters.h:169
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:249
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:251
An LC-MS feature.
Definition: Feature.h:72
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
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< FloatDataArray > FloatDataArrays
Definition: MSSpectrum.h:114
std::vector< IntegerDataArray > IntegerDataArrays
Definition: MSSpectrum.h:120
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:61
A more convenient string class.
Definition: String.h:61
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:47
Representation of a peak/feature filter combining FilterType, FilterOperation and a value (either dou...
Definition: DataFilters.h:75
DataFilter(const FilterType type, const FilterOperation op, const String &val, const String &meta_name="")
ctor for common case of string filter
Definition: DataFilters.h:82
String toString() const
Returns a string representation of the filter.
bool operator==(const DataFilter &rhs) const
Equality operator.
DataFilter()
Definition: DataFilters.h:76
FilterType field
Field to filter.
Definition: DataFilters.h:86
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:94
bool operator!=(const DataFilter &rhs) const
Inequality operator.
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:92
DataFilter(const FilterType type, const FilterOperation op, const double val, const String &meta_name="")
ctor for common case of numerical filter
Definition: DataFilters.h:78
FilterOperation op
Filter operation.
Definition: DataFilters.h:88
double value
Value for comparison.
Definition: DataFilters.h:90