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-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: 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 
42 
43 #include <set>
44 
45 namespace OpenMS
46 {
47 
55  class OPENMS_DLLAPI WindowMower :
56  public DefaultParamHandler
57  {
58 public:
59 
60  // @name Constructors, destructors and assignment operators
61  // @{
63  WindowMower();
65  virtual ~WindowMower();
66 
68  WindowMower(const WindowMower& source);
70  WindowMower& operator=(const WindowMower& source);
71  // @}
72 
74  template <typename SpectrumType>
76  {
77  typedef typename SpectrumType::ConstIterator ConstIterator;
78 
79  windowsize_ = (double)param_.getValue("windowsize");
80  peakcount_ = (UInt)param_.getValue("peakcount");
81 
82  //copy spectrum
83  SpectrumType old_spectrum = spectrum;
84  old_spectrum.sortByPosition();
85 
86  //find high peak positions
87  bool end = false;
88  std::set<double> positions;
89  for (ConstIterator it = old_spectrum.begin(); it != old_spectrum.end(); ++it)
90  {
91  // copy the window from the spectrum
92  SpectrumType window;
93  for (ConstIterator it2 = it; (it2->getPosition() - it->getPosition() < windowsize_); )
94  {
95  window.push_back(*it2);
96  if (++it2 == old_spectrum.end())
97  {
98  end = true;
99  break;
100  }
101  }
102 
103  //extract peakcount most intense peaks
104  window.sortByIntensity(true);
105  for (Size i = 0; i < peakcount_; ++i)
106  {
107  if (i < window.size())
108  {
109  positions.insert(window[i].getMZ());
110  }
111  }
112  //abort at the end of the spectrum
113  if (end) break;
114  }
115 
116  // select peaks that were retained
117  std::vector<Size> indices;
118  for (ConstIterator it = spectrum.begin(); it != spectrum.end(); ++it)
119  {
120  if (positions.find(it->getMZ()) != positions.end())
121  {
122  Size index(it - spectrum.begin());
123  indices.push_back(index);
124  }
125  }
126  spectrum.select(indices);
127  }
128 
129  void filterPeakSpectrum(PeakSpectrum& spectrum);
130 
131  void filterPeakMap(PeakMap& exp);
132 
133  // jumping window version (faster)
134  template <typename SpectrumType>
136  {
137  if (spectrum.empty())
138  {
139  return;
140  }
141 
142  spectrum.sortByPosition();
143 
144  windowsize_ = static_cast<double>(param_.getValue("windowsize"));
145  peakcount_ = static_cast<UInt>(param_.getValue("peakcount"));
146 
147  // copy meta data
148  SpectrumType out = spectrum;
149  out.clear(false);
150 
151  SpectrumType peaks_in_window;
152  double window_start = spectrum[0].getMZ();
153  for (Size i = 0; i != spectrum.size(); ++i)
154  {
155  if (spectrum[i].getMZ() - window_start < windowsize_) // collect peaks in window
156  {
157  peaks_in_window.push_back(spectrum[i]);
158  }
159  else // step over window boundaries
160  {
161  window_start = spectrum[i].getMZ(); // as there might be large gaps between peaks resulting in empty windows, set new window start to next peak
162 
163  // copy N highest peaks to out
164  if (peaks_in_window.size() > peakcount_)
165  {
166  std::partial_sort(peaks_in_window.begin(), peaks_in_window.begin() + peakcount_, peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
167  copy(peaks_in_window.begin(), peaks_in_window.begin() + peakcount_, back_inserter(out));
168  }
169  else
170  {
171  std::sort(peaks_in_window.begin(), peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
172  copy(peaks_in_window.begin(), peaks_in_window.end(), back_inserter(out));
173  }
174 
175  peaks_in_window.clear(false);
176  peaks_in_window.push_back(spectrum[i]);
177  }
178  }
179 
180  if (peaks_in_window.empty()) // last window is empty -> no special handling needed
181  {
182  // select peaks that were retained
183  std::vector<Size> indices;
184  for (typename SpectrumType::ConstIterator it = spectrum.begin(); it != spectrum.end(); ++it)
185  {
186  if (std::find(out.begin(), out.end(), *it) != out.end())
187  {
188  Size index(it - spectrum.begin());
189  indices.push_back(index);
190  }
191  }
192  spectrum.select(indices);
193  return;
194  }
195 
196  // Note that the last window might be much smaller than windowsize.
197  // Therefor the number of peaks copied from this window should be adapted accordingly.
198  // Otherwise a lot of noise peaks are copied from each end of a spectrum.
199 
200  double last_window_size = peaks_in_window.back().getMZ() - window_start;
201  double last_window_size_fraction = last_window_size / windowsize_;
202  Size last_window_peakcount = last_window_size_fraction * peakcount_;
203 
204  if (last_window_peakcount) // handle single peak in last window (will produce no proper fraction)
205  {
206  last_window_peakcount = 1;
207  }
208 
209  // sort for last_window_peakcount highest peaks
210  std::partial_sort(peaks_in_window.begin(), peaks_in_window.begin() + last_window_peakcount, peaks_in_window.end(), reverseComparator(typename SpectrumType::PeakType::IntensityLess()));
211 
212  if (peaks_in_window.size() > last_window_peakcount)
213  {
214  std::copy(peaks_in_window.begin(), peaks_in_window.begin() + last_window_peakcount, back_inserter(out));
215  }
216  else
217  {
218  std::copy(peaks_in_window.begin(), peaks_in_window.end(), std::back_inserter(out));
219  }
220 
221  // select peaks that were retained
222  std::vector<Size> indices;
223  for (typename SpectrumType::ConstIterator it = spectrum.begin(); it != spectrum.end(); ++it)
224  {
225  if (std::find(out.begin(), out.end(), *it) != out.end())
226  {
227  Size index(it - spectrum.begin());
228  indices.push_back(index);
229  }
230  }
231  spectrum.select(indices);
232 
233  return;
234  }
235 
236  //TODO reimplement DefaultParamHandler::updateMembers_()
237 
238 private:
239  double windowsize_;
241  };
242 
243 }
244 
245 #endif //OPENMS_FILTERING_TRANSFORMERS_WINDOWMOWER_H
246 
WindowMower augments the highest peaks in a sliding or jumping window.
Definition: WindowMower.h:55
void sortByPosition()
Lexicographically sorts the peaks by their position.
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:104
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:75
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
void filterPeakSpectrumForTopNInJumpingWindow(SpectrumType &spectrum)
Definition: WindowMower.h:135
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
UInt peakcount_
Definition: WindowMower.h:240
void clear(bool clear_meta_data)
Clears all data and meta data.
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
double windowsize_
Definition: WindowMower.h:239
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
MSSpectrum & select(const std::vector< Size > &indices)

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