chemistry_tools.formulae.formula
Attention
This module has the following additional requirements:
cawdrey>=0.5.0 mathematical>=0.5.1 pyparsing>=2.4.6 tabulate>=0.8.9
These can be installed as follows:
python -m pip install chemistry-tools[formulae]
Parse formulae into a Python object.
Data:
Invariant |
Classes:
|
A Formula object stores a chemical composition of a compound. |
-
F
= TypeVar(F, bound=Formula) Type:
TypeVar
Invariant
TypeVar
bound tochemistry_tools.formulae.formula.Formula
.
-
class
Formula
(composition=None, charge=0)[source] Bases:
defaultdict
,Counter
A Formula object stores a chemical composition of a compound. It is based on
dict
, with the symbols of chemical elements as keys and the values equal to the number of atoms of the corresponding element in the compound.- Parameters
Methods:
__add__
(other)Return
self + value
.__eq__
(other)Return
self == other
.__iadd__
(other)Inplace add from another counter, keeping only positive counts.
__imul__
(other)- rtype
__isub__
(other)Inplace subtract counter, but keep only results with positive counts.
__mul__
(other)Return
self * value
.__radd__
(other)Return
value + self
.__repr__
()Return a string representation of the
Formula
.__rmul__
(other)Return
value * self
.__rsub__
(other)Return
value - self
.__setitem__
(key, value)Set
self[key]
tovalue
.__str__
()Return
str(self)
.__sub__
(other)Return
value - self
.copy
()Returns a copy of the
Formula
.from_kwargs
(*[, charge])Create a new
Formula
object from keyword arguments representing the elements in the compound.from_mass_fractions
(fractions[, charge, …])Create a new
Formula
object from elemental mass fractions by parsing a string.from_string
(formula[, charge])Create a new
Formula
object by parsing a string.get_mz
([average, charge])Calculate the average mass:charge ratio (m/z) of a
Formula
.Returns an
IsotopeDistribution
object representing the distribution of the isotopologues of the formula.iter_isotopologues
([report_abundance, …])Iterate over possible isotopic states of the molecule.
Calculate the most probable isotopic composition of a molecule/ion.
Attributes:
Calculate the average mass of a
Formula
.The average mass to charge ratio of the formula.
A
Composition
object representing the elemental composition of the Formula.A list of the element symbols in the formula.
Returns the empirical formula in Hill notation.
Calculate the monoisotopic mass of a
Formula
.Returns the formula in Hill notation.
Calculate the relative abundance of the current isotopic composition of this molecule.
Calculate the average mass of a
Formula
.Calculate the monoisotopic mass of a
Formula
.The mass to charge ratio of the formula.
Return the number of atoms in the formula.
Return the number of elements in the formula.
Returns formula in Hill notation, without any isotopes specified.
Calculate the monoisotopic mass of a
Formula
.-
__iadd__
(other)[source] Inplace add from another counter, keeping only positive counts.
>>> c = Counter('abbb') >>> c += Counter('bcc') >>> c Counter({'b': 4, 'c': 2, 'a': 1})
- Return type
-
__isub__
(other)[source] Inplace subtract counter, but keep only results with positive counts.
>>> c = Counter('abbbc') >>> c -= Counter('bccd') >>> c Counter({'b': 2, 'a': 1})
- Return type
-
property
average_mass
Calculate the average mass of a
Formula
.Note that mass is not averaged for elements with specified isotopes.
- Return type
-
property
composition
A
Composition
object representing the elemental composition of the Formula.- Return type
-
property
empirical_formula
Returns the empirical formula in Hill notation.
The empirical formula has the simplest whole number ratio of atoms of each element present in the formula.
Examples:
>>> Formula.from_string('H2O').empirical_formula 'H2O' >>> Formula.from_string('S4').empirical_formula 'S' >>> Formula.from_string('C6H12O6').empirical_formula 'CH2O'
- Return type
-
property
exact_mass
Calculate the monoisotopic mass of a
Formula
. If any isotopes are already present in the formula, the mass of these will be preserved- Return type
-
classmethod
from_kwargs
(*, charge=0, **kwargs)[source] Create a new
Formula
object from keyword arguments representing the elements in the compound.
-
classmethod
from_mass_fractions
(fractions, charge=0, maxcount=10, precision=0.0001)[source] Create a new
Formula
object from elemental mass fractions by parsing a string.Note
Isotopes cannot (currently) be parsed using this method
- Parameters
Examples:
>>> Formula.from_mass_fractions({'H': 0.112, 'O': 0.888}) 'H2O' >>> Formula.from_mass_fractions({'D': 0.2, 'O': 0.8}) 'O[2H]2' >>> Formula.from_mass_fractions({'H': 8.97, 'C': 59.39, 'O': 31.64}) 'C5H9O2' >>> Formula.from_mass_fractions({'O': 0.26, '30Si': 0.74}) 'O2[30Si]3'
- Return type
-
classmethod
from_string
(formula, charge=0)[source] Create a new
Formula
object by parsing a string.Note
Isotopes cannot (currently) be parsed using this method
- Return type
-
get_mz
(average=True, charge=None)[source] Calculate the average mass:charge ratio (m/z) of a
Formula
.- Parameters
- Return type
-
property
hill_formula
Returns the formula in Hill notation.
Example:
>>> Formula.from_string('BrC2H5').hill_formula 'C2H5Br' >>> Formula.from_string('HBr').hill_formula 'BrH' >>> Formula.from_string('[(CH3)3Si2]2NNa').hill_formula 'C6H18NNaSi4'
- Return type
-
isotope_distribution
()[source] Returns an
IsotopeDistribution
object representing the distribution of the isotopologues of the formula.- Return type
-
property
isotopic_composition_abundance
Calculate the relative abundance of the current isotopic composition of this molecule.
- Return type
- Returns
The relative abundance of the current isotopic composition.
-
iter_isotopologues
(report_abundance=False, elements_with_isotopes=None, isotope_threshold=0.0005, overall_threshold=0)[source] Iterate over possible isotopic states of the molecule.
The space of possible isotopic compositions is restrained by parameters
elements_with_isotopes
,isotope_threshold
,overall_threshold
.- Parameters
report_abundance (
bool
) – IfTrue
, the output will contain 2-tuples: (composition, abundance). Otherwise, only compositions are yielded. DefaultFalse
.elements_with_isotopes (
Optional
[Sequence
[str
]]) – A set of elements to be considered in isotopic distributions (by default, every element has an isotopic distribution). DefaultNone
.isotope_threshold (
float
) – The threshold abundance of a specific isotope to be considered. Default0.0005
.overall_threshold (
float
) – The threshold abundance of the calculated isotopic composition. Default0
.
- Return type
- Returns
Iterator over possible isotopic compositions.
-
property
mass
Calculate the average mass of a
Formula
.Note that mass is not averaged for elements with specified isotopes.
- Return type
-
property
monoisotopic_mass
Calculate the monoisotopic mass of a
Formula
. If any isotopes are already present in the formula, the mass of these will be preserved- Return type
-
most_probable_isotopic_composition
(elements_with_isotopes=None)[source] Calculate the most probable isotopic composition of a molecule/ion.
For each element, only two most abundant isotopes are considered. Any isotopes already in the Formula will be changed to the most abundant isotope
-
property
n_atoms
Return the number of atoms in the formula.
Example:
>>> Formula.from_string('CH3COOH').n_atoms 8
- Return type
-
property
n_elements
Return the number of elements in the formula.
- Return type
Example:
>>> Formula.from_string('CH3COOH').n_elements 3
-
property
no_isotope_hill_formula
Returns formula in Hill notation, without any isotopes specified.
Example:
>>> Formula.from_string('BrC2H5').no_isotope_hill_formula 'C2H5Br' >>> Formula.from_string('HBr').no_isotope_hill_formula 'BrH' >>> Formula.from_string('[(CH3)3Si2]2NNa').no_isotope_hill_formula 'C6H18NNaSi4'
- Return type