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

LinearResampler.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 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00028 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00029 
00030 #include <OpenMS/KERNEL/MSExperiment.h>
00031 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00032 #include <OpenMS/CONCEPT/ProgressLogger.h>
00033 
00034 #include <limits>
00035 #include <cmath>
00036 
00037 namespace OpenMS
00038 {
00053   class LinearResampler 
00054     : public DefaultParamHandler, 
00055       public ProgressLogger
00056   {
00057   
00058   public:
00059   
00061       LinearResampler()
00062         : DefaultParamHandler("LinearResampler")
00063       {
00064         defaults_.setValue("spacing",0.05,"Spacing of the resampled output peaks.");
00065         defaultsToParam_();
00066       }
00067     
00069       ~LinearResampler()
00070       {
00071       }
00072   
00085       template < typename InputPeakIterator, typename OutputPeakContainer >
00086       void raster(InputPeakIterator first, InputPeakIterator last, OutputPeakContainer& resampled_peak_container)
00087       {
00088           double end_pos = (last-1)->getMZ();
00089           double start_pos = first->getMZ();
00090           int number_raw_points = distance(first,last);
00091           int number_resampled_points = (int)(ceil((end_pos -start_pos) / spacing_ + 1));
00092   
00093           resampled_peak_container.resize(number_resampled_points);
00094   
00095           // generate the resampled peaks at positions origin+i*spacing_
00096           typename OutputPeakContainer::iterator it = resampled_peak_container.begin();
00097           for (int i=0; i < number_resampled_points; ++i)
00098           {
00099               it->setMZ( start_pos + i*spacing_);
00100               ++it;
00101           }
00102   
00103           // spread the intensity h of the data point at position x to the left and right
00104           // adjacent resampled peaks
00105           double distance_left = 0.;
00106           double distance_right = 0.;
00107           int left_index = 0;
00108           int right_index = 0;
00109   
00110           it = resampled_peak_container.begin();
00111           for (int i=0; i < number_raw_points ; ++i)
00112           {
00113               left_index = (int)floor(((first+i)->getMZ() - start_pos) / spacing_);
00114              // std::cout << "Left index " << left_index << std::endl;
00115               right_index = left_index + 1;
00116 //              std::cout << "Right index " << right_index << std::endl;
00117 //                
00118 //              std::cout << "Number of points " << number_raw_points << '\n'
00119 //                        << "Act pos " << (first+i)->getPosition()[0] 
00120 //                        << " Start pos " << start_pos << " End position " << end_pos << std::endl;
00121 //  
00122               // compute the distance between x and the left adjacent resampled peak
00123               distance_left = fabs((first+i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
00124              // std::cout << "Distance left " << distance_left << std::endl;
00125               // compute the distance between x and the right adjacent resampled peak
00126               distance_right = fabs((first+i)->getMZ() - (it + right_index)->getMZ());
00127               //std::cout << "Distance right " << distance_right << std::endl;
00128   
00129               // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
00130               DoubleReal intensity = (it + left_index)->getIntensity();
00131               intensity += (first+i)->getIntensity()*distance_right / spacing_;
00132               (it + left_index)->setIntensity(intensity);
00133               intensity = (it + right_index)->getIntensity();
00134               intensity += (first+i)->getIntensity()*distance_left;
00135               (it + right_index)->setIntensity(intensity);
00136           }
00137       }
00138   
00139   
00152       template <typename InputPeakContainer, typename OutputPeakContainer >
00153       void raster(const InputPeakContainer& input_peak_container, OutputPeakContainer& baseline_filtered_container)
00154       {
00155         // copy the spectrum settings
00156         static_cast<SpectrumSettings&>(baseline_filtered_container) = input_peak_container;
00157 
00158         raster(input_peak_container.begin(), input_peak_container.end(), baseline_filtered_container);
00159       }
00160   
00161   
00173       template <typename InputSpectrumIterator, typename OutputPeakType >
00174       void rasterExperiment(InputSpectrumIterator first, InputSpectrumIterator last, MSExperiment<OutputPeakType>& ms_exp_filtered)
00175       {
00176           UInt n = distance(first,last);
00177           ms_exp_filtered.reserve(n);
00178           startProgress(0,n,"resampling of data");
00179           // pick peaks on each scan
00180           for (UInt i = 0; i < n; ++i)
00181           {
00182               MSSpectrum< OutputPeakType > spectrum;
00183               InputSpectrumIterator input_it(first+i);
00184   
00185               // pick the peaks in scan i
00186               raster(*input_it,spectrum);
00187               setProgress(i);
00188   
00189               // if any peaks are found copy the spectrum settings
00190               if (spectrum.size() > 0)
00191               {
00192                   // copy the spectrum settings
00193                   static_cast<SpectrumSettings&>(spectrum) = *input_it;
00194                   spectrum.setType(SpectrumSettings::RAWDATA);
00195   
00196                   // copy the spectrum information
00197                   spectrum.getPrecursorPeak() = input_it->getPrecursorPeak();
00198                   spectrum.setRT(input_it->getRT());
00199                   spectrum.setMSLevel(input_it->getMSLevel());
00200                   spectrum.getName() = input_it->getName();
00201   
00202                   ms_exp_filtered.push_back(spectrum);
00203               }
00204           }
00205           endProgress();
00206       }
00207   
00208   
00209   
00219       template <typename InputPeakType, typename OutputPeakType >
00220       void rasterExperiment(const MSExperiment< InputPeakType >& ms_exp_raw, MSExperiment<OutputPeakType>& ms_exp_filtered)
00221       {
00222         // copy the experimental settings
00223         static_cast<ExperimentalSettings&>(ms_exp_filtered) = ms_exp_raw;
00224 
00225         rasterExperiment(ms_exp_raw.begin(), ms_exp_raw.end(), ms_exp_filtered);
00226       }
00227   
00228   protected:
00230       double spacing_;
00231       
00232       virtual void updateMembers_()
00233       {
00234         spacing_ =  param_.getValue("spacing");
00235       }
00236   };
00237 
00238 
00239 } // namespace OpenMS
00240 
00241 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H

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