Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2013.
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: Stephan Aiche $
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_DATAREDUCTION_DATAFILTERS_H
36 #define OPENMS_FILTERING_DATAREDUCTION_DATAFILTERS_H
37 
40 
41 #include <iostream>
42 
43 namespace OpenMS
44 {
45  class Feature;
46  class ConsensusFeature;
53  class OPENMS_DLLAPI DataFilters
54  {
55 public:
57  filters_(),
58  meta_indices_(),
59  is_active_(false)
60  {
61  }
62 
65  {
69  SIZE,
70  META_DATA
71  };
74  {
78  EXISTS
79  };
80 
82  struct OPENMS_DLLAPI DataFilter
83  {
86  field(DataFilters::INTENSITY),
87  op(DataFilters::GREATER_EQUAL),
88  value(0.0),
89  value_string(),
90  meta_name(),
91  value_is_numerical(false)
92  {
93  }
94 
107 
109  String toString() const;
110 
118  void fromString(const String & filter);
119 
121  bool operator==(const DataFilter & rhs) const
122  {
123  return field == rhs.field
124  && op == rhs.op
125  && value == rhs.value
126  && value_string == rhs.value_string
127  && meta_name == rhs.meta_name
128  && value_is_numerical == rhs.value_is_numerical;
129  }
130 
132  bool operator!=(const DataFilter & rhs) const
133  {
134  return !operator==(rhs);
135  }
136 
137  };
138 
140  Size size() const;
141 
147  const DataFilter & operator[](Size index) const;
148 
150  void add(const DataFilter & filter);
151 
157  void remove(Size index);
158 
164  void replace(Size index, const DataFilter & filter);
165 
167  void clear();
168 
170  void setActive(bool is_active);
171 
178  inline bool isActive() const
179  {
180  return is_active_;
181  }
182 
184  bool passes(const Feature & feature) const;
185 
187  bool passes(const ConsensusFeature & consensus_feature) const;
188 
190  template <class PeakType>
191  inline bool passes(const MSSpectrum<PeakType> & spectrum, Size peak_index) const
192  {
193  if (!is_active_) return true;
194 
195  for (Size i = 0; i < filters_.size(); i++)
196  {
197  const DataFilters::DataFilter & filter = filters_[i];
198  if (filter.field == INTENSITY)
199  {
200  switch (filter.op)
201  {
202  case GREATER_EQUAL:
203  if (spectrum[peak_index].getIntensity() < filter.value) return false;
204 
205  break;
206 
207  case EQUAL:
208  if (spectrum[peak_index].getIntensity() != filter.value) return false;
209 
210  break;
211 
212  case LESS_EQUAL:
213  if (spectrum[peak_index].getIntensity() > filter.value) return false;
214 
215  break;
216 
217  default:
218  break;
219  }
220  }
221  else if (filter.field == META_DATA)
222  {
223  const typename MSSpectrum<PeakType>::FloatDataArrays & f_arrays = spectrum.getFloatDataArrays();
224  //find the right meta data array
225  SignedSize f_index = -1;
226  for (Size j = 0; j < f_arrays.size(); ++j)
227  {
228  if (f_arrays[j].getName() == filter.meta_name)
229  {
230  f_index = j;
231  break;
232  }
233  }
234  //if it is present, compare it
235  if (f_index != -1)
236  {
237  if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
238  else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
239  else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
240  }
241 
242  //if float array not found, search in integer arrays
243  const typename MSSpectrum<PeakType>::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
244  //find the right meta data array
245  SignedSize i_index = -1;
246  for (Size j = 0; j < i_arrays.size(); ++j)
247  {
248  if (i_arrays[j].getName() == filter.meta_name)
249  {
250  i_index = j;
251  break;
252  }
253  }
254  //if it is present, compare it
255  if (i_index != -1)
256  {
257  if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
258  else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
259  else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
260  }
261 
262  //if it is not present, abort
263  if (f_index == -1 && i_index == -1) return false;
264  }
265  }
266  return true;
267  }
268 
269 protected:
271  std::vector<DataFilter> filters_;
273  std::vector<Size> meta_indices_;
274 
277 
279  inline bool metaPasses_(const MetaInfoInterface & meta_interface, const DataFilters::DataFilter & filter, Size index) const
280  {
281  if (!meta_interface.metaValueExists((UInt)index)) return false;
282  else if (filter.op != EXISTS)
283  {
284  const DataValue & data_value = meta_interface.getMetaValue((UInt)index);
285  if (!filter.value_is_numerical)
286  {
287  if (data_value.valueType() != DataValue::STRING_VALUE) return false;
288  else
289  {
290  // for string values, equality is the only valid operation (besides "exists", see above)
291  if (filter.op != EQUAL) return false;
292  else if (filter.value_string != data_value.toString()) return false;
293  }
294  }
295  else // value_is_numerical
296  {
297  if (data_value.valueType() == DataValue::STRING_VALUE || data_value.valueType() == DataValue::EMPTY_VALUE) return false;
298  else
299  {
300  if (filter.op == EQUAL && (double)data_value != filter.value) return false;
301  else if (filter.op == LESS_EQUAL && (double)data_value > filter.value) return false;
302  else if (filter.op == GREATER_EQUAL && (double)data_value < filter.value) return false;
303  }
304  }
305  }
306  return true;
307  }
308 
309  };
310 
311 } //namespace
312 
313 #endif
const DataValue & getMetaValue(const String &name) const
returns the value corresponding to a string
bool passes(const MSSpectrum< PeakType > &spectrum, Size peak_index) const
Returns if the peak fulfills the current filter criteria.
Definition: DataFilters.h:191
bool operator!=(const DataFilter &rhs) const
Inequality operator.
Definition: DataFilters.h:132
Filter the intensity value.
Definition: DataFilters.h:66
A more convenient string class.
Definition: String.h:56
DataFilters()
Definition: DataFilters.h:56
FilterOperation
Filter operation.
Definition: DataFilters.h:73
bool operator==(const DataFilter &rhs) const
Equality operator.
Definition: DataFilters.h:121
String toString() const
Conversion to String.
DataFilter()
Default constructor.
Definition: DataFilters.h:85
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:151
std::vector< Size > meta_indices_
Vector of meta indices acting as index cache.
Definition: DataFilters.h:273
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
Definition: MSSpectrum.h:294
DoubleReal value
Value for comparison.
Definition: DataFilters.h:100
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:57
FilterType
Information to filter.
Definition: DataFilters.h:64
Representation of a peak/feature filter combining FilterType, FilterOperation and a value...
Definition: DataFilters.h:82
Equal to the value.
Definition: DataFilters.h:76
DataType valueType() const
returns the type of value stored
Definition: DataValue.h:330
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...
Definition: DataFilters.h:279
FilterType field
Field to filter.
Definition: DataFilters.h:96
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:271
string value
Definition: DataValue.h:68
An LC-MS feature.
Definition: Feature.h:66
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:61
Filter the charge value.
Definition: DataFilters.h:68
bool isActive() const
Returns if the filters are enabled.
Definition: DataFilters.h:178
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:102
FilterOperation op
Filter operation.
Definition: DataFilters.h:98
String meta_name
Name of the considered meta information.
Definition: DataFilters.h:104
Filter the overall quality value.
Definition: DataFilters.h:67
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
DataFilter array providing some convenience functions.
Definition: DataFilters.h:53
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
Definition: MSSpectrum.h:270
bool metaValueExists(const String &name) const
returns if this MetaInfo is set
Greater than the value or equal to the value.
Definition: DataFilters.h:75
bool value_is_numerical
Bool value that indicates if the specified value is numerical.
Definition: DataFilters.h:106
bool is_active_
Determines if the filters are activated.
Definition: DataFilters.h:276
Less than the value or equal to the value.
Definition: DataFilters.h:77
A 2-dimensional consensus feature.
Definition: ConsensusFeature.h:59
empty value
Definition: DataValue.h:74
Filter the number of subordinates/elements.
Definition: DataFilters.h:69

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:12 using doxygen 1.8.5