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

FeaFiModule.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
00028 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
00029 
00030 #include <OpenMS/CONCEPT/Types.h>
00031 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00032 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/FeatureFinderDefs.h>
00033 
00034 namespace OpenMS
00035 {
00036 
00037   class FeatureFinder;
00038 
00039   namespace Internal
00040   {
00041     //-------------------------------------------------------------------
00042     
00044     template<typename FeaFiModuleType> struct IntensityLess :
00045       std::binary_function< typename FeatureFinderDefs::IndexPair, typename FeatureFinderDefs::IndexPair, bool>
00046     {
00048       IntensityLess(const FeaFiModuleType& module) :
00049         module_(module)
00050       {
00051       }
00052 
00054       IntensityLess(const IntensityLess& rhs) :
00055         module_(rhs.module_)
00056       {
00057       }
00058 
00060       inline bool operator() ( const typename FeatureFinderDefs::IndexPair& left, const typename FeatureFinderDefs::IndexPair& right) const
00061       {
00062         return module_.getPeakIntensity(left) < module_.getPeakIntensity(right);
00063       }
00064 
00065       private:
00067       const FeaFiModuleType& module_;
00069       IntensityLess();
00070     }; // struct IntensityLess
00071 
00072     //-------------------------------------------------------------------
00073     
00075     template<typename FeaFiModuleType> struct IntensityIterator :
00076       FeatureFinderDefs::IndexSet::const_iterator
00077     {
00078       IntensityIterator(const FeatureFinderDefs::IndexSet::const_iterator& iter, const FeaFiModuleType* module ) :
00079         FeatureFinderDefs::IndexSet::const_iterator(iter),
00080         module_(module)
00081       {
00082       }
00083 
00084       typename FeaFiModuleType::IntensityType operator*() const throw()
00085       {
00086         return module_->getPeakIntensity(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00087       }
00088 
00089       protected:
00090       const FeaFiModuleType* module_;
00091     };
00092 
00093     //-------------------------------------------------------------------
00094     
00096     template<typename FeaFiModuleType> struct MzIterator :
00097       FeatureFinderDefs::IndexSet::const_iterator
00098     {
00099       MzIterator(const FeatureFinderDefs::IndexSet::const_iterator& iter, const FeaFiModuleType* module ) :
00100         FeatureFinderDefs::IndexSet::const_iterator(iter),
00101         module_(module)
00102       {
00103       }
00104 
00105       typename FeaFiModuleType::IntensityType operator*() const throw()
00106       {
00107         return module_->getPeakMz(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00108       }
00109 
00110       protected:
00111       const FeaFiModuleType* module_;
00112     };
00113 
00114     //-------------------------------------------------------------------
00115     
00117     template<typename FeaFiModuleType> struct RtIterator :
00118       FeatureFinderDefs::IndexSet::const_iterator
00119     {
00120       RtIterator(const FeatureFinderDefs::IndexSet::const_iterator& iter, const FeaFiModuleType* module ) :
00121         FeatureFinderDefs::IndexSet::const_iterator(iter),
00122         module_(module)
00123       {
00124       }
00125 
00126       typename FeaFiModuleType::IntensityType operator*() const throw()
00127       {
00128         return module_->getPeakRt(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00129       }
00130 
00131       protected:
00132       const FeaFiModuleType* module_;
00133     };
00134 
00135     //-------------------------------------------------------------------
00136   } // namespace Internal
00137 
00143   template<class PeakType, class FeatureType>
00144     class FeaFiModule 
00145     : public DefaultParamHandler
00146     {
00147       public:
00149         typedef FeatureMap<FeatureType> FeatureMapType;
00151         typedef MSExperiment<PeakType> MapType;
00153         typedef typename MapType::SpectrumType SpectrumType;
00155         typedef typename PeakType::IntensityType IntensityType;
00157         typedef typename PeakType::CoordinateType CoordinateType;
00158 
00160         FeaFiModule(const MSExperiment<PeakType>* map, FeatureMap<FeatureType>* features, FeatureFinder* ff)
00161           : DefaultParamHandler("FeaFiModule"), 
00162           map_(0),
00163           features_(0),
00164           ff_(0)
00165           {
00166             map_ = map;
00167             features_ = features;
00168             ff_ = ff;
00169           }
00170 
00172         virtual ~FeaFiModule()
00173         {
00174         }
00175 
00177         inline IntensityType getPeakIntensity(const FeatureFinderDefs::IndexPair& index) const
00178         { 
00179           //Corrupt index
00180           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00181           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00182 
00183           return (*map_)[index.first][index.second].getIntensity(); 
00184         }
00185         
00187         inline CoordinateType getPeakMz(const FeatureFinderDefs::IndexPair& index) const
00188         { 
00189           //Corrupt index
00190           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00191           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00192 
00193           return (*map_)[index.first][index.second].getMZ(); 
00194         }
00195         
00197         inline CoordinateType getPeakRt(const FeatureFinderDefs::IndexPair& index) const
00198         { 
00199           //Corrupt index
00200           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00201           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00202 
00203           return (*map_)[index.first].getRT();
00204         }
00205 
00207         inline void getNextMz(FeatureFinderDefs::IndexPair& index) const throw (FeatureFinderDefs::NoSuccessor, Exception::Precondition)
00208         {
00209           //Corrupt index
00210           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00211           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00212 
00213           //At the last peak of this spectrum
00214           if (index.second==(*map_)[index.first].size()-1)
00215           {
00216             throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextMz", index);
00217           }
00218 
00219           ++index.second;
00220         }
00221 
00223         inline void getPrevMz(FeatureFinderDefs::IndexPair& index) const throw (FeatureFinderDefs::NoSuccessor, Exception::Precondition)
00224         {
00225           //Corrupt index
00226           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00227           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00228 
00229           //begin of scan
00230           if (index.second==0)
00231           {
00232             throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevMz", index);
00233           }
00234 
00235           --index.second;
00236         }
00237 
00239         void getNextRt(FeatureFinderDefs::IndexPair& index) throw (FeatureFinderDefs::NoSuccessor, Exception::Precondition)
00240         {
00241           //Corrupt index
00242           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00243           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00244 
00245           //last scan
00246           if (index.first == map_->size()-1)
00247           {
00248             throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextRt", index);
00249           }
00250 
00251           // perform binary search to find the neighbour in rt dimension
00252           CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ(); // mz value we want to find
00253           ++index.first;
00254           typename SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(), (*map_)[index.first].end(), (*map_)[index.first-1][index.second], typename PeakType::PositionLess()); 
00255 
00256           // if the found peak is at the end of the spectrum, there is not much we can do...
00257           if ( it == (*map_)[index.first].end() )
00258           {
00259             // check for empty scans
00260             if ( (*map_)[index.first].size() > 0 )
00261               index.second = (*map_)[index.first].size()-1;
00262             else
00263               index.second = 0;
00264           }
00265           // if the found peak is at the beginning of the spectrum, there is also not much we can do ! 
00266           else if ( it == (*map_)[index.first].begin() ) 
00267           {
00268             index.second = 0;
00269           }
00270           // see if the next smaller one fits better
00271           else 
00272           { 
00273             // peak to the right is closer (in m/z dimension)
00274             if (it->getMZ() - mz_pos < mz_pos - (it-1)->getMZ() )
00275             {       
00276               index.second = it - (*map_)[index.first].begin(); 
00277             }
00278             else  // left one is closer
00279             {
00280               index.second = --it - (*map_)[index.first].begin(); 
00281             }
00282           }
00283         }
00284 
00286         void getPrevRt(FeatureFinderDefs::IndexPair& index) throw (FeatureFinderDefs::NoSuccessor, Exception::Precondition)
00287         {
00288           //Corrupt index
00289           OPENMS_PRECONDITION(index.first<map_->size(), "Scan index outside of map!");
00290           OPENMS_PRECONDITION(index.second<(*map_)[index.first].size(), "Peak index outside of scan!");
00291 
00292           if (index.first>=map_->size() )
00293           {
00294             std::cout << "Scan index outside of map!" << std::endl;
00295             std::cout << index.first << " " << index.second << std::endl;
00296             return;
00297           }
00298           if (index.second>=(*map_)[index.first].size())
00299           {
00300             std::cout << "Peak index outside of scan!" << std::endl;
00301             std::cout << index.first << " " << index.second << std::endl;
00302             return;   
00303           } 
00304 
00305           // first scan
00306           if (index.first == 0)
00307           {
00308             throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevRt", index);
00309           }
00310 
00311           // perform binary search to find the neighbour in rt dimension
00312           CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ();
00313           --index.first;
00314           typename MapType::SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(), 
00315               (*map_)[index.first].end(), 
00316               (*map_)[index.first+1][index.second], 
00317               typename PeakType::PositionLess()); 
00318 
00319           // if the found peak is at the end of the spectrum, there is not much we can do.
00320           if ( it == (*map_)[index.first].end() )
00321           {
00322             // check for empty scans
00323             if ( (*map_)[index.first].size() > 0 )
00324               index.second = (*map_)[index.first].size()-1;
00325             else
00326               index.second = 0;
00327           }
00328           // if the found peak is at the beginning of the spectrum, there is not much we can do.
00329           else if ( it == (*map_)[index.first].begin() ) 
00330           {
00331             index.second = 0;
00332           }
00333           // see if the next smaller one fits better
00334           else 
00335           { 
00336             // peak to the right is closer (in m/z dimension)
00337             if (it->getMZ() - mz_pos < mz_pos - (it-1)->getMZ() )
00338             {
00339               index.second = it - (*map_)[index.first].begin(); 
00340             }
00341             else
00342             {
00343               index.second = --it - (*map_)[index.first].begin(); 
00344             }
00345           }
00346         }
00347 
00349         void addConvexHull(const FeatureFinderDefs::IndexSet& set, Feature& feature) const
00350         {
00351           std::vector< DPosition<2> > points;
00352           points.reserve(set.size());
00353           DPosition<2> tmp;
00354           for (FeatureFinderDefs::IndexSet::const_iterator it=set.begin(); it!=set.end(); ++it)
00355           {
00356             tmp[RawDataPoint2D::MZ] = (*map_)[it->first][it->second].getMZ();
00357             tmp[RawDataPoint2D::RT] = (*map_)[it->first].getRT();
00358             points.push_back(tmp);
00359           }
00360           feature.getConvexHulls().resize(feature.getConvexHulls().size()+1);
00361           // assignment operator computes convex hull
00362           feature.getConvexHulls().back() = points; 
00363         }
00364 
00365       protected:
00367         const MapType* map_;
00369         FeatureMapType* features_;
00371         FeatureFinder* ff_;
00372 
00373       private:
00375         FeaFiModule();
00377         FeaFiModule& operator=(const FeaFiModule&);
00379         FeaFiModule(const FeaFiModule&);
00380 
00381     }; // class FeaFiModule
00382 
00383 } // namespace OpenMS
00384 
00385 #endif // OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H

Generated Tue Apr 1 15:36:34 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1