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:
76 {
77 bool operator()(const Element* a, const Element* b) const
78 {
79 if (a == nullptr || b == nullptr) return std::less<const Element*>()(a, b);
80 if (a->getAtomicNumber() != b->getAtomicNumber()) return a->getAtomicNumber() < b->getAtomicNumber();
81 const std::string& symbol_a = a->getSymbol();
82 const std::string& symbol_b = b->getSymbol();
83 if (symbol_a.size() != symbol_b.size()) return symbol_a.size() < symbol_b.size();
84 if (symbol_a != symbol_b) return symbol_a < symbol_b;
85 return std::less<const Element*>()(a, b);
86 }
87 };
88
90 typedef std::map<const Element*, SignedSize, ElementLess_> MapType_;
91
92public:
97 typedef MapType_::const_iterator ConstIterator;
98 typedef MapType_::const_iterator const_iterator;
99 typedef MapType_::iterator Iterator;
100 typedef MapType_::iterator iterator;
102
108
111
114
120 explicit EmpiricalFormula(const std::string& rhs);
121
123 EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge = 0);
124
128
136 static EmpiricalFormula fromString(const std::string& rhs)
137 {
138 EmpiricalFormula ef(rhs);
139 return ef;
140 }
141
146 double getMonoWeight() const;
147
150
152 double getAverageWeight() const;
153
156
170 bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P);
171
185 bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P);
186
201 bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P);
202
203
212
224 const std::set<UInt>& precursor_isotopes,
225 const CoarseIsotopePatternGenerator& method) const;
226
228 SignedSize getNumberOf(const Element* element) const;
229
232
234 Int getCharge() const;
235
237 void setCharge(Int charge);
238
272 EmpiricalFormula& addChargeAdduct(Int count = 1, const std::string& adduct = "H");
273
275 std::string toString() const;
276
278 std::map<std::string, int> toMap() const;
280
284
287
290
293
296
299
302
305
307
312 bool isEmpty() const;
313
315 bool isCharged() const;
316
318 bool hasElement(const Element* element) const;
319
321 bool contains(const EmpiricalFormula& ef) const;
322
324 bool operator==(const EmpiricalFormula& rhs) const;
325
327 bool operator!=(const EmpiricalFormula& rhs) const;
328
330 bool operator<(const EmpiricalFormula& rhs) const;
331
333
335 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
336
343 inline ConstIterator begin() const { return formula_.begin(); }
344
345 inline ConstIterator end() const { return formula_.end(); }
346
347 inline Iterator begin() { return formula_.begin(); }
348
349 inline Iterator end() { return formula_.end(); }
351
354 // @TODO: make these static member variables instead?
356
357 static EmpiricalFormula hydrogen(int n_atoms = 1);
358
360 static EmpiricalFormula water(int n_molecules = 1);
362
363protected:
364
367
369
371
372 Int parseFormula_(MapType_& ef, const std::string& formula) const;
373
374 };
375
376 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
377
378} // namespace OpenMS
379
380// Hash function specialization for EmpiricalFormula
381// Placed in std namespace to allow use with std::unordered_map/set
382namespace std
383{
397 template<>
398 struct hash<OpenMS::EmpiricalFormula>
399 {
400 std::size_t operator()(const OpenMS::EmpiricalFormula& ef) const noexcept
401 {
402 // Collect elements with symbols for deterministic ordering
403 // (map iteration order depends on pointer addresses which vary across runs)
404 // Use symbols instead of atomic numbers to distinguish isotopes like (13)C vs C
405 // Typical formulas have only 4-6 elements, so no need to reserve
406 std::vector<std::pair<std::string, OpenMS::SignedSize>> elements;
407 for (const auto& [element_ptr, count] : ef)
408 {
409 elements.emplace_back(element_ptr->getSymbol(), count);
410 }
411
412 // Sort by symbol for reproducible hash
413 std::sort(elements.begin(), elements.end());
414
415 // Hash in sorted order
416 std::size_t seed = 0;
417 for (const auto& [symbol, count] : elements)
418 {
421 }
422
423 // Hash the charge
424 OpenMS::hash_combine(seed, OpenMS::hash_int(ef.getCharge()));
425
426 return seed;
427 }
428 };
429} // namespace std
Isotope pattern generator for coarse isotope distributions.
Definition CoarseIsotopePatternGenerator.h:81
Representation of an element.
Definition Element.h:34
const std::string & getSymbol() const
returns symbol of the element
unsigned int getAtomicNumber() const
returns the unique atomic number
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
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:347
static EmpiricalFormula water(int n_molecules=1)
Efficiently generates a formula for water.
bool operator<(const EmpiricalFormula &rhs) const
less operator: by number of elements, then charge, then the elements and their counts (elements order...
double getMonoWeight() const
returns the monoisotopic (most abundant isotope per element) weight of the formula (includes proton c...
EmpiricalFormula & operator+=(const EmpiricalFormula &rhs)
adds the elements of the given formula
MapType_::const_iterator ConstIterator
Iterators.
Definition EmpiricalFormula.h:97
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:345
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:368
Int parseFormula_(MapType_ &ef, const std::string &formula) const
static EmpiricalFormula fromString(const std::string &rhs)
Create EmpiricalFormula object from a String.
Definition EmpiricalFormula.h:136
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:100
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:349
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:98
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:343
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:99
std::map< const Element *, SignedSize, ElementLess_ > MapType_
Internal typedef for the used map type (elements in the order of ElementLess_)
Definition EmpiricalFormula.h:90
Int charge_
Definition EmpiricalFormula.h:370
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
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.
Orders the elements of a formula independently of where the Element objects are in memory.
Definition EmpiricalFormula.h:76
bool operator()(const Element *a, const Element *b) const
Definition EmpiricalFormula.h:77
std::size_t operator()(const OpenMS::EmpiricalFormula &ef) const noexcept
Definition EmpiricalFormula.h:400