Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
WindowMower.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: Mathias Walzer $
32 // $Authors: Mathias Walzer, Timo Sachsenberg$
33 // --------------------------------------------------------------------------
34 //
35 #ifndef OPENMS_FILTERING_TRANSFORMERS_WINDOWMOWER_H
36 #define OPENMS_FILTERING_TRANSFORMERS_WINDOWMOWER_H
37 
40 
41 #include <set>
42 
43 namespace OpenMS
44 {
45 
53  class OPENMS_DLLAPI WindowMower :
54  public DefaultParamHandler
55  {
56 public:
57 
58  // @name Constructors, destructors and assignment operators
59  // @{
61  WindowMower();
63  virtual ~WindowMower();
64 
66  WindowMower(const WindowMower& source);
68  WindowMower& operator=(const WindowMower& source);
69  // @}
70 
72  template <typename SpectrumType>
73  void filterPeakSpectrumForTopNInSlidingWindow(SpectrumType& spectrum)
74  {
75  typedef typename SpectrumType::ConstIterator ConstIterator;
76 
77  windowsize_ = (double)param_.getValue("windowsize");
78  peakcount_ = (UInt)param_.getValue("peakcount");
79 
80  //copy spectrum
81  SpectrumType old_spectrum = spectrum;
82  old_spectrum.sortByPosition();
83 
84  //find high peak positions
85  bool end = false;
86  std::set<double> positions;
87  for (ConstIterator it = old_spectrum.begin(); it != old_spectrum.end(); ++it)
88  {
89  // copy the window from the spectrum
90  SpectrumType window;
91  for (ConstIterator it2 = it; (it2->getPosition() - it->getPosition() < windowsize_); )
92  {
93  window.push_back(*it2);
94  if (++it2 == old_spectrum.end())
95  {
96  end = true;
97  break;
98  }
99  }
100 
101  //extract peakcount most intense peaks
102  window.sortByIntensity(true);
103  for (Size i = 0; i < peakcount_; ++i)
104  {
105  if (i < window.size())
106  {
107  positions.insert(window[i].getMZ());
108  }
109  }
110  //abort at the end of the spectrum
111  if (end) break;
112  }
113 
114  // replace the old peaks by the new ones
115  spectrum.clear(false);
116  for (ConstIterator it = old_spectrum.begin(); it != old_spectrum.end(); ++it)
117  {
118  if (positions.find(it->getMZ()) != positions.end())
119  {
120  spectrum.push_back(*it);
121  }
122  }
123  }
124 
125  void filterPeakSpectrum(PeakSpectrum& spectrum);
126 
127  void filterPeakMap(PeakMap& exp);
128 
129  // jumping window version (faster)
130  template <typename SpectrumType>
131  void filterPeakSpectrumForTopNInJumpingWindow(SpectrumType& spectrum)
132  {
133  if (spectrum.empty())
134  {
135  return;
136  }
137 
138  spectrum.sortByPosition();
139 
140  windowsize_ = static_cast<double>(param_.getValue("windowsize"));
141  peakcount_ = static_cast<UInt>(param_.getValue("peakcount"));
142 
143  // copy meta data
144  SpectrumType out = spectrum;
145  out.clear(false);
146 
147  SpectrumType peaks_in_window;
148  double window_start = spectrum[0].getMZ();
149  for (Size i = 0; i != spectrum.size(); ++i)
150  {
151  if (spectrum[i].getMZ() - window_start < windowsize_) // collect peaks in window
152  {
153  peaks_in_window.push_back(spectrum[i]);
154  }
155  else // step over window boundaries
156  {
157  window_start = spectrum[i].getMZ(); // as there might be large gaps between peaks resulting in empty windows, set new window start to next peak
158 
159  // copy N highest peaks to out
160  if (peaks_in_window.size() > peakcount_)
161  {
162  std::partial_sort(peaks_in_window.begin(), peaks_in_window.begin() + peakcount_, peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
163  copy(peaks_in_window.begin(), peaks_in_window.begin() + peakcount_, back_inserter(out));
164  }
165  else
166  {
167  std::sort(peaks_in_window.begin(), peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
168  copy(peaks_in_window.begin(), peaks_in_window.end(), back_inserter(out));
169  }
170 
171  peaks_in_window.clear(false);
172  peaks_in_window.push_back(spectrum[i]);
173  }
174  }
175 
176  if (peaks_in_window.empty()) // last window is empty -> no special handling needed
177  {
178  out.sortByPosition();
179  spectrum = out;
180  return;
181  }
182 
183  // Note that the last window might be much smaller than windowsize.
184  // Therefor the number of peaks copied from this window should be adapted accordingly.
185  // Otherwise a lot of noise peaks are copied from each end of a spectrum.
186 
187  double last_window_size = peaks_in_window.back().getMZ() - window_start;
188  double last_window_size_fraction = last_window_size / windowsize_;
189  Size last_window_peakcount = last_window_size_fraction * peakcount_;
190 
191  if (last_window_peakcount) // handle single peak in last window (will produce no proper fraction)
192  {
193  last_window_peakcount = 1;
194  }
195 
196  // sort for last_window_peakcount highest peaks
197  std::partial_sort(peaks_in_window.begin(), peaks_in_window.begin() + last_window_peakcount, peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
198 
199  if (peaks_in_window.size() > last_window_peakcount)
200  {
201  std::copy(peaks_in_window.begin(), peaks_in_window.begin() + last_window_peakcount, back_inserter(out));
202  }
203  else
204  {
205  std::copy(peaks_in_window.begin(), peaks_in_window.end(), std::back_inserter(out));
206  }
207 
208  out.sortByPosition();
209  spectrum = out;
210  return;
211  }
212 
213  //TODO reimplement DefaultParamHandler::updateMembers_()
214 
215 private:
216  double windowsize_;
218  };
219 
220 }
221 
222 #endif //OPENMS_FILTERING_TRANSFORMERS_WINDOWMOWER_H
WindowMower augments the highest peaks in a sliding or jumping window.
Definition: WindowMower.h:53
unsigned int UInt
Unsigned integer type.
Definition: Types.h:88
ReverseComparator< Cmp > reverseComparator(Cmp const &cmp)
Make-function to create a ReverseComparator from another comparator without the need to specify the t...
Definition: ComparatorUtils.h:261
void filterPeakSpectrumForTopNInSlidingWindow(SpectrumType &spectrum)
sliding window version (slower)
Definition: WindowMower.h:73
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void filterPeakSpectrumForTopNInJumpingWindow(SpectrumType &spectrum)
Definition: WindowMower.h:131
UInt peakcount_
Definition: WindowMower.h:217
double windowsize_
Definition: WindowMower.h:216
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92

OpenMS / TOPP release 2.0.0 Documentation generated on Sat May 16 2015 16:13:38 using doxygen 1.8.9.1