OpenMS
LinearResamplerAlign.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: Hannes Roest $
6 // $Authors: Hannes Roest $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 #include <OpenMS/CONCEPT/Macros.h>
13 
14 namespace OpenMS
15 {
16 
31  class OPENMS_DLLAPI LinearResamplerAlign :
32  public LinearResampler
33  {
34 
35 public:
36 
38  {
39  defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
40  defaults_.setValue("ppm", "false", "Whether spacing is in ppm or Th");
41  defaultsToParam_();
42  }
43 
53  template <class SpecT>
54  void raster(SpecT& container)
55  {
56  //return if nothing to do
57  if (container.empty()) return;
58 
59  typename SpecT::iterator first = container.begin();
60  typename SpecT::iterator last = container.end();
61 
62  double end_pos = (last - 1)->getMZ();
63  double start_pos = first->getMZ();
64  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
65 
66  std::vector<typename SpecT::PeakType> resampled_peak_container;
67  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
68 
69  raster(container.begin(), container.end(), resampled_peak_container.begin(), resampled_peak_container.end());
70 
71  container.swap(resampled_peak_container);
72  }
73 
89  template <typename SpecT>
90  void raster_align(SpecT& container, double start_pos, double end_pos)
91  {
92  //return if nothing to do
93  if (container.empty()) return;
94 
95  if (end_pos < start_pos)
96  {
97  std::vector<typename SpecT::PeakType> empty;
98  container.swap(empty);
99  return;
100  }
101 
102  typename SpecT::iterator first = container.begin();
103  typename SpecT::iterator last = container.end();
104 
105  // get the iterators just before / after the two points start_pos / end_pos
106  while (first != container.end() && (first)->getMZ() < start_pos) {++first;}
107  while (last != first && (last - 1)->getMZ() > end_pos) {--last;}
108 
109  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
110 
111  std::vector<typename SpecT::PeakType> resampled_peak_container;
112  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
113 
114  raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
115 
116  container.swap(resampled_peak_container);
117  }
118 
140  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
141  void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resampled_begin, PeakTypeIterator resampled_end)
142  {
143  OPENMS_PRECONDITION(resampled_begin != resampled_end, "Output iterators cannot be identical") // as we use +1
144  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
145 
146  PeakTypeIterator resample_start = resampled_begin;
147 
148  // need to get the raw iterator between two resampled iterators of the raw data
149  while (raw_it != raw_end && raw_it->getMZ() < resampled_begin->getMZ())
150  {
151  resampled_begin->setIntensity(resampled_begin->getIntensity() + raw_it->getIntensity());
152  raw_it++;
153  }
154 
155  while (raw_it != raw_end)
156  {
157  //advance the resample iterator until our raw point is between two resampled iterators
158  while (resampled_begin != resampled_end && resampled_begin->getMZ() < raw_it->getMZ()) {resampled_begin++;}
159  if (resampled_begin != resample_start) {resampled_begin--;}
160 
161  // if we have the last datapoint we break
162  if ((resampled_begin + 1) == resampled_end) {break;}
163 
164  double dist_left = fabs(raw_it->getMZ() - resampled_begin->getMZ());
165  double dist_right = fabs(raw_it->getMZ() - (resampled_begin + 1)->getMZ());
166 
167  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
168  resampled_begin->setIntensity(resampled_begin->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left + dist_right));
169  (resampled_begin + 1)->setIntensity((resampled_begin + 1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left + dist_right));
170 
171  raw_it++;
172  }
173 
174  // add the final intensity to the right
175  while (raw_it != raw_end)
176  {
177  resampled_begin->setIntensity(resampled_begin->getIntensity() + raw_it->getIntensity());
178  raw_it++;
179  }
180  }
181 
208  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
209  void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
210  ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator int_raw_end,
211  ConstPeakTypeIterator mz_resample_it, ConstPeakTypeIterator mz_resample_end,
212  PeakTypeIterator int_resample_it, PeakTypeIterator int_resample_end)
213  {
214  (void)int_raw_end; // avoid 'unused parameter' compile error
215  (void)int_resample_end; // avoid 'unused parameter' compile error
216  OPENMS_PRECONDITION(mz_resample_it != mz_resample_end, "Output iterators cannot be identical") // as we use +1
217  OPENMS_PRECONDITION(std::distance(mz_resample_it, mz_resample_end) == std::distance(int_resample_it, int_resample_end),
218  "Resample m/z and intensity iterators need to cover the same distance")
219  OPENMS_PRECONDITION(std::distance(mz_raw_it, mz_raw_end) == std::distance(int_raw_it, int_raw_end),
220  "Raw m/z and intensity iterators need to cover the same distance")
221  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
222 
223  PeakTypeIterator mz_resample_start = mz_resample_it;
224 
225  // need to get the raw iterator between two resampled iterators of the raw data
226  while (mz_raw_it != mz_raw_end && (*mz_raw_it) < (*mz_resample_it) )
227  {
228  (*int_resample_it) = *int_resample_it + *int_raw_it;
229  ++mz_raw_it;
230  ++int_raw_it;
231  }
232 
233  while (mz_raw_it != mz_raw_end)
234  {
235  //advance the resample iterator until our raw point is between two resampled iterators
236  while (mz_resample_it != mz_resample_end && *mz_resample_it < *mz_raw_it)
237  {
238  ++mz_resample_it; ++int_resample_it;
239  }
240  if (mz_resample_it != mz_resample_start)
241  {
242  --mz_resample_it; --int_resample_it;
243  }
244 
245  // if we have the last datapoint we break
246  if ((mz_resample_it + 1) == mz_resample_end) {break;}
247 
248  double dist_left = fabs(*mz_raw_it - *mz_resample_it);
249  double dist_right = fabs(*mz_raw_it - *(mz_resample_it + 1));
250 
251  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
252  *(int_resample_it) = *int_resample_it + (*int_raw_it) * dist_right / (dist_left + dist_right);
253  *(int_resample_it + 1) = *(int_resample_it + 1) + (*int_raw_it) * dist_left / (dist_left + dist_right);
254 
255  ++mz_raw_it;
256  ++int_raw_it;
257  }
258 
259  // add the final intensity to the right
260  while (mz_raw_it != mz_raw_end)
261  {
262  *int_resample_it = *int_resample_it + (*int_raw_it);
263  ++mz_raw_it;
264  ++int_raw_it;
265  }
266  }
267 
284  template <typename PeakTypeIterator>
285  void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resampled_start, PeakTypeIterator resampled_end)
286  {
287  // OPENMS_PRECONDITION(resampled_start != resampled_end, "Output iterators cannot be identical")
288  OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical") // as we use +1
289 
290  PeakTypeIterator raw_start = raw_it;
291 
292  // need to get the resampled iterator between two iterators of the raw data
293  while (resampled_start != resampled_end && resampled_start->getMZ() < raw_it->getMZ()) {resampled_start++;}
294 
295  while (resampled_start != resampled_end)
296  {
297  //advance the raw_iterator until our current point we want to interpolate is between them
298  while (raw_it != raw_end && raw_it->getMZ() < resampled_start->getMZ()) {raw_it++;}
299  if (raw_it != raw_start) {raw_it--;}
300 
301  // if we have the last datapoint we break
302  if ((raw_it + 1) == raw_end) {break;}
303 
304  // use a linear interpolation between raw_it and raw_it+1
305  double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
306  resampled_start->setIntensity(raw_it->getIntensity() + (resampled_start->getMZ() - raw_it->getMZ()) * m);
307  resampled_start++;
308  }
309 
310  }
311 
312 protected:
313 
315  bool ppm_;
316 
317  void updateMembers_() override
318  {
319  spacing_ = param_.getValue("spacing");
320  ppm_ = (bool)param_.getValue("ppm").toBool();
321  }
322 
324  template <typename PeakType>
325  void populate_raster_(std::vector<PeakType>& resampled_peak_container,
326  double start_pos, double end_pos, int number_resampled_points)
327  {
328  if (!ppm_)
329  {
330  // generate the resampled peaks at positions origin+i*spacing_
331  resampled_peak_container.resize(number_resampled_points);
332  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
333  for (int i = 0; i < number_resampled_points; ++i)
334  {
335  it->setMZ(start_pos + i * spacing_);
336  ++it;
337  }
338  }
339  else
340  {
341  // generate resampled peaks with ppm distance (not fixed)
342  double current_mz = start_pos;
343  while (current_mz < end_pos)
344  {
345  PeakType p;
346  p.setIntensity(0);
347  p.setMZ(current_mz);
348  resampled_peak_container.push_back(p);
349 
350  // increment current_mz
351  current_mz += current_mz * (spacing_ / 1e6);
352  }
353  }
354  }
355  };
356 
357 }
358 
359 
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:33
void raster(SpecT &container)
Applies the resampling algorithm to a container (MSSpectrum or MSChromatogram).
Definition: LinearResamplerAlign.h:54
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:141
void raster_align(SpecT &container, double start_pos, double end_pos)
Applies the resampling algorithm to a container (MSSpectrum or MSChromatogram) with fixed coordinates...
Definition: LinearResamplerAlign.h:90
LinearResamplerAlign()
Definition: LinearResamplerAlign.h:37
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:285
bool ppm_
Spacing of the resampled data.
Definition: LinearResamplerAlign.h:315
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: LinearResamplerAlign.h:317
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:325
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:209
Linear Resampling of raw data.
Definition: LinearResampler.h:38
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:29
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:178
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition: Peak2D.h:148
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19