OpenMS
Loading...
Searching...
No Matches
LinearResamplerAlign.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: Hannes Roest $
6// $Authors: Hannes Roest, Luis Jacob Keller, Alen Saric$
7// --------------------------------------------------------------------------
8
9#pragma once
10
18
19namespace OpenMS
20{
21 namespace Internal
22 {
49 } // namespace Internal
50
69 class OPENMS_DLLAPI LinearResamplerAlign :
71 public ProgressLogger
72 {
73
74 public:
75 LinearResamplerAlign() : DefaultParamHandler("LinearResamplerAlign")
76 {
77 defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
78 defaults_.setValue("ppm", "false", "Whether spacing is in ppm or Th");
79 defaultsToParam_();
80 }
81
91 template <class PeakContainerT>
92 void raster(PeakContainerT& container)
93 {
94 Internal::LinearResampling(spacing_, ppm_).raster(container);
95 }
96
111 template <typename PeakContainerT>
112 void raster_align(PeakContainerT& container, double start_pos, double end_pos)
113 {
114 //return if nothing to do
115 if (container.empty()) return;
116
117 if (end_pos < start_pos)
118 {
119 std::vector<typename PeakContainerT::PeakType> empty;
120 container.swap(empty);
121 return;
122 }
123
124 auto first = container.begin();
125 auto last = container.end();
126
127 verifySpacing_(first, last, [](auto x){return x->getPos();});
128
129 // get the iterators just before / after the two points start_pos / end_pos
130 while (first != container.end() && (first)->getPos() < start_pos) {++first;}
131 while (last != first && (last - 1)->getPos() > end_pos) {--last;}
132
133 int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
134
135 std::vector<typename PeakContainerT::PeakType> resampled_peak_container;
136 populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
137
138 raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
139
140 container.swap(resampled_peak_container);
141 }
142
164 template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
165 void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resampled_begin, PeakTypeIterator resampled_end)
166 {
167 Internal::LinearResampling(spacing_, ppm_).raster(raw_it, raw_end, resampled_begin, resampled_end);
168 }
169
196 template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
197 void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
198 ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator int_raw_end,
199 ConstPeakTypeIterator mz_resample_it, ConstPeakTypeIterator mz_resample_end,
200 PeakTypeIterator int_resample_it, PeakTypeIterator int_resample_end)
201 {
202 (void)int_raw_end; // avoid 'unused parameter' compile error
203 (void)int_resample_end; // avoid 'unused parameter' compile error
204 OPENMS_PRECONDITION(mz_resample_it != mz_resample_end, "Output iterators cannot be identical") // as we use +1
205 OPENMS_PRECONDITION(std::distance(mz_resample_it, mz_resample_end) == std::distance(int_resample_it, int_resample_end),
206 "Resample m/z and intensity iterators need to cover the same distance")
207 OPENMS_PRECONDITION(std::distance(mz_raw_it, mz_raw_end) == std::distance(int_raw_it, int_raw_end),
208 "Raw m/z and intensity iterators need to cover the same distance")
209 // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
210
211 verifySpacing_(mz_raw_it, mz_raw_end, [](auto x){return *x;});
212
213 PeakTypeIterator mz_resample_start = mz_resample_it;
214
215 // need to get the raw iterator between two resampled iterators of the raw data
216 while (mz_raw_it != mz_raw_end && (*mz_raw_it) < (*mz_resample_it) )
217 {
218 (*int_resample_it) = *int_resample_it + *int_raw_it;
219 ++mz_raw_it;
220 ++int_raw_it;
221 }
222
223 while (mz_raw_it != mz_raw_end)
224 {
225 //advance the resample iterator until our raw point is between two resampled iterators
226 while (mz_resample_it != mz_resample_end && *mz_resample_it < *mz_raw_it)
227 {
228 ++mz_resample_it; ++int_resample_it;
229 }
230 if (mz_resample_it != mz_resample_start)
231 {
232 --mz_resample_it; --int_resample_it;
233 }
234
235 // if we have the last datapoint we break
236 if ((mz_resample_it + 1) == mz_resample_end) {break;}
237
238 double dist_left = fabs(*mz_raw_it - *mz_resample_it);
239 double dist_right = fabs(*mz_raw_it - *(mz_resample_it + 1));
240
241 // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
242 *(int_resample_it) = *int_resample_it + (*int_raw_it) * dist_right / (dist_left + dist_right);
243 *(int_resample_it + 1) = *(int_resample_it + 1) + (*int_raw_it) * dist_left / (dist_left + dist_right);
244
245 ++mz_raw_it;
246 ++int_raw_it;
247 }
248
249 // add the final intensity to the right
250 while (mz_raw_it != mz_raw_end)
251 {
252 *int_resample_it = *int_resample_it + (*int_raw_it);
253 ++mz_raw_it;
254 ++int_raw_it;
255 }
256 }
257
274 template <typename PeakTypeIterator>
275 void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resampled_start, PeakTypeIterator resampled_end)
276 {
277 // OPENMS_PRECONDITION(resampled_start != resampled_end, "Output iterators cannot be identical")
278 OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical") // as we use +1
279
280 verifySpacing_(raw_it, raw_end, [](PeakTypeIterator x){return x->getPos();});
281
282 PeakTypeIterator raw_start = raw_it;
283
284 // need to get the resampled iterator between two iterators of the raw data
285 while (resampled_start != resampled_end && resampled_start->getPos() < raw_it->getPos()) {resampled_start++;}
286
287 while (resampled_start != resampled_end)
288 {
289 //advance the raw_iterator until our current point we want to interpolate is between them
290 while (raw_it != raw_end && raw_it->getPos() < resampled_start->getPos()) {raw_it++;}
291 if (raw_it != raw_start) {raw_it--;}
292
293 // if we have the last datapoint we break
294 if ((raw_it + 1) == raw_end) {break;}
295
296 // use a linear interpolation between raw_it and raw_it+1
297 double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getPos() - raw_it->getPos());
298 resampled_start->setIntensity(raw_it->getIntensity() + (resampled_start->getPos() - raw_it->getPos()) * m);
299 resampled_start++;
300 }
301
302 }
303
312 {
313 startProgress(0, exp.size(), "resampling of data");
314 for (Size i = 0; i < exp.size(); ++i)
315 {
316 raster(exp[i]);
317 setProgress(i);
318 }
319 endProgress();
320 }
321
322
323protected:
324
326 double spacing_;
327
329 bool ppm_;
330
331 void updateMembers_() override
332 {
333 spacing_ = param_.getValue("spacing");
334 ppm_ = (bool)param_.getValue("ppm").toBool();
335 }
336
338 template <typename PeakType>
339 void populate_raster_(std::vector<PeakType>& resampled_peak_container,
340 double start_pos, double end_pos, int number_resampled_points)
341 {
342 Internal::LinearResampling(spacing_, ppm_).populateRaster(resampled_peak_container, start_pos, end_pos, number_resampled_points);
343 }
344
345
347 template <typename PeakTypeIterator>
348 void verifySpacing_(PeakTypeIterator it, PeakTypeIterator end, auto access)
349 {
350 Internal::LinearResampling(spacing_, ppm_).verifySpacing(it, end, access);
351 }
352
353 };
354}
A base class for all classes handling default parameters.
Definition DefaultParamHandler.h:66
Shared linear resampling implementation without parameter or progress handling.
Definition LinearResampling.h:33
void verifySpacing(PeakTypeIterator it, PeakTypeIterator end, auto access)
Emit the existing spacing warning for absolute grids when enabled.
Definition LinearResampling.h:171
void populateRaster(std::vector< PeakType > &resampled_peak_container, double start_pos, double end_pos, int number_resampled_points)
Populate an absolute or ppm grid using the existing endpoint convention.
Definition LinearResampling.h:134
void raster(PeakContainerT &container)
Resample a peak container onto a grid spanning its first and last points.
Definition LinearResampling.h:49
Internal RAII guard for temporary resampling warning suppression.
Definition LinearResamplerAlign.h:31
ScopedResamplingWarningSuppression(bool suppress=true)
Definition LinearResamplerAlign.h:33
bool previous_
Definition LinearResamplerAlign.h:47
~ScopedResamplingWarningSuppression()
Definition LinearResamplerAlign.h:38
ScopedResamplingWarningSuppression & operator=(const ScopedResamplingWarningSuppression &)=delete
ScopedResamplingWarningSuppression(const ScopedResamplingWarningSuppression &)=delete
Linear Resampling of raw data with alignment.
Definition LinearResamplerAlign.h:72
void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resampled_begin, PeakTypeIterator resampled_end)
Resample points (e.g. Peak1D) from an input range onto a prepopulated output range with given m/z,...
Definition LinearResamplerAlign.h:165
double spacing_
Spacing of the resampled data.
Definition LinearResamplerAlign.h:326
LinearResamplerAlign()
Definition LinearResamplerAlign.h:75
void raster_align(PeakContainerT &container, double start_pos, double end_pos)
Applies the resampling algorithm to a container (MSSpectrum or MSChromatogram) with fixed coordinates...
Definition LinearResamplerAlign.h:112
void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resampled_start, PeakTypeIterator resampled_end)
Applies the resampling algorithm using a linear interpolation.
Definition LinearResamplerAlign.h:275
void raster(PeakContainerT &container)
Applies the resampling algorithm to a container (MSSpectrum or MSChromatogram).
Definition LinearResamplerAlign.h:92
bool ppm_
Whether spacing_ is interpreted in ppm (true) or Th (false)
Definition LinearResamplerAlign.h:329
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition LinearResamplerAlign.h:331
void populate_raster_(std::vector< PeakType > &resampled_peak_container, double start_pos, double end_pos, int number_resampled_points)
Generate raster for resampled peak container.
Definition LinearResamplerAlign.h:339
void rasterExperiment(PeakMap &exp)
Applies the resampling algorithm to all spectra of an MSExperiment.
Definition LinearResamplerAlign.h:311
void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end, ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator int_raw_end, ConstPeakTypeIterator mz_resample_it, ConstPeakTypeIterator mz_resample_end, PeakTypeIterator int_resample_it, PeakTypeIterator int_resample_end)
Resample points (with m/z and intensity in separate containers, but of same length) from an input ran...
Definition LinearResamplerAlign.h:197
void verifySpacing_(PeakTypeIterator it, PeakTypeIterator end, auto access)
helper function to warn the user when the resampling rate is too high
Definition LinearResamplerAlign.h:348
In-Memory representation of a mass spectrometry run.
Definition MSExperiment.h:49
Size size() const noexcept
The number of spectra.
Base class for all classes that want to report their progress.
Definition ProgressLogger.h:27
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:179
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition openms/include/OpenMS/CONCEPT/Macros.h:91
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::atomic< bool > suppress_resampling_spacing_warning
Shared process-wide flag controlling resampling-spacing warnings.