OpenMS
Loading...
Searching...
No Matches
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
14
15namespace OpenMS
16{
17 class Feature;
18 class ConsensusFeature;
25 class OPENMS_DLLAPI DataFilters
26 {
27public:
28 DataFilters() = default;
29
31 bool operator==(const DataFilters&) const = default;
32
35 {
40 META_DATA
41 };
50
52 struct OPENMS_DLLAPI DataFilter
53 {
56 DataFilter(const FilterType type, const FilterOperation op, const double val, const std::string& meta_name = "")
57 : field(type), op(op), value(val), value_string(), meta_name(meta_name), value_is_numerical(true)
58 {};
60 DataFilter(const FilterType type, const FilterOperation op, const std::string& val, const std::string& meta_name = "")
61 : field(type), op(op), value(0.0), value_string(val), meta_name(meta_name), value_is_numerical(false)
62 {};
64 FilterType field{ DataFilters::INTENSITY };
66 FilterOperation op{ DataFilters::GREATER_EQUAL} ;
68 double value{ 0.0 };
70 std::string value_string;
72 std::string meta_name;
74 bool value_is_numerical{ false };
75
77 std::string toString() const;
78
86 void fromString(const std::string & filter);
87
89 bool operator==(const DataFilter & rhs) const;
90
92 bool operator!=(const DataFilter & rhs) const;
93
94 };
95
97 Size size() const;
98
104 const DataFilter & operator[](Size index) const;
105
107 void add(const DataFilter & filter);
108
114 void remove(Size index);
115
121 void replace(Size index, const DataFilter & filter);
122
124 void clear();
125
127 void setActive(bool is_active);
128
135 inline bool isActive() const
136 {
137 return is_active_;
138 }
139
141 bool passes(const Feature& feature) const;
142
144 bool passes(const ConsensusFeature& consensus_feature) const;
145
147 inline bool passes(const MSSpectrum& spectrum, Size peak_index) const
148 {
149 if (!is_active_) return true;
150
151 for (Size i = 0; i < filters_.size(); i++)
152 {
153 const DataFilters::DataFilter & filter = filters_[i];
154 if (filter.field == INTENSITY)
155 {
156 switch (filter.op)
157 {
158 case GREATER_EQUAL:
159 if (spectrum[peak_index].getIntensity() < filter.value) return false;
160
161 break;
162
163 case EQUAL:
164 if (spectrum[peak_index].getIntensity() != filter.value) return false;
165
166 break;
167
168 case LESS_EQUAL:
169 if (spectrum[peak_index].getIntensity() > filter.value) return false;
170
171 break;
172
173 default:
174 break;
175 }
176 }
177 else if (filter.field == META_DATA)
178 {
179 const auto& f_arrays = spectrum.getFloatDataArrays();
180 //find the right meta data array
181 SignedSize f_index = -1;
182 for (Size j = 0; j < f_arrays.size(); ++j)
183 {
184 if (f_arrays[j].getName() == filter.meta_name)
185 {
186 f_index = j;
187 break;
188 }
189 }
190 //if it is present, compare it
191 if (f_index != -1)
192 {
193 if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
194 else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
195 else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
196 }
197
198 //if float array not found, search in integer arrays
199 const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
200 //find the right meta data array
201 SignedSize i_index = -1;
202 for (Size j = 0; j < i_arrays.size(); ++j)
203 {
204 if (i_arrays[j].getName() == filter.meta_name)
205 {
206 i_index = j;
207 break;
208 }
209 }
210 //if it is present, compare it
211 if (i_index != -1)
212 {
213 if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
214 else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
215 else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
216 }
217
218 //if it is not present, abort
219 if (f_index == -1 && i_index == -1) return false;
220 }
221 }
222 return true;
223 }
224
226 inline bool passes(const MSChromatogram& chrom, Size peak_index) const
227 {
228 if (!is_active_) return true;
229
230 for (Size i = 0; i < filters_.size(); i++)
231 {
232 const DataFilters::DataFilter& filter = filters_[i];
233 if (filter.field == INTENSITY)
234 {
235 switch (filter.op)
236 {
237 case GREATER_EQUAL:
238 if (chrom[peak_index].getIntensity() < filter.value)
239 return false;
240
241 break;
242
243 case EQUAL:
244 if (chrom[peak_index].getIntensity() != filter.value)
245 return false;
246
247 break;
248
249 case LESS_EQUAL:
250 if (chrom[peak_index].getIntensity() > filter.value)
251 return false;
252
253 break;
254
255 default:
256 break;
257 }
258 }
259 else if (filter.field == META_DATA)
260 {
261 const auto& f_arrays = chrom.getFloatDataArrays();
262 // find the right meta data array
263 SignedSize f_index = -1;
264 for (Size j = 0; j < f_arrays.size(); ++j)
265 {
266 if (f_arrays[j].getName() == filter.meta_name)
267 {
268 f_index = j;
269 break;
270 }
271 }
272 // if it is present, compare it
273 if (f_index != -1)
274 {
275 if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
276 else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
277 else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
278 }
279
280 // if float array not found, search in integer arrays
281 const typename MSSpectrum::IntegerDataArrays& i_arrays = chrom.getIntegerDataArrays();
282 // find the right meta data array
283 SignedSize i_index = -1;
284 for (Size j = 0; j < i_arrays.size(); ++j)
285 {
286 if (i_arrays[j].getName() == filter.meta_name)
287 {
288 i_index = j;
289 break;
290 }
291 }
292 // if it is present, compare it
293 if (i_index != -1)
294 {
295 if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
296 else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
297 else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
298 }
299
300 // if it is not present, abort
301 if (f_index == -1 && i_index == -1) return false;
302 }
303 }
304 return true;
305 }
306
308 inline bool passes(const Mobilogram& mobilogram, Size peak_index) const
309 {
310 if (!is_active_) {
311 return true;
312 }
313
314
315 for (Size i = 0; i < filters_.size(); i++)
316 {
317 const DataFilters::DataFilter& filter = filters_[i];
318 if (filter.field == INTENSITY)
319 {
320 switch (filter.op)
321 {
322 case GREATER_EQUAL:
323 if (mobilogram[peak_index].getIntensity() < filter.value)
324 return false;
325
326 break;
327
328 case EQUAL:
329 if (mobilogram[peak_index].getIntensity() != filter.value)
330 return false;
331
332 break;
333
334 case LESS_EQUAL:
335 if (mobilogram[peak_index].getIntensity() > filter.value)
336 return false;
337
338 break;
339
340 default:
341 break;
342 }
343 }
344 else if (filter.field == META_DATA)
345 { // no metadata arrays so far...
346 return false;
347 }
348 }
349 return true;
350 }
351
352 protected:
354 std::vector<DataFilter> filters_;
356 std::vector<Size> meta_indices_;
357
359 bool is_active_ = false;
360
362 bool metaPasses_(const MetaInfoInterface& meta_interface, const DataFilters::DataFilter& filter, Size index) const;
363 };
364
365} //namespace
366
A consensus feature spanning multiple LC-MS/MS experiments.
Definition ConsensusFeature.h:45
DataFilter array providing some convenience functions.
Definition DataFilters.h:26
const DataFilter & operator[](Size index) const
Filter accessor.
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:135
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:308
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:226
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:35
@ INTENSITY
Filter the intensity value.
Definition DataFilters.h:36
@ SIZE
Filter the number of subordinates/elements.
Definition DataFilters.h:39
@ QUALITY
Filter the overall quality value.
Definition DataFilters.h:37
@ CHARGE
Filter the charge value.
Definition DataFilters.h:38
FilterOperation
Filter operation.
Definition DataFilters.h:44
@ GREATER_EQUAL
Greater than the value or equal to the value.
Definition DataFilters.h:45
@ EQUAL
Equal to the value.
Definition DataFilters.h:46
@ LESS_EQUAL
Less than the value or equal to the value.
Definition DataFilters.h:47
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:147
std::vector< DataFilter > filters_
Array of DataFilters.
Definition DataFilters.h:354
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.
std::vector< Size > meta_indices_
Vector of meta indices acting as index cache.
Definition DataFilters.h:356
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:112
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
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:53
DataFilter(const FilterType type, const FilterOperation op, const std::string &val, const std::string &meta_name="")
ctor for common case of string filter
Definition DataFilters.h:60
std::string toString() const
Returns a string representation of the filter.
bool operator==(const DataFilter &rhs) const
Equality operator.
DataFilter()
Definition DataFilters.h:54
DataFilter(const FilterType type, const FilterOperation op, const double val, const std::string &meta_name="")
ctor for common case of numerical filter
Definition DataFilters.h:56
void fromString(const std::string &filter)
Parses filter and sets the filter properties accordingly.
FilterType field
Field to filter.
Definition DataFilters.h:64
bool operator!=(const DataFilter &rhs) const
Inequality operator.
std::string meta_name
Name of the considered meta information (key)
Definition DataFilters.h:72
std::string value_string
std::string value for comparison (for meta data)
Definition DataFilters.h:70
FilterOperation op
Filter operation.
Definition DataFilters.h:66
double value
Value for comparison.
Definition DataFilters.h:68