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
216 std::string toString() const;
217
219 std::map<std::string, int> toMap() const;
221
225
228
231
234
237
240
243
246
248
253 bool isEmpty() const;
254
256 bool isCharged() const;
257
259 bool hasElement(const Element* element) const;
260
262 bool contains(const EmpiricalFormula& ef) const;
263
265 bool operator==(const EmpiricalFormula& rhs) const;
266
268 bool operator!=(const EmpiricalFormula& rhs) const;
269
271 bool operator<(const EmpiricalFormula& rhs) const;
272
274
276 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
277
281 inline ConstIterator begin() const { return formula_.begin(); }
282
283 inline ConstIterator end() const { return formula_.end(); }
284
285 inline Iterator begin() { return formula_.begin(); }
286
287 inline Iterator end() { return formula_.end(); }
289
292 // @TODO: make these static member variables instead?
294
295 static EmpiricalFormula hydrogen(int n_atoms = 1);
296
298 static EmpiricalFormula water(int n_molecules = 1);
300
301protected:
302
305
307
309
310 Int parseFormula_(std::map<const Element*, SignedSize>& ef, const std::string& formula) const;
311
312 };
313
314 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
315
316} // namespace OpenMS
317
318// Hash function specialization for EmpiricalFormula
319// Placed in std namespace to allow use with std::unordered_map/set
320namespace std
321{
335 template<>
336 struct hash<OpenMS::EmpiricalFormula>
337 {
338 std::size_t operator()(const OpenMS::EmpiricalFormula& ef) const noexcept
339 {
340 // Collect elements with symbols for deterministic ordering
341 // (map iteration order depends on pointer addresses which vary across runs)
342 // Use symbols instead of atomic numbers to distinguish isotopes like (13)C vs C
343 // Typical formulas have only 4-6 elements, so no need to reserve
344 std::vector<std::pair<std::string, OpenMS::SignedSize>> elements;
345 for (const auto& [element_ptr, count] : ef)
346 {
347 elements.emplace_back(element_ptr->getSymbol(), count);
348 }
349
350 // Sort by symbol for reproducible hash
351 std::sort(elements.begin(), elements.end());
352
353 // Hash in sorted order
354 std::size_t seed = 0;
355 for (const auto& [symbol, count] : elements)
356 {
359 }
360
361 // Hash the charge
362 OpenMS::hash_combine(seed, OpenMS::hash_int(ef.getCharge()));
363
364 return seed;
365 }
366 };
367} // 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:285
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:283
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:306
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:287
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:281
EmpiricalFormula(const std::string &rhs)
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:308
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:338