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