OpenMS
LinearResamplerAlign.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, 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 #ifdef OPENMS_ASSERTIONS
210  void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
211  ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator int_raw_end,
212  ConstPeakTypeIterator mz_resample_it, ConstPeakTypeIterator mz_resample_end,
213  PeakTypeIterator int_resample_it, PeakTypeIterator int_resample_end)
214 #else
215  void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
216  ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator /* int_raw_end */,
217  PeakTypeIterator mz_resample_it, PeakTypeIterator mz_resample_end,
218  PeakTypeIterator int_resample_it, PeakTypeIterator /* int_resample_end */)
219 #endif
220  {
221  OPENMS_PRECONDITION(mz_resample_it != mz_resample_end, "Output iterators cannot be identical") // as we use +1
222  OPENMS_PRECONDITION(std::distance(mz_resample_it, mz_resample_end) == std::distance(int_resample_it, int_resample_end),
223  "Resample m/z and intensity iterators need to cover the same distance")
224  OPENMS_PRECONDITION(std::distance(mz_raw_it, mz_raw_end) == std::distance(int_raw_it, int_raw_end),
225  "Raw m/z and intensity iterators need to cover the same distance")
226  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
227 
228  PeakTypeIterator mz_resample_start = mz_resample_it;
229 
230  // need to get the raw iterator between two resampled iterators of the raw data
231  while (mz_raw_it != mz_raw_end && (*mz_raw_it) < (*mz_resample_it) )
232  {
233  (*int_resample_it) = *int_resample_it + *int_raw_it;
234  ++mz_raw_it;
235  ++int_raw_it;
236  }
237 
238  while (mz_raw_it != mz_raw_end)
239  {
240  //advance the resample iterator until our raw point is between two resampled iterators
241  while (mz_resample_it != mz_resample_end && *mz_resample_it < *mz_raw_it)
242  {
243  ++mz_resample_it; ++int_resample_it;
244  }
245  if (mz_resample_it != mz_resample_start)
246  {
247  --mz_resample_it; --int_resample_it;
248  }
249 
250  // if we have the last datapoint we break
251  if ((mz_resample_it + 1) == mz_resample_end) {break;}
252 
253  double dist_left = fabs(*mz_raw_it - *mz_resample_it);
254  double dist_right = fabs(*mz_raw_it - *(mz_resample_it + 1));
255 
256  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
257  *(int_resample_it) = *int_resample_it + (*int_raw_it) * dist_right / (dist_left + dist_right);
258  *(int_resample_it + 1) = *(int_resample_it + 1) + (*int_raw_it) * dist_left / (dist_left + dist_right);
259 
260  ++mz_raw_it;
261  ++int_raw_it;
262  }
263 
264  // add the final intensity to the right
265  while (mz_raw_it != mz_raw_end)
266  {
267  *int_resample_it = *int_resample_it + (*int_raw_it);
268  ++mz_raw_it;
269  ++int_raw_it;
270  }
271  }
272 
289  template <typename PeakTypeIterator>
290  void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resampled_start, PeakTypeIterator resampled_end)
291  {
292  // OPENMS_PRECONDITION(resampled_start != resampled_end, "Output iterators cannot be identical")
293  OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical") // as we use +1
294 
295  PeakTypeIterator raw_start = raw_it;
296 
297  // need to get the resampled iterator between two iterators of the raw data
298  while (resampled_start != resampled_end && resampled_start->getMZ() < raw_it->getMZ()) {resampled_start++;}
299 
300  while (resampled_start != resampled_end)
301  {
302  //advance the raw_iterator until our current point we want to interpolate is between them
303  while (raw_it != raw_end && raw_it->getMZ() < resampled_start->getMZ()) {raw_it++;}
304  if (raw_it != raw_start) {raw_it--;}
305 
306  // if we have the last datapoint we break
307  if ((raw_it + 1) == raw_end) {break;}
308 
309  // use a linear interpolation between raw_it and raw_it+1
310  double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
311  resampled_start->setIntensity(raw_it->getIntensity() + (resampled_start->getMZ() - raw_it->getMZ()) * m);
312  resampled_start++;
313  }
314 
315  }
316 
317 protected:
318 
320  bool ppm_;
321 
322  void updateMembers_() override
323  {
324  spacing_ = param_.getValue("spacing");
325  ppm_ = (bool)param_.getValue("ppm").toBool();
326  }
327 
329  template <typename PeakType>
330  void populate_raster_(std::vector<PeakType>& resampled_peak_container,
331  double start_pos, double end_pos, int number_resampled_points)
332  {
333  if (!ppm_)
334  {
335  // generate the resampled peaks at positions origin+i*spacing_
336  resampled_peak_container.resize(number_resampled_points);
337  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
338  for (int i = 0; i < number_resampled_points; ++i)
339  {
340  it->setMZ(start_pos + i * spacing_);
341  ++it;
342  }
343  }
344  else
345  {
346  // generate resampled peaks with ppm distance (not fixed)
347  double current_mz = start_pos;
348  while (current_mz < end_pos)
349  {
350  PeakType p;
351  p.setIntensity(0);
352  p.setMZ(current_mz);
353  resampled_peak_container.push_back(p);
354 
355  // increment current_mz
356  current_mz += current_mz * (spacing_ / 1e6);
357  }
358  }
359  }
360  };
361 
362 }
363 
364 
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:290
void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end, ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator, PeakTypeIterator mz_resample_it, PeakTypeIterator mz_resample_end, PeakTypeIterator int_resample_it, PeakTypeIterator)
Resample points (with m/z and intensity in separate containers, but of same length) from an input ran...
Definition: LinearResamplerAlign.h:215
bool ppm_
Spacing of the resampled data.
Definition: LinearResamplerAlign.h:320
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
Definition: LinearResamplerAlign.h:322
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:330
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: FeatureDeconvolution.h:22