| OpenMS 2.5.0

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-2020.
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
37 #include <OpenMS/KERNEL/StandardTypes.h>
38 #include <OpenMS/KERNEL/MSSpectrum.h>
39 #include <OpenMS/CONCEPT/Exception.h>
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:
95 // default bin width for low-resolution data (adapted from doi:10.1007/s13361-015-1179-x)
96 static constexpr const float DEFAULT_BIN_WIDTH_LOWRES = 1.0005f;
97
98 // default bin width for high-resolution data (adapted from doi:10.1007/s13361-015-1179-x)
99 static constexpr const float DEFAULT_BIN_WIDTH_HIRES = 0.02f;
100
102 static constexpr const float DEFAULT_BIN_OFFSET_HIRES = 0.0f;
103
105 static constexpr const float DEFAULT_BIN_OFFSET_LOWRES = 0.4f;
106
108 using SparseVectorType = Eigen::SparseVector<float>;
109
111 using SparseVectorIndexType = Eigen::SparseVector<float>::Index;
112
114 using SparseVectorIteratorType = Eigen::SparseVector<float>::InnerIterator;
115
117 static const SparseVectorType EmptySparseVector;
118
120 // BinnedSpectrum() = delete;
121 BinnedSpectrum() {}
122
124 BinnedSpectrum(const PeakSpectrum& ps, float size, bool unit_ppm, UInt spread, float offset);
125
127 BinnedSpectrum(const BinnedSpectrum&) = default;
128
130 virtual ~BinnedSpectrum();
131
133 BinnedSpectrum& operator=(const BinnedSpectrum&) = default;
134
136 bool operator==(const BinnedSpectrum& rhs) const;
137
139 bool operator!=(const BinnedSpectrum& rhs) const;
140
142 inline float getBinIntensity(double mz) { return bins_.coeffRef(getBinIndex(mz)); }
143
145 inline SparseVectorIndexType getBinIndex(float mz) const
146 {
147 if (unit_ppm_)
148 {
149 /*
150 * By solving: mz = MIN_MZ_ * (1.0 + bin_size_ * 1e-6)^index for index
151 * we get: index = floor(log(mz/MIN_MZ_)/log(1.0 + bin_size_ * 1e-6))
152 * Note: for ppm we don't need to consider an offset_.
153 */
154 return static_cast<SparseVectorIndexType>(floor(log(mz/MIN_MZ_)/log1p(bin_size_ * 1e-6)));
155 }
156 else
157 { // implemented as described in PMC4607604
158 // Note: Consider a peak offset (important for low-resolution data, where most peak boundaries
159 // may fall on the mass peak apex. See publication for details.).
160 return static_cast<SparseVectorIndexType>(floor(mz / bin_size_ + offset_));
161 }
162 }
163
165 inline float getBinLowerMZ(size_t i) const
166 {
167 if (unit_ppm_)
168 {
169 // mz = MIN_MZ_ * (1.0 + bin_size_)^index for index
170 return (MIN_MZ_ * pow(1.0 + bin_size_ * 1e-6, i));
171 }
172 else
173 {
174 return ((static_cast<float>(i) - offset_) * bin_size_);
175 }
176 }
177
179 inline float getBinSize() const { return bin_size_; }
180
182 inline size_t getBinSpread() const { return bin_spread_; }
183
185 const SparseVectorType& getBins() const;
186
188 SparseVectorType& getBins();
189
191 inline float getOffset() const { return offset_; }
192
194 const std::vector<Precursor>& getPrecursors() const;
195
197 std::vector<Precursor>& getPrecursors();
198
200 // returns true if bin size, unit and offset are equal, otherwise false
201 static bool isCompatible(const BinnedSpectrum& a, const BinnedSpectrum& b);
202
203 private:
205 UInt bin_spread_;
206
208 float bin_size_;
209
211 bool unit_ppm_;
212
214 float offset_;
215
217 SparseVectorType bins_;
218
220 void binSpectrum_(const PeakSpectrum& ps);
221
223 std::vector<Precursor> precursors_;
224 };
225
226 }
227
OpenMS::BinnedSpectrum::SparseVectorIndexType
Eigen::SparseVector< float >::Index SparseVectorIndexType
typedef for the index into the sparse vector
Definition: BinnedSpectrum.h:111
OpenMS::BinnedSpectrum::getBinLowerMZ
float getBinLowerMZ(size_t i) const
return the lower m/z of a bin given its index
Definition: BinnedSpectrum.h:165
Exception.h
KDTree::operator!=
bool operator!=(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:824
StandardTypes.h
OpenMS::BinnedSpectrum::getOffset
float getOffset() const
return offset
Definition: BinnedSpectrum.h:191
OpenMS::BinnedSpectrum::getBinIntensity
float getBinIntensity(double mz)
returns the bin intensity at a given m/z position
Definition: BinnedSpectrum.h:142
OpenMS::BinnedSpectrum::EmptySparseVector
static const SparseVectorType EmptySparseVector
the empty SparseVector
Definition: BinnedSpectrum.h:117
OpenMS::BinnedSpectrum::SparseVectorIteratorType
Eigen::SparseVector< float >::InnerIterator SparseVectorIteratorType
typedef for the index into the sparse vector
Definition: BinnedSpectrum.h:114
OpenMS::MSSpectrum
The representation of a 1D spectrum.
Definition: MSSpectrum.h:67
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::BinnedSpectrum::SparseVectorType
Eigen::SparseVector< float > SparseVectorType
typedef for the underlying sparse vector
Definition: BinnedSpectrum.h:108
OpenMS::BinnedSpectrum::offset_
float offset_
offset of bin start
Definition: BinnedSpectrum.h:214
OpenMS::BinnedSpectrum::precursors_
std::vector< Precursor > precursors_
precursor information
Definition: BinnedSpectrum.h:223
OpenMS::BinnedSpectrum::getBinSize
float getBinSize() const
get the bin size
Definition: BinnedSpectrum.h:179
MSSpectrum.h
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::BinnedSpectrum::BinnedSpectrum
BinnedSpectrum()
default constructor
Definition: BinnedSpectrum.h:121
OpenMS::BinnedSpectrum::bin_size_
float bin_size_
the size of each bin
Definition: BinnedSpectrum.h:208
OpenMS::BinnedSpectrum::bins_
SparseVectorType bins_
bins
Definition: BinnedSpectrum.h:217
OpenMS::BinnedSpectrum::getBinIndex
SparseVectorIndexType getBinIndex(float mz) const
return the bin index of a given m/z position
Definition: BinnedSpectrum.h:145
OpenMS::BinnedSpectrum
This is a binned representation of a PeakSpectrum.
Definition: BinnedSpectrum.h:75
OpenMS::BinnedSpectrum::unit_ppm_
bool unit_ppm_
absolute bin size or relative bin size
Definition: BinnedSpectrum.h:211
OpenMS::BinnedSpectrum::bin_spread_
UInt bin_spread_
the spread to left or right
Definition: BinnedSpectrum.h:205
OpenMS::BinnedSpectrum::getBinSpread
size_t getBinSpread() const
get the bin spread
Definition: BinnedSpectrum.h:182
KDTree::operator==
bool operator==(_Iterator< _Val, _Ref, _Ptr > const &, _Iterator< _Val, _Ref, _Ptr > const &)
Definition: KDTree.h:806