OpenMS
Loading...
Searching...
No Matches
EmpiricalFormula.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow, Ahmed Khalil $
6// $Authors: Andreas Bertsch, Chris Bielow $
7// --------------------------------------------------------------------------
8//
9#pragma once
10
11#include <iosfwd>
12#include <algorithm>
13#include <map>
14#include <set>
15#include <string>
16#include <functional>
17#include <vector>
18
22
23namespace OpenMS
24{
25 class ElementDB;
26 class IsotopeDistribution;
27 class IsotopePatternGenerator;
28 class CoarseIsotopePatternGenerator;
29
61 class OPENMS_DLLAPI EmpiricalFormula
62 {
63
64protected:
66 typedef std::map<const Element*, SignedSize> MapType_;
67
68public:
73 typedef MapType_::const_iterator ConstIterator;
74 typedef MapType_::const_iterator const_iterator;
75 typedef MapType_::iterator Iterator;
76 typedef MapType_::iterator iterator;
78
84
87
90
96 explicit EmpiricalFormula(const std::string& rhs);
97
99 EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge = 0);
100
104
112 static EmpiricalFormula fromString(const std::string& rhs)
113 {
114 EmpiricalFormula ef(rhs);
115 return ef;
116 }
117
122 double getMonoWeight() const;
123
126
128 double getAverageWeight() const;
129
132
146 bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P);
147
161 bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P);
162
177 bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P);
178
179
188
200 const std::set<UInt>& precursor_isotopes,
201 const CoarseIsotopePatternGenerator& method) const;
202
204 SignedSize getNumberOf(const Element* element) const;
205
208
210 Int getCharge() const;
211
213 void setCharge(Int charge);
214
248 EmpiricalFormula& addChargeAdduct(Int count = 1, const std::string& adduct = "H");
249
251 std::string toString() const;
252
254 std::map<std::string, int> toMap() const;
256
260
263
266
269
272
275
278
281
283
288 bool isEmpty() const;
289
291 bool isCharged() const;
292
294 bool hasElement(const Element* element) const;
295
297 bool contains(const EmpiricalFormula& ef) const;
298
300 bool operator==(const EmpiricalFormula& rhs) const;
301
303 bool operator!=(const EmpiricalFormula& rhs) const;
304
306 bool operator<(const EmpiricalFormula& rhs) const;
307
309
311 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
312
316 inline ConstIterator begin() const { return formula_.begin(); }
317
318 inline ConstIterator end() const { return formula_.end(); }
319
320 inline Iterator begin() { return formula_.begin(); }
321
322 inline Iterator end() { return formula_.end(); }
324
327 // @TODO: make these static member variables instead?
329
330 static EmpiricalFormula hydrogen(int n_atoms = 1);
331
333 static EmpiricalFormula water(int n_molecules = 1);
335
336protected:
337
340
342
344
345 Int parseFormula_(std::map<const Element*, SignedSize>& ef, const std::string& formula) const;
346
347 };
348
349 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
350
351} // namespace OpenMS
352
353// Hash function specialization for EmpiricalFormula
354// Placed in std namespace to allow use with std::unordered_map/set
355namespace std
356{
370 template<>
371 struct hash<OpenMS::EmpiricalFormula>
372 {
373 std::size_t operator()(const OpenMS::EmpiricalFormula& ef) const noexcept
374 {
375 // Collect elements with symbols for deterministic ordering
376 // (map iteration order depends on pointer addresses which vary across runs)
377 // Use symbols instead of atomic numbers to distinguish isotopes like (13)C vs C
378 // Typical formulas have only 4-6 elements, so no need to reserve
379 std::vector<std::pair<std::string, OpenMS::SignedSize>> elements;
380 for (const auto& [element_ptr, count] : ef)
381 {
382 elements.emplace_back(element_ptr->getSymbol(), count);
383 }
384
385 // Sort by symbol for reproducible hash
386 std::sort(elements.begin(), elements.end());
387
388 // Hash in sorted order
389 std::size_t seed = 0;
390 for (const auto& [symbol, count] : elements)
391 {
394 }
395
396 // Hash the charge
397 OpenMS::hash_combine(seed, OpenMS::hash_int(ef.getCharge()));
398
399 return seed;
400 }
401 };
402} // namespace std
Isotope pattern generator for coarse isotope distributions.
Definition CoarseIsotopePatternGenerator.h:81
Representation of an element.
Definition Element.h:34
Representation of an empirical formula.
Definition EmpiricalFormula.h:62
EmpiricalFormula & operator-=(const EmpiricalFormula &rhs)
subtracts the elements of a formula
bool hasElement(const Element *element) const
returns true if the formula contains the element
EmpiricalFormula operator*(const SignedSize &times) const
multiplies the elements and charge with a factor
Int parseFormula_(std::map< const Element *, SignedSize > &ef, const std::string &formula) const
EmpiricalFormula operator+(const EmpiricalFormula &rhs) const
adds the elements of the given formula and returns a new formula
std::string toString() const
returns the formula as a string (charges are not included)
bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P)
Fills this EmpiricalFormula with an approximate elemental composition for a given average weight and ...
Iterator begin()
Definition EmpiricalFormula.h:320
static EmpiricalFormula water(int n_molecules=1)
Efficiently generates a formula for water.
bool operator<(const EmpiricalFormula &rhs) const
less operator
double getMonoWeight() const
returns the monoisotopic (most abundant isotope per element) weight of the formula (includes proton c...
std::map< const Element *, SignedSize > MapType_
Internal typedef for the used map type.
Definition EmpiricalFormula.h:66
EmpiricalFormula & operator+=(const EmpiricalFormula &rhs)
adds the elements of the given formula
MapType_::const_iterator ConstIterator
Iterators.
Definition EmpiricalFormula.h:73
EmpiricalFormula(EmpiricalFormula &&)=default
Move constructor.
EmpiricalFormula()
Default constructor.
bool isCharged() const
returns true if charge != 0
EmpiricalFormula(const EmpiricalFormula &)=default
Copy constructor.
ConstIterator end() const
Definition EmpiricalFormula.h:318
EmpiricalFormula operator-(const EmpiricalFormula &rhs) const
subtracts the elements of a formula an returns a new formula
double getLightestIsotopeWeight() const
returns the sum of the lightest isotopes per element in the formula (includes proton charges)
Int getCharge() const
returns the charge
IsotopeDistribution getIsotopeDistribution(const IsotopePatternGenerator &method) const
returns the isotope distribution of the formula The details of the calculation of the isotope distrib...
void removeZeroedElements_()
remove elements with count 0
MapType_ formula_
Definition EmpiricalFormula.h:341
static EmpiricalFormula fromString(const std::string &rhs)
Create EmpiricalFormula object from a String.
Definition EmpiricalFormula.h:112
SignedSize getNumberOf(const Element *element) const
returns the number of atoms for a certain element (can be negative)
bool operator!=(const EmpiricalFormula &rhs) const
returns true if the formulas differ in elements composition
double getAverageWeight() const
returns the average weight of the formula (includes proton charges)
bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P)
Fills this EmpiricalFormula with an approximate elemental composition for a given monoisotopic weight...
bool operator==(const EmpiricalFormula &rhs) const
returns true if the formulas contain equal elements in equal quantities
friend std::ostream & operator<<(std::ostream &os, const EmpiricalFormula &formula)
writes the formula to a stream
MapType_::iterator iterator
Definition EmpiricalFormula.h:76
EmpiricalFormula & operator=(const EmpiricalFormula &)=default
Assignment operator.
bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P)
Fills this EmpiricalFormula with an approximate elemental composition for a given average weight,...
EmpiricalFormula(SignedSize number, const Element *element, SignedSize charge=0)
Constructor with element pointer and number.
SignedSize getNumberOfAtoms() const
returns the atoms total (not absolute: negative counts for certain elements will reduce the overall c...
Iterator end()
Definition EmpiricalFormula.h:322
EmpiricalFormula & operator=(EmpiricalFormula &&) &=default
Move assignment operator.
double calculateTheoreticalIsotopesNumber() const
returns the total number of discrete isotopes
void setCharge(Int charge)
sets the charge
MapType_::const_iterator const_iterator
Definition EmpiricalFormula.h:74
bool isEmpty() const
returns true if the formula does not contain a element
IsotopeDistribution getConditionalFragmentIsotopeDist(const EmpiricalFormula &precursor, const std::set< UInt > &precursor_isotopes, const CoarseIsotopePatternGenerator &method) const
returns the fragment isotope distribution of this given a precursor formula and conditioned on a set ...
ConstIterator begin() const
Definition EmpiricalFormula.h:316
EmpiricalFormula(const std::string &rhs)
EmpiricalFormula & addChargeAdduct(Int count=1, const std::string &adduct="H")
Makes ionization explicit: adds count copies of adduct (default: a hydrogen atom) to the formula and ...
virtual ~EmpiricalFormula()
Destructor.
static EmpiricalFormula hydrogen(int n_atoms=1)
Efficiently generates a formula for hydrogen.
MapType_::iterator Iterator
Definition EmpiricalFormula.h:75
Int charge_
Definition EmpiricalFormula.h:343
std::map< std::string, int > toMap() const
returns the formula as a map (charges are not included)
bool contains(const EmpiricalFormula &ef) const
returns true if all elements from ef are LESS abundant (negative allowed) than the corresponding elem...
Definition IsotopeDistribution.h:40
Provides an interface for different isotope pattern generator methods.
Definition IsotopePatternGenerator.h:42
You can set more CMake variables adding< code > linking and adding include directories</td ></tr >< tr >< th valign="top"> CMAKE_PREFIX_PATH</td >< td > Additional search path for the contrib libraries[MacOSX only] If you want to use libraries installed via Homebrew or MacPorts you might need to provide the corresponding paths< code > e g< code > C
Definition common-cmake-parameters.doxygen:35
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition Types.h:104
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
std::size_t hash_int(T value) noexcept
Hash for an integer type.
Definition HashUtils.h:107
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
std::size_t fnv1a_hash_string(const std::string &s) noexcept
FNV-1a hash for a string.
Definition HashUtils.h:70
STL namespace.
std::size_t operator()(const OpenMS::EmpiricalFormula &ef) const noexcept
Definition EmpiricalFormula.h:373