Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
GaussFilter.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: Timo Sachsenberg $
32 // $Authors: Eva Lange $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_FILTERING_SMOOTHING_GAUSSFILTER_H
36 #define OPENMS_FILTERING_SMOOTHING_GAUSSFILTER_H
37 
45 
46 #include <cmath>
47 
48 namespace OpenMS
49 {
73 //#define DEBUG_FILTERING
74 
75  class OPENMS_DLLAPI GaussFilter :
76  public ProgressLogger,
77  public DefaultParamHandler
78  {
79 public:
81  GaussFilter();
82 
84  virtual ~GaussFilter();
85 
93  void filter(MSSpectrum & spectrum)
94  {
95  typedef std::vector<double> ContainerT;
96 
97  // make sure the right data type is set
99  bool found_signal = false;
100  const Size data_size = spectrum.size();
101  ContainerT mz_in(data_size), int_in(data_size), mz_out(data_size), int_out(data_size);
102 
103  // copy spectrum to container
104  for (Size p = 0; p < spectrum.size(); ++p)
105  {
106  mz_in[p] = spectrum[p].getMZ();
107  int_in[p] = static_cast<double>(spectrum[p].getIntensity());
108  }
109 
110  // apply filter
111  ContainerT::iterator mz_out_it = mz_out.begin();
112  ContainerT::iterator int_out_it = int_out.begin();
113  found_signal = gauss_algo_.filter(mz_in.begin(), mz_in.end(), int_in.begin(), mz_out_it, int_out_it);
114 
115  // If all intensities are zero in the scan and the scan has a reasonable size, throw an exception.
116  // This is the case if the Gaussian filter is smaller than the spacing of raw data
117  if (!found_signal && spectrum.size() >= 3)
118  {
119  String error_message = "Found no signal. The Gaussian width is probably smaller than the spacing in your profile data. Try to use a bigger width.";
120  if (spectrum.getRT() > 0.0)
121  {
122  error_message += String(" The error occurred in the spectrum with retention time ") + spectrum.getRT() + ".";
123  }
124  LOG_ERROR << error_message << std::endl;
125  }
126  else
127  {
128  // copy the new data into the spectrum
129  ContainerT::iterator mz_it = mz_out.begin();
130  ContainerT::iterator int_it = int_out.begin();
131  for (Size p = 0; mz_it != mz_out.end(); mz_it++, int_it++, p++)
132  {
133  spectrum[p].setIntensity(*int_it);
134  spectrum[p].setMZ(*mz_it);
135  }
136  }
137  }
138 
139  void filter(MSChromatogram & chromatogram)
140  {
141  typedef std::vector<double> ContainerT;
142 
143  if (param_.getValue("use_ppm_tolerance").toBool())
144  {
145  throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
146  "GaussFilter: Cannot use ppm tolerance on chromatograms");
147  }
148 
149  bool found_signal = false;
150  const Size data_size = chromatogram.size();
151  ContainerT rt_in(data_size), int_in(data_size), rt_out(data_size), int_out(data_size);
152 
153  // copy spectrum to container
154  for (Size p = 0; p < chromatogram.size(); ++p)
155  {
156  rt_in[p] = chromatogram[p].getRT();
157  int_in[p] = chromatogram[p].getIntensity();
158  }
159 
160  // apply filter
161  ContainerT::iterator mz_out_it = rt_out.begin();
162  ContainerT::iterator int_out_it = int_out.begin();
163  found_signal = gauss_algo_.filter(rt_in.begin(), rt_in.end(), int_in.begin(), mz_out_it, int_out_it);
164 
165  // If all intensities are zero in the scan and the scan has a reasonable size, throw an exception.
166  // This is the case if the Gaussian filter is smaller than the spacing of raw data
167  if (!found_signal && chromatogram.size() >= 3)
168  {
169  String error_message = "Found no signal. The Gaussian width is probably smaller than the spacing in your chromatogram data. Try to use a bigger width.";
170  if (chromatogram.getMZ() > 0.0)
171  {
172  error_message += String(" The error occurred in the chromatogram with m/z time ") + chromatogram.getMZ() + ".";
173  }
174  LOG_ERROR << error_message << std::endl;
175  }
176  else
177  {
178  // copy the new data into the spectrum
179  ContainerT::iterator mz_it = rt_out.begin();
180  ContainerT::iterator int_it = int_out.begin();
181  for (Size p = 0; mz_it != rt_out.end(); mz_it++, int_it++, p++)
182  {
183  chromatogram[p].setIntensity(*int_it);
184  chromatogram[p].setMZ(*mz_it);
185  }
186  }
187  }
188 
195  {
196  Size progress = 0;
197  startProgress(0, map.size() + map.getChromatograms().size(), "smoothing data");
198  for (Size i = 0; i < map.size(); ++i)
199  {
200  filter(map[i]);
201  setProgress(++progress);
202  }
203  for (Size i = 0; i < map.getChromatograms().size(); ++i)
204  {
205  filter(map.getChromatogram(i));
206  setProgress(++progress);
207  }
208  endProgress();
209  }
210 
211 protected:
212 
214 
216  double spacing_;
217 
218  // Docu in base class
219  virtual void updateMembers_();
220  };
221 
222 } // namespace OpenMS
223 #endif
const std::vector< MSChromatogram > & getChromatograms() const
returns the chromatogram list
Definition: MSExperiment.h:861
A more convenient string class.
Definition: String.h:57
double spacing_
The spacing of the pre-tabulated kernel coefficients.
Definition: GaussFilter.h:216
The representation of a chromatogram.
Definition: MSChromatogram.h:55
Raw data (also called profile data)
Definition: SpectrumSettings.h:75
double getMZ() const
returns the mz of the product entry, makes sense especially for MRM scans
Size size() const
Definition: MSExperiment.h:132
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
#define LOG_ERROR
Macro to be used if non-fatal error are reported (processing continues)
Definition: LogStream.h:447
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
A method or algorithm argument contains illegal values.
Definition: Exception.h:649
GaussFilterAlgorithm gauss_algo_
Definition: GaussFilter.h:213
void filter(MSChromatogram &chromatogram)
Definition: GaussFilter.h:139
This class represents a Gaussian lowpass-filter which works on uniform as well as on non-uniform prof...
Definition: GaussFilterAlgorithm.h:70
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
This class represents a Gaussian lowpass-filter which works on uniform as well as on non-uniform prof...
Definition: GaussFilter.h:75
void filterExperiment(PeakMap &map)
Smoothes an MSExperiment containing profile data.
Definition: GaussFilter.h:194
void setType(SpectrumType type)
sets the spectrum type
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:55
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
void filter(MSSpectrum &spectrum)
Smoothes an MSSpectrum containing profile data.
Definition: GaussFilter.h:93
MSChromatogram & getChromatogram(Size id)
returns a single chromatogram
Definition: MSExperiment.h:875
double getRT() const

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