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-2016.
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 
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");
69  }
70 
74  template <template <typename> class SpecT, typename PeakType>
75  void raster(SpecT<PeakType>& spectrum)
76  {
77  //return if nothing to do
78  if (spectrum.empty()) return;
79 
80  typename SpecT<PeakType>::iterator first = spectrum.begin();
81  typename SpecT<PeakType>::iterator last = spectrum.end();
82 
83  double end_pos = (last - 1)->getMZ();
84  double start_pos = first->getMZ();
85  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
86 
87  typename std::vector<PeakType> resampled_peak_container;
88  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
89 
90  raster(spectrum.begin(), spectrum.end(), resampled_peak_container.begin(), resampled_peak_container.end());
91 
92  spectrum.swap(resampled_peak_container);
93  }
94 
102  template <template <typename> class SpecT, typename PeakType>
103  void raster_align(SpecT<PeakType>& spectrum, double start_pos, double end_pos)
104  {
105  //return if nothing to do
106  if (spectrum.empty()) return;
107 
108  if (end_pos < start_pos)
109  {
110  typename std::vector<PeakType> empty;
111  spectrum.swap(empty);
112  return;
113  }
114 
115  typename SpecT<PeakType>::iterator first = spectrum.begin();
116  typename SpecT<PeakType>::iterator last = spectrum.end();
117 
118  // get the iterators just before / after the two points start_pos / end_pos
119  while (first != spectrum.end() && (first)->getMZ() < start_pos) {++first; }
120  while (last != first && (last - 1)->getMZ() > end_pos) {--last; }
121 
122  int number_resampled_points = (int)(ceil((end_pos - start_pos) / spacing_ + 1));
123 
124  typename std::vector<PeakType> resampled_peak_container;
125  populate_raster_(resampled_peak_container, start_pos, end_pos, number_resampled_points);
126 
127  raster(first, last, resampled_peak_container.begin(), resampled_peak_container.end());
128 
129  spectrum.swap(resampled_peak_container);
130  }
131 
146  template <typename PeakTypeIterator, typename ConstPeakTypeIterator>
147  void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
148  {
149  OPENMS_PRECONDITION(resample_it != resample_end, "Output iterators cannot be identical") // as we use +1
150  // OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical")
151 
152  PeakTypeIterator resample_start = resample_it;
153 
154  // need to get the raw iterator between two resampled iterators of the raw data
155  while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ())
156  {
157  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
158  raw_it++;
159  }
160 
161  while (raw_it != raw_end)
162  {
163  //advance the resample iterator until our raw point is between two resampled iterators
164  while (resample_it != resample_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++; }
165  if (resample_it != resample_start) {resample_it--; }
166 
167  // if we have the last datapoint we break
168  if ((resample_it + 1) == resample_end) {break; }
169 
170  double dist_left = fabs(raw_it->getMZ() - resample_it->getMZ());
171  double dist_right = fabs(raw_it->getMZ() - (resample_it + 1)->getMZ());
172 
173  // distribute the intensity of the raw point according to the distance to resample_it and resample_it+1
174  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity() * dist_right / (dist_left + dist_right));
175  (resample_it + 1)->setIntensity((resample_it + 1)->getIntensity() + raw_it->getIntensity() * dist_left / (dist_left + dist_right));
176 
177  raw_it++;
178  }
179 
180  // add the final intensity to the right
181  while (raw_it != raw_end)
182  {
183  resample_it->setIntensity(resample_it->getIntensity() + raw_it->getIntensity());
184  raw_it++;
185  }
186  }
187 
202  template <typename PeakTypeIterator>
203  void raster_interpolate(PeakTypeIterator raw_it, PeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resampled_end)
204  {
205  // OPENMS_PRECONDITION(resample_it != resampled_end, "Output iterators cannot be identical")
206  OPENMS_PRECONDITION(raw_it != raw_end, "Input iterators cannot be identical") // as we use +1
207 
208  PeakTypeIterator raw_start = raw_it;
209 
210  // need to get the resampled iterator between two iterators of the raw data
211  while (resample_it != resampled_end && resample_it->getMZ() < raw_it->getMZ()) {resample_it++; }
212 
213  while (resample_it != resampled_end)
214  {
215  //advance the raw_iterator until our current point we want to interpolate is between them
216  while (raw_it != raw_end && raw_it->getMZ() < resample_it->getMZ()) {raw_it++; }
217  if (raw_it != raw_start) {raw_it--; }
218 
219  // if we have the last datapoint we break
220  if ((raw_it + 1) == raw_end) {break; }
221 
222  // use a linear interpolation between raw_it and raw_it+1
223  double m = ((raw_it + 1)->getIntensity() - raw_it->getIntensity()) / ((raw_it + 1)->getMZ() - raw_it->getMZ());
224  resample_it->setIntensity(raw_it->getIntensity() + (resample_it->getMZ() - raw_it->getMZ()) * m);
225  resample_it++;
226  }
227 
228  }
229 
230 protected:
231 
233  bool ppm_;
234 
235  virtual void updateMembers_()
236  {
237  spacing_ = param_.getValue("spacing");
238  ppm_ = (bool)param_.getValue("ppm").toBool();
239  }
240 
242  template <typename PeakType>
243  void populate_raster_(std::vector<PeakType>& resampled_peak_container,
244  double start_pos, double end_pos, int number_resampled_points)
245  {
246  if (!ppm_)
247  {
248  // generate the resampled peaks at positions origin+i*spacing_
249  resampled_peak_container.resize(number_resampled_points);
250  typename std::vector<PeakType>::iterator it = resampled_peak_container.begin();
251  for (int i = 0; i < number_resampled_points; ++i)
252  {
253  it->setMZ(start_pos + i * spacing_);
254  ++it;
255  }
256  }
257  else
258  {
259  // generate resampled peaks with ppm distance (not fixed)
260  double current_mz = start_pos;
261  while (current_mz < end_pos)
262  {
263  PeakType p;
264  p.setIntensity(0);
265  p.setMZ(current_mz);
266  resampled_peak_container.push_back(p);
267 
268  // increment current_mz
269  current_mz += current_mz * (spacing_ / 1e6);
270  }
271  }
272  }
273  };
274 
275 }
276 
277 #endif
void raster(SpecT< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:75
Param defaults_
Container for default parameters. This member should be filled in the constructor of derived classes!...
Definition: DefaultParamHandler.h:157
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:203
void setValue(const String &key, const DataValue &value, const String &description="", const StringList &tags=StringList())
Sets a value.
Linear Resampling of raw data.
Definition: LinearResampler.h:61
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
Param param_
Container for current parameters.
Definition: DefaultParamHandler.h:150
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
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:103
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:243
const DataValue & getValue(const String &key) const
Returns a value of a parameter.
bool ppm_
Spacing of the resampled data.
Definition: LinearResamplerAlign.h:233
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:235
void raster(ConstPeakTypeIterator raw_it, ConstPeakTypeIterator raw_end, PeakTypeIterator resample_it, PeakTypeIterator resample_end)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:147
bool toBool() const
Conversion to bool.
LinearResamplerAlign()
Definition: LinearResamplerAlign.h:64
double spacing_
Spacing of the resampled data.
Definition: LinearResampler.h:162
void defaultsToParam_()
Updates the parameters after the defaults have been set in the constructor.

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Jan 31 2017 06:39:47 using doxygen 1.8.8