Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DataFilters.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Stephan Aiche $
00032 // $Authors: Marc Sturm $
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           //find the right meta data array
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           //if it is present, compare it
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           //if float array not found, search in integer arrays
00243           const typename MSSpectrum<PeakType>::IntegerDataArrays & i_arrays = spectrum.getIntegerDataArrays();
00244           //find the right meta data array
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           //if it is present, compare it
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           //if it is not present, abort
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             // for string values, equality is the only valid operation (besides "exists", see above)
00291             if (filter.op != EQUAL) return false;
00292             else if (filter.value_string != data_value.toString()) return false;
00293           }
00294         }
00295         else             // value_is_numerical
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 } //namespace
00312 
00313 #endif

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:38 using doxygen 1.7.1