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

PoseClusteringAffineSuperimposer.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_POSECLUSTERINGAFFINESUPERIMPOSER_H
00029 #define OPENMS_ANALYSIS_MAPMATCHING_POSECLUSTERINGAFFINESUPERIMPOSER_H
00030 
00031 #include <OpenMS/KERNEL/DPeakConstReferenceArray.h>
00032 #include <OpenMS/DATASTRUCTURES/DBoundingBox.h>
00033 #include <OpenMS/DATASTRUCTURES/String.h>
00034 #include <OpenMS/ANALYSIS/MAPMATCHING/LinearMapping.h>
00035 #include <OpenMS/ANALYSIS/MAPMATCHING/BaseSuperimposer.h>
00036 
00037 #include <fstream>
00038 #include <vector>
00039 #include <map>
00040 #include <math.h>
00041 
00042 #define V_PoseClusteringAffineSuperimposer(bla) //  std::cout << bla << std::endl;
00043 
00044 namespace OpenMS
00045 {
00046 
00062   template < typename MapT = FeatureMap<> >
00063   class PoseClusteringAffineSuperimposer
00064         : public BaseSuperimposer< MapT >
00065   {
00066   public:
00067 
00068     enum HashMap
00069     {
00070       SHIFT = 0,
00071       SCALING = 1
00072     };
00073 
00077     enum Maps
00078     {
00079       MODEL = 0,
00080       SCENE = 1
00081     };
00082 
00083     typedef BaseSuperimposer< MapT > Base;
00084 
00085   protected:
00086 
00093   public:
00094     typedef typename Base::QualityType QualityType;
00095     typedef typename Base::PositionType PositionType;
00096     typedef typename Base::IntensityType IntensityType;
00097     typedef typename Base::PointType PointType;
00098     typedef typename Base::PointMapType PointMapType;
00099     typedef typename PositionType::CoordinateType CoordinateType;
00100     typedef DPeakConstReferenceArray< PointMapType > PeakPointerArray;
00101     typedef DBoundingBox<2>  PositionBoundingBoxType;
00102     typedef DBoundingBox<1> IntensityBoundingBoxType;
00103     typedef LinearMapping AffineTransformationType;
00104     typedef std::pair<int,int> PairType;
00105     typedef std::map< PairType, QualityType> AffineTransformationMapType;
00106 
00107     using Base::param_;
00108     using Base::setParameters;
00109     using Base::getParameters;
00110     using Base::subsections_;
00111     using Base::defaultsToParam_;
00112     using Base::defaults_;
00113     using Base::element_map_;
00114     using Base::final_transformation_;
00115 
00117     PoseClusteringAffineSuperimposer()
00118         : Base()
00119     {
00120       Base::setName(getProductName());
00121 
00122       defaults_.setValue("tuple_search:mz_bucket_size",.5,"An estimate of m/z deviation of corresponding elements in different maps.");
00123       defaults_.setValue("transformation_space:shift_bucket_size:RT",10.0,"Defines the shift parameter's bucket size during histograming.",true);
00124       defaults_.setValue("transformation_space:shift_bucket_size:MZ",0.01,"Defines the shift parameter's bucket size during histograming.",true);
00125       defaults_.setValue("transformation_space:scaling_bucket_size:RT",0.01,"Defines the scaling parameter's bucket size during histograming.",true);
00126       defaults_.setValue("transformation_space:scaling_bucket_size:MZ",0.01,"Defines the scaling parameter's bucket size during histograming.",true);
00127       defaults_.setValue("transformation_space:bucket_window_shift:RT",2,"Number of surrounding buckets of element indices to be considered when computing the shift parameter.",true);
00128       defaults_.setValue("transformation_space:bucket_window_shift:MZ",2,"Number of surrounding buckets of element indices to be considered when computing the shift parameter.",true);
00129       defaults_.setValue("transformation_space:bucket_window_scaling:RT",2,"Number of surrounding buckets of element indices to be considered when computing the scaling parameter.",true);
00130       defaults_.setValue("transformation_space:bucket_window_scaling:MZ",2,"Number of surrounding buckets of element indices to be considered when computing the scaling parameter.",true);
00131       defaults_.setValue("transformation_space:min_shift:RT",-1000.0,"Minimal shift parameter which is considered during histogramming.",true);
00132       defaults_.setValue("transformation_space:min_shift:MZ",-5.0,"Minimal shift parameter which is considered during histogramming.",true);
00133       defaults_.setValue("transformation_space:max_shift:RT",1000.0,"Maximal shift parameter which is considered during histogramming.",true);
00134       defaults_.setValue("transformation_space:max_shift:MZ",5.0,"Maximal shift parameter which is considered during histogramming.",true);
00135       defaults_.setValue("transformation_space:min_scaling:RT",0.5,"Minimal scaling parameter which is considered during histogramming.",true);
00136       defaults_.setValue("transformation_space:min_scaling:MZ",0.8,"Minimal scaling parameter which is considered during histogramming.",true);
00137       defaults_.setValue("transformation_space:max_scaling:RT",2.0,"Maximal scaling parameter which is considered during histogramming.",true);
00138       defaults_.setValue("transformation_space:max_scaling:MZ",1.2,"Maximal scaling parameter which is considered during histogramming.",true);
00139 
00140       defaultsToParam_();
00141     }
00142 
00144     virtual ~PoseClusteringAffineSuperimposer()
00145     {
00146       V_PoseClusteringAffineSuperimposer("~PoseClusteringAffineSuperimposer");
00147     }
00148 
00150     virtual void run()
00151     {
00152       if ( !this->element_map_[MODEL]->empty() && !this->element_map_[SCENE]->empty() )
00153       {
00154         // compute total intensities of both maps for normalisation
00155         IntensityType total_int_model_map = 0;
00156         UInt n = element_map_[MODEL]->size();
00157         for (UInt i = 0; i < n; ++i)
00158         {
00159           total_int_model_map += (*element_map_[MODEL])[i].getIntensity();
00160         }
00161 
00162         IntensityType total_int_scene_map = 0;
00163         n = element_map_[SCENE]->size();
00164         for (UInt i = 0; i < n; ++i)
00165         {
00166           total_int_scene_map += (*element_map_[SCENE])[i].getIntensity();
00167         }
00168         // clear scene_map_partners_
00169         scene_map_partners_.clear();
00170         // clear model_map_red_
00171         model_map_red_.clear();
00172         // clear rt hash
00173         rt_hash_.clear();
00174         // clear mz hash
00175         mz_hash_.clear();
00176 
00177 
00178 
00179         preprocess_();
00180         hashAffineTransformations_(total_int_model_map,total_int_scene_map);
00181         estimateFinalAffineTransformation_();
00182       }
00183       else
00184       {
00185         std::cerr << "PoseClusteringAffineSuperimposer::run():  Oops, one of the element maps is empty!\n";
00186       }
00187     }
00188 
00190     static BaseSuperimposer<PointMapType>* create()
00191     {
00192       return new PoseClusteringAffineSuperimposer();
00193     }
00194 
00196     static const String getProductName()
00197     {
00198       return "poseclustering_affine";
00199     }
00200 
00202     void setMzBucketSize(double mz_bucket_size)
00203     {
00204       mz_bucket_size_ = mz_bucket_size;
00205       param_.setValue("tuple_search:mz_bucket_size", mz_bucket_size);
00206       std::cout << "Mz bucket size " << mz_bucket_size_ << std::endl;
00207     }
00208 
00210     double getMzBucketSize() const
00211     {
00212       return mz_bucket_size_;
00213     }
00214 
00216     void setShiftBucketSize(UInt dim, double shift_bucket_size)
00217     {
00218       shift_bucket_size_[dim] = shift_bucket_size;
00219       param_.setValue(String("transformation_space:shift_bucket_size:") + RawDataPoint2D::shortDimensionName(dim), shift_bucket_size);
00220     }
00221 
00223     double getShiftBucketSize(UInt dim) const
00224     {
00225       return shift_bucket_size_[dim];
00226     }
00227 
00229     void setScalingBucketSize(UInt dim, double scaling_bucket_size)
00230     {
00231       scaling_bucket_size_[dim] = scaling_bucket_size;
00232       param_.setValue(String("transformation_space:scaling_bucket_size:") + RawDataPoint2D::shortDimensionName(dim), scaling_bucket_size);
00233     }
00234 
00236     double getScalingBucketSize(UInt dim) const
00237     {
00238       return scaling_bucket_size_[dim];
00239     }
00240 
00242     void setBucketWindowShift(UInt dim, UInt bucket_window_shift)
00243     {
00244       bucket_window_shift_[dim] = bucket_window_shift;
00245       param_.setValue(String("transformation_space:bucket_window_shift:") + RawDataPoint2D::shortDimensionName(dim), bucket_window_shift);
00246     }
00247 
00249     UInt getBucketWindowShift(UInt dim) const
00250     {
00251       return bucket_window_shift_[dim];
00252     }
00253 
00255     void setBucketWindowScaling(UInt dim, UInt bucket_window_scaling)
00256     {
00257       bucket_window_scaling_[dim] = bucket_window_scaling;
00258       param_.setValue(String("transformation_space:bucket_window_scaling:") + RawDataPoint2D::shortDimensionName(dim), bucket_window_scaling);
00259     }
00260 
00262     UInt getBucketWindowScaling(UInt dim) const
00263     {
00264       return bucket_window_scaling_[dim];
00265     }
00266 
00267 
00268   protected:
00269     virtual void updateMembers_()
00270     {
00271       mz_bucket_size_ = (CoordinateType)param_.getValue("tuple_search:mz_bucket_size");
00272       shift_bucket_size_[0] = (CoordinateType)param_.getValue("transformation_space:shift_bucket_size:RT");
00273       shift_bucket_size_[1] = (CoordinateType)param_.getValue("transformation_space:shift_bucket_size:MZ");
00274       scaling_bucket_size_[0] = (CoordinateType)param_.getValue("transformation_space:scaling_bucket_size:RT");
00275       scaling_bucket_size_[1] = (CoordinateType)param_.getValue("transformation_space:scaling_bucket_size:MZ");
00276       bucket_window_shift_[0]  = (UInt)param_.getValue("transformation_space:bucket_window_shift:RT");
00277       bucket_window_shift_[1]  = (UInt)param_.getValue("transformation_space:bucket_window_shift:MZ");
00278       bucket_window_scaling_[0] = (UInt)param_.getValue("transformation_space:bucket_window_scaling:RT");
00279       bucket_window_scaling_[1] = (UInt)param_.getValue("transformation_space:bucket_window_scaling:MZ");
00280 
00281       PositionType min;
00282       PositionType max;
00283       min[RawDataPoint2D::RT] = (CoordinateType)param_.getValue("transformation_space:min_shift:RT");
00284       min[RawDataPoint2D::MZ] = (CoordinateType)param_.getValue("transformation_space:min_shift:MZ");
00285       max[RawDataPoint2D::RT] = (CoordinateType)param_.getValue("transformation_space:max_shift:RT");
00286       max[RawDataPoint2D::MZ] = (CoordinateType)param_.getValue("transformation_space:max_shift:MZ");
00287 
00288       shift_bounding_box_.setMin(min);
00289       shift_bounding_box_.setMax(max);
00290 
00291       min[RawDataPoint2D::RT] = (CoordinateType)param_.getValue("transformation_space:min_scaling:RT");
00292       min[RawDataPoint2D::MZ] = (CoordinateType)param_.getValue("transformation_space:min_scaling:MZ");
00293       max[RawDataPoint2D::RT] = (CoordinateType)param_.getValue("transformation_space:max_scaling:RT");
00294       max[RawDataPoint2D::MZ] = (CoordinateType)param_.getValue("transformation_space:max_scaling:MZ");
00295 
00296       scaling_bounding_box_.setMin(min);
00297       scaling_bounding_box_.setMax(max);
00298 
00299     }
00300 
00305     void preprocess_()
00306     {
00307 #define V_preprocessSceneMap(bla)  V_PoseClusteringAffineSuperimposer(bla)
00308     
00309       
00310 //       // build an array of pointer to all elements in the model map
00311 //       PeakPointerArray model_map(element_map_[MODEL]->begin(), element_map_[MODEL]->end());
00312 //       //build an array of pointer to all elements in the scene map
00313 //       PeakPointerArray scene_map(element_map_[SCENE]->begin(), element_map_[SCENE]->end());
00314       
00316           // build an array of pointer to all elements in the model map
00317       PeakPointerArray model_map_1(element_map_[MODEL]->begin(), element_map_[MODEL]->end());
00318       // build an array of pointer to all elements in the scene map
00319       PeakPointerArray scene_map_1(element_map_[SCENE]->begin(), element_map_[SCENE]->end());
00320       
00321       model_map_1.sortByIntensity();
00322       scene_map_1.sortByIntensity();
00323         
00324       UInt m=2000;
00325       UInt number = (model_map_1.size() > m) ? m : model_map_1.size();
00326       // build an array of pointer to all elements in the model map
00327       PeakPointerArray model_map(model_map_1.end() - number, model_map_1.end());
00328       
00329       number = (scene_map_1.size() > m) ? m : scene_map_1.size();
00330       // build an array of pointer to all elements in the model map
00331       PeakPointerArray scene_map(scene_map_1.end() - number, scene_map_1.end());
00333       
00334 
00335       // for each element (rt_m,mz_m) of the model map
00336       // search for corresponding elements (rt_i,mz_i) in the scene map
00337       // which lie in a predefined mz intervall (mz_i in [mz_m-eps,mz_m+eps))
00338       model_map.sortByNthPosition(RawDataPoint2D::MZ);
00339       scene_map.sortByNthPosition(RawDataPoint2D::MZ);
00340 
00341       // take only elements of the model map which have partners in the scene map
00342       typename PeakPointerArray::const_iterator it_first = scene_map.begin();
00343       typename PeakPointerArray::const_iterator it_last = it_first;
00344       UInt n = model_map.size();
00345       
00346       UInt max_partners = 0;
00347       for (UInt i = 0; i < n; ++i)
00348       {
00349         DoubleReal act_mz = model_map[i].getMZ();
00350         DoubleReal min_mz = act_mz - mz_bucket_size_;
00351         DoubleReal max_mz = act_mz + mz_bucket_size_;
00352 
00353         CoordinateType act_rt = model_map[i].getRT();
00354         CoordinateType min_rt = act_rt - 1000;
00355         CoordinateType max_rt = act_rt + 1000;
00356 
00357         std::vector< const PointType* > partners;
00358         // search for the left end of the intervall
00359         while ((it_first != scene_map.end()) 
00360               && (it_first->getMZ() < min_mz))
00361         {
00362           ++it_first;
00363         }
00364 
00365         it_last = it_first;
00366 
00367         // search for the right end of the intervall
00368         while ((it_last != scene_map.end()) 
00369                && (it_last->getMZ() < max_mz))
00370 
00371         {
00372           ++it_last;
00373         }
00374 
00376         PeakPointerArray partner_;
00377         for (typename PeakPointerArray::const_iterator it = it_first; it != it_last; ++it)
00378         {
00379           if ((it->getRT() < max_rt) && (it->getRT() > min_rt) )
00380           {
00381             partners.push_back(&(*it));
00382           }
00383 
00384         }
00385 
00386         
00387         if (partners.size() > 0)
00388         {
00389           model_map_red_.push_back(model_map[i]);
00390           scene_map_partners_.push_back(partners);
00391           if (partners.size() > max_partners)
00392             max_partners = partners.size();
00393         }
00394       }
00395       std::cout << "Max number of partners " << max_partners << std::endl;
00396       //       std::cout.flush();
00397 
00398       // Compute shift_bucket_size_ and num_buckets.
00399       PositionType diagonal_shift = shift_bounding_box_.diagonal();
00400       PositionType diagonal_scaling = scaling_bounding_box_.diagonal();
00401       V_preprocessSceneMap("diagonal shift: " << diagonal_shift);
00402       V_preprocessSceneMap("diagonal scaling: " << diagonal_scaling);
00403 
00404       for ( UInt dimension = 0; dimension < 2; ++dimension)
00405       {
00406         num_buckets_shift_[dimension] = (int)(ceil(diagonal_shift[dimension]/shift_bucket_size_[dimension]));
00407         num_buckets_scaling_[dimension] = (int)(ceil(diagonal_scaling[dimension]/scaling_bucket_size_[dimension]));
00408         shift_bucket_size_[dimension] = diagonal_shift[dimension] / (CoordinateType)num_buckets_shift_[dimension];
00409         scaling_bucket_size_[dimension] = diagonal_scaling[dimension] / (CoordinateType)num_buckets_scaling_[dimension];
00410       }
00411       V_preprocessSceneMap("shift bucket size : " << shift_bucket_size_[RawDataPoint2D::RT] << ' ' << shift_bucket_size_[RawDataPoint2D::MZ]);
00412       V_preprocessSceneMap("scaling bucket size : " << scaling_bucket_size_);
00413       V_preprocessSceneMap("shift number_buckets: " << num_buckets_shift_[0] << ' ' << num_buckets_shift_[1]);
00414       V_preprocessSceneMap("scaling number_buckets : " << num_buckets_scaling_[0] << ' ' << num_buckets_scaling_[1]);
00415 #undef V_preprocessSceneMap
00416 
00417     } // preprocess_
00418 
00421     void hashAffineTransformations_(IntensityType total_int_model_map, IntensityType total_int_scene_map )
00422     {
00423 #define V_hashAffineTransformations_(bla) V_PoseClusteringAffineSuperimposer(bla)
00424       // take each point pair in the model map
00425       UInt n = model_map_red_.size();
00426       std::cout << "Modelmap size " << n << std::endl;
00427       for (UInt i = 0; i < n; ++i)
00428       {
00429         // take only the next 10 neighbours in m/z as partner in the model map
00430         //        UInt k=((i+50)>= n) ? n : (i+50);
00431         for (UInt j = i+1; j < n; ++j)
00432         {
00433           // avoid cross mappings (i,j) -> (k,l) (e.g. i_rt < j_rt and k_rt > l_rt)
00434           // and point pairs with equal retention times (e.g. i_rt == j_rt)
00435           PositionType diff = model_map_red_[i].getPosition() - model_map_red_[j].getPosition();
00436           // and compute the affine transformation to all corresponding points pair in the scene map
00437           std::vector< const PointType* >& partners_i = scene_map_partners_[i];
00438           std::vector< const PointType* >& partners_j  = scene_map_partners_[j];
00439           UInt m = partners_i.size();
00440           UInt p = partners_j.size();
00441 
00442           for (UInt k = 0; k < m; ++k)
00443           {
00444             for (UInt l = 0; l < p; ++l)
00445             {
00446               //               std::cout << "Partner of " << model_map_red_[j].getPosition() << ' ' << l << ' ' << partners_j.size() << std::endl;
00447               //               std::cout << *(partners_j[l]) << std::endl;
00448               PositionType diff_2 = (partners_j[l]->getPosition() - partners_i[k]->getPosition());
00449 
00450               // compute the transformation (i,j) -> (k,l)
00451               PositionType shift;
00452               PositionType scaling;
00453               bool transformation_ok[2];
00454               transformation_ok[0] = false;
00455               transformation_ok[1] = false;
00456 
00457               if ((fabs(diff[RawDataPoint2D::RT]) > 0.001) && (fabs(diff_2[RawDataPoint2D::RT]) > 0.001))
00458               {
00459                 scaling[RawDataPoint2D::RT] = (model_map_red_[j].getRT() - model_map_red_[i].getRT())
00460                                               /(partners_j[l]->getRT() - partners_i[k]->getRT());
00461 
00462                 shift[RawDataPoint2D::RT] =  model_map_red_[i].getRT() - partners_i[k]->getRT()*scaling[RawDataPoint2D::RT];
00463                 transformation_ok[RawDataPoint2D::RT] = true;
00464               }
00465 
00466 
00467               if ((fabs(diff[RawDataPoint2D::MZ]) > 0.001) && (fabs(diff_2[RawDataPoint2D::MZ]) > 0.001))
00468               {
00469                 scaling[RawDataPoint2D::MZ] = (model_map_red_[j].getMZ() - model_map_red_[i].getMZ())
00470                                               /(partners_j[l]->getMZ() - partners_i[k]->getMZ());
00471 
00472                 shift[RawDataPoint2D::MZ] =  model_map_red_[i].getMZ() - partners_i[k]->getMZ()*scaling[RawDataPoint2D::MZ];
00473                 transformation_ok[RawDataPoint2D::MZ] = true;
00474               }
00475 
00476               // compute the hash indices
00477               int bucket_shift_index;
00478               int bucket_scaling_index;
00479               CoordinateType bucket_fraction_shift = 0.;
00480               CoordinateType bucket_fraction_scaling = 0.;
00481 
00482               IntensityType int_i = model_map_red_[i].getIntensity()/total_int_model_map;
00483               IntensityType int_k = partners_i[k]->getIntensity()/total_int_scene_map;
00484               IntensityType int_j = model_map_red_[j].getIntensity()/total_int_model_map;
00485               IntensityType int_l = partners_j[l]->getIntensity()/total_int_scene_map;
00486               QualityType int_similarity_jl = (int_j < int_l) ? int_j/int_l : int_l/int_j;
00487               QualityType int_similarity_ik = (int_i < int_k) ? int_i/int_k : int_k/int_i;
00488               QualityType int_similarity =  int_similarity_ik + int_similarity_jl;
00489 
00490               // check if the transformation paramerters lie in the hash map
00491               if ( transformation_ok[RawDataPoint2D::RT] && (scaling_bounding_box_.min()[RawDataPoint2D::RT] < scaling[RawDataPoint2D::RT]) && (scaling_bounding_box_.max()[RawDataPoint2D::RT] > scaling[RawDataPoint2D::RT])
00492                    && (shift_bounding_box_.min()[RawDataPoint2D::RT] < shift[RawDataPoint2D::RT]) && (shift_bounding_box_.max()[RawDataPoint2D::RT] > shift[RawDataPoint2D::RT]))
00493               {
00494                 bucket_fraction_shift = (shift[RawDataPoint2D::RT] - shift_bounding_box_.min()[RawDataPoint2D::RT]) / shift_bucket_size_[RawDataPoint2D::RT];  // floating point division
00495                 bucket_fraction_scaling = (scaling[RawDataPoint2D::RT] - scaling_bounding_box_.min()[RawDataPoint2D::RT]) / scaling_bucket_size_[RawDataPoint2D::RT];  // floating point division
00496                 bucket_shift_index    = (int) bucket_fraction_shift; // round down (yes we are >= 0)
00497                 //                       std::cout << "bucket_shift_index " <<  bucket_shift_index[RawDataPoint2D::RT] << std::endl;
00498                 bucket_scaling_index    = (int) bucket_fraction_scaling; // round down (yes we are >= 0)
00499                 //                       std::cout << "bucket_scaling_index " << bucket_shift_index[RawDataPoint2D::RT] << std::endl;
00500                 bucket_fraction_shift -= bucket_shift_index;          // fractional part
00501                 //                       std::cout << "bucket_fraction_shift " << bucket_fraction_shift[RawDataPoint2D::RT]<< std::endl;
00502                 bucket_fraction_scaling -= bucket_scaling_index;          // fractional part
00503                 //                       std::cout << "bucket_fraction_scaling" << bucket_fraction_scaling[RawDataPoint2D::RT]<< std::endl;
00504 
00505                 //                     std::cout << "Buckets rt (" << bucket_shift_index[RawDataPoint2D::RT] << ',' << bucket_scaling_index[RawDataPoint2D::RT] << ')' << std::endl;
00506                 //                     std::cout << "Buckets mz (" << bucket_shift_index[RawDataPoint2D::MZ] << ',' << bucket_scaling_index[RawDataPoint2D::MZ] << ')' << std::endl;
00507 
00508                 // hash the transformation if possible
00509                 CoordinateType bucket_fraction_complement_shift = 1.;
00510                 CoordinateType bucket_fraction_complement_scaling = 1.;
00511                 bucket_fraction_complement_shift -= bucket_fraction_shift;
00512                 bucket_fraction_complement_scaling -= bucket_fraction_scaling;
00513 
00514                 // Distribute the vote of the shift among the four neighboring buckets.
00515                 QualityType factor;
00516                 // rt
00517                 factor = bucket_fraction_complement_shift * bucket_fraction_complement_scaling * int_similarity;
00518                 rt_hash_[PairType(bucket_shift_index, bucket_scaling_index)] += factor;
00519                 //                     std::cout << "hash shift " << bucket_shift_index*shift_bucket_size_ + shift_bounding_box_.min() << std::endl;
00520                 //                     std::cout << "hash scaling " << bucket_scaling_index*scaling_bucket_size_ + scaling_bounding_box_.min()[RawDataPoint2D::RT] << std::endl;
00521                 //                     std::cout << "factor" << factor << std::endl;
00522 
00523                 factor = bucket_fraction_complement_shift * bucket_fraction_scaling * int_similarity;
00524                 rt_hash_[PairType(bucket_shift_index, bucket_scaling_index + 1 )] += factor;
00525                 //                     std::cout << "hash shift " << bucket_shift_index*shift_bucket_size_ + shift_bounding_box_.min()[RawDataPoint2D::RT] << std::endl;
00526                 //                     std::cout << "hash scaling " << (bucket_scaling_index+1)*scaling_bucket_size_ + scaling_bounding_box_.min() << std::endl;
00527 
00528                 factor = bucket_fraction_shift * bucket_fraction_complement_scaling * int_similarity;
00529                 rt_hash_[PairType( bucket_shift_index + 1, bucket_scaling_index )] += factor;
00530 
00531                 factor = bucket_fraction_shift * bucket_fraction_scaling * int_similarity_ik;
00532                 rt_hash_[PairType( bucket_shift_index + 1, bucket_scaling_index + 1 )] += factor;
00533               }
00534 
00535               if ( transformation_ok[RawDataPoint2D::MZ] && (scaling_bounding_box_.min()[RawDataPoint2D::MZ] < scaling[RawDataPoint2D::MZ]) && (scaling_bounding_box_.max()[RawDataPoint2D::MZ] > scaling[RawDataPoint2D::MZ])
00536                    && (shift_bounding_box_.min()[RawDataPoint2D::MZ] < shift[RawDataPoint2D::MZ]) && (shift_bounding_box_.max()[RawDataPoint2D::MZ] > shift[RawDataPoint2D::MZ]))
00537               {
00538                 bucket_fraction_shift = (shift[RawDataPoint2D::MZ] - shift_bounding_box_.min()[RawDataPoint2D::MZ]) / shift_bucket_size_[RawDataPoint2D::MZ];  // floating point division
00539                 bucket_fraction_scaling = (scaling[RawDataPoint2D::MZ] - scaling_bounding_box_.min()[RawDataPoint2D::MZ]) / scaling_bucket_size_[RawDataPoint2D::MZ];  // floating point division
00540                 bucket_shift_index    = (int) bucket_fraction_shift; // round down (yes we are >= 0)
00541                 //                       std::cout << "bucket_shift_index " <<  bucket_shift_index[RawDataPoint2D::MZ] << std::endl;
00542                 bucket_scaling_index    = (int) bucket_fraction_scaling; // round down (yes we are >= 0)
00543                 //                       std::cout << "bucket_scaling_index " << bucket_shift_index[RawDataPoint2D::MZ] << std::endl;
00544                 bucket_fraction_shift -= bucket_shift_index;          // fractional part
00545                 //                       std::cout << "bucket_fraction_shift " << bucket_fraction_shift[RawDataPoint2D::MZ]<< std::endl;
00546                 bucket_fraction_scaling -= bucket_scaling_index;          // fractional part
00547                 //                       std::cout << "bucket_fraction_scaling" << bucket_fraction_scaling[RawDataPoint2D::MZ]<< std::endl;
00548 
00549                 //                     std::cout << "Buckets rt (" << bucket_shift_index[RawDataPoint2D::MZ] << ',' << bucket_scaling_index[RawDataPoint2D::MZ] << ')' << std::endl;
00550                 //                     std::cout << "Buckets mz (" << bucket_shift_index[RawDataPoint2D::MZ] << ',' << bucket_scaling_index[RawDataPoint2D::MZ] << ')' << std::endl;
00551 
00552                 // hash the transformation if possible
00553                 CoordinateType bucket_fraction_complement_shift = 1.;
00554                 CoordinateType bucket_fraction_complement_scaling = 1.;
00555                 bucket_fraction_complement_shift -= bucket_fraction_shift;
00556                 bucket_fraction_complement_scaling -= bucket_fraction_scaling;
00557 
00558                 // Distribute the vote of the shift among the four neighboring buckets.
00559                 QualityType factor;
00560 
00561                 // mz
00562                 factor = bucket_fraction_complement_shift * bucket_fraction_complement_scaling * int_similarity;
00563                 mz_hash_[PairType(bucket_shift_index, bucket_scaling_index)] += factor;
00564                 //                     std::cout << "hash shift " << bucket_shift_index*shift_bucket_size_ + shift_bounding_box_.min() << std::endl;
00565                 //                     std::cout << "hash scaling " << bucket_scaling_index*scaling_bucket_size_ + scaling_bounding_box_.min()[RawDataPoint2D::MZ] << std::endl;
00566                 //                     std::cout << "factor" << factor << std::endl;
00567 
00568                 factor = bucket_fraction_complement_shift * bucket_fraction_scaling * int_similarity;
00569                 mz_hash_[PairType(bucket_shift_index, bucket_scaling_index + 1 )] += factor;
00570                 //                     std::cout << "hash shift " << bucket_shift_index*shift_bucket_size_ + shift_bounding_box_.min()[RawDataPoint2D::MZ] << std::endl;
00571                 //                     std::cout << "hash scaling " << (bucket_scaling_index+1)*scaling_bucket_size_ + scaling_bounding_box_.min() << std::endl;
00572 
00573                 factor = bucket_fraction_shift * bucket_fraction_complement_scaling * int_similarity;
00574                 mz_hash_[PairType( bucket_shift_index + 1, bucket_scaling_index )] += factor;
00575 
00576                 factor = bucket_fraction_shift * bucket_fraction_scaling * int_similarity;
00577                 mz_hash_[PairType( bucket_shift_index + 1, bucket_scaling_index + 1 )] += factor;
00578               }
00579             } // for l
00580           } // for k
00581         } // for j
00582       } // for i
00583 
00584 
00585 
00586       //       std::ofstream rt_os("rt_matrix.dat", std::ios::out);
00587       //       std::ofstream mz_os("mz_matrix.dat", std::ios::out);
00588       //
00589       //       typename AffineTransformationMapType::const_iterator it = rt_hash_.begin();
00590       //       while (it != rt_hash_.end())
00591       //       {
00592       //         rt_os << ((it->first).first)*shift_bucket_size_[RawDataPoint2D::RT] + shift_bounding_box_.min()[RawDataPoint2D::RT] << ' '
00593       //         << ((it->first).second)*scaling_bucket_size_[RawDataPoint2D::RT] + scaling_bounding_box_.min()[RawDataPoint2D::RT] << ' '
00594       //         << it->second << '\n';
00595       //         ++it;
00596       //       }
00597       //
00598       //       it = mz_hash_.begin();
00599       //       while (it != mz_hash_.end())
00600       //       {
00601       //         mz_os << ((it->first).first)*shift_bucket_size_[RawDataPoint2D::MZ] + shift_bounding_box_.min()[RawDataPoint2D::MZ] << ' '
00602       //         << ((it->first).second)*scaling_bucket_size_[RawDataPoint2D::MZ] + scaling_bounding_box_.min()[RawDataPoint2D::MZ] << ' '
00603       //         << it->second << '\n';
00604       //         ++it;
00605       //       }
00606       //       rt_os.flush();
00607       //       mz_os.flush();
00608 #undef V_hashAffineTransformations_
00609 
00610     } // hashAffineTransformations_
00611 
00612 
00614     void estimateFinalAffineTransformation_()
00615     {
00616 #define V_estimateFinalAffineTransformation_(bla) V_PoseClusteringAffineSuperimposer(bla)
00617       V_estimateFinalAffineTransformation_("@@@ computeShift_()");
00618 
00619       // search for the maximal vote parameter of the rt transformation
00620       PairType max_element_index_rt;
00621       QualityType act_max_rt = 0;
00622       for (typename AffineTransformationMapType::const_iterator it = rt_hash_.begin(); it != rt_hash_.end(); ++it)
00623       {
00624         if (it->second > act_max_rt)
00625         {
00626           max_element_index_rt = it->first;
00627           act_max_rt = it->second;
00628         }
00629       }
00630 
00631       V_estimateFinalAffineTransformation_("Max element in rt: Indizes: "<< max_element_index_rt.first << ' ' << max_element_index_rt.second
00632                                            << " Votes: " << act_max_rt
00633                                            << " shift: "  << max_element_index_rt.first*shift_bucket_size_[RawDataPoint2D::RT] + shift_bounding_box_.min()[RawDataPoint2D::RT]
00634                                            << " scaling: " << max_element_index_rt.second*scaling_bucket_size_[RawDataPoint2D::RT] + scaling_bounding_box_.min()[RawDataPoint2D::RT]);
00635 
00636 
00637       // Compute a weighted average of the transformation parameters nearby the max_element_index.
00638       PositionType rt_trafo;
00639       PositionType rt_bounding_box_min(shift_bounding_box_.min()[RawDataPoint2D::RT],scaling_bounding_box_.min()[RawDataPoint2D::RT]);
00640       int rt_run_indices[2];
00641       QualityType quality = 0;
00642       PositionType rt_window(bucket_window_shift_[RawDataPoint2D::RT],bucket_window_scaling_[RawDataPoint2D::RT]);
00643       //         std::cout << "rt_window " << rt_window << std::endl;
00644       for ( rt_run_indices[SHIFT]  = std::max ( int (max_element_index_rt.first - rt_window[SHIFT]), 0 );
00645             rt_run_indices[SHIFT] <= std::min ( int (max_element_index_rt.first + rt_window[SHIFT]), num_buckets_shift_[RawDataPoint2D::RT]-1 );
00646             ++rt_run_indices[SHIFT])
00647       {
00648         for ( rt_run_indices[SCALING]  = std::max ( int (max_element_index_rt.second - rt_window[SCALING]), 0 );
00649               rt_run_indices[SCALING] <= std::min ( int (max_element_index_rt.second + rt_window[SCALING]), num_buckets_scaling_[RawDataPoint2D::RT]-1 );
00650               ++rt_run_indices[SCALING])
00651 
00652         {
00653           PositionType contribution_position(shift_bucket_size_[RawDataPoint2D::RT],scaling_bucket_size_[RawDataPoint2D::RT]);
00654           // is the neighbouring bucket in the map?
00655           typename AffineTransformationMapType::const_iterator it = rt_hash_.find(PairType(rt_run_indices[SHIFT], rt_run_indices[SCALING]));
00656           if ( it != rt_hash_.end())
00657           {
00658             for ( UInt dimension = 0; dimension < 2; ++dimension)
00659             {
00660               contribution_position[dimension] *= rt_run_indices[dimension];
00661             }
00662             contribution_position += rt_bounding_box_min;
00663             // std::cout << "contribution_position " << contribution_position << std::endl;
00664             QualityType contribution_quality = it->second;
00665             // std::cout << "contribution_quality " << contribution_quality << std::endl;
00666             quality += contribution_quality;
00667             // std::cout << "quality " << quality << std::endl;
00668             contribution_position *= contribution_quality;
00669             // std::cout << "contribution_position " << contribution_position << std::endl;
00670             rt_trafo += contribution_position;
00671             // std::cout << "rt_trafo " << rt_trafo << std::endl;
00672           }
00673         }
00674       }
00675 
00676       if ( quality != 0 )
00677       {
00678         rt_trafo /= quality;
00679       }
00680 
00681       // Assign the result.
00682       // set slope and intercept
00683       final_transformation_[RawDataPoint2D::RT].setSlope(rt_trafo[SCALING]);
00684       final_transformation_[RawDataPoint2D::RT].setIntercept(rt_trafo[SHIFT]);
00685       V_estimateFinalAffineTransformation_("estimateFinalAffineTransformation_() hat geklappt rt: " << rt_trafo);
00686 
00687 
00688       // search for the maximal vote parameter of the mz transformation
00689       PairType max_element_index_mz;
00690       QualityType act_max_mz = 0;
00691       for (typename AffineTransformationMapType::const_iterator it = mz_hash_.begin(); it != mz_hash_.end(); ++it)
00692       {
00693         if (it->second > act_max_mz)
00694         {
00695           max_element_index_mz = it->first;
00696           act_max_mz = it->second;
00697         }
00698       }
00699       
00700       std::cout << "Max index: " << max_element_index_mz.first << " " << max_element_index_mz.second << std::endl;
00701       std::cout << "Votes: " << act_max_mz << std::endl;
00702 
00703       V_estimateFinalAffineTransformation_("Max element in mz: Indizes: "<< max_element_index_mz.first << ' ' << max_element_index_mz.second
00704                                            << " Votes: " << act_max_mz
00705                                            << " shift: "  << max_element_index_mz.first*shift_bucket_size_[RawDataPoint2D::MZ] + shift_bounding_box_.min()[RawDataPoint2D::MZ]
00706                                            << " scaling: " << max_element_index_mz.second*scaling_bucket_size_[RawDataPoint2D::MZ] + scaling_bounding_box_.min()[RawDataPoint2D::MZ]);
00707 
00708       PositionType mz_trafo;
00709       PositionType mz_bounding_box_min(shift_bounding_box_.min()[RawDataPoint2D::MZ],scaling_bounding_box_.min()[RawDataPoint2D::MZ]);
00710       int mz_run_indices[2];
00711       quality=0;
00712       PositionType mz_window(bucket_window_shift_[RawDataPoint2D::MZ],bucket_window_scaling_[RawDataPoint2D::MZ]);
00713       for ( mz_run_indices[SHIFT]  = std::max ( int (max_element_index_mz.first - mz_window[SHIFT]), 0 );
00714             mz_run_indices[SHIFT] <= std::min ( int (max_element_index_mz.first + mz_window[SHIFT]), num_buckets_shift_[RawDataPoint2D::MZ]-1 );
00715             ++mz_run_indices[SHIFT])
00716       {
00717         for ( mz_run_indices[SCALING]  = std::max ( int (max_element_index_mz.second - mz_window[SCALING]), 0 );
00718               mz_run_indices[SCALING] <= std::min ( int (max_element_index_mz.second + mz_window[SCALING]), num_buckets_scaling_[RawDataPoint2D::MZ]-1 );
00719               ++mz_run_indices[SCALING])
00720         {
00721           PositionType contribution_position(shift_bucket_size_[RawDataPoint2D::MZ],scaling_bucket_size_[RawDataPoint2D::MZ]);
00722           // is the neighbouring bucket in the map?
00723           typename AffineTransformationMapType::const_iterator it = mz_hash_.find(PairType(mz_run_indices[SHIFT], mz_run_indices[SCALING]));
00724           if ( it != mz_hash_.end())
00725           {
00726             for ( UInt dimension = 0; dimension < 2; ++dimension)
00727             {
00728               contribution_position[dimension] *= mz_run_indices[dimension];
00729             }
00730             contribution_position += mz_bounding_box_min;
00731             //               std::cout << "contribution_position " << contribution_position << std::endl;
00732             QualityType contribution_quality = it->second;
00733             //               std::cout << "contribution_quality " << contribution_quality << std::endl;
00734             quality += contribution_quality;
00735             //               std::cout << "quality " << quality << std::endl;
00736             contribution_position *= contribution_quality;
00737             //               std::cout << "contribution_position " << contribution_position << std::endl;
00738             mz_trafo += contribution_position;
00739             //               std::cout << "mz_trafo " << mz_trafo << std::endl;
00740           }
00741         }
00742       }
00743       if ( quality != 0 )
00744       {
00745         mz_trafo /= quality;
00746       }
00747       // set slope and intercept
00748       final_transformation_[RawDataPoint2D::MZ].setSlope(mz_trafo[SCALING]);
00749       final_transformation_[RawDataPoint2D::MZ].setIntercept(mz_trafo[SHIFT]);
00750       V_estimateFinalAffineTransformation_("estimateFinalAffineTransformation_() hat geklappt mz: " << mz_trafo);
00751 
00752       //         std::ofstream rt_os("rt_matrix.pgm", std::ios::out);
00753       //         std::ofstream mz_os("mz_matrix.pgm", std::ios::out);
00754       //
00755       //         typename AffineTransformationMapType::const_iterator it = rt_hash_.begin();
00756       //         while (it != rt_hash_.end())
00757       //         {
00758       //           rt_os << (it->first).first*shift_bucket_size_[RawDataPoint2D::RT] + shift_bounding_box_.min()[RawDataPoint2D::RT]
00759       //           << ' ' << (it->first).second*scaling_bucket_size_[RawDataPoint2D::RT] + scaling_bounding_box_.min()[RawDataPoint2D::RT] << '\n';
00760       //           ++it;
00761       //         }
00762       //         rt_os.flush();
00763 #undef V_estimateFinalAffineTransformation_
00764     } // estimateFinalAffineTransformation_
00765 
00767     PeakPointerArray model_map_red_;
00768 
00770     std::vector< std::vector< const PointType* > > scene_map_partners_;
00771 
00773     AffineTransformationMapType rt_hash_;
00774 
00776     AffineTransformationMapType mz_hash_;
00777 
00779     PositionBoundingBoxType shift_bounding_box_;
00780 
00782     PositionBoundingBoxType scaling_bounding_box_;
00783 
00785     PositionType shift_bucket_size_;
00786 
00788     PositionType scaling_bucket_size_;
00789 
00791     int num_buckets_shift_[2];
00792 
00794     int num_buckets_scaling_[2];
00795 
00797     UInt bucket_window_shift_[2];
00798 
00800     UInt bucket_window_scaling_[2];
00801 
00803     CoordinateType mz_bucket_size_;
00804 
00805   }
00806   ; // PoseClusteringAffineSuperimposer
00807 } // namespace OpenMS
00808 
00809 #endif  // OPENMS_ANALYSIS_MAPMATCHING_PoseClusteringAffineSuperimposer_H

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