OpenMS
EmpiricalFormula.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- 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 <map>
13 #include <set>
14 #include <string>
15 
16 #include <OpenMS/CONCEPT/Types.h>
17 
18 namespace OpenMS
19 {
20  class String;
21  class Element;
22  class ElementDB;
23  class IsotopeDistribution;
24  class IsotopePatternGenerator;
25  class CoarseIsotopePatternGenerator;
26 
58  class OPENMS_DLLAPI EmpiricalFormula
59  {
60 
61 protected:
63  typedef std::map<const Element*, SignedSize> MapType_;
64 
65 public:
70  typedef MapType_::const_iterator ConstIterator;
71  typedef MapType_::const_iterator const_iterator;
72  typedef MapType_::iterator Iterator;
73  typedef MapType_::iterator iterator;
75 
81 
84 
87 
93  explicit EmpiricalFormula(const String& rhs);
94 
96  EmpiricalFormula(SignedSize number, const Element* element, SignedSize charge = 0);
97 
99  virtual ~EmpiricalFormula();
101 
109  static EmpiricalFormula fromString(const String& rhs)
110  {
111  EmpiricalFormula ef(rhs);
112  return ef;
113  }
114 
119  double getMonoWeight() const;
120 
122  double getLightestIsotopeWeight() const;
123 
125  double getAverageWeight() const;
126 
129 
143  bool estimateFromWeightAndComp(double average_weight, double C, double H, double N, double O, double S, double P);
144 
158  bool estimateFromMonoWeightAndComp(double mono_weight, double C, double H, double N, double O, double S, double P);
159 
174  bool estimateFromWeightAndCompAndS(double average_weight, UInt S, double C, double H, double N, double O, double P);
175 
176 
185 
197  const std::set<UInt>& precursor_isotopes,
198  const CoarseIsotopePatternGenerator& method) const;
199 
201  SignedSize getNumberOf(const Element* element) const;
202 
205 
207  Int getCharge() const;
208 
210  void setCharge(Int charge);
211 
213  String toString() const;
214 
216  std::map<std::string, int> toMap() const;
218 
222 
225 
228 
231 
233  EmpiricalFormula operator*(const SignedSize& times) const;
234 
237 
240 
243 
245 
250  bool isEmpty() const;
251 
253  bool isCharged() const;
254 
256  bool hasElement(const Element* element) const;
257 
259  bool contains(const EmpiricalFormula& ef) const;
260 
262  bool operator==(const EmpiricalFormula& rhs) const;
263 
265  bool operator!=(const EmpiricalFormula& rhs) const;
266 
268  bool operator<(const EmpiricalFormula& rhs) const;
269 
271 
273  friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
274 
278  inline ConstIterator begin() const { return formula_.begin(); }
279 
280  inline ConstIterator end() const { return formula_.end(); }
281 
282  inline Iterator begin() { return formula_.begin(); }
283 
284  inline Iterator end() { return formula_.end(); }
286 
289  // @TODO: make these static member variables instead?
291  static EmpiricalFormula hydrogen(int n_atoms = 1);
293 
295  static EmpiricalFormula water(int n_molecules = 1);
297 
298 protected:
299 
302 
304 
306 
307  Int parseFormula_(std::map<const Element*, SignedSize>& ef, const String& formula) const;
308 
309  };
310 
311  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const EmpiricalFormula& formula);
312 
313 } // namespace OpenMS
Isotope pattern generator for coarse isotope distributions.
Definition: CoarseIsotopePatternGenerator.h:79
Representation of an element.
Definition: Element.h:32
Representation of an empirical formula.
Definition: EmpiricalFormula.h:59
Int parseFormula_(std::map< const Element *, SignedSize > &ef, const String &formula) const
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:282
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:63
MapType_::const_iterator ConstIterator
Iterators.
Definition: EmpiricalFormula.h:70
friend std::ostream & operator<<(std::ostream &os, const EmpiricalFormula &formula)
writes the formula to a stream
EmpiricalFormula & operator=(const EmpiricalFormula &)=default
Assignment operator.
EmpiricalFormula & operator-=(const EmpiricalFormula &rhs)
subtracts the elements of a formula
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:280
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:303
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
MapType_::iterator iterator
Definition: EmpiricalFormula.h:73
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:284
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:71
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:278
EmpiricalFormula & operator+=(const EmpiricalFormula &rhs)
adds the elements of the given formula
virtual ~EmpiricalFormula()
Destructor.
EmpiricalFormula & operator=(EmpiricalFormula &&) &=default
Move assignment operator.
MapType_::iterator Iterator
Definition: EmpiricalFormula.h:72
Int charge_
Definition: EmpiricalFormula.h:305
std::map< std::string, int > toMap() const
returns the formula as a map (charges are not included)
static EmpiricalFormula fromString(const String &rhs)
create EmpiricalFormular object by parsing an OpenMS string
Definition: EmpiricalFormula.h:109
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:39
Provides an interface for different isotope pattern generator methods.
Definition: IsotopePatternGenerator.h:42
A more convenient string class.
Definition: String.h:34
int Int
Signed integer type.
Definition: Types.h:76
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
ptrdiff_t SignedSize
Signed Size type e.g. used as pointer difference.
Definition: Types.h:108
static String number(double d, UInt n)
Definition: StringUtils.h:191
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)