Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
FeaFiModule.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: Marc Sturm, Clemens Groepl $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
36 #define OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
37 
43 
44 namespace OpenMS
45 {
46 
47  class FeatureFinder;
48 
49  namespace Internal
50  {
51  //-------------------------------------------------------------------
52 
56  template <typename FeaFiModuleType>
57  struct IntensityLess :
58  std::binary_function<typename FeatureFinderDefs::IndexPair, typename FeatureFinderDefs::IndexPair, bool>
59  {
61  IntensityLess(const FeaFiModuleType & module) :
62  module_(module)
63  {
64  }
65 
68  module_(rhs.module_)
69  {
70  }
71 
73  inline bool operator()(const typename FeatureFinderDefs::IndexPair & left, const typename FeatureFinderDefs::IndexPair & right) const
74  {
75  return module_.getPeakIntensity(left) < module_.getPeakIntensity(right);
76  }
77 
78 private:
80  const FeaFiModuleType & module_;
82  IntensityLess();
83  }; // struct IntensityLess
84 
85  //-------------------------------------------------------------------
86 
88  template <typename FeaFiModuleType>
90  FeatureFinderDefs::IndexSet::const_iterator
91  {
92  IntensityIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
93  FeatureFinderDefs::IndexSet::const_iterator(iter),
94  module_(module)
95  {
96  }
97 
98  typename FeaFiModuleType::IntensityType operator*() const
99  {
100  return module_->getPeakIntensity(FeatureFinderDefs::IndexSet::const_iterator::operator*());
101  }
102 
103 protected:
104  const FeaFiModuleType * module_;
105  };
106 
107  //-------------------------------------------------------------------
108 
110  template <typename FeaFiModuleType>
111  struct MzIterator :
112  FeatureFinderDefs::IndexSet::const_iterator
113  {
114  MzIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
115  FeatureFinderDefs::IndexSet::const_iterator(iter),
116  module_(module)
117  {
118  }
119 
120  typename FeaFiModuleType::IntensityType operator*() const
121  {
122  return module_->getPeakMz(FeatureFinderDefs::IndexSet::const_iterator::operator*());
123  }
124 
125 protected:
126  const FeaFiModuleType * module_;
127  };
128 
129  //-------------------------------------------------------------------
130 
132  template <typename FeaFiModuleType>
133  struct RtIterator :
134  FeatureFinderDefs::IndexSet::const_iterator
135  {
136  RtIterator(const FeatureFinderDefs::IndexSet::const_iterator & iter, const FeaFiModuleType * module) :
137  FeatureFinderDefs::IndexSet::const_iterator(iter),
138  module_(module)
139  {
140  }
141 
142  typename FeaFiModuleType::IntensityType operator*() const
143  {
144  return module_->getPeakRt(FeatureFinderDefs::IndexSet::const_iterator::operator*());
145  }
146 
147 protected:
148  const FeaFiModuleType * module_;
149  };
150 
151  //-------------------------------------------------------------------
152  } // namespace Internal
153 
157  template <class PeakType>
158  class FeaFiModule :
159  public DefaultParamHandler
160  {
161 public:
163  typedef PeakMap MapType;
170 
172  FeaFiModule(const PeakMap * map, FeatureMap* features, FeatureFinder * ff) :
173  DefaultParamHandler("FeaFiModule"),
174  map_(0),
175  features_(0),
176  ff_(0)
177  {
178  map_ = map;
179  features_ = features;
180  ff_ = ff;
181  }
182 
184  virtual ~FeaFiModule()
185  {
186  }
187 
189  inline IntensityType getPeakIntensity(const FeatureFinderDefs::IndexPair & index) const
190  {
191  //Corrupt index
192  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
193  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
194 
195  return (*map_)[index.first][index.second].getIntensity();
196  }
197 
199  inline CoordinateType getPeakMz(const FeatureFinderDefs::IndexPair & index) const
200  {
201  //Corrupt index
202  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
203  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
204 
205  return (*map_)[index.first][index.second].getMZ();
206  }
207 
209  inline CoordinateType getPeakRt(const FeatureFinderDefs::IndexPair & index) const
210  {
211  //Corrupt index
212  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
213  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
214 
215  return (*map_)[index.first].getRT();
216  }
217 
224  inline void getNextMz(FeatureFinderDefs::IndexPair & index) const
225  {
226  //Corrupt index
227  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
228  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
229 
230  //At the last peak of this spectrum
231  if (index.second + 1 >= (*map_)[index.first].size())
232  {
233  throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextMz", index);
234  }
235 
236  ++index.second;
237  }
238 
245  inline void getPrevMz(FeatureFinderDefs::IndexPair & index) const
246  {
247  //Corrupt index
248  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
249  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
250 
251  //begin of scan
252  if (index.second == 0)
253  {
254  throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevMz", index);
255  }
256 
257  --index.second;
258  }
259 
267  {
268  //Corrupt index
269  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
270  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
271 
272  CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ(); // mz value we want to find
273  Size index_first_tmp = index.first;
274 
275  ++index.first;
276  while (index.first < map_->size() &&
277  (*map_)[index.first].empty())
278  {
279  ++index.first;
280  }
281  //last scan
282  if (index.first >= map_->size())
283  {
284  throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getNextRt", index);
285  }
286  // now we have a spectrum with scans in it ...
287 
288  // perform binary search to find the neighbour in mz dimension
289  typename SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(), (*map_)[index.first].end(), (*map_)[index_first_tmp][index.second], typename PeakType::PositionLess());
290 
291  // if the found peak is at the end of the spectrum, there is not much we can do...
292  if (it == (*map_)[index.first].end())
293  {
294  index.second = (*map_)[index.first].size() - 1;
295  }
296  // if the found peak is at the beginning of the spectrum, there is also not much we can do !
297  else if (it == (*map_)[index.first].begin())
298  {
299  index.second = 0;
300  }
301  // see if the next smaller one fits better
302  else
303  {
304  // peak to the right is closer (in m/z dimension)
305  if (it->getMZ() - mz_pos < mz_pos - (it - 1)->getMZ())
306  {
307  index.second = it - (*map_)[index.first].begin();
308  }
309  else // left one is closer
310  {
311  index.second = --it - (*map_)[index.first].begin();
312  }
313  }
314  }
315 
323  {
324  //Corrupt index
325  OPENMS_PRECONDITION(index.first < map_->size(), "Scan index outside of map!");
326  OPENMS_PRECONDITION(index.second < (*map_)[index.first].size(), "Peak index outside of scan!");
327 
328  // TODO: this seems useless (at least for debug mode) given preconditions above... (and why not in getNextRt()??)
329  if (index.first >= map_->size())
330  {
331  std::cout << "Scan index outside of map!" << std::endl;
332  std::cout << index.first << " " << index.second << std::endl;
333  return;
334  }
335  if (index.second >= (*map_)[index.first].size())
336  {
337  std::cout << "Peak index outside of scan!" << std::endl;
338  std::cout << index.first << " " << index.second << std::endl;
339  return;
340  }
341 
342  CoordinateType mz_pos = (*map_)[index.first][index.second].getMZ();
343  Size index_first_tmp = index.first;
344 
345  // first scan
346  if (index.first == 0)
347  {
348  throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevRt", index);
349  }
350 
351  --index.first;
352  while ((index.first > 0) && ((*map_)[index.first].empty()))
353  {
354  --index.first;
355  }
356  // we only found an empty scan
357  if ((*map_)[index.first].empty()) throw FeatureFinderDefs::NoSuccessor(__FILE__, __LINE__, "FeatureFinder::getPrevRt", index);
358 
359  // perform binary search to find the neighbour in mz dimension
360 
361  typename MapType::SpectrumType::ConstIterator it = lower_bound((*map_)[index.first].begin(),
362  (*map_)[index.first].end(),
363  (*map_)[index_first_tmp][index.second],
364  typename PeakType::PositionLess());
365 
366  // if the found peak is at the end of the spectrum, there is not much we can do.
367  if (it == (*map_)[index.first].end())
368  {
369  index.second = (*map_)[index.first].size() - 1;
370  }
371  // if the found peak is at the beginning of the spectrum, there is not much we can do.
372  else if (it == (*map_)[index.first].begin())
373  {
374  index.second = 0;
375  }
376  // see if the next smaller one fits better
377  else
378  {
379  // peak to the right is closer (in m/z dimension)
380  if (it->getMZ() - mz_pos < mz_pos - (it - 1)->getMZ())
381  {
382  index.second = it - (*map_)[index.first].begin();
383  }
384  else
385  {
386  index.second = --it - (*map_)[index.first].begin();
387  }
388  }
389  }
390 
392  void addConvexHull(const FeatureFinderDefs::IndexSet & set, Feature & feature) const
393  {
394  std::vector<DPosition<2> > points;
395  points.reserve(set.size());
396  DPosition<2> tmp;
397  for (FeatureFinderDefs::IndexSet::const_iterator it = set.begin(); it != set.end(); ++it)
398  {
399  tmp[Peak2D::MZ] = (*map_)[it->first][it->second].getMZ();
400  tmp[Peak2D::RT] = (*map_)[it->first].getRT();
401  points.push_back(tmp);
402  }
403  feature.getConvexHulls().resize(feature.getConvexHulls().size() + 1);
404  // computes convex hull
405  feature.getConvexHulls().back().addPoints(points);
406  }
407 
408 protected:
410  const MapType * map_;
415 
416 private:
418  FeaFiModule();
420  FeaFiModule & operator=(const FeaFiModule &);
422  FeaFiModule(const FeaFiModule &);
423 
424  }; // class FeaFiModule
425 
426 } // namespace OpenMS
427 
428 #endif // OPENMS_TRANSFORMATIONS_FEATUREFINDER_FEAFIMODULE_H
CoordinateType getPeakMz(const FeatureFinderDefs::IndexPair &index) const
Returns the m/z of a peak.
Definition: FeaFiModule.h:199
Implements a module of the FeatureFinder algorithm.
Definition: FeaFiModule.h:158
IsotopeCluster::IndexPair IndexPair
Index to peak consisting of two UInts (scan index / peak index)
Definition: FeatureFinderDefs.h:54
IntensityIterator(const FeatureFinderDefs::IndexSet::const_iterator &iter, const FeaFiModuleType *module)
Definition: FeaFiModule.h:92
FeaFiModuleType::IntensityType operator*() const
Definition: FeaFiModule.h:98
FeaFiModuleType::IntensityType operator*() const
Definition: FeaFiModule.h:142
IntensityLess(const IntensityLess &rhs)
Copy ctor.
Definition: FeaFiModule.h:67
Retention time dimension id (0 if used as a const int)
Definition: Peak2D.h:76
void getNextRt(FeatureFinderDefs::IndexPair &index)
fills index with the index of the nearest peak in the next scan
Definition: FeaFiModule.h:266
PeakType::IntensityType IntensityType
Input intensity type.
Definition: FeaFiModule.h:167
void getPrevMz(FeatureFinderDefs::IndexPair &index) const
fills index with the index of previous peak in m/z dimension
Definition: FeaFiModule.h:245
A container for features.
Definition: FeatureMap.h:94
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
IntensityType getPeakIntensity(const FeatureFinderDefs::IndexPair &index) const
Returns the intensity of a peak.
Definition: FeaFiModule.h:189
FeaFiModule(const PeakMap *map, FeatureMap *features, FeatureFinder *ff)
Constructor.
Definition: FeaFiModule.h:172
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSSpectrum.h:104
IntensityLess()
Default ctor undefined since we cannot compare without a FeaFiModule.
Mass-to-charge dimension id (1 if used as a const int)
Definition: Peak2D.h:77
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
const std::vector< ConvexHull2D > & getConvexHulls() const
Non-mutable access to the convex hulls.
PeakMap MapType
Input map type.
Definition: FeaFiModule.h:163
FeatureFinder * ff_
Pointer to the calling FeatureFinder that is used to access the feature flags and report progress...
Definition: FeaFiModule.h:414
The purpose of this struct is to provide definitions of classes and typedefs which are used throughou...
Definition: FeatureFinderDefs.h:51
const MapType * map_
Input data pointer.
Definition: FeaFiModule.h:410
void getNextMz(FeatureFinderDefs::IndexPair &index) const
fills index with the index of next peak in m/z dimension
Definition: FeaFiModule.h:224
Comparator by position. Lexicographical comparison (first RT then m/z) is done.
Definition: Peak2D.h:333
RtIterator(const FeatureFinderDefs::IndexSet::const_iterator &iter, const FeaFiModuleType *module)
Definition: FeaFiModule.h:136
void addConvexHull(const FeatureFinderDefs::IndexSet &set, Feature &feature) const
Calculates the convex hull of a index set and adds it to the feature.
Definition: FeaFiModule.h:392
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
FeatureMap * features_
Output data pointer.
Definition: FeaFiModule.h:412
void getPrevRt(FeatureFinderDefs::IndexPair &index)
fills index with the index of the nearest peak in the previous scan
Definition: FeaFiModule.h:322
const FeaFiModuleType & module_
Reference to the FeaFiModule.
Definition: FeaFiModule.h:80
IsotopeCluster::IndexSet IndexSet
A set of peak indices.
Definition: FeatureFinderDefs.h:60
const FeaFiModuleType * module_
Definition: FeaFiModule.h:126
Retention time iterator for a FeatureFinderDefs::IndexSet.
Definition: FeaFiModule.h:133
bool operator()(const typename FeatureFinderDefs::IndexPair &left, const typename FeatureFinderDefs::IndexPair &right) const
Compare with respect to intensity.
Definition: FeaFiModule.h:73
Comparator that allows to compare the indices of two peaks by their intensity.
Definition: FeaFiModule.h:57
FeaFiModuleType::IntensityType operator*() const
Definition: FeaFiModule.h:120
Exception that is thrown if a method an invalid IndexPair is given.
Definition: FeatureFinderDefs.h:66
An LC-MS feature.
Definition: Feature.h:70
MzIterator(const FeatureFinderDefs::IndexSet::const_iterator &iter, const FeaFiModuleType *module)
Definition: FeaFiModule.h:114
CoordinateType getPeakRt(const FeatureFinderDefs::IndexPair &index) const
Returns the retention time of a peak.
Definition: FeaFiModule.h:209
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:82
m/z iterator for a FeatureFinderDefs::IndexSet
Definition: FeaFiModule.h:111
PeakType::CoordinateType CoordinateType
Input coordinate type.
Definition: FeaFiModule.h:169
Intensity iterator for a FeatureFinderDefs::IndexSet.
Definition: FeaFiModule.h:89
std::vector< SpectrumType >::const_iterator ConstIterator
Non-mutable iterator.
Definition: MSExperiment.h:118
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
IntensityLess(const FeaFiModuleType &module)
Constructor that takes a FeaFiModule reference.
Definition: FeaFiModule.h:61
The main feature finder class.
Definition: FeatureFinder.h:57
virtual ~FeaFiModule()
destructor
Definition: FeaFiModule.h:184
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
MapType::SpectrumType SpectrumType
Input spectrum type.
Definition: FeaFiModule.h:165
const FeaFiModuleType * module_
Definition: FeaFiModule.h:148
const FeaFiModuleType * module_
Definition: FeaFiModule.h:104

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