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

SimplePairFinder.h (Maintainer: Eva Lange)

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: Eva Lange $
00025 // --------------------------------------------------------------------------
00026 
00027 
00028 #ifndef OPENMS_ANALYSIS_MAPMATCHING_SIMPLEPAIRFINDER_H
00029 #define OPENMS_ANALYSIS_MAPMATCHING_SIMPLEPAIRFINDER_H
00030 
00031 #include <OpenMS/ANALYSIS/MAPMATCHING/BasePairFinder.h>
00032 
00033 #define V_SimplePairFinder(bla) // std::cout << bla << std::endl;
00034 
00035 namespace OpenMS
00036 {
00037 
00050   template < typename MapT = FeatureMap< > >
00051   class SimplePairFinder : public BasePairFinder< MapT >
00052   {
00053    public:
00057     enum Maps
00058       {
00059         MODEL = 0,
00060         SCENE = 1
00061       };
00062 
00063     typedef BasePairFinder< MapT > Base;
00064     //typedef typename Base::TraitsType             TraitsType;
00065 
00066     typedef typename Base::QualityType QualityType;
00067     typedef typename Base::PositionType           PositionType;
00068     typedef typename Base::IntensityType          IntensityType;
00069     typedef typename Base::PointType              PointType;
00070     typedef typename Base::PointMapType           PointMapType;
00071     typedef typename Base::ElementPairType        ElementPairType;
00072     typedef typename Base::ElementPairVectorType  ElementPairVectorType;
00073     typedef typename Base::TransformationType     TransformationType;
00074 
00075     using Base::param_;
00076     using Base::defaults_;
00077     using Base::setName;
00078     using Base::element_map_;
00079     using Base::element_pairs_;
00080     using Base::transformation_;
00081 
00083     SimplePairFinder()
00084       : Base()
00085     {
00086       //set the name for DefaultParamHandler error messages
00087       setName(getProductName());
00088       
00089       defaults_.setValue("similarity:diff_intercept:RT",1.0,"This parameter controls the asymptotic decay rate for large differences (for more details see the similarity measurement).",true);
00090       defaults_.setValue("similarity:diff_intercept:MZ",0.1,"This parameter controls the asymptotic decay rate for large differences (for more details see the similarity measurement).",true);
00091       defaults_.setValue("similarity:diff_exponent:RT",2.0,"This parameter is important for small differences (for more details see the similarity measurement).",true);
00092       defaults_.setValue("similarity:diff_exponent:MZ",1.0,"This parameter is important for small differences (for more details see the similarity measurement).",true);
00093       defaults_.setValue("similarity:pair_min_quality",0.01,"Minimum required pair quality.",true);
00094 
00095       Base::defaultsToParam_();
00096     }
00097 
00099     virtual ~SimplePairFinder()
00100     {
00101     }
00102 
00104     static BasePairFinder<PointMapType>* create()
00105     {
00106       return new SimplePairFinder();
00107     }
00108 
00110     static const String getProductName()
00111     {
00112       return "SimplePairFinder";
00113     }
00114 
00116     double getDiffExponent(UInt dim)
00117     {
00118       return diff_exponent_[dim];
00119     }
00120 
00122     void setDiffExponent(UInt dim, DoubleReal exponent)
00123     {
00124       diff_exponent_[dim] = exponent;
00125       String param_name_prefix = "similarity:diff_exponent:";
00126       String param_name = param_name_prefix + RawDataPoint2D::shortDimensionName(dim);
00127       param_.setValue(param_name, exponent);
00128     }
00129 
00131     double getDiffIntercept(UInt dim)
00132     {
00133       return diff_intercept_[dim];
00134     }
00135 
00137     void setDiffIntercept(UInt dim, DoubleReal intercept)
00138     {
00139       diff_intercept_[dim] = intercept;
00140       param_.setValue(String("similarity:diff_intercept:") + RawDataPoint2D::shortDimensionName(dim), intercept);
00141     }
00142 
00144     double getPairMinQuality()
00145     {
00146       return pair_min_quality_;
00147     }
00148 
00150     void setPairMinQuality(DoubleReal quality)
00151     {
00152       pair_min_quality_ = quality;
00153       param_.setValue("similarity:pair_min_quality", quality);
00154     }
00155 
00161     virtual void findElementPairs()
00162     {
00163 #define V_findElementPairs(bla) V_SimplePairFinder(bla)
00164       UInt n = element_map_[SCENE]->size();
00165 
00166       transformed_positions_second_map_.clear();
00167       transformed_positions_second_map_.resize(n);
00168 
00169       for (UInt i = 0; i < n; ++i)
00170       {
00171         transformed_positions_second_map_[i] = (*element_map_[SCENE])[i].getPosition();
00172       }
00173 
00174       V_findElementPairs("SimplePairFinder::run(): apply transformation");
00175 
00176       for ( UInt dim = 0; dim < 2; ++dim )
00177       {
00178         for (UInt i = 0; i < n; ++i)
00179         {
00180           transformation_[dim].apply( transformed_positions_second_map_[i][dim] );
00181         }
00182       }
00183 
00184       V_findElementPairs("SimplePairFinder::run(): find element pairs" << pair_min_quality_);
00185 
00186       // progress dots
00187       Int progress_dots = 0;
00188       if (this->param_.exists("debug::progress_dots"))
00189       {
00190         progress_dots = (Int)this->param_.getValue("debug:progress_dots");
00191       }
00192       Int number_of_considered_element_pairs = 0;
00193 
00194       // For each element in map 0, find his/her best friend in map 1
00195       std::vector<UInt>        best_companion_index_0(element_map_[MODEL]->size(),UInt(-1));
00196       std::vector<QualityType> best_companion_quality_0(element_map_[MODEL]->size(),0);
00197       for ( UInt fi0 = 0; fi0 < element_map_[MODEL]->size(); ++fi0 )
00198       {
00199         QualityType best_quality = -std::numeric_limits<QualityType>::max();
00200         for ( UInt fi1 = 0; fi1 < element_map_[SCENE]->size(); ++ fi1 )
00201         {
00202           QualityType quality = similarity_( (*element_map_[MODEL])[fi0], (*element_map_[SCENE])[fi1], transformed_positions_second_map_[fi1]);
00203           if ( quality > best_quality )
00204           {
00205             best_quality = quality;
00206             best_companion_index_0[fi0] = fi1;
00207           }
00208 
00209           ++number_of_considered_element_pairs;
00210           if ( progress_dots &&
00211                ! (number_of_considered_element_pairs % progress_dots)
00212              )
00213           {
00214             std::cout << '-' << std::flush;
00215           }
00216 
00217         }
00218         best_companion_quality_0[fi0] = best_quality;
00219       }
00220 
00221       // For each element in map 1, find his/her best friend in map 0
00222       std::vector<UInt>        best_companion_index_1(element_map_[SCENE]->size(),UInt(-1));
00223       std::vector<QualityType> best_companion_quality_1(element_map_[SCENE]->size(),0);
00224       for ( UInt fi1 = 0; fi1 < element_map_[SCENE]->size(); ++fi1 )
00225       {
00226         QualityType best_quality = -std::numeric_limits<QualityType>::max();
00227         for ( UInt fi0 = 0; fi0 < element_map_[MODEL]->size(); ++ fi0 )
00228         {
00229           QualityType quality = similarity_( (*element_map_[MODEL])[fi0], (*element_map_[SCENE])[fi1], transformed_positions_second_map_[fi1]);
00230           if ( quality > best_quality )
00231           {
00232             best_quality = quality;
00233             best_companion_index_1[fi1] = fi0;
00234           }
00235 
00236           ++number_of_considered_element_pairs;
00237           if ( progress_dots &&
00238                ! (number_of_considered_element_pairs % progress_dots)
00239              )
00240           {
00241             std::cout << '+' << std::flush;
00242           }
00243 
00244         }
00245         best_companion_quality_1[fi1] = best_quality;
00246       }
00247 
00248       // And if both like each other, they become a pair.
00249       // element_pairs_->clear();
00250       for ( UInt fi0 = 0; fi0 < element_map_[MODEL]->size(); ++fi0 )
00251       {
00252         // fi0 likes someone ...
00253         if ( best_companion_quality_0[fi0] > pair_min_quality_ )
00254         {
00255           // ... who likes him too ...
00256           UInt best_companion_of_fi0 = best_companion_index_0[fi0];
00257           if ( best_companion_index_1[best_companion_of_fi0] == fi0 &&
00258                best_companion_quality_1[best_companion_of_fi0] > pair_min_quality_
00259              )
00260           {
00261             element_pairs_->push_back( ElementPairType ( (*element_map_[SCENE])[best_companion_of_fi0],
00262                                                          (*element_map_[MODEL])[fi0],
00263                                                          best_companion_quality_0[fi0] + best_companion_quality_1[best_companion_of_fi0]
00264                                                        ));
00265           }
00266         }
00267       }
00268 
00269 #undef V_findElementPairs
00270 
00271     } // findElementPairs
00272 
00273 
00274    protected:
00275     virtual void updateMembers_()
00276     {
00277       diff_intercept_[RawDataPoint2D::RT] = (QualityType)param_.getValue("similarity:diff_intercept:RT");
00278       diff_intercept_[RawDataPoint2D::MZ] = (QualityType)param_.getValue("similarity:diff_intercept:MZ");
00279       diff_exponent_[RawDataPoint2D::RT] = (QualityType)param_.getValue("similarity:diff_exponent:RT");
00280       diff_exponent_[RawDataPoint2D::MZ] = (QualityType)param_.getValue("similarity:diff_exponent:MZ");
00281       pair_min_quality_ = (QualityType)param_.getValue("similarity:pair_min_quality");
00282     }
00283 
00285     QualityType diff_exponent_[2];
00286 
00288     QualityType diff_intercept_[2];
00289 
00291     QualityType pair_min_quality_;
00292 
00294     std::vector<PositionType> transformed_positions_second_map_;
00295 
00318     QualityType similarity_ ( PointType const & left, PointType const & right, const PositionType& new_position) const
00319     {
00320       QualityType right_intensity(right.getIntensity());
00321       if ( right_intensity == 0 )
00322         return 0;
00323       QualityType intensity_ratio = left.getIntensity() / right_intensity;
00324       if ( intensity_ratio > 1. )
00325         intensity_ratio = 1. / intensity_ratio;
00326 
00327       // if the right map is the transformed map, take the transformed right position
00328       PositionType position_difference = left.getPosition() - new_position;
00329 
00330       for ( UInt dimension = 0; dimension < 2; ++dimension )
00331       {
00332         // Take the absolute value
00333         if ( position_difference[dimension] < 0 )
00334         {
00335           position_difference[dimension] = -position_difference[dimension];
00336         }
00337         // Raise the difference to a (potentially fractional) power
00338         position_difference[dimension] =
00339           pow(position_difference[dimension],diff_exponent_[dimension]);
00340         // Add an absolute number
00341         position_difference[dimension] += diff_intercept_[dimension];
00342       }
00343 
00344       return intensity_ratio / position_difference[RawDataPoint2D::RT] / position_difference[RawDataPoint2D::MZ];
00345     }
00346 
00347   }
00348   ; // SimplePairFinder
00349 
00350 } // namespace OpenMS
00351 
00352 #endif  // OPENMS_ANALYSIS_MAPMATCHING_SimplePairFinder_H

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