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:
67 typedef std::map<const Element*, SignedSize> MapType_;
68
69public:
74 typedef MapType_::const_iterator ConstIterator;
75 typedef MapType_::const_iterator const_iterator;
76 typedef MapType_::iterator Iterator;
77 typedef MapType_::iterator iterator;
79
85
88
91
97 explicit EmpiricalFormula(const String& rhs);
98
100 EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge = 0);
101
105
114 {
115 EmpiricalFormula ef(rhs);
116 return ef;
117 }
118
123 double getMonoWeight() const;
124
127
129 double getAverageWeight() const;
130
133
147 bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P);
148
162 bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P);
163
178 bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P);
179
180
189
201 const std::set<UInt>& precursor_isotopes,
202 const CoarseIsotopePatternGenerator& method) const;
203
205 SignedSize getNumberOf(const Element* element) const;
206
209
211 Int getCharge() const;
212
214 void setCharge(Int charge);
215
218
220 std::map<std::string, int> toMap() const;
222
226
229
232
235
238
241
244
247
249
254 bool isEmpty() const;
255
257 bool isCharged() const;
258
260 bool hasElement(const Element* element) const;
261
263 bool contains(const EmpiricalFormula& ef) const;
264
266 bool operator==(const EmpiricalFormula& rhs) const;
267
269 bool operator!=(const EmpiricalFormula& rhs) const;
270
272 bool operator<(const EmpiricalFormula& rhs) const;
273
275
277 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
278
282 inline ConstIterator begin() const { return formula_.begin(); }
283
284 inline ConstIterator end() const { return formula_.end(); }
285
286 inline Iterator begin() { return formula_.begin(); }
287
288 inline Iterator end() { return formula_.end(); }
290
293 // @TODO: make these static member variables instead?
295
296 static EmpiricalFormula hydrogen(int n_atoms = 1);
297
299 static EmpiricalFormula water(int n_molecules = 1);
301
302protected:
303
306
308
310
311 Int parseFormula_(std::map<const Element*, SignedSize>& ef, const String& formula) const;
312
313 };
314
315 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
316
317} // namespace OpenMS
318
319// Hash function specialization for EmpiricalFormula
320// Placed in std namespace to allow use with std::unordered_map/set
321namespace std
322{
336 template<>
337 struct hash<OpenMS::EmpiricalFormula>
338 {
339 std::size_t operator()(const OpenMS::EmpiricalFormula& ef) const noexcept
340 {
341 // Collect elements with symbols for deterministic ordering
342 // (map iteration order depends on pointer addresses which vary across runs)
343 // Use symbols instead of atomic numbers to distinguish isotopes like (13)C vs C
344 // Typical formulas have only 4-6 elements, so no need to reserve
345 std::vector<std::pair<std::string, OpenMS::SignedSize>> elements;
346 for (const auto& [element_ptr, count] : ef)
347 {
348 elements.emplace_back(element_ptr->getSymbol(), count);
349 }
350
351 // Sort by symbol for reproducible hash
352 std::sort(elements.begin(), elements.end());
353
354 // Hash in sorted order
355 std::size_t seed = 0;
356 for (const auto& [symbol, count] : elements)
357 {
360 }
361
362 // Hash the charge
363 OpenMS::hash_combine(seed, OpenMS::hash_int(ef.getCharge()));
364
365 return seed;
366 }
367 };
368} // namespace std
Isotope pattern generator for coarse isotope distributions.
Definition CoarseIsotopePatternGenerator.h:79
Representation of an element.
Definition Element.h:34
Representation of an empirical formula.
Definition EmpiricalFormula.h:63
Int parseFormula_(std::map< const Element *, SignedSize > &ef, const String &formula) const
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:286
String toString() const
returns the formula as a string (charges are not included)
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:67
EmpiricalFormula & operator+=(const EmpiricalFormula &rhs)
adds the elements of the given formula
MapType_::const_iterator ConstIterator
Iterators.
Definition EmpiricalFormula.h:74
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:284
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:307
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:77
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:288
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:75
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:282
virtual ~EmpiricalFormula()
Destructor.
static EmpiricalFormula hydrogen(int n_atoms=1)
Efficiently generates a formula for hydrogen.
MapType_::iterator Iterator
Definition EmpiricalFormula.h:76
Int charge_
Definition EmpiricalFormula.h:309
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:113
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:34
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:339