Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_FILTERING_DATAREDUCTION_DATAFILTERS_H
00036 #define OPENMS_FILTERING_DATAREDUCTION_DATAFILTERS_H
00037
00038 #include <OpenMS/DATASTRUCTURES/String.h>
00039 #include <OpenMS/KERNEL/MSSpectrum.h>
00040
00041 #include <iostream>
00042
00043 namespace OpenMS
00044 {
00045 class Feature;
00046 class ConsensusFeature;
00053 class OPENMS_DLLAPI DataFilters
00054 {
00055 public:
00056 DataFilters() :
00057 filters_(),
00058 meta_indices_(),
00059 is_active_(false)
00060 {
00061 }
00062
00064 enum FilterType
00065 {
00066 INTENSITY,
00067 QUALITY,
00068 CHARGE,
00069 SIZE,
00070 META_DATA
00071 };
00073 enum FilterOperation
00074 {
00075 GREATER_EQUAL,
00076 EQUAL,
00077 LESS_EQUAL,
00078 EXISTS
00079 };
00080
00082 struct OPENMS_DLLAPI DataFilter
00083 {
00085 DataFilter() :
00086 field(DataFilters::INTENSITY),
00087 op(DataFilters::GREATER_EQUAL),
00088 value(0.0),
00089 value_string(),
00090 meta_name(),
00091 value_is_numerical(false)
00092 {
00093 }
00094
00096 FilterType field;
00098 FilterOperation op;
00100 DoubleReal value;
00102 String value_string;
00104 String meta_name;
00106 bool value_is_numerical;
00107
00109 String toString() const;
00110
00118 void fromString(const String & filter);
00119
00121 bool operator==(const DataFilter & rhs) const
00122 {
00123 return field == rhs.field
00124 && op == rhs.op
00125 && value == rhs.value
00126 && value_string == rhs.value_string
00127 && meta_name == rhs.meta_name
00128 && value_is_numerical == rhs.value_is_numerical;
00129 }
00130
00132 bool operator!=(const DataFilter & rhs) const
00133 {
00134 return !operator==(rhs);
00135 }
00136
00137 };
00138
00140 Size size() const;
00141
00147 const DataFilter & operator[](Size index) const;
00148
00150 void add(const DataFilter & filter);
00151
00157 void remove(Size index);
00158
00164 void replace(Size index, const DataFilter & filter);
00165
00167 void clear();
00168
00170 void setActive(bool is_active);
00171
00178 inline bool isActive() const
00179 {
00180 return is_active_;
00181 }
00182
00184 bool passes(const Feature & feature) const;
00185
00187 bool passes(const ConsensusFeature & consensus_feature) const;
00188
00190 template <class PeakType>
00191 inline bool passes(const MSSpectrum<PeakType> & spectrum, Size peak_index) const
00192 {
00193 if (!is_active_) return true;
00194
00195 for (Size i = 0; i < filters_.size(); i++)
00196 {
00197 const DataFilters::DataFilter & filter = filters_[i];
00198 if (filter.field == INTENSITY)
00199 {
00200 switch (filter.op)
00201 {
00202 case GREATER_EQUAL:
00203 if (spectrum[peak_index].getIntensity() < filter.value) return false;
00204
00205 break;
00206
00207 case EQUAL:
00208 if (spectrum[peak_index].getIntensity() != filter.value) return false;
00209
00210 break;
00211
00212 case LESS_EQUAL:
00213 if (spectrum[peak_index].getIntensity() > filter.value) return false;
00214
00215 break;
00216
00217 default:
00218 break;
00219 }
00220 }
00221 else if (filter.field == META_DATA)
00222 {
00223 const typename MSSpectrum<PeakType>::FloatDataArrays & f_arrays = spectrum.getFloatDataArrays();
00224
00225 SignedSize f_index = -1;
00226 for (Size j = 0; j < f_arrays.size(); ++j)
00227 {
00228 if (f_arrays[j].getName() == filter.meta_name)
00229 {
00230 f_index = j;
00231 break;
00232 }
00233 }
00234
00235 if (f_index != -1)
00236 {
00237 if (filter.op == EQUAL && f_arrays[f_index][peak_index] != filter.value) return false;
00238 else if (filter.op == LESS_EQUAL && f_arrays[f_index][peak_index] > filter.value) return false;
00239 else if (filter.op == GREATER_EQUAL && f_arrays[f_index][peak_index] < filter.value) return false;
00240 }
00241
00242
00243 const typename MSSpectrum<PeakType>::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
00244
00245 SignedSize i_index = -1;
00246 for (Size j = 0; j < i_arrays.size(); ++j)
00247 {
00248 if (i_arrays[j].getName() == filter.meta_name)
00249 {
00250 i_index = j;
00251 break;
00252 }
00253 }
00254
00255 if (i_index != -1)
00256 {
00257 if (filter.op == EQUAL && i_arrays[i_index][peak_index] != filter.value) return false;
00258 else if (filter.op == LESS_EQUAL && i_arrays[i_index][peak_index] > filter.value) return false;
00259 else if (filter.op == GREATER_EQUAL && i_arrays[i_index][peak_index] < filter.value) return false;
00260 }
00261
00262
00263 if (f_index == -1 && i_index == -1) return false;
00264 }
00265 }
00266 return true;
00267 }
00268
00269 protected:
00271 std::vector<DataFilter> filters_;
00273 std::vector<Size> meta_indices_;
00274
00276 bool is_active_;
00277
00279 inline bool metaPasses_(const MetaInfoInterface & meta_interface, const DataFilters::DataFilter & filter, Size index) const
00280 {
00281 if (!meta_interface.metaValueExists((UInt)index)) return false;
00282 else if (filter.op != EXISTS)
00283 {
00284 const DataValue & data_value = meta_interface.getMetaValue((UInt)index);
00285 if (!filter.value_is_numerical)
00286 {
00287 if (data_value.valueType() != DataValue::STRING_VALUE) return false;
00288 else
00289 {
00290
00291 if (filter.op != EQUAL) return false;
00292 else if (filter.value_string != data_value.toString()) return false;
00293 }
00294 }
00295 else
00296 {
00297 if (data_value.valueType() == DataValue::STRING_VALUE || data_value.valueType() == DataValue::EMPTY_VALUE) return false;
00298 else
00299 {
00300 if (filter.op == EQUAL && (double)data_value != filter.value) return false;
00301 else if (filter.op == LESS_EQUAL && (double)data_value > filter.value) return false;
00302 else if (filter.op == GREATER_EQUAL && (double)data_value < filter.value) return false;
00303 }
00304 }
00305 }
00306 return true;
00307 }
00308
00309 };
00310
00311 }
00312
00313 #endif