Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
LinearResampler.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2015.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Alexandra Zerck $
32 // $Authors: Eva Lange $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
36 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
37 
41 
42 #include <limits>
43 #include <cmath>
44 
45 namespace OpenMS
46 {
61  class OPENMS_DLLAPI LinearResampler :
62  public DefaultParamHandler,
63  public ProgressLogger
64  {
65 
66 public:
67 
70  DefaultParamHandler("LinearResampler")
71  {
72  defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
73  defaultsToParam_();
74  }
75 
78  {
79  }
80 
84  template <typename PeakType>
85  void raster(MSSpectrum<PeakType>& spectrum)
86  {
87  //return if nothing to do
88  if (spectrum.empty()) return;
89 
90  typename MSSpectrum<PeakType>::iterator first = spectrum.begin();
91  typename MSSpectrum<PeakType>::iterator last = spectrum.end();
92 
93  double end_pos = (last - 1)->getMZ();
94  double start_pos = first->getMZ();
95  int number_raw_points = static_cast<int>(spectrum.size());
96  int number_resampled_points = static_cast<int>(ceil((end_pos - start_pos) / spacing_ + 1));
97 
98  typename std::vector<PeakType> resampled_peak_container;
99  resampled_peak_container.resize(number_resampled_points);
100 
101  // generate the resampled peaks at positions origin+i*spacing_
102  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
103  for (int i = 0; i < number_resampled_points; ++i)
104  {
105  it->setMZ(start_pos + i * spacing_);
106  ++it;
107  }
108 
109  // spread the intensity h of the data point at position x to the left and right
110  // adjacent resampled peaks
111  double distance_left = 0.;
112  double distance_right = 0.;
113  int left_index = 0;
114  int right_index = 0;
115 
116  it = resampled_peak_container.begin();
117  for (int i = 0; i < number_raw_points; ++i)
118  {
119  int help = static_cast<int>(floor(((first + i)->getMZ() - start_pos) / spacing_));
120  left_index = (help < 0) ? 0 : help;
121  help = distance(first, last) - 1;
122  right_index = (left_index >= help) ? help : left_index + 1;
123 
124  // compute the distance between x and the left adjacent resampled peak
125  distance_left = fabs((first + i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
126  //std::cout << "Distance left " << distance_left << std::endl;
127  // compute the distance between x and the right adjacent resampled peak
128  distance_right = fabs((first + i)->getMZ() - (it + right_index)->getMZ());
129  //std::cout << "Distance right " << distance_right << std::endl;
130 
131 
132  // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
133  double intensity = (it + left_index)->getIntensity();
134  intensity += (first + i)->getIntensity() * distance_right / spacing_;
135  (it + left_index)->setIntensity(intensity);
136  intensity = (it + right_index)->getIntensity();
137  intensity += (first + i)->getIntensity() * distance_left;
138  (it + right_index)->setIntensity(intensity);
139  }
140 
141  spectrum.swap(resampled_peak_container);
142  }
143 
147  template <typename PeakType>
149  {
150  startProgress(0, exp.size(), "resampling of data");
151  for (Size i = 0; i < exp.size(); ++i)
152  {
153  raster(exp[i]);
154  setProgress(i);
155  }
156  endProgress();
157  }
158 
159 protected:
160 
162  double spacing_;
163 
164  virtual void updateMembers_()
165  {
166  spacing_ = param_.getValue("spacing");
167  }
168 
169  };
170 
171 
172 } // namespace OpenMS
173 
174 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
Linear Resampling of raw data.
Definition: LinearResampler.h:61
Size size() const
Definition: MSExperiment.h:117
~LinearResampler()
Destructor.
Definition: LinearResampler.h:77
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
LinearResampler()
Constructor.
Definition: LinearResampler.h:69
void rasterExperiment(MSExperiment< PeakType > &exp)
Resamples the data in an MSExperiment.
Definition: LinearResampler.h:148
virtual void updateMembers_()
This method is used to update extra member variables at the end of the setParameters() method...
Definition: LinearResampler.h:164
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:69
void raster(MSSpectrum< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResampler.h:85
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:162

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Aug 25 2015 05:53:48 using doxygen 1.8.9.1