| OpenMS 2.5.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-2020.
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
37 #include <OpenMS/DATASTRUCTURES/String.h>
38 #include <OpenMS/KERNEL/MSSpectrum.h>
39
40 namespace OpenMS
41 {
42 class Feature;
43 class ConsensusFeature;
50 class OPENMS_DLLAPI DataFilters
51 {
52 public:
53 DataFilters();
54
56 enum FilterType
57 {
58 INTENSITY,
59 QUALITY,
60 CHARGE,
61 SIZE,
62 META_DATA
63 };
65 enum FilterOperation
66 {
67 GREATER_EQUAL,
68 EQUAL,
69 LESS_EQUAL,
70 EXISTS
71 };
72
74 struct OPENMS_DLLAPI DataFilter
75 {
77 DataFilter();
78
80 FilterType field;
82 FilterOperation op;
84 double value;
86 String value_string;
88 String meta_name;
90 bool value_is_numerical;
91
93 String toString() const;
94
102 void fromString(const String & filter);
103
105 bool operator==(const DataFilter & rhs) const;
106
108 bool operator!=(const DataFilter & rhs) const;
109
110 };
111
113 Size size() const;
114
120 const DataFilter & operator[](Size index) const;
121
123 void add(const DataFilter & filter);
124
130 void remove(Size index);
131
137 void replace(Size index, const DataFilter & filter);
138
140 void clear();
141
143 void setActive(bool is_active);
144
151 inline bool isActive() const
152 {
153 return is_active_;
154 }
155
157 bool passes(const Feature & feature) const;
158
160 bool passes(const ConsensusFeature & consensus_feature) const;
161
163 inline bool passes(const MSSpectrum & spectrum, Size peak_index) const
164 {
165 if (!is_active_) return true;
166
167 for (Size i = 0; i < filters_.size(); i++)
168 {
169 const DataFilters::DataFilter & filter = filters_[i];
170 if (filter.field == INTENSITY)
171 {
172 switch (filter.op)
173 {
174 case GREATER_EQUAL:
175 if (spectrum[peak_index].getIntensity() < filter.value) return false;
176
177 break;
178
179 case EQUAL:
180 if (spectrum[peak_index].getIntensity() != filter.value) return false;
181
182 break;
183
184 case LESS_EQUAL:
185 if (spectrum[peak_index].getIntensity() > filter.value) return false;
186
187 break;
188
189 default:
190 break;
191 }
192 }
193 else if (filter.field == META_DATA)
194 {
195 const typename MSSpectrum::FloatDataArrays & f_arrays = spectrum.getFloatDataArrays();
196 //find the right meta data array
197 SignedSize f_index = -1;
198 for (Size j = 0; j < f_arrays.size(); ++j)
199 {
200 if (f_arrays[j].getName() == filter.meta_name)
201 {
202 f_index = j;
203 break;
204 }
205 }
206 //if it is present, compare it
207 if (f_index != -1)
208 {
209 if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
210 else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
211 else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
212 }
213
214 //if float array not found, search in integer arrays
215 const typename MSSpectrum::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
216 //find the right meta data array
217 SignedSize i_index = -1;
218 for (Size j = 0; j < i_arrays.size(); ++j)
219 {
220 if (i_arrays[j].getName() == filter.meta_name)
221 {
222 i_index = j;
223 break;
224 }
225 }
226 //if it is present, compare it
227 if (i_index != -1)
228 {
229 if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
230 else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
231 else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
232 }
233
234 //if it is not present, abort
235 if (f_index == -1 && i_index == -1) return false;
236 }
237 }
238 return true;
239 }
240
241 protected:
243 std::vector<DataFilter> filters_;
245 std::vector<Size> meta_indices_;
246
248 bool is_active_;
249
251 inline bool metaPasses_(const MetaInfoInterface & meta_interface, const DataFilters::DataFilter & filter, Size index) const
252 {
253 if (!meta_interface.metaValueExists((UInt)index)) return false;
254 else if (filter.op != EXISTS)
255 {
256 const DataValue & data_value = meta_interface.getMetaValue((UInt)index);
257 if (!filter.value_is_numerical)
258 {
259 if (data_value.valueType() != DataValue::STRING_VALUE) return false;
260 else
261 {
262 // for string values, equality is the only valid operation (besides "exists", see above)
263 if (filter.op != EQUAL) return false;
264 else if (filter.value_string != data_value.toString()) return false;
265 }
266 }
267 else // value_is_numerical
268 {
269 if (data_value.valueType() == DataValue::STRING_VALUE || data_value.valueType() == DataValue::EMPTY_VALUE) return false;
270 else
271 {
272 if (filter.op == EQUAL && (double)data_value != filter.value) return false;
273 else if (filter.op == LESS_EQUAL && (double)data_value > filter.value) return false;
274 else if (filter.op == GREATER_EQUAL && (double)data_value < filter.value) return false;
275 }
276 }
277 }
278 return true;
279 }
280
281 };
282
283 } //namespace
284
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::SignedSize
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:134
OpenMS::DataFilters::DataFilter::op
FilterOperation op
Filter operation.
Definition: DataFilters.h:82
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::DataValue
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
OpenMS::MetaInfoInterface::getMetaValue
const DataValue & getMetaValue(const String &name, const DataValue &default_value=DataValue::EMPTY) const
Returns the value corresponding to a string, or a default value (default: DataValue::EMPTY) if not fo...
OpenMS::DataValue::toString
String toString(bool full_precision=true) const
Conversion to String full_precision Controls number of fractional digits for all double types or list...
OpenMS::MSSpectrum::getFloatDataArrays
const FloatDataArrays & getFloatDataArrays() const
Returns a const reference to the float meta data arrays.
OpenMS::MSSpectrum::getIntegerDataArrays
const IntegerDataArrays & getIntegerDataArrays() const
Returns a const reference to the integer meta data arrays.
OpenMS::DataFilters::DataFilter::field
FilterType field
Field to filter.
Definition: DataFilters.h:80
OpenMS::MSSpectrum::IntegerDataArrays
std::vector< IntegerDataArray > IntegerDataArrays
Definition: MSSpectrum.h:96
OpenMS::DataFilters::filters_
std::vector< DataFilter > filters_
Array of DataFilters.
Definition: DataFilters.h:243
OpenMS::StringConversions::toString
String toString(const T &i)
fallback template for general purpose using Boost::Karma; more specializations below ...
Definition: StringUtils.h:85
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
OpenMS::DataFilters::DataFilter
Representation of a peak/feature filter combining FilterType, FilterOperation and a value...
Definition: DataFilters.h:74
OpenMS::ConsensusFeature
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
OpenMS::DataFilters::FilterOperation
FilterOperation
Filter operation.
Definition: DataFilters.h:65
OpenMS::DataFilters::DataFilter::value_is_numerical
bool value_is_numerical
Bool value that indicates if the specified value is numerical.
Definition: DataFilters.h:90
OpenMS::DataValue::valueType
DataType valueType() const
returns the type of value stored
Definition: DataValue.h:365
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
OpenMS::DataFilters::metaPasses_
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:251
OpenMS::DataFilters::SIZE
Filter the number of subordinates/elements.
Definition: DataFilters.h:61
String.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::DataFilters::is_active_
bool is_active_
Determines if the filters are activated.
Definition: DataFilters.h:248
OpenMS::DataFilters::DataFilter::value
double value
Value for comparison.
Definition: DataFilters.h:84
MSSpectrum.h
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::DataFilters::CHARGE
Filter the charge value.
Definition: DataFilters.h:60
OpenMS::DataValue::STRING_VALUE
string value
Definition: DataValue.h:67
OpenMS::DataValue::EMPTY_VALUE
empty value
Definition: DataValue.h:73
OpenMS::MSSpectrum::FloatDataArrays
std::vector< FloatDataArray > FloatDataArrays
Definition: MSSpectrum.h:90
OpenMS::DataFilters::DataFilter::value_string
String value_string
String value for comparison (for meta data)
Definition: DataFilters.h:86
OpenMS::DataFilters::isActive
bool isActive() const
Returns if the filters are enabled.
Definition: DataFilters.h:151
OpenMS::DataFilters::EQUAL
Equal to the value.
Definition: DataFilters.h:68
OpenMS::DataFilters::QUALITY
Filter the overall quality value.
Definition: DataFilters.h:59
OpenMS::DataFilters::passes
bool passes(const MSSpectrum &spectrum, Size peak_index) const
Returns if the peak fulfills the current filter criteria.
Definition: DataFilters.h:163
OpenMS::DataFilters::DataFilter::meta_name
String meta_name
Name of the considered meta information.
Definition: DataFilters.h:88
OpenMS::DataFilters::INTENSITY
Filter the intensity value.
Definition: DataFilters.h:58
OpenMS::DataFilters::meta_indices_
std::vector< Size > meta_indices_
Vector of meta indices acting as index cache.
Definition: DataFilters.h:245
OpenMS::DataFilters::LESS_EQUAL
Less than the value or equal to the value.
Definition: DataFilters.h:69
OpenMS::MetaInfoInterface
Interface for classes that can store arbitrary meta information (Type-Name-Value tuples).
Definition: MetaInfoInterface.h:60
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 array providing some convenience functions.
Definition: DataFilters.h:50
OpenMS::DataFilters::GREATER_EQUAL
Greater than the value or equal to the value.
Definition: DataFilters.h:67
KDTree::operator==
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
OpenMS::MetaInfoInterface::metaValueExists
bool metaValueExists(const String &name) const
Returns whether an entry with the given name exists.
OpenMS::DataFilters::FilterType
FilterType
Information to filter.
Definition: DataFilters.h:56