OpenMS
Loading...
Searching...
No Matches
LinearResampler.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
19namespace OpenMS
20{
35 class OPENMS_DLLAPI LinearResampler :
37 public ProgressLogger
38 {
39
40public:
41
44 DefaultParamHandler("LinearResampler")
45 {
46 defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
47 defaultsToParam_();
48 }
49
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 = number_resampled_points - 1;
95 right_index = (left_index >= help) ? help : left_index + 1;
96
97 // boundary case: raw point falls on (or is clamped to) the last resampled
98 // point so left_index == right_index. Both interpolation distances are 0,
99 // which would lose the entire intensity. Assign it directly instead.
100 if (left_index == right_index)
101 {
102 double intensity = static_cast<double>((it + left_index)->getIntensity());
103 intensity += static_cast<double>((first + i)->getIntensity());
104 (it + left_index)->setIntensity(intensity);
105 }
106 else
107 {
108 // compute the distance between x and the left adjacent resampled peak
109 distance_left = fabs((first + i)->getMZ() - (it + left_index)->getMZ()) / spacing_;
110 // compute the distance between x and the right adjacent resampled peak
111 distance_right = fabs((first + i)->getMZ() - (it + right_index)->getMZ());
112
113 // add the distance_right*h to the left resampled peak and distance_left*h to the right resampled peak
114 double intensity = static_cast<double>((it + left_index)->getIntensity());
115 intensity += static_cast<double>((first + i)->getIntensity()) * distance_right / spacing_;
116 (it + left_index)->setIntensity(intensity);
117 intensity = static_cast<double>((it + right_index)->getIntensity());
118 intensity += static_cast<double>((first + i)->getIntensity()) * distance_left;
119 (it + right_index)->setIntensity(intensity);
120 }
121 }
122
123 spectrum.swap(resampled_peak_container);
124 }
125
130 {
131 startProgress(0, exp.size(), "resampling of data");
132 for (Size i = 0; i < exp.size(); ++i)
133 {
134 raster(exp[i]);
135 setProgress(i);
136 }
137 endProgress();
138 }
139
140protected:
141
143 double spacing_;
144
145 void updateMembers_() override
146 {
147 spacing_ = param_.getValue("spacing");
148 }
149
150 };
151
152
153} // namespace OpenMS
154
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:143
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:145
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:129
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
Size size() const noexcept
The number of spectra.
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