Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
BinnedSpectrum.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: Timo Sachsenberg, Mathias Walzer $
33 // --------------------------------------------------------------------------
34 //
35 #pragma once
36 
40 
41 #include <Eigen/Sparse>
42 
43 #include <cmath>
44 
45 namespace OpenMS
46 {
47 
75  class OPENMS_DLLAPI BinnedSpectrum
76  {
77  // smallest possible m/z value (needs to be >= 1)
78  static constexpr const float MIN_MZ_ = 1.0;
79 
80 public:
96  // default bin width for low-resolution data (adapted from doi:10.1007/s13361-015-1179-x)
97  static constexpr const float DEFAULT_BIN_WIDTH_LOWRES = 1.0005;
98 
99  // default bin width for high-resolution data (adapted from doi:10.1007/s13361-015-1179-x)
100  static constexpr const float DEFAULT_BIN_WIDTH_HIRES = 0.02;
102  using SparseVectorType = Eigen::SparseVector<float>;
103 
105  using SparseVectorIndexType = Eigen::SparseVector<float>::Index;
106 
108  using SparseVectorIteratorType = Eigen::SparseVector<float>::InnerIterator;
109 
112 
114  // BinnedSpectrum() = delete;
116 
118  BinnedSpectrum(const PeakSpectrum& ps, float size, bool unit_ppm, UInt spread);
119 
121  BinnedSpectrum(const BinnedSpectrum&) = default;
122 
124  virtual ~BinnedSpectrum();
125 
127  BinnedSpectrum& operator=(const BinnedSpectrum&) = default;
128 
130  bool operator==(const BinnedSpectrum& rhs) const;
131 
133  bool operator!=(const BinnedSpectrum& rhs) const;
134 
136  inline float getBinIntensity(double mz) { return bins_.coeffRef(getBinIndex(mz)); }
137 
139  inline SparseVectorIndexType getBinIndex(float mz) const
140  {
141  if (unit_ppm_)
142  {
143  /*
144  * By solving: mz = MIN_MZ_ * (1.0 + bin_size_)^index for index
145  * we get: index = floor(log(mz/MIN_MZ_)/log(1.0 + bin_size_))
146  */
147  return static_cast<SparseVectorIndexType>(floor(log(mz/MIN_MZ_)/log1p(bin_size_ * 1e-6)));
148  }
149  else
150  {
151  return static_cast<SparseVectorIndexType>(floor(mz / bin_size_));
152  }
153  }
154 
156  inline float getBinLowerMZ(size_t i) const
157  {
158  if (unit_ppm_)
159  {
160  // mz = MIN_MZ_ * (1.0 + bin_size_)^index for index
161  return (MIN_MZ_ * pow(1.0 + bin_size_ * 1e-6, i));
162  }
163  else
164  {
165  return (i * bin_size_);
166  }
167  }
168 
170  inline float getBinSize() const { return bin_size_; }
171 
173  inline size_t getBinSpread() const { return bin_spread_; }
174 
176  const SparseVectorType& getBins() const;
177 
179  SparseVectorType& getBins();
180 
181  // immutable access to precursors
182  const std::vector<Precursor>& getPrecursors() const;
183 
185  std::vector<Precursor>& getPrecursors();
186 
188  // returns true if bin size and unit are equal, otherwise false
189  static bool isCompatible(const BinnedSpectrum& a, const BinnedSpectrum& b);
190 
191 private:
194 
196  float bin_size_;
197 
199  bool unit_ppm_;
200 
203 
205  void binSpectrum_(const PeakSpectrum& ps);
206 
208  std::vector<Precursor> precursors_;
209  };
210 
211 }
212 
float getBinSize() const
get the bin size
Definition: BinnedSpectrum.h:170
float bin_size_
the size of each bin
Definition: BinnedSpectrum.h:196
bool unit_ppm_
absolute bin size or relative bin size
Definition: BinnedSpectrum.h:199
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
SparseVectorIndexType getBinIndex(float mz) const
return the bin index of a given m/z position
Definition: BinnedSpectrum.h:139
BinnedSpectrum()
default constructor
Definition: BinnedSpectrum.h:115
Eigen::SparseVector< float > SparseVectorType
typedef for the underlying sparse vector
Definition: BinnedSpectrum.h:102
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806
UInt bin_spread_
the spread to left or right
Definition: BinnedSpectrum.h:193
std::vector< Precursor > precursors_
precursor information
Definition: BinnedSpectrum.h:208
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Eigen::SparseVector< float >::Index SparseVectorIndexType
typedef for the index into the sparse vector
Definition: BinnedSpectrum.h:105
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
float getBinIntensity(double mz)
returns the bin intensity at a given m/z position
Definition: BinnedSpectrum.h:136
Eigen::SparseVector< float >::InnerIterator SparseVectorIteratorType
typedef for the index into the sparse vector
Definition: BinnedSpectrum.h:108
static const SparseVectorType EmptySparseVector
the empty SparseVector
Definition: BinnedSpectrum.h:111
float getBinLowerMZ(size_t i) const
return the lower m/z of a bin given its index
Definition: BinnedSpectrum.h:156
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:75
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
SparseVectorType bins_
bins
Definition: BinnedSpectrum.h:202
size_t getBinSpread() const
get the bin spread
Definition: BinnedSpectrum.h:173

OpenMS / TOPP release 2.3.0 Documentation generated on Wed Apr 18 2018 19:29:04 using doxygen 1.8.14