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

FeaFiModule.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: Clemens Groepl $
00032 // $Authors: Marc Sturm, Clemens Groepl $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
00036 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00039 #include <OpenMS/TRANSFORMATIONS/FEATUREFINDER/FeatureFinderDefs.h>
00040 #include <OpenMS/KERNEL/FeatureMap.h>
00041 #include <OpenMS/KERNEL/MSExperiment.h>
00042 
00043 namespace OpenMS
00044 {
00045 
00046   class FeatureFinder;
00047 
00048   namespace Internal
00049   {
00050     //-------------------------------------------------------------------
00051 
00055     template <typename FeaFiModuleType>
00056     struct IntensityLess :
00057       std::binary_function<typename FeatureFinderDefs::IndexPair, typename FeatureFinderDefs::IndexPair, bool>
00058     {
00060       IntensityLess(const FeaFiModuleType & module) :
00061         module_(module)
00062       {
00063       }
00064 
00066       IntensityLess(const IntensityLess & rhs) :
00067         module_(rhs.module_)
00068       {
00069       }
00070 
00072       inline bool operator()(const typename FeatureFinderDefs::IndexPair & left, const typename FeatureFinderDefs::IndexPair & right) const
00073       {
00074         return module_.getPeakIntensity(left) < module_.getPeakIntensity(right);
00075       }
00076 
00077 private:
00079       const FeaFiModuleType & module_;
00081       IntensityLess();
00082     };     // struct IntensityLess
00083 
00084     //-------------------------------------------------------------------
00085 
00087     template <typename FeaFiModuleType>
00088     struct IntensityIterator :
00089       FeatureFinderDefs::IndexSet::const_iterator
00090     {
00091       IntensityIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
00092         FeatureFinderDefs::IndexSet::const_iterator(iter),
00093         module_(module)
00094       {
00095       }
00096 
00097       typename FeaFiModuleType::IntensityType operator*() const
00098       {
00099         return module_->getPeakIntensity(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00100       }
00101 
00102 protected:
00103       const FeaFiModuleType * module_;
00104     };
00105 
00106     //-------------------------------------------------------------------
00107 
00109     template <typename FeaFiModuleType>
00110     struct MzIterator :
00111       FeatureFinderDefs::IndexSet::const_iterator
00112     {
00113       MzIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
00114         FeatureFinderDefs::IndexSet::const_iterator(iter),
00115         module_(module)
00116       {
00117       }
00118 
00119       typename FeaFiModuleType::IntensityType operator*() const
00120       {
00121         return module_->getPeakMz(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00122       }
00123 
00124 protected:
00125       const FeaFiModuleType * module_;
00126     };
00127 
00128     //-------------------------------------------------------------------
00129 
00131     template <typename FeaFiModuleType>
00132     struct RtIterator :
00133       FeatureFinderDefs::IndexSet::const_iterator
00134     {
00135       RtIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
00136         FeatureFinderDefs::IndexSet::const_iterator(iter),
00137         module_(module)
00138       {
00139       }
00140 
00141       typename FeaFiModuleType::IntensityType operator*() const
00142       {
00143         return module_->getPeakRt(FeatureFinderDefs::IndexSet::const_iterator::operator*());
00144       }
00145 
00146 protected:
00147       const FeaFiModuleType * module_;
00148     };
00149 
00150     //-------------------------------------------------------------------
00151   }   // namespace Internal
00152 
00156   template <class PeakType, class FeatureType>
00157   class FeaFiModule :
00158     public DefaultParamHandler
00159   {
00160 public:
00162     typedef FeatureMap<FeatureType> FeatureMapType;
00164     typedef MSExperiment<PeakType> MapType;
00166     typedef typename MapType::SpectrumType SpectrumType;
00168     typedef typename PeakType::IntensityType IntensityType;
00170     typedef typename PeakType::CoordinateType CoordinateType;
00171 
00173     FeaFiModule(const MSExperiment<PeakType> * map, FeatureMap<FeatureType> * features, FeatureFinder * ff) :
00174       DefaultParamHandler("FeaFiModule"),
00175       map_(0),
00176       features_(0),
00177       ff_(0)
00178     {
00179       map_ = map;
00180       features_ = features;
00181       ff_ = ff;
00182     }
00183 
00185     virtual ~FeaFiModule()
00186     {
00187     }
00188 
00190     inline IntensityType getPeakIntensity(const FeatureFinderDefs::IndexPair & index) const
00191     {
00192       //Corrupt index
00193       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00194       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00195 
00196       return (*map_)[index.first][index.second].getIntensity();
00197     }
00198 
00200     inline CoordinateType getPeakMz(const FeatureFinderDefs::IndexPair & index) const
00201     {
00202       //Corrupt index
00203       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00204       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00205 
00206       return (*map_)[index.first][index.second].getMZ();
00207     }
00208 
00210     inline CoordinateType getPeakRt(const FeatureFinderDefs::IndexPair & index) const
00211     {
00212       //Corrupt index
00213       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00214       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00215 
00216       return (*map_)[index.first].getRT();
00217     }
00218 
00225     inline void getNextMz(FeatureFinderDefs::IndexPair & index) const
00226     {
00227       //Corrupt index
00228       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00229       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00230 
00231       //At the last peak of this spectrum
00232       if (index.second + 1 >= (*map_)[index.first].size())
00233       {
00234         throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextMz", index);
00235       }
00236 
00237       ++index.second;
00238     }
00239 
00246     inline void getPrevMz(FeatureFinderDefs::IndexPair & index) const
00247     {
00248       //Corrupt index
00249       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00250       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00251 
00252       //begin of scan
00253       if (index.second == 0)
00254       {
00255         throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevMz", index);
00256       }
00257 
00258       --index.second;
00259     }
00260 
00267     void getNextRt(FeatureFinderDefs::IndexPair & index)
00268     {
00269       //Corrupt index
00270       OPENMS_PRECONDITION(index.first  < map_->size(), "Scan index outside of map!");
00271       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00272 
00273       CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ();       // mz value we want to find
00274       Size index_first_tmp = index.first;
00275 
00276       ++index.first;
00277       while (index.first < map_->size() &&
00278              (*map_)[index.first].empty())
00279       {
00280         ++index.first;
00281       }
00282       //last scan
00283       if (index.first >= map_->size())
00284       {
00285         throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextRt", index);
00286       }
00287       // now we have a spectrum with scans in it ...
00288 
00289       // perform binary search to find the neighbour in mz dimension
00290       typename SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(), (*map_)[index.first].end(), (*map_)[index_first_tmp][index.second], typename PeakType::PositionLess());
00291 
00292       // if the found peak is at the end of the spectrum, there is not much we can do...
00293       if (it == (*map_)[index.first].end())
00294       {
00295         index.second = (*map_)[index.first].size() - 1;
00296       }
00297       // if the found peak is at the beginning of the spectrum, there is also not much we can do !
00298       else if (it == (*map_)[index.first].begin())
00299       {
00300         index.second = 0;
00301       }
00302       // see if the next smaller one fits better
00303       else
00304       {
00305         // peak to the right is closer (in m/z dimension)
00306         if (it->getMZ() - mz_pos < mz_pos - (it - 1)->getMZ())
00307         {
00308           index.second = it - (*map_)[index.first].begin();
00309         }
00310         else            // left one is closer
00311         {
00312           index.second = --it - (*map_)[index.first].begin();
00313         }
00314       }
00315     }
00316 
00323     void getPrevRt(FeatureFinderDefs::IndexPair & index)
00324     {
00325       //Corrupt index
00326       OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
00327       OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
00328 
00329       // TODO: this seems useless (at least for debug mode) given preconditions above... (and why not in getNextRt()??)
00330       if (index.first >= map_->size())
00331       {
00332         std::cout << "Scan index outside of map!" << std::endl;
00333         std::cout << index.first << " " << index.second << std::endl;
00334         return;
00335       }
00336       if (index.second >= (*map_)[index.first].size())
00337       {
00338         std::cout << "Peak index outside of scan!" << std::endl;
00339         std::cout << index.first << " " << index.second << std::endl;
00340         return;
00341       }
00342 
00343       CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ();
00344       Size index_first_tmp = index.first;
00345 
00346       // first scan
00347       if (index.first == 0)
00348       {
00349         throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevRt", index);
00350       }
00351 
00352       --index.first;
00353       while ((index.first > 0) && ((*map_)[index.first].empty()))
00354       {
00355         --index.first;
00356       }
00357       // we only found an empty scan
00358       if ((*map_)[index.first].empty()) throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevRt", index);
00359 
00360       // perform binary search to find the neighbour in mz dimension
00361 
00362       typename MapType::SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(),
00363                                                                      (*map_)[index.first].end(),
00364                                                                      (*map_)[index_first_tmp][index.second],
00365                                                                      typename PeakType::PositionLess());
00366 
00367       // if the found peak is at the end of the spectrum, there is not much we can do.
00368       if (it == (*map_)[index.first].end())
00369       {
00370         index.second = (*map_)[index.first].size() - 1;
00371       }
00372       // if the found peak is at the beginning of the spectrum, there is not much we can do.
00373       else if (it == (*map_)[index.first].begin())
00374       {
00375         index.second = 0;
00376       }
00377       // see if the next smaller one fits better
00378       else
00379       {
00380         // peak to the right is closer (in m/z dimension)
00381         if (it->getMZ() - mz_pos < mz_pos - (it - 1)->getMZ())
00382         {
00383           index.second = it - (*map_)[index.first].begin();
00384         }
00385         else
00386         {
00387           index.second = --it - (*map_)[index.first].begin();
00388         }
00389       }
00390     }
00391 
00393     void addConvexHull(const FeatureFinderDefs::IndexSet & set, Feature & feature) const
00394     {
00395       std::vector<DPosition<2> > points;
00396       points.reserve(set.size());
00397       DPosition<2> tmp;
00398       for (FeatureFinderDefs::IndexSet::const_iterator it = set.begin(); it != set.end(); ++it)
00399       {
00400         tmp[Peak2D::MZ] = (*map_)[it->first][it->second].getMZ();
00401         tmp[Peak2D::RT] = (*map_)[it->first].getRT();
00402         points.push_back(tmp);
00403       }
00404       feature.getConvexHulls().resize(feature.getConvexHulls().size() + 1);
00405       // computes convex hull
00406       feature.getConvexHulls().back().addPoints(points);
00407     }
00408 
00409 protected:
00411     const MapType * map_;
00413     FeatureMapType * features_;
00415     FeatureFinder * ff_;
00416 
00417 private:
00419     FeaFiModule();
00421     FeaFiModule & operator=(const FeaFiModule &);
00423     FeaFiModule(const FeaFiModule &);
00424 
00425   };   // class FeaFiModule
00426 
00427 } // namespace OpenMS
00428 
00429 #endif // OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H

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