OpenMS  2.4.0
MathFunctions.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-2018.
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 $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
38 
39 #include <boost/math/special_functions/gamma.hpp>
40 #include <cmath>
41 #include <utility>
42 
43 namespace OpenMS
44 {
52  namespace Math
53  {
65  inline static double ceilDecimal(double x, int decPow)
66  {
67  return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
68  }
69 
80  inline static double roundDecimal(double x, int decPow)
81  {
82  if (x > 0)
83  return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
84 
85  return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
86  }
87 
93  inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
94  {
95  return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
96  }
97 
105  inline double linear2log(double x)
106  {
107  return log10(x + 1); //+1 to avoid negative logarithms
108  }
109 
117  inline double log2linear(double x)
118  {
119  return pow(10, x) - 1;
120  }
121 
127  inline bool isOdd(UInt x)
128  {
129  return (x & 1) != 0;
130  }
131 
137  template <typename T>
138  T round(T x)
139  {
140  if (x >= T(0))
141  {
142  return T(floor(x + T(0.5)));
143  }
144  else
145  {
146  return T(ceil(x - T(0.5)));
147  }
148  }
149 
155  inline static bool approximatelyEqual(double a, double b, double tol)
156  {
157  return std::fabs(a - b) <= tol;
158  }
159 
168  template <typename T>
169  T gcd(T a, T b)
170  {
171  T c;
172  while (b != 0)
173  {
174  c = a % b;
175  a = b;
176  b = c;
177  }
178  return a;
179  }
180 
193  template <typename T>
194  T gcd(T a, T b, T & u1, T & u2)
195  {
196  u1 = 1;
197  u2 = 0;
198  T u3 = a;
199 
200  T v1 = 0;
201  T v2 = 1;
202  T v3 = b;
203 
204  while (v3 != 0)
205  {
206  T q = u3 / v3;
207  T t1 = u1 - v1 * q;
208  T t2 = u2 - v2 * q;
209  T t3 = u3 - v3 * q;
210 
211  u1 = v1;
212  u2 = v2;
213  u3 = v3;
214 
215  v1 = t1;
216  v2 = t2;
217  v3 = t3;
218  }
219 
220  return u3;
221  }
222 
232  template <typename T>
233  T getPPM(T mz_obs, T mz_ref)
234  {
235  return (mz_obs - mz_ref) / mz_ref * 1e6;
236  }
237 
247  template <typename T>
248  T getPPMAbs(T mz_obs, T mz_ref)
249  {
250  return std::fabs(getPPM(mz_obs, mz_ref));
251  }
252 
262  template <typename T>
263  T ppmToMass(T ppm, T mz_ref)
264  {
265  return (ppm / 1e6) * mz_ref;
266  }
267 
268  /*
269  @brief Compute the absolute mass diff in [Th], given a ppm value and a reference point.
270 
271  The returned mass diff is always positive!
272 
273  @param ppm Parts-per-million error
274  @param mz_ref Reference m/z
275  @return The absolute mass diff in [Th]
276  */
277  template <typename T>
278  T ppmToMassAbs(T ppm, T mz_ref)
279  {
280  return std::fabs(ppmToMass(ppm, mz_ref));
281  }
282 
296  inline static std::pair<double, double> getTolWindow(double val, double tol, bool ppm)
297  {
298  double left, right;
299 
300  if (ppm)
301  {
302  left = val - val * tol * 1e-6;
303  right = val / (1.0 - tol * 1e-6);
304  }
305  else
306  {
307  left = val - tol;
308  right = val + tol;
309  }
310 
311  return std::make_pair(left, right);
312  }
313 
322  inline double factLn(UInt x)
323  {
324  return lgamma(double(x+1));
325  }
326 
327  } // namespace Math
328 } // namespace OpenMS
329 
ConsensusXMLFile.h
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:150
OpenMS::ConsensusMap::applyMemberFunction
Size applyMemberFunction(Size(Type::*member_function)())
Applies a member function of Type to the container itself and all consensus features....
Definition: ConsensusMap.h:296
OpenMS::MassTraceDetection::run
void run(const PeakMap &, std::vector< MassTrace > &)
Main method of MassTraceDetection. Extracts mass traces of a MSExperiment and gathers them into a vec...
OpenMS::Param::copy
Param copy(const String &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
FileHandler.h
OpenMS::MassTraceDetection
A mass trace extraction method that gathers peaks similar in m/z and moving along retention time.
Definition: MassTraceDetection.h:72
OpenMS::ConsensusXMLFile::store
void store(const String &filename, const ConsensusMap &consensus_map)
Stores a consensus map to file.
OpenMS::MassTrace::const_iterator
std::vector< PeakType >::const_iterator const_iterator
Definition: MassTrace.h:109
Types.h
OpenMS::Math::approximatelyEqual
static bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:155
OpenMS::Feature::getConvexHulls
const std::vector< ConvexHull2D > & getConvexHulls() const
Non-mutable access to the convex hulls.
OpenMS::MSExperiment::sortSpectra
void sortSpectra(bool sort_mz=true)
Sorts the data points by retention time.
OpenMS::FileHandler::getTypeByFileName
static FileTypes::Type getTypeByFileName(const String &filename)
Determines the file type from a file name.
OpenMS::Constants::k
const double k
OpenMS::Math::isOdd
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:127
OpenMS::Param::setValue
void setValue(const String &key, const DataValue &value, const String &description="", const StringList &tags=StringList())
Sets a value.
OpenMS::MzMLFile
File adapter for MzML files.
Definition: MzMLFile.h:55
OpenMS::String
A more convenient string class.
Definition: String.h:57
MzMLFile.h
OpenMS::Param::setValidStrings
void setValidStrings(const String &key, const std::vector< String > &strings)
Sets the valid strings for the parameter key.
OpenMS::MSExperiment
In-Memory representation of a mass spectrometry experiment.
Definition: MSExperiment.h:77
OpenMS::FileTypes::CONSENSUSXML
OpenMS consensus map format (.consensusXML)
Definition: FileTypes.h:67
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
FeatureXMLFile.h
OpenMS::Constants::c
const double c
LOG_INFO
#define LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:454
OpenMS::Param::getValue
const DataValue & getValue(const String &key) const
Returns a value of a parameter.
OpenMS::FeatureMap::setPrimaryMSRunPath
void setPrimaryMSRunPath(const StringList &s)
set the file path to the primary MS run (usually the mzML file obtained after data conversion from ra...
OpenMS::Math::getTolWindow
static std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition: MathFunctions.h:296
OpenMS::FileTypes::UNKNOWN
Unknown file extension.
Definition: FileTypes.h:60
OpenMS::Peak2D::setIntensity
void setIntensity(IntensityType intensity)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:172
OpenMS::MSExperiment::size
Size size() const
Definition: MSExperiment.h:127
OpenMS::Math::getPPMAbs
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition: MathFunctions.h:248
MassTrace.h
OpenMS::Math::log2linear
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double)
Definition: MathFunctions.h:117
OpenMS::BaseFeature::setQuality
void setQuality(QualityType q)
Set the overall quality.
OpenMS::Peak2D::setRT
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:214
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::DataValue::toBool
bool toBool() const
Conversion to bool.
OpenMS::ElutionPeakDetection
Extracts chromatographic peaks from a mass trace.
Definition: ElutionPeakDetection.h:76
OpenMS::Math::ppmToMassAbs
T ppmToMassAbs(T ppm, T mz_ref)
Definition: MathFunctions.h:278
LOG_WARN
#define LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition: LogStream.h:450
OpenMS::MetaInfoInterface::setMetaValue
void setMetaValue(const String &name, const DataValue &value)
Sets the DataValue corresponding to a name.
OpenMS::MzMLFile::load
void load(const String &filename, PeakMap &map)
Loads a map from a MzML file. Spectra and chromatograms are sorted by default (this can be disabled u...
OpenMS::FileHandler
Facilitates file handling by file type recognition.
Definition: FileHandler.h:62
FeatureMap.h
OpenMS::FileTypes::Type
Type
Actual file types enum.
Definition: FileTypes.h:58
OpenMS::Math::intervalTransformation
static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
transforms point x of interval [left1,right1] into interval [left2,right2]
Definition: MathFunctions.h:93
MathFunctions.h
OpenMS::Math::getPPM
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition: MathFunctions.h:233
OpenMS::ConsensusFeature
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
OpenMS::UniqueIdInterface::setUniqueId
Size setUniqueId()
Assigns a new, valid unique id. Always returns 1.
Definition: UniqueIdInterface.h:150
OpenMS::Math::round
T round(T x)
Rounds the value.
Definition: MathFunctions.h:138
OpenMS::Peak2D::setMZ
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:202
OpenMS::Feature::setOverallQuality
void setOverallQuality(QualityType q)
Set the overall quality.
OpenMS::Math::gcd
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm.
Definition: MathFunctions.h:169
OpenMS::DefaultParamHandler::setParameters
void setParameters(const Param &param)
Sets the parameters.
OpenMS::FeatureMap::applyMemberFunction
Size applyMemberFunction(Size(Type::*member_function)())
Applies a member function of Type to the container itself and all features (including subordinates)....
Definition: FeatureMap.h:274
OpenMS::DefaultParamHandler::getDefaults
const Param & getDefaults() const
Non-mutable access to the default parameters.
OpenMS::BaseFeature::setWidth
void setWidth(WidthType fwhm)
Set the width of the feature (FWHM)
OpenMS::DefaultParamHandler::getParameters
const Param & getParameters() const
Non-mutable access to the parameters.
OpenMS::ElutionPeakDetection::filterByPeakWidth
void filterByPeakWidth(std::vector< MassTrace > &, std::vector< MassTrace > &)
Filter out mass traces below lower 5 % quartile and above upper 95 % quartile.
OpenMS::Math::ceilDecimal
static double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:65
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:75
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
OpenMS::FeatureXMLFile::store
void store(const String &filename, const FeatureMap &feature_map)
stores the map feature_map in file with name filename.
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
MSExperiment.h
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:93
OpenMS::Param::remove
void remove(const String &key)
Remove the entry key or a section key (when suffix is ':')
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::FeatureHandle
Representation of a Peak2D, RichPeak2D or Feature .
Definition: FeatureHandle.h:57
OpenMS::Math::linear2log
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1.
Definition: MathFunctions.h:105
OpenMS::FeatureXMLFile
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:68
OpenMS::MSExperiment::getPrimaryMSRunPath
void getPrimaryMSRunPath(StringList &toFill) const
get the file path to the first MS run
OpenMS::DataProcessing::QUANTITATION
Quantitation.
Definition: DataProcessing.h:72
OpenMS::ElutionPeakDetection::detectPeaks
void detectPeaks(MassTrace &mt, std::vector< MassTrace > &single_mtraces)
Extracts chromatographic peaks from a single MassTrace and stores the resulting split traces in a vec...
ElutionPeakDetection.h
OpenMS::FileTypes::nameToType
static Type nameToType(const String &name)
Converts a file type name into a Type.
OpenMS::Math::ppmToMass
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition: MathFunctions.h:263
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:74
OpenMS::Peak2D::getMZ
CoordinateType getMZ() const
Returns the m/z coordinate (index 1)
Definition: Peak2D.h:196
OpenMS::Math::roundDecimal
static double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:80
MassTraceDetection.h
OpenMS::Param::insert
void insert(const String &prefix, const Param &param)
OpenMS::BaseFeature::setCharge
void setCharge(const ChargeType &ch)
Set charge state.
OpenMS::MzMLFile::getOptions
PeakFileOptions & getOptions()
Mutable access to the options for loading/storing.
OpenMS::ProgressLogger::setLogType
void setLogType(LogType type) const
Sets the progress log that should be used. The default type is NONE!
TOPPBase.h
OpenMS::ConsensusMap::setPrimaryMSRunPath
void setPrimaryMSRunPath(const StringList &s)
set the file paths to the primary MS run (stored in ColumnHeaders)
OpenMS::Math::factLn
double factLn(UInt x)
Return the ln(x!) of a value.
Definition: MathFunctions.h:322
OpenMS::ConsensusFeature::insert
void insert(const ConsensusFeature &cf)
Adds all feature handles (of the CF) into the consensus feature.
OpenMS::ConsensusXMLFile
This class provides Input functionality for ConsensusMaps and Output functionality for alignments and...
Definition: ConsensusXMLFile.h:61
OpenMS::Math::sd
static double sd(IteratorType begin, IteratorType end, double mean=std::numeric_limits< double >::max())
Calculates the standard deviation of a range of values.
Definition: StatisticFunctions.h:306