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

MapMatcherRegression.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_MAPMATCHERREGRESSION_H
00029 #define OPENMS_ANALYSIS_MAPMATCHING_MAPMATCHERREGRESSION_H
00030 
00031 #include <OpenMS/KERNEL/Feature.h>
00032 #include <OpenMS/ANALYSIS/MAPMATCHING/LinearMapping.h>
00033 #include <OpenMS/ANALYSIS/MAPMATCHING/BaseMapMatcher.h>
00034 
00035 #include <iostream>
00036 #include <utility>
00037 
00038 #include <gsl/gsl_fit.h>
00039 
00040 #define DEBUG_MAPMATCHING
00041 #undef DEBUG_MAPMATCHING
00042 
00043 namespace OpenMS
00044 {
00045 
00050   template <typename ElementT = Feature >
00051   class MapMatcherRegression
00052     : public BaseMapMatcher<ElementT>
00053   {
00054    public:
00055 
00059 
00060     typedef BaseMapMatcher<ElementT> Base;
00061     typedef typename Base::ElementType ElementType;
00062     typedef typename Base::ElementPairVector ElementPairVector;
00064 
00066     MapMatcherRegression()
00067       : BaseMapMatcher<ElementType>()
00068     {}
00069 
00071     bool operator == (const MapMatcherRegression& rhs)
00072     {
00073       return (BaseMapMatcher<ElementType>::operator == (rhs) );
00074     }
00075 
00077     virtual ~MapMatcherRegression()
00078     {}
00079 
00081     void estimateTransform()
00082     {
00083       for ( typename Grid::iterator grid_iter = this->grid_.begin();
00084             grid_iter != this->grid_.end();
00085             ++grid_iter
00086           )
00087       {
00088         ElementPairVector selection; // stores the pairs contained in the current cell
00089 
00090 #ifdef DEBUG_MAPMATCHING
00091 
00092         std::cout << "Estimate the transformation with : " << this->element_pairs_.size() << " pairs " << std::endl;
00093 #endif
00094 
00095         for (typename ElementPairVector::iterator pair_iter = this->element_pairs_.begin();
00096              pair_iter != this->element_pairs_.end();
00097              ++pair_iter)
00098         {
00099           // check whether the current feature is contained in the cell
00100           // and fulfills our quality requirement.
00101           if (grid_iter->encloses(pair_iter->getFirst().getPosition())
00102               && pair_iter->getQuality() > this->min_quality_ )
00103           {
00104             selection.push_back(*pair_iter);
00105 
00106 #ifdef DEBUG_MAPMATCHING
00107 //            std::cout << "Pair " << pair_iter->first.getPosition() << " " << pair_iter->second.getPosition() << std::endl;
00108 #endif
00109 
00110           }
00111         } // end for (pair_iter)
00112 
00113     // build arrays
00114         int num = selection.size();
00115         if (num > 2)
00116         {
00117 
00118           double* x = new double[num];
00119           double* y = new double[num];
00120 
00121           grid_iter->getMappings().clear();
00122 
00123           // loop over all dimensions
00124           for (UInt d=0; d<2;d++)
00125           {
00126 
00127             for (int i=0; i<num;i++)
00128             {
00129               x[i] = selection[i].getFirst().getPosition()[d];
00130               y[i] = selection[i].getSecond().getPosition()[d];
00131             }
00132 
00133             // estimate the transform for this dimension
00134             double slope, intercept, cov00, cov01, cov11, sumsq;
00135 
00136             gsl_fit_linear(x, 1, y, 1, num, &intercept, &slope, &cov00, &cov01, &cov11, &sumsq);
00137 
00138 #ifdef DEBUG_MAPMATCHING
00139 
00140             std::cout << "Estimating transform for dimension " << d << std::endl;
00141             std::cout << "Best fit: Y = " << intercept << " + " << slope << "* X" << std::endl;
00142             std::cout << "Sumsquares: " << sumsq << std::endl;
00143             std::cout << "Pearson: " << (cov01*cov01)/(cov00*cov11) << std::endl;
00144 #endif
00145 
00146             // create the transform and save it in the cell
00147             LinearMapping tmp_mapping;
00148             tmp_mapping.setSlope(slope);
00149             tmp_mapping.setIntercept(intercept);
00150             grid_iter->getMappings().push_back(tmp_mapping);
00151 
00152           } // end for (d)
00153 
00154           delete [] x;
00155           delete [] y;
00156 
00157         } // end for (gridcell)
00158       }
00159 
00160     } // end void estimateTransform()
00161 
00162   }
00163   ; // end of class MapMatcherRegression
00164 
00165 } // end of namespace OpenMS
00166 
00167 #endif  // OPENMS_ANALYSIS_MAPMATCHING_MAPMATCHERREGRESSION_H

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