OpenMS  2.7.0
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-2021.
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: Timo Sachsenberg $
32 // $Authors: Eva Lange $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
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 
77  ~LinearResampler() override
78  {
79  }
80 
84  void raster(MSSpectrum& spectrum)
85  {
86  //return if nothing to do
87  if (spectrum.empty()) return;
88 
89  typename MSSpectrum::iterator first = spectrum.begin();
90  typename MSSpectrum::iterator last = spectrum.end();
91 
92  double end_pos = (last - 1)->getMZ();
93  double start_pos = first->getMZ();
94  int number_raw_points = static_cast<int>(spectrum.size());
95  int number_resampled_points = static_cast<int>(ceil((end_pos - start_pos) / spacing_ + 1));
96 
97  std::vector<Peak1D> resampled_peak_container;
98  resampled_peak_container.resize(number_resampled_points);
99 
100  // generate the resampled peaks at positions origin+i*spacing_
101  std::vector<Peak1D>::iterator it = resampled_peak_container.begin();
102  for (int i = 0; i < number_resampled_points; ++i)
103  {
104  it->setMZ(start_pos + i * spacing_);
105  ++it;
106  }
107 
108  // spread the intensity h of the data point at position x to the left and right
109  // adjacent resampled peaks
110  double distance_left = 0.;
111  double distance_right = 0.;
112  int left_index = 0;
113  int right_index = 0;
114 
115  it = resampled_peak_container.begin();
116  for (int i = 0; i < number_raw_points; ++i)
117  {
118  int help = static_cast<int>(floor(((first + i)->getMZ() - start_pos) / spacing_));
119  left_index = (help < 0) ? 0 : help;
120  help = distance(first, last) - 1;
121  right_index = (left_index >= help) ? help : left_index + 1;
122 
123  // compute the distance between x and the left adjacent resampled peak
124  distance_left = fabs((first + i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
125  //std::cout << "Distance left " << distance_left << std::endl;
126  // compute the distance between x and the right adjacent resampled peak
127  distance_right = fabs((first + i)->getMZ() - (it + right_index)->getMZ());
128  //std::cout << "Distance right " << distance_right << std::endl;
129 
130 
131  // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
132  double intensity = static_cast<double>((it + left_index)->getIntensity());
133  intensity += static_cast<double>((first + i)->getIntensity()) * distance_right / spacing_;
134  (it + left_index)->setIntensity(intensity);
135  intensity = static_cast<double>((it + right_index)->getIntensity());
136  intensity += static_cast<double>((first + i)->getIntensity()) * distance_left;
137  (it + right_index)->setIntensity(intensity);
138  }
139 
140  spectrum.swap(resampled_peak_container);
141  }
142 
147  {
148  startProgress(0, exp.size(), "resampling of data");
149  for (Size i = 0; i < exp.size(); ++i)
150  {
151  raster(exp[i]);
152  setProgress(i);
153  }
154  endProgress();
155  }
156 
157 protected:
158 
160  double spacing_;
161 
162  void updateMembers_() override
163  {
164  spacing_ = param_.getValue("spacing");
165  }
166 
167  };
168 
169 
170 } // namespace OpenMS
171 
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:93
Linear Resampling of raw data.
Definition: LinearResampler.h:64
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:160
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: LinearResampler.h:162
LinearResampler()
Constructor.
Definition: LinearResampler.h:69
~LinearResampler() override
Destructor.
Definition: LinearResampler.h:77
void rasterExperiment(PeakMap &exp)
Resamples the data in an MSExperiment.
Definition: LinearResampler.h:146
void raster(MSSpectrum &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResampler.h:84
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:80
Size size() const
Definition: MSExperiment.h:127
The representation of a 1D spectrum.
Definition: MSSpectrum.h:71
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47