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-2015.
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 
40 namespace OpenMS
41 {
42 
58  public LinearResampler
59  {
60 
61 public:
62 
66  template <template <typename> class SpecT, typename PeakType>
67  void raster(SpecT<PeakType>& spectrum)
68  {
69  //return if nothing to do
70  if (spectrum.empty()) return;
71 
72  typename SpecT<PeakType>::iterator first = spectrum.begin();
73  typename SpecT<PeakType>::iterator last = spectrum.end();
74 
75  double end_pos = (last - 1)->getMZ();
76  double start_pos = first->getMZ();
77  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
78 
79  typename std::vector<PeakType> resampled_peak_container;
80  resampled_peak_container.resize(number_resampled_points);
81 
82  // generate the resampled peaks at positions origin+i*spacing_
83  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
84  for (int i = 0; i < number_resampled_points; ++i)
85  {
86  it->setMZ(start_pos + i * spacing_);
87  ++it;
88  }
89 
90  raster(spectrum.begin(), spectrum.end(), resampled_peak_container.begin(), resampled_peak_container.end());
91 
92  spectrum.swap(resampled_peak_container);
93  }
94 
98  template <template <typename> class SpecT, typename PeakType>
99  void raster_align(SpecT<PeakType>& spectrum, double start_pos, double end_pos)
100  {
101  //return if nothing to do
102  if (spectrum.empty()) return;
103 
104  if (end_pos < start_pos)
105  {
106  typename std::vector<PeakType> empty;
107  spectrum.swap(empty);
108  return;
109  }
110 
111  typename SpecT<PeakType>::iterator first = spectrum.begin();
112  typename SpecT<PeakType>::iterator last = spectrum.end();
113 
114  // get the iterators just before / after the two points start_pos / end_pos
115  while (first != spectrum.end() && (first)->getMZ() < start_pos) {++first; }
116  while (last != first && (last - 1)->getMZ() > end_pos) {--last; }
117 
118  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
119 
120  typename std::vector<PeakType> resampled_peak_container;
121  resampled_peak_container.resize(number_resampled_points);
122 
123  // generate the resampled peaks at positions origin+i*spacing_
124  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
125  for (int i = 0; i < number_resampled_points; ++i)
126  {
127  it->setMZ(start_pos + i * spacing_);
128  ++it;
129  }
130 
131  raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
132 
133  spectrum.swap(resampled_peak_container);
134  }
135 
139  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
140  void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
141  {
142  PeakTypeIterator resample_start = resample_it;
143 
144  // need to get the raw iterator between two resampled iterators of the raw data
145  while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ())
146  {
147  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
148  raw_it++;
149  }
150 
151  while (raw_it != raw_end)
152  {
153  //advance the resample iterator until our raw point is between two resampled iterators
154  while (resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++; }
155  if (resample_it != resample_start) {resample_it--; }
156 
157  // if we have the last datapoint we break
158  if ((resample_it + 1) == resample_end) {break; }
159 
160  double dist_left = fabs(raw_it->getMZ() - resample_it->getMZ());
161  double dist_right = fabs(raw_it->getMZ() - (resample_it + 1)->getMZ());
162 
163  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
164  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left + dist_right));
165  (resample_it + 1)->setIntensity((resample_it + 1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left + dist_right));
166 
167  raw_it++;
168  }
169 
170  // add the final intensity to the right
171  while (raw_it != raw_end)
172  {
173  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
174  raw_it++;
175  }
176  }
177 
181  template <typename PeakTypeIterator>
182  void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator it, PeakTypeIterator resampled_end)
183  {
184  PeakTypeIterator raw_start = raw_it;
185 
186  // need to get the resampled iterator between two iterators of the raw data
187  while (it != resampled_end && it->getMZ() < raw_it->getMZ()) {it++; }
188 
189  while (it != resampled_end)
190  {
191  //advance the raw_iterator until our current point we want to interpolate is between them
192  while (raw_it != raw_end && raw_it->getMZ() < it->getMZ()) {raw_it++; }
193  if (raw_it != raw_start) {raw_it--; }
194 
195  // if we have the last datapoint we break
196  if ((raw_it + 1) == raw_end) {break; }
197 
198  // use a linear interpolation between raw_it and raw_it+1
199  double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
200  it->setIntensity(raw_it->getIntensity() + (it->getMZ() - raw_it->getMZ()) * m);
201  it++;
202  }
203 
204  }
205 
206  };
207 
208 }
209 
210 #endif
void raster(SpecT< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:67
Linear Resampling of raw data.
Definition: LinearResampler.h:61
A 2-dimensional raw data point or peak.
Definition: Peak2D.h:55
void raster_align(SpecT< PeakType > &spectrum, double start_pos, double end_pos)
Applies the resampling algorithm to an MSSpectrum but it will be aligned between start_pos and end_po...
Definition: LinearResamplerAlign.h:99
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:57
void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:140
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:162
void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator it, PeakTypeIterator resampled_end)
Applies the resampling algorithm using a linear interpolation.
Definition: LinearResamplerAlign.h:182

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Nov 1 2016 16:34:46 using doxygen 1.8.11