OpenMS
LinearResampler.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Eva Lange $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
15 
16 #include <limits>
17 #include <cmath>
18 
19 namespace OpenMS
20 {
35  class OPENMS_DLLAPI LinearResampler :
36  public DefaultParamHandler,
37  public ProgressLogger
38  {
39 
40 public:
41 
44  DefaultParamHandler("LinearResampler")
45  {
46  defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
47  defaultsToParam_();
48  }
49 
51  ~LinearResampler() override
52  {
53  }
54 
58  void raster(MSSpectrum& spectrum) const
59  {
60  //return if nothing to do
61  if (spectrum.empty()) return;
62 
63  typename MSSpectrum::iterator first = spectrum.begin();
64  typename MSSpectrum::iterator last = spectrum.end();
65 
66  double end_pos = (last - 1)->getMZ();
67  double start_pos = first->getMZ();
68  int number_raw_points = static_cast<int>(spectrum.size());
69  int number_resampled_points = static_cast<int>(ceil((end_pos - start_pos) / spacing_ + 1));
70 
71  std::vector<Peak1D> resampled_peak_container;
72  resampled_peak_container.resize(number_resampled_points);
73 
74  // generate the resampled peaks at positions origin+i*spacing_
75  std::vector<Peak1D>::iterator it = resampled_peak_container.begin();
76  for (int i = 0; i < number_resampled_points; ++i)
77  {
78  it->setMZ(start_pos + i * spacing_);
79  ++it;
80  }
81 
82  // spread the intensity h of the data point at position x to the left and right
83  // adjacent resampled peaks
84  double distance_left = 0.;
85  double distance_right = 0.;
86  int left_index = 0;
87  int right_index = 0;
88 
89  it = resampled_peak_container.begin();
90  for (int i = 0; i < number_raw_points; ++i)
91  {
92  int help = static_cast<int>(floor(((first + i)->getMZ() - start_pos) / spacing_));
93  left_index = (help < 0) ? 0 : help;
94  help = distance(first, last) - 1;
95  right_index = (left_index >= help) ? help : left_index + 1;
96 
97  // compute the distance between x and the left adjacent resampled peak
98  distance_left = fabs((first + i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
99  //std::cout << "Distance left " << distance_left << std::endl;
100  // compute the distance between x and the right adjacent resampled peak
101  distance_right = fabs((first + i)->getMZ() - (it + right_index)->getMZ());
102  //std::cout << "Distance right " << distance_right << std::endl;
103 
104 
105  // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
106  double intensity = static_cast<double>((it + left_index)->getIntensity());
107  intensity += static_cast<double>((first + i)->getIntensity()) * distance_right / spacing_;
108  (it + left_index)->setIntensity(intensity);
109  intensity = static_cast<double>((it + right_index)->getIntensity());
110  intensity += static_cast<double>((first + i)->getIntensity()) * distance_left;
111  (it + right_index)->setIntensity(intensity);
112  }
113 
114  spectrum.swap(resampled_peak_container);
115  }
116 
121  {
122  startProgress(0, exp.size(), "resampling of data");
123  for (Size i = 0; i < exp.size(); ++i)
124  {
125  raster(exp[i]);
126  setProgress(i);
127  }
128  endProgress();
129  }
130 
131 protected:
132 
134  double spacing_;
135 
136  void updateMembers_() override
137  {
138  spacing_ = param_.getValue("spacing");
139  }
140 
141  };
142 
143 
144 } // namespace OpenMS
145 
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:66
Linear Resampling of raw data.
Definition: LinearResampler.h:38
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:134
void raster(MSSpectrum &spectrum) const
Applies the resampling algorithm to an MSSpectrum, without alignment between spectra.
Definition: LinearResampler.h:58
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: LinearResampler.h:136
LinearResampler()
Constructor.
Definition: LinearResampler.h:43
~LinearResampler() override
Destructor.
Definition: LinearResampler.h:51
void rasterExperiment(PeakMap &exp)
Resamples the data in an MSExperiment, without alignment between spectra.
Definition: LinearResampler.h:120
In-Memory representation of a mass spectrometry run.
Definition: MSExperiment.h:45
Size size() const noexcept
The number of spectra.
Definition: MSExperiment.h:120
The representation of a 1D spectrum.
Definition: MSSpectrum.h:44
void swap(MetaInfoInterface &rhs)
Swap contents.
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:27
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19