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

LinearResampler.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: Alexandra Zerck $
00032 // $Authors: Eva Lange $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00036 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
00037 
00038 #include <OpenMS/KERNEL/MSExperiment.h>
00039 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
00040 #include <OpenMS/CONCEPT/ProgressLogger.h>
00041 
00042 #include <limits>
00043 #include <cmath>
00044 
00045 namespace OpenMS
00046 {
00061   class OPENMS_DLLAPI LinearResampler :
00062     public DefaultParamHandler,
00063     public ProgressLogger
00064   {
00065 
00066 public:
00067 
00069     LinearResampler() :
00070       DefaultParamHandler("LinearResampler")
00071     {
00072       defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
00073       defaultsToParam_();
00074     }
00075 
00077     ~LinearResampler()
00078     {
00079     }
00080 
00084     template <typename PeakType>
00085     void raster(MSSpectrum<PeakType> & spectrum)
00086     {
00087       //return if nothing to do
00088       if (spectrum.empty()) return;
00089 
00090       typename MSSpectrum<PeakType>::iterator first = spectrum.begin();
00091       typename MSSpectrum<PeakType>::iterator last = spectrum.end();
00092 
00093       double end_pos = (last - 1)->getMZ();
00094       double start_pos = first->getMZ();
00095       int number_raw_points = (int)spectrum.size();
00096       int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
00097 
00098       typename std::vector<PeakType> resampled_peak_container;
00099       resampled_peak_container.resize(number_resampled_points);
00100 
00101       // generate the resampled peaks at positions origin+i*spacing_
00102       typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
00103       for (int i = 0; i < number_resampled_points; ++i)
00104       {
00105         it->setMZ(start_pos + i * spacing_);
00106         ++it;
00107       }
00108 
00109       // spread the intensity h of the data point at position x to the left and right
00110       // adjacent resampled peaks
00111       double distance_left = 0.;
00112       double distance_right = 0.;
00113       int left_index = 0;
00114       int right_index = 0;
00115 
00116       it = resampled_peak_container.begin();
00117       for (int i = 0; i < number_raw_points; ++i)
00118       {
00119         int help = (int)floor(((first + i)->getMZ() - start_pos) / spacing_);
00120         left_index = (help < 0) ? 0 : help;
00121         help = distance(first, last) - 1;
00122         right_index = (left_index >= help) ? help : left_index + 1;
00123 
00124         // compute the distance between x and the left adjacent resampled peak
00125         distance_left = fabs((first + i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
00126         //std::cout << "Distance left " << distance_left << std::endl;
00127         // compute the distance between x and the right adjacent resampled peak
00128         distance_right = fabs((first + i)->getMZ() - (it + right_index)->getMZ());
00129         //std::cout << "Distance right " << distance_right << std::endl;
00130 
00131 
00132         // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
00133         DoubleReal intensity = (it + left_index)->getIntensity();
00134         intensity += (first + i)->getIntensity() * distance_right / spacing_;
00135         (it + left_index)->setIntensity(intensity);
00136         intensity = (it + right_index)->getIntensity();
00137         intensity += (first + i)->getIntensity() * distance_left;
00138         (it + right_index)->setIntensity(intensity);
00139       }
00140 
00141       resampled_peak_container.swap(spectrum);
00142     }
00143 
00147     template <typename PeakType>
00148     void rasterExperiment(MSExperiment<PeakType> & exp)
00149     {
00150       startProgress(0, exp.size(), "resampling of data");
00151       for (Size i = 0; i < exp.size(); ++i)
00152       {
00153         raster(exp[i]);
00154         setProgress(i);
00155       }
00156       endProgress();
00157     }
00158 
00159 protected:
00160 
00162     double spacing_;
00163 
00164     virtual void updateMembers_()
00165     {
00166       spacing_ =  param_.getValue("spacing");
00167     }
00168 
00169   };
00170 
00171 
00172 } // namespace OpenMS
00173 
00174 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H

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