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-2017.
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 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
36 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
37 
42 
43 #include <limits>
44 #include <cmath>
45 
46 namespace OpenMS
47 {
62  class OPENMS_DLLAPI LinearResampler :
63  public DefaultParamHandler,
64  public ProgressLogger
65  {
66 
67 public:
68 
71  DefaultParamHandler("LinearResampler")
72  {
73  defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
74  defaultsToParam_();
75  }
76 
79  {
80  }
81 
85  void raster(MSSpectrum& spectrum)
86  {
87  //return if nothing to do
88  if (spectrum.empty()) return;
89 
90  typename MSSpectrum::iterator first = spectrum.begin();
91  typename MSSpectrum::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  std::vector<Peak1D> resampled_peak_container;
99  resampled_peak_container.resize(number_resampled_points);
100 
101  // generate the resampled peaks at positions origin+i*spacing_
102  std::vector<Peak1D>::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 = static_cast<double>((it + left_index)->getIntensity());
134  intensity += static_cast<double>((first + i)->getIntensity()) * distance_right / spacing_;
135  (it + left_index)->setIntensity(intensity);
136  intensity = static_cast<double>((it + right_index)->getIntensity());
137  intensity += static_cast<double>((first + i)->getIntensity()) * distance_left;
138  (it + right_index)->setIntensity(intensity);
139  }
140 
141  spectrum.swap(resampled_peak_container);
142  }
143 
148  {
149  startProgress(0, exp.size(), "resampling of data");
150  for (Size i = 0; i < exp.size(); ++i)
151  {
152  raster(exp[i]);
153  setProgress(i);
154  }
155  endProgress();
156  }
157 
158 protected:
159 
161  double spacing_;
162 
163  virtual void updateMembers_()
164  {
165  spacing_ = param_.getValue("spacing");
166  }
167 
168  };
169 
170 
171 } // namespace OpenMS
172 
173 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLER_H
Linear Resampling of raw data.
Definition: LinearResampler.h:62
~LinearResampler()
Destructor.
Definition: LinearResampler.h:78
void rasterExperiment(PeakMap &exp)
Resamples the data in an MSExperiment.
Definition: LinearResampler.h:147
Size size() const
Definition: MSExperiment.h:132
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
LinearResampler()
Constructor.
Definition: LinearResampler.h:70
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
virtual void updateMembers_()
This method is used to update extra member variables at the end of the setParameters() method...
Definition: LinearResampler.h:163
void raster(MSSpectrum &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResampler.h:85
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
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:161

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13