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

ContinuousWaveletTransformNumIntegration.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 
00036 #ifndef OPENMS_TRANSFORMATIONS_RAW2PEAK_CONTINUOUSWAVELETTRANSFORMNUMINTEGRATION_H
00037 #define OPENMS_TRANSFORMATIONS_RAW2PEAK_CONTINUOUSWAVELETTRANSFORMNUMINTEGRATION_H
00038 
00039 #include <cmath>
00040 
00041 #include <OpenMS/MATH/MISC/MathFunctions.h>
00042 #include <OpenMS/TRANSFORMATIONS/RAW2PEAK/ContinuousWaveletTransform.h>
00043 
00044 
00045 #ifdef DEBUG_PEAK_PICKING
00046 #include <iostream>
00047 #include <fstream>
00048 #endif
00049 
00050 namespace OpenMS
00051 {
00057   class OPENMS_DLLAPI ContinuousWaveletTransformNumIntegration :
00058     public ContinuousWaveletTransform
00059   {
00060 public:
00062     typedef ContinuousWaveletTransform::PeakConstIterator PeakConstIterator;
00063 
00064     using ContinuousWaveletTransform::signal_;
00065     using ContinuousWaveletTransform::wavelet_;
00066     using ContinuousWaveletTransform::scale_;
00067     using ContinuousWaveletTransform::spacing_;
00068     using ContinuousWaveletTransform::end_left_padding_;
00069     using ContinuousWaveletTransform::begin_right_padding_;
00070     using ContinuousWaveletTransform::signal_length_;
00071 
00072 
00074     ContinuousWaveletTransformNumIntegration() :
00075       ContinuousWaveletTransform()
00076     {}
00077 
00079     virtual ~ContinuousWaveletTransformNumIntegration() {}
00080 
00094     template <typename InputPeakIterator>
00095     void transform(InputPeakIterator begin_input,
00096                    InputPeakIterator end_input,
00097                    float resolution,
00098                    unsigned int zeros = 0)
00099     {
00100 
00101 #ifdef DEBUG_PEAK_PICKING
00102       std::cout << "ContinuousWaveletTransformNumIntegration::transform: start " << begin_input->getMZ() << " until " << (end_input - 1)->getMZ() << std::endl;
00103 #endif
00104       if (fabs(resolution - 1) < 0.0001)
00105       {
00106         // resolution = 1 corresponds to the cwt at supporting points which have a distance corresponding to the minimal spacing in [begin_input,end_input)
00107         SignedSize n = distance(begin_input, end_input);
00108         signal_length_ = n;
00109 
00110         signal_.clear();
00111         signal_.resize(n);
00112 
00113         // TODO avoid to compute the cwt for the zeros in signal
00114 #ifdef DEBUG_PEAK_PICKING
00115         std::cout << "---------START TRANSFORM---------- \n";
00116 #endif
00117         InputPeakIterator help = begin_input;
00118         for (int i = 0; i < n; ++i)
00119         {
00120           signal_[i].setMZ(help->getMZ());
00121           signal_[i].setIntensity((Peak1D::IntensityType)integrate_(help, begin_input, end_input));
00122           ++help;
00123         }
00124 #ifdef DEBUG_PEAK_PICKING
00125         std::cout << "---------END TRANSFORM----------" << std::endl;
00126 #endif
00127         // no zeropadding
00128         begin_right_padding_ = n;
00129         end_left_padding_ = -1;
00130       }
00131       else
00132       {
00133         SignedSize n = SignedSize(resolution * distance(begin_input, end_input));
00134         double origin  = begin_input->getMZ();
00135         double spacing = ((end_input - 1)->getMZ() - origin) / (n - 1);
00136 
00137         // zero-padding at the ends?
00138         if (zeros > 0)
00139         {
00140           n += (2 * zeros);
00141         }
00142 
00143 
00144         std::vector<double> processed_input(n);
00145         signal_.clear();
00146         signal_.resize(n);
00147 
00148         InputPeakIterator it_help = begin_input;
00149         if (zeros > 0)
00150         {
00151           processed_input[0] = it_help->getMZ() - zeros * spacing;
00152           for (unsigned int i = 0; i < zeros; ++i) processed_input[i] = 0;
00153         }
00154         else processed_input[0] = it_help->getIntensity();
00155 
00156         double x;
00157         for (SignedSize k = 1; k < n - (int)zeros; ++k)
00158         {
00159           x = origin + k * spacing;
00160           // go to the real data point next to x
00161           while (((it_help + 1) < end_input) && ((it_help + 1)->getMZ() < x))
00162           {
00163             ++it_help;
00164           }
00165           processed_input[k] = getInterpolatedValue_(x, it_help);
00166         }
00167         if (zeros > 0)
00168         {
00169           for (unsigned int i = 0; i < zeros; ++i) processed_input[n - zeros + i] = 0;
00170         }
00171 
00172         // TODO avoid to compute the cwt for the zeros in signal
00173         for (Int i = 0; i < n; ++i)
00174         {
00175           signal_[i].setMZ(origin + i * spacing);
00176           signal_[i].setIntensity((Peak1D::IntensityType)integrate_(processed_input, spacing, i));
00177         }
00178 
00179         if (zeros == 0)
00180         {
00181           begin_right_padding_ = n;
00182           end_left_padding_ = -1;
00183         }
00184         else
00185         {
00186           begin_right_padding_ = n - zeros;
00187           end_left_padding_ = zeros - 1;
00188         }
00189       }
00190     }
00191 
00202     virtual void init(double scale, double spacing);
00203 
00204 protected:
00205 
00207     template <typename InputPeakIterator>
00208     double integrate_(InputPeakIterator x, InputPeakIterator first, InputPeakIterator last)
00209     {
00210 #ifdef DEBUG_PEAK_PICKING
00211       std::cout << "integrate_" << std::endl;
00212 #endif
00213 
00214       double v = 0.;
00215       Size middle = wavelet_.size();
00216 
00217       double start_pos = ((x->getMZ() - (middle * spacing_)) > first->getMZ()) ? (x->getMZ() - (middle * spacing_))
00218                          : first->getMZ();
00219       double end_pos = ((x->getMZ() + (middle * spacing_)) < (last - 1)->getMZ()) ? (x->getMZ() + (middle * spacing_))
00220                        : (last - 1)->getMZ();
00221 
00222       InputPeakIterator help = x;
00223 
00224 #ifdef DEBUG_PEAK_PICKING
00225       std::cout << "integrate from middle to start_pos " << help->getMZ() << " until " << start_pos << std::endl;
00226 #endif
00227 
00228       //integrate from middle to start_pos
00229       while ((help != first) && ((help - 1)->getMZ() > start_pos))
00230       {
00231         // search for the corresponding datapoint of help in the wavelet (take the left most adjacent point)
00232         double distance = fabs(x->getMZ() - help->getMZ());
00233         Size index_w_r = (Size) Math::round(distance / spacing_);
00234         if (index_w_r >= wavelet_.size())
00235         {
00236           index_w_r = wavelet_.size() - 1;
00237         }
00238         double wavelet_right =  wavelet_[index_w_r];
00239 
00240 #ifdef DEBUG_PEAK_PICKING
00241         std::cout << "distance x help " << distance << std::endl;
00242         std::cout << "distance in wavelet_ " << index_w_r * spacing_ << std::endl;
00243         std::cout << "wavelet_right "  <<  wavelet_right << std::endl;
00244 #endif
00245 
00246         // search for the corresponding datapoint for (help-1) in the wavelet (take the left most adjacent point)
00247         distance = fabs(x->getMZ() - (help - 1)->getMZ());
00248         Size index_w_l = (Size) Math::round(distance / spacing_);
00249         if (index_w_l >= wavelet_.size())
00250         {
00251           index_w_l = wavelet_.size() - 1;
00252         }
00253         double wavelet_left =  wavelet_[index_w_l];
00254 
00255         // start the interpolation for the true value in the wavelet
00256 
00257 #ifdef DEBUG_PEAK_PICKING
00258         std::cout << " help-1 " << (help - 1)->getMZ() << " distance x, help-1" << distance << std::endl;
00259         std::cout << "distance in wavelet_ " << index_w_l * spacing_ << std::endl;
00260         std::cout << "wavelet_ at left " <<   wavelet_left << std::endl;
00261 
00262         std::cout << " intensity " << fabs((help - 1)->getMZ() - help->getMZ()) / 2. << " * " << (help - 1)->getIntensity() << " * " << wavelet_left << " + " << (help)->getIntensity() << "* " << wavelet_right
00263                   << std::endl;
00264 #endif
00265 
00266         v += fabs((help - 1)->getMZ() - help->getMZ()) / 2. * ((help - 1)->getIntensity() * wavelet_left + help->getIntensity() * wavelet_right);
00267         --help;
00268       }
00269 
00270 
00271       //integrate from middle to end_pos
00272       help = x;
00273 #ifdef DEBUG_PEAK_PICKING
00274       std::cout << "integrate from middle to endpos " << (help)->getMZ() << " until " << end_pos << std::endl;
00275 #endif
00276       while ((help != (last - 1)) && ((help + 1)->getMZ() < end_pos))
00277       {
00278         // search for the corresponding datapoint for help in the wavelet (take the left most adjacent point)
00279         double distance = fabs(x->getMZ() - help->getMZ());
00280         Size index_w_l = (Size) Math::round(distance / spacing_);
00281         if (index_w_l >= wavelet_.size())
00282         {
00283           index_w_l = wavelet_.size() - 1;
00284         }
00285         double wavelet_left =  wavelet_[index_w_l];
00286 
00287 #ifdef DEBUG_PEAK_PICKING
00288         std::cout << " help " << (help)->getMZ() << " distance x, help" << distance << std::endl;
00289         std::cout << "distance in wavelet_ " << index_w_l * spacing_ << std::endl;
00290         std::cout << "wavelet_ at left " <<   wavelet_left << std::endl;
00291 #endif
00292 
00293         // search for the corresponding datapoint for (help+1) in the wavelet (take the left most adjacent point)
00294         distance = fabs(x->getMZ() - (help + 1)->getMZ());
00295         Size index_w_r = (Size) Math::round(distance / spacing_);
00296         if (index_w_r >= wavelet_.size())
00297         {
00298           index_w_r = wavelet_.size() - 1;
00299         }
00300         double wavelet_right =  wavelet_[index_w_r];
00301 
00302 #ifdef DEBUG_PEAK_PICKING
00303         std::cout << " help+1 " << (help + 1)->getMZ() << " distance x, help+1" << distance << std::endl;
00304         std::cout << "distance in wavelet_ " << index_w_r * spacing_ << std::endl;
00305         std::cout << "wavelet_ at right " <<   wavelet_right << std::endl;
00306 #endif
00307 
00308         v += fabs(help->getMZ() - (help + 1)->getMZ()) / 2. * (help->getIntensity() * wavelet_left + (help + 1)->getIntensity() * wavelet_right);
00309         ++help;
00310       }
00311 
00312 
00313 #ifdef DEBUG_PEAK_PICKING
00314       std::cout << "return" << (v / sqrt(scale_)) << std::endl;
00315 #endif
00316       return v / sqrt(scale_);
00317     }
00318 
00320     double integrate_(const std::vector<double> & processed_input, double spacing_data, int index);
00321 
00323     inline double marr_(double x)
00324     {
00325       return (1 - x * x) * exp(-x * x / 2);
00326     }
00327 
00328   };
00329 } //namespace OpenMS
00330 #endif

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