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

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:16 using doxygen 1.8.5