Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
LinearResamplerAlign.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
36 #define OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
37 
39 #include <OpenMS/CONCEPT/Macros.h>
40 
41 namespace OpenMS
42 {
43 
58  class OPENMS_DLLAPI LinearResamplerAlign :
59  public LinearResampler
60  {
61 
62 public:
63 
65  {
66  defaults_.setValue("spacing", 0.05, "Spacing of the resampled output peaks.");
67  defaults_.setValue("ppm", "false", "Whether spacing is in ppm or Th");
68  defaultsToParam_();
69  }
70 
80  template <class SpecT>
81  void raster(SpecT& container)
82  {
83  //return if nothing to do
84  if (container.empty()) return;
85 
86  typename SpecT::iterator first = container.begin();
87  typename SpecT::iterator last = container.end();
88 
89  double end_pos = (last - 1)->getMZ();
90  double start_pos = first->getMZ();
91  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
92 
93  std::vector<typename SpecT::PeakType> resampled_peak_container;
94  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
95 
96  raster(container.begin(), container.end(), resampled_peak_container.begin(), resampled_peak_container.end());
97 
98  container.swap(resampled_peak_container);
99  }
100 
116  template <typename SpecT>
117  void raster_align(SpecT& container, double start_pos, double end_pos)
118  {
119  //return if nothing to do
120  if (container.empty()) return;
121 
122  if (end_pos < start_pos)
123  {
124  std::vector<typename SpecT::PeakType> empty;
125  container.swap(empty);
126  return;
127  }
128 
129  typename SpecT::iterator first = container.begin();
130  typename SpecT::iterator last = container.end();
131 
132  // get the iterators just before / after the two points start_pos / end_pos
133  while (first != container.end() && (first)->getMZ() < start_pos) {++first;}
134  while (last != first && (last - 1)->getMZ() > end_pos) {--last;}
135 
136  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
137 
138  std::vector<typename SpecT::PeakType> resampled_peak_container;
139  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
140 
141  raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
142 
143  container.swap(resampled_peak_container);
144  }
145 
167  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
168  void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
169  {
170  OPENMS_PRECONDITION(resample_it != resample_end, "Output iterators cannot be identical") // as we use +1
171  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
172 
173  PeakTypeIterator resample_start = resample_it;
174 
175  // need to get the raw iterator between two resampled iterators of the raw data
176  while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ())
177  {
178  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
179  raw_it++;
180  }
181 
182  while (raw_it != raw_end)
183  {
184  //advance the resample iterator until our raw point is between two resampled iterators
185  while (resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++;}
186  if (resample_it != resample_start) {resample_it--;}
187 
188  // if we have the last datapoint we break
189  if ((resample_it + 1) == resample_end) {break;}
190 
191  double dist_left = fabs(raw_it->getMZ() - resample_it->getMZ());
192  double dist_right = fabs(raw_it->getMZ() - (resample_it + 1)->getMZ());
193 
194  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
195  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left + dist_right));
196  (resample_it + 1)->setIntensity((resample_it + 1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left + dist_right));
197 
198  raw_it++;
199  }
200 
201  // add the final intensity to the right
202  while (raw_it != raw_end)
203  {
204  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
205  raw_it++;
206  }
207  }
208 
234  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
235 #ifdef OPENMS_ASSERTIONS
236  void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
237  ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator int_raw_end,
238  PeakTypeIterator mz_resample_it, PeakTypeIterator mz_resample_end,
239  PeakTypeIterator int_resample_it, PeakTypeIterator int_resample_end)
240 #else
241  void raster(ConstPeakTypeIterator mz_raw_it, ConstPeakTypeIterator mz_raw_end,
242  ConstPeakTypeIterator int_raw_it, ConstPeakTypeIterator /* int_raw_end */,
243  PeakTypeIterator mz_resample_it, PeakTypeIterator mz_resample_end,
244  PeakTypeIterator int_resample_it, PeakTypeIterator /* int_resample_end */)
245 #endif
246  {
247  OPENMS_PRECONDITION(mz_resample_it != mz_resample_end, "Output iterators cannot be identical") // as we use +1
248  OPENMS_PRECONDITION(std::distance(mz_resample_it, mz_resample_end) == std::distance(int_resample_it, int_resample_end),
249  "Resample m/z and intensity iterators need to cover the same distance")
250  OPENMS_PRECONDITION(std::distance(mz_raw_it, mz_raw_end) == std::distance(int_raw_it, int_raw_end),
251  "Raw m/z and intensity iterators need to cover the same distance")
252  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
253 
254  PeakTypeIterator mz_resample_start = mz_resample_it;
255 
256  // need to get the raw iterator between two resampled iterators of the raw data
257  while (mz_raw_it != mz_raw_end && (*mz_raw_it) < (*mz_resample_it) )
258  {
259  (*int_resample_it) = *int_resample_it + *int_raw_it;
260  ++mz_raw_it;
261  ++int_raw_it;
262  }
263 
264  while (mz_raw_it != mz_raw_end)
265  {
266  //advance the resample iterator until our raw point is between two resampled iterators
267  while (mz_resample_it != mz_resample_end && *mz_resample_it < *mz_raw_it)
268  {
269  ++mz_resample_it; ++int_resample_it;
270  }
271  if (mz_resample_it != mz_resample_start)
272  {
273  --mz_resample_it; --int_resample_it;
274  }
275 
276  // if we have the last datapoint we break
277  if ((mz_resample_it + 1) == mz_resample_end) {break;}
278 
279  double dist_left = fabs(*mz_raw_it - *mz_resample_it);
280  double dist_right = fabs(*mz_raw_it - *(mz_resample_it + 1));
281 
282  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
283  *(int_resample_it) = *int_resample_it + (*int_raw_it) * dist_right / (dist_left + dist_right);
284  *(int_resample_it + 1) = *(int_resample_it + 1) + (*int_raw_it) * dist_left / (dist_left + dist_right);
285 
286  ++mz_raw_it;
287  ++int_raw_it;
288  }
289 
290  // add the final intensity to the right
291  while (mz_raw_it != mz_raw_end)
292  {
293  *int_resample_it = *int_resample_it + (*int_raw_it);
294  ++mz_raw_it;
295  ++int_raw_it;
296  }
297  }
298 
315  template <typename PeakTypeIterator>
316  void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resampled_end)
317  {
318  // OPENMS_PRECONDITION(resample_it != resampled_end, "Output iterators cannot be identical")
319  OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical") // as we use +1
320 
321  PeakTypeIterator raw_start = raw_it;
322 
323  // need to get the resampled iterator between two iterators of the raw data
324  while (resample_it != resampled_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++;}
325 
326  while (resample_it != resampled_end)
327  {
328  //advance the raw_iterator until our current point we want to interpolate is between them
329  while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ()) {raw_it++;}
330  if (raw_it != raw_start) {raw_it--;}
331 
332  // if we have the last datapoint we break
333  if ((raw_it + 1) == raw_end) {break;}
334 
335  // use a linear interpolation between raw_it and raw_it+1
336  double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
337  resample_it->setIntensity(raw_it->getIntensity() + (resample_it->getMZ() - raw_it->getMZ()) * m);
338  resample_it++;
339  }
340 
341  }
342 
343 protected:
344 
346  bool ppm_;
347 
348  virtual void updateMembers_()
349  {
350  spacing_ = param_.getValue("spacing");
351  ppm_ = (bool)param_.getValue("ppm").toBool();
352  }
353 
355  template <typename PeakType>
356  void populate_raster_(std::vector<PeakType>& resampled_peak_container,
357  double start_pos, double end_pos, int number_resampled_points)
358  {
359  if (!ppm_)
360  {
361  // generate the resampled peaks at positions origin+i*spacing_
362  resampled_peak_container.resize(number_resampled_points);
363  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
364  for (int i = 0; i < number_resampled_points; ++i)
365  {
366  it->setMZ(start_pos + i * spacing_);
367  ++it;
368  }
369  }
370  else
371  {
372  // generate resampled peaks with ppm distance (not fixed)
373  double current_mz = start_pos;
374  while (current_mz < end_pos)
375  {
376  PeakType p;
377  p.setIntensity(0);
378  p.setMZ(current_mz);
379  resampled_peak_container.push_back(p);
380 
381  // increment current_mz
382  current_mz += current_mz * (spacing_ / 1e6);
383  }
384  }
385  }
386  };
387 
388 }
389 
390 #endif // OPENMS_FILTERING_TRANSFORMERS_LINEARRESAMPLERALIGN_H
391 
void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resampled_end)
Applies the resampling algorithm using a linear interpolation.
Definition: LinearResamplerAlign.h:316
Linear Resampling of raw data.
Definition: LinearResampler.h:62
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:55
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:203
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setIntensity(IntensityType intensity)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:173
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:356
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)
Applies the resampling algorithm.
Definition: LinearResamplerAlign.h:241
bool ppm_
Spacing of the resampled data.
Definition: LinearResamplerAlign.h:346
void raster(SpecT &container)
Applies the resampling algorithm to a container (MSSpectrum or MSChromatogram).
Definition: LinearResamplerAlign.h:81
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:58
virtual void updateMembers_()
This method is used to update extra member variables at the end of the setParameters() method...
Definition: LinearResamplerAlign.h:348
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:117
void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
Applies the resampling algorithm.
Definition: LinearResamplerAlign.h:168
LinearResamplerAlign()
Definition: LinearResamplerAlign.h:64

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:01 using doxygen 1.8.13