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 String;
26 class ElementDB;
27 class IsotopeDistribution;
28 class IsotopePatternGenerator;
29 class CoarseIsotopePatternGenerator;
30
62 class OPENMS_DLLAPI EmpiricalFormula
63 {
64
65protected:
68 {
69 bool operator()(const Element* a, const Element* b) const
70 {
71 // Compare by atomic number first for grouping elements
72 if (a->getAtomicNumber() != b->getAtomicNumber())
73 {
74 return a->getAtomicNumber() < b->getAtomicNumber();
75 }
76 // Same atomic number - compare by mono weight to distinguish isotopes (e.g., C-12 vs C-13)
77 return a->getMonoWeight() < b->getMonoWeight();
78 }
79 };
80
82 typedef std::map<const Element*, SignedSize, ElementPtrComparator_> MapType_;
83
84public:
89 typedef MapType_::const_iterator ConstIterator;
90 typedef MapType_::const_iterator const_iterator;
91 typedef MapType_::iterator Iterator;
92 typedef MapType_::iterator iterator;
94
100
103
106
112 explicit EmpiricalFormula(const String& rhs);
113
115 EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge = 0);
116
120
129 {
130 EmpiricalFormula ef(rhs);
131 return ef;
132 }
133
138 double getMonoWeight() const;
139
142
144 double getAverageWeight() const;
145
148
162 bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P);
163
177 bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P);
178
193 bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P);
194
195
204
216 const std::set<UInt>& precursor_isotopes,
217 const CoarseIsotopePatternGenerator& method) const;
218
220 SignedSize getNumberOf(const Element* element) const;
221
224
226 Int getCharge() const;
227
229 void setCharge(Int charge);
230
233
235 std::map<std::string, int> toMap() const;
237
241
244
247
250
253
256
259
262
264
269 bool isEmpty() const;
270
272 bool isCharged() const;
273
275 bool hasElement(const Element* element) const;
276
278 bool contains(const EmpiricalFormula& ef) const;
279
281 bool operator==(const EmpiricalFormula& rhs) const;
282
284 bool operator!=(const EmpiricalFormula& rhs) const;
285
287 bool operator<(const EmpiricalFormula& rhs) const;
288
290
292 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
293
297 inline ConstIterator begin() const { return formula_.begin(); }
298
299 inline ConstIterator end() const { return formula_.end(); }
300
301 inline Iterator begin() { return formula_.begin(); }
302
303 inline Iterator end() { return formula_.end(); }
305
308 // @TODO: make these static member variables instead?
310
311 static EmpiricalFormula hydrogen(int n_atoms = 1);
312
314 static EmpiricalFormula water(int n_molecules = 1);
316
317protected:
318
321
323
325
326 Int parseFormula_(MapType_& ef, const String& formula) const;
327
328 };
329
330 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
331
332} // namespace OpenMS
333
334// Hash function specialization for EmpiricalFormula
335// Placed in std namespace to allow use with std::unordered_map/set
336namespace std
337{
351 template<>
352 struct hash<OpenMS::EmpiricalFormula>
353 {
354 std::size_t operator()(const OpenMS::EmpiricalFormula& ef) const noexcept
355 {
356 // Collect elements with symbols for deterministic ordering
357 // (map iteration order depends on pointer addresses which vary across runs)
358 // Use symbols instead of atomic numbers to distinguish isotopes like (13)C vs C
359 // Typical formulas have only 4-6 elements, so no need to reserve
360 std::vector<std::pair<std::string, OpenMS::SignedSize>> elements;
361 for (const auto& [element_ptr, count] : ef)
362 {
363 elements.emplace_back(element_ptr->getSymbol(), count);
364 }
365
366 // Sort by symbol for reproducible hash
367 std::sort(elements.begin(), elements.end());
368
369 // Hash in sorted order
370 std::size_t seed = 0;
371 for (const auto& [symbol, count] : elements)
372 {
375 }
376
377 // Hash the charge
378 OpenMS::hash_combine(seed, OpenMS::hash_int(ef.getCharge()));
379
380 return seed;
381 }
382 };
383} // namespace std
Isotope pattern generator for coarse isotope distributions.
Definition CoarseIsotopePatternGenerator.h:79
Representation of an element.
Definition Element.h:34
double getMonoWeight() const
returns the mono isotopic weight of the element
unsigned int getAtomicNumber() const
returns the unique atomic number
Representation of an empirical formula.
Definition EmpiricalFormula.h:63
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
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 ...
EmpiricalFormula(const String &rhs)
Iterator begin()
Definition EmpiricalFormula.h:301
String toString() const
returns the formula as a string (charges are not included)
Int parseFormula_(MapType_ &ef, const String &formula) const
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...
EmpiricalFormula & operator+=(const EmpiricalFormula &rhs)
adds the elements of the given formula
MapType_::const_iterator ConstIterator
Iterators.
Definition EmpiricalFormula.h:89
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:299
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:322
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:92
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:303
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:90
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:297
std::map< const Element *, SignedSize, ElementPtrComparator_ > MapType_
Internal typedef for the used map type.
Definition EmpiricalFormula.h:82
virtual ~EmpiricalFormula()
Destructor.
static EmpiricalFormula hydrogen(int n_atoms=1)
Efficiently generates a formula for hydrogen.
MapType_::iterator Iterator
Definition EmpiricalFormula.h:91
Int charge_
Definition EmpiricalFormula.h:324
std::map< std::string, int > toMap() const
returns the formula as a map (charges are not included)
static EmpiricalFormula fromString(const String &rhs)
Create EmpiricalFormula object from a String.
Definition EmpiricalFormula.h:128
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
A more convenient string class.
Definition String.h:32
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.
Comparator for Element pointers that uses atomic number and mono weight for deterministic ordering.
Definition EmpiricalFormula.h:68
bool operator()(const Element *a, const Element *b) const
Definition EmpiricalFormula.h:69
std::size_t operator()(const OpenMS::EmpiricalFormula &ef) const noexcept
Definition EmpiricalFormula.h:354