| | |
The Formula class provides sophisticated abstractions to work with formulas. More...
| Header: | #include <Formula.hpp> |
| Inherits: | QObject |
| enum class | SplitResult { NOT_SET, FAILURE, HAS_PLUS_COMPONENT, HAS_MINUS_COMPONENT, HAS_BOTH_COMPONENTS } |
| Formula(QObject *parent = nullptr) | |
| Formula(const QString &formula_string, QObject *parent = nullptr) | |
| Formula(const QDomElement &element, int version = 1, QObject *parent = nullptr) | |
| Formula(const MsXpS::libXpertMassCore::Formula &other, QObject *parent = nullptr) | |
| virtual | ~Formula() |
| std::size_t | accountFormula(const QString &formula_string, MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, double times, bool &ok) |
| MsXpS::libXpertMassCore::Formula & | accountMasses(bool &ok, MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, double &mono, double &avg, double times = 1) |
| double | accountSymbolCountPair(const QString &symbol, double count = 1) |
| double | accountSymbolCountPair(std::map<QString, double> &symbol_count_map, const QString &symbol, double count = 1) const |
| bool | accountSymbolCounts(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, int times) |
| QChar | actions() const |
| bool | appendActionFormula(const QString &action_formula) |
| bool | checkSyntax() const |
| void | clear() |
| MsXpS::libXpertMassCore::Formula * | clone(const MsXpS::libXpertMassCore::Formula &other, QObject *parent = nullptr) |
| QString | elementalComposition(std::vector<std::pair<QString, double>> *symbol_count_pairs_p = nullptr) const |
| QString | extractTitle() const |
| QString | formatXmlFormulaElement(int offset, const QString &indent = Utils::xmlIndentationToken) |
| QString | getActionFormula(bool with_title = false) const |
| QString | getPlusFormula() const |
| const std::map<QString, double> & | getSymbolCountMapCstRef() const |
| QString | getTitle() const |
| bool | hasNetMinusPart() |
| MsXpS::libXpertMassCore::Formula & | initialize(const MsXpS::libXpertMassCore::Formula &other) |
| bool | isForceCountIndex() const |
| virtual bool | isValid() const |
| int | removeSpaces() |
| QString | removeTitle() |
| bool | renderXmlFormulaElement(const QDomElement &element, int version = 1) |
| void | setActionFormula(const MsXpS::libXpertMassCore::Formula &formula) |
| void | setActionFormula(const QString &formula) |
| void | setForceCountIndex(bool forceCountIndex) |
| void | setTitle(const QString &title) |
| MsXpS::libXpertMassCore::Formula::SplitResult | splitActionParts(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, double times = 1, bool store = false, bool reset = false) |
| MsXpS::libXpertMassCore::Formula::SplitResult | splitActionParts(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, QString &plus_formula, QString &minus_formula, std::map<QString, double> &symbol_count_map, double times = 1, bool store = false, bool reset = false) const |
| double | symbolCount(const QString &symbol) const |
| double | totalAtoms() const |
| double | totalIsotopes(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp) const |
| virtual bool | validate(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, MsXpS::libXpertMassCore::ErrorList *error_list_p) const |
| virtual bool | validate(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, bool store, bool reset, MsXpS::libXpertMassCore::ErrorList *error_list_p) |
| virtual bool | operator!=(const MsXpS::libXpertMassCore::Formula &other) const |
| virtual MsXpS::libXpertMassCore::Formula & | operator=(const MsXpS::libXpertMassCore::Formula &other) |
| virtual bool | operator==(const MsXpS::libXpertMassCore::Formula &other) const |
| void | actionFormulaChanged() |
| void | titleChanged() |
| void | validChanged() |
| MsXpS::libXpertMassCore::Formula & | accountMasses(MsXpS::libXpertMassCore::Formula &formula, bool &ok, MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, double &mono, double &avg, double times = 1) |
| QChar | actions(const QString &formula) |
| bool | checkSyntax(const QString &formula_string, bool force_count_index = false) |
| QString | m_actionFormula |
| bool | m_forceCountIndex |
| bool | m_isValid |
| QString | m_minusFormula |
| QString | m_plusFormula |
| std::map<QString, double> | m_symbolCountMap |
| QString | m_title |
There are two peculiarities with this Formula implementation:
The action-formula: the main textual element in this Formula class is the action-formula (member m_actionFormula). A formula is the description of the atomic composition of a compound. For example, the string C2H6 is a formula. While the previous C2H6 example describes a static chemical object, a Formula can also describe a dynamic chemical event, like a reaction, by describing what chemical entities are gained by the molecule during the chemical reaction (the "plus" component of the action-formula) and what chemical entities are lost by the molecule (the "minus" component). For example, an acetylation reaction can be described by the loss of H2O with gain of CH3COOH. The net chemical gain on the molecule will be CH3CO. In this example, one would thus define an action-formula in the following way: -H20+CH3COOH. The "minus" formula associated with the '-' action accounts for the leaving group of the reaction, while the "plus" formula associated with the '+' action accounts for the entering group of the reaction. Note that there is no limitation on the amount of such actions, as one could have an action formula like this -H+CO2-H2O+C2H6. An action-formula does not need to have any action sign (+ or -), and if it has no sign, the action-formula is a plus-signed formula by default, which is what the reader would expect for a standard formula.
The title: the action-formula may be documented with a title: a prefix text enclosed in double quotes, like the following: "Decomposed adenine" C5H4N5 +H. This documentation element is called the title. Note that the presence of a title in a formula does not change anything to its workings as long as the title is effectively enclosed in double quotes. The title is by no means for an action-formula to work correctly. It is mainly used in some particular context, like the calculator. An action-formula behaves exactly the same as a simple formula from an end user perspective. Behind the scenes, functions are called to separate all the '+'-associated formulas from all the '-'-associated formulas so that masses are correctly associated to each "leaving" or "entering" chemical groups. Formulas that are '-'-associated are stored in the so-called "minus formula", while '+'-associated ones are stored in the "plus formula". Note that all the formulas in Formula are QString objects.
Upon parsing of the action-formula, the m_minusFormula and the m_plusFormula members are populated with formulas (in the -H+CO2-H2O+C2H6 example, the "minus formula" would contain "HH2O", while the "plus formula" would contain "CO2C2H6") and these are next used to account for the net formula.
This enum type specifies the result of an action-formula parsing process:
| Constant | Value | Description |
|---|---|---|
MsXpS::libXpertMassCore::Formula::SplitResult::NOT_SET | 0x0000 | The value was not set |
MsXpS::libXpertMassCore::Formula::SplitResult::FAILURE | 1 << 0 | The splitting work failed |
MsXpS::libXpertMassCore::Formula::SplitResult::HAS_PLUS_COMPONENT | 1 << 1 | The action formula has a plus component |
MsXpS::libXpertMassCore::Formula::SplitResult::HAS_MINUS_COMPONENT | 1 << 2 | The action formula has a minus component |
MsXpS::libXpertMassCore::Formula::SplitResult::HAS_BOTH_COMPONENTS | (HAS_PLUS_COMPONENT |
HAS_MINUS_COMPONENT) | The action formula has both plus and minus components |
[explicit invokable] Formula::Formula(QObject *parent = nullptr)Constructs a Formula instance.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[explicit invokable] Formula::Formula(const QString &formula_string, QObject *parent = nullptr)Constructs a formula initialized with the formula_string action-formula string.
formula_string needs not be an action-formula, but it might be an action-formula. This formula gets copied into the m_actionFormula without any processing afterwards.
The default status of the Formula (m_isValid) is let to false because the formula cannot be validated without reference isotopic data.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[explicit] Formula::Formula(const QDomElement &element, int version = 1, QObject *parent = nullptr)Constructs a Formula instance using the XML element according to the version.
[explicit] Formula::Formula(const MsXpS::libXpertMassCore::Formula &other, QObject *parent = nullptr)Constructs a formula as a copy of other.
The copy is deep with all the data copied from other to the new formula. There is no processing afterwards.
[virtual noexcept] Formula::~Formula()Destructs this formula.
There is nothing to be delete explicitly.
Accounts into this Formula the formula_string action-formula using isotopic_data_csp as reference data using times as a compounding factor. The result of the operation is set to ok.
The formula_string formula is converted into a temporary Formula and processed:
Returns the size of the member symbol/count m_symbolCountMap.
Accounts this formula's monoisotopic and average masses into mono and avg, using times as a compounding factor.
The masses corresponding to the member action-formula are calculated first and then the mono and avg parameters are updated by incrementing their value with the calculated values. This incrementation might be compounded by that times factor.
The masses of the member action-formula are computed using data from isotopic_data_csp.
Sets ok to false if the calculation failed, to true otherwise.
Returns this object.
See also splitActionParts().
[static] MsXpS::libXpertMassCore::Formula &Formula::accountMasses(MsXpS::libXpertMassCore::Formula &formula, bool &ok, MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, double &mono, double &avg, double times = 1)Accounts the formula monoisotopic and average masses into mono and avg, using times as a compounding factor.
The masses corresponding to the formula are calculated first and then the mono and avg parameters are updated by incrementing their value with the calculated values. This incrementation might be compounded by that times factor.
The masses of the formula are computed using data from isotopic_data_csp.
Sets ok to false if the calculation failed, to true otherwise.
Returns this object.
See also splitActionParts().
Accounts for symbol and corresponding count in the member map.
The m_symbolCountMap relates each atom (chemical element) symbol with its occurrence count as encountered while parsing the member action-formula.
If the symbol was not encountered yet, a new key/value pair is created. Otherwise, the count value is updated.
Returns the new count status for symbol.
Accounts for symbol and corresponding count in the symbol_count_map map.
The symbol_count_map relates each atom (chemical element) symbol with its occurrence count as encountered while parsing the member action-formula.
If the symbol was not encountered yet, a new key/value pair is created. Otherwise, the count value is updated.
Returns the new count status for symbol.
Accounts this Formula's action-formula (m_actionFormula) in the symbol / count member container (m_symbolCountMap).
Calls splitActionParts() to actually parse m_actionFormula and account its components to m_symbolCountMap. The accounting of the symbol / count can be compounded by the times factor.
While splitting the "plus" and "minus" components of the action-formula, their validity is checked against the reference isotopic data isotopic_data_csp.
This function is used when processively accounting many different formulas into the symbol / count map. The formula is set to a new value and this function is called without resetting the symbol / count map, effectively adding formulas onto formulas sequentially.
Returns true if no error was encountered, false otherwise.
See also splitActionParts() and Polymer::elementalComposition.
[invokable] QChar Formula::actions() constCalls actions(const QString &formula) on this Formula's action-formula m_actionFormula. Returns '+' if it only contains "plus" elements or '-' if at least one "minus" element was found.
If m_actionFormula contains no sign at all, then it is considered to contain only '+' elements and the function returns '+'. If at least one element is found associated to a '-', then the "minus" action prevails and the function returns '-'.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also actions(const QString &formula) and splitActionParts().
[static invokable] QChar Formula::actions(const QString &formula)Returns '+' if formula only contains "plus" elements or '-' if at least one "minus" element was found.
If formula contains no sign at all, then it is considered to contain only '+' elements and the function returns '+'. If at least one element is found associated to a '-', then the "minus" action prevails and the function returns '-'.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also actions() and splitActionParts().
[invokable] bool Formula::appendActionFormula(const QString &action_formula)Appends to this formula the action_formula.
The action_formula string is first stripped of its whitespace with QString::simplified(). Then it is appended to the m_actionFormula only if:
The status of this Formula is set to false because there was not explicit validation done.
The function returns false if nothing was changed and true if something was actually appended to m_actionFormula.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] bool Formula::checkSyntax() constReturns true if the member action-formula is syntactically valid, false otherwise.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also checkSyntax(const QString &formula, bool force_count_index).
[static invokable] bool Formula::checkSyntax(const QString &formula_string, bool force_count_index = false)Returns true if the formula_string action-formula is syntactically valid, false otherwise.
If force_count_index is true, the syntax check accounts for the requirement that all the symbols in the formula must be indexed, even if that symbol's count is 1. This means that H2O would not pass the check, while H2O1 would.
The formula is first stripped of its title (if any), then all the spaces are removed.
MsXpS::libXpertMassCore::Formula::subFormulaRegExp is then used to extract each "plus" and / or "minus" component while checking its syntactic validity.
Note: The syntax checking code does not verify that the action-formula is chemically valid, that is, the "Cz4" symbol / count pair would check even if the Cz chemical element does not exist.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also validate().
Clears all the formula member data.
[invokable] MsXpS::libXpertMassCore::Formula *Formula::clone(const MsXpS::libXpertMassCore::Formula &other, QObject *parent = nullptr)Return a newly allocated Formula that is initialized using other and setting its parent to parent.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] QString Formula::elementalComposition(std::vector<std::pair<QString, double>> *symbol_count_pairs_p = nullptr) constReturns a formula matching the contents of the memeber symbol / count map.
The returned formula is formatted according to the IUPAC convention about the ordering of the chemical elements: CxxHxxNxxOxxSxxPxx.
The "plus" components are output first and the "minus" components after.
If symbol_count_pairs_p is not nullptr, each symbol / count pair is added to it.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] QString Formula::extractTitle() constReturns the title from the member action-formula.
The title of a formula is the string, enclosed in double quotes, that is located in front of the actual chemical action-formula. This function removes that title string from the member action-formula using a QRegularExpression.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] QString Formula::formatXmlFormulaElement(int offset, const QString &indent = Utils::xmlIndentationToken)Returns a string containing a formula XML element documenting this Formula instance.
offset and indent define the formatting of the XML element.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] QString Formula::getActionFormula(bool with_title = false) constReturns the action formula, along the the title if with_title is true.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Getter function for property actionFormula.
[invokable] QString Formula::getPlusFormula() constReturns the m_plusFormula formula.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Returns a const reference to the std::map<QString, double> container that relates chemical symbols with corresponding counts.
[invokable] QString Formula::getTitle() constReturns the "title" leading component of a formula.
A fully selfdescribed formula might look like this:
"Acetylation"+CH3COOH-H2O
The first string between quotes is called the title.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Getter function for property title.
[invokable] bool Formula::hasNetMinusPart()Returns true if the member "minus" formula component is not empty, false otherwise.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] MsXpS::libXpertMassCore::Formula &Formula::initialize(const MsXpS::libXpertMassCore::Formula &other)Return a reference to this Formula after having initialized it using other.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[invokable] bool Formula::isForceCountIndex() constReturns true if single atoms should have a count index, false otherwise.
The force count index is set to true, when a formula needs to have atom indices set even if the count for these atoms is 1. For example, H2O1 as compared to H2O.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
[virtual] bool Formula::isValid() constReturns the status of the formula, that is, the result of validate().
Note: Getter function for property valid.
Removes all the space characters from the member action-formula.
Spaces can be placed anywhere in formula for more readability. However, it might be required that these character spaces be removed. This function does just this, using a QRegularExpression.
Returns the number of removed characters.
[invokable] QString Formula::removeTitle()Removes the title from m_actionFormula and returns it.
The title of a formula is the string, enclosed in double quotes, that is located in front of the actual chemical action-formula. This function removes that title string from the member action-formula using a QRegularExpression.
The caller may use the returned title string to set it to m_title.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also Formula::extractTitle() and Formula::setTitle().
Parses a formula XML element according to version and sets the data to the member action-formula checking it syntax.
Returns true if parsing and syntax checking were successful, false otherwise.
See also checkSyntax().
[invokable] void Formula::setActionFormula(const MsXpS::libXpertMassCore::Formula &formula)Sets the action-formula from formula to this Formula.
The action-formula from formula is copied to this m_actionFormula. Since the new formula was not validated, the status of this formula (m_isValid) is set to false. No other processing is performed afterwards.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property actionFormula.
[invokable] void Formula::setActionFormula(const QString &formula)Sets the action-formula formula to this Formula.
The formula is copied to this m_actionFormula. Since the new formula was not validated, the status of this formula (m_isValid) is set to false. No other processing is performed afterwards.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property actionFormula.
[invokable] void Formula::setForceCountIndex(bool forceCountIndex)Sets m_forceCountIndex to forceCountIndex.
When a formula contains a chemical element in a single copy, it is standard practice to omit the count index: H2O is the same as H2O1. If forceCountIndex is true, then the formula has to be in the form H2O1. This is required for some specific calculations. The status (m_isValid) is set to false.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
See also isForceCountIndex().
[invokable] void Formula::setTitle(const QString &title)Sets the title leading component of a formula to title.
A fully self-described formula might look like this:
"Acetylation"+CH3COOH-H2O
The first string between double quotes is called the title.
Note: This function can be invoked via the meta-object system and from QML. See Q_INVOKABLE.
Note: Setter function for property title.
Tells the "plus" ('+') and "minus" ('-') parts in the member actionformula.
Parses the m_actionFormula action-formula and separates all the minus components of that action-formula from all the plus components. The different components are set to their corresponding formula (m_minusFormula and m_plusFormula).
This function delegate its work to the other splitActionParts() passing arguments m_plusFormula, m_minusFormula, m_symbolCountMap, such that this function modifies the content of this very object.
In all the computations above, reference isotopic data are accessed at isotopic_data_csp. If times is not 1, then that value is used to compound the results of the computations. If store is true, then the results of the computations are stored in this object. If reset is true, then the intermediate computation values and objects are reset.
Tells the "plus" ('+') and "minus" ('-') parts in the member action-formula.
Parses the m_actionFormula action-formula and separates all the minus components of that action-formula from all the plus components. The different components are set to plus_formula and minus_formula.
At the end of the split work, each sub-formula (plus_formula and minus_formula) is actually parsed for validity, using the isotopic_data_csp IsotopicData as reference.
If times is not 1, then the accounting of the plus/minus formulas is compounded by this factor.
If store is true, the symbol/count data obtained while parsing of the plus/minus action-formula components are stored in symbol_count_map.
If reset is true, the symbol/count data in symbol_count_map are reset before the parsing operation. Setting this parameter to false may be useful if the caller needs to "accumulate" the accounting of the formulas.
The parsing of the action-formula is performed by performing its deconstruction using Utils::subFormulaRegExp.
Returns FormulaSplitResult::FAILURE if the splitting failed, FormulaSplitResult::HAS_PLUS_COMPONENT if at least one of the components of the action-formula was found to be of type plus, FormulaSplitResult::HAS_MINUS_COMPONENT if at least one of the components of the action-formula was found to be of type minus. The result can be an OR'ing of both values (FormulaSplitResult::HAS_BOTH_COMPONENTS) in the m_actionFormula action-formula.
Because this function does not modify member data, by writing the results of the computations to the passed variables, it is declared const.
If this function completes successfully, then that validates it successfully (syntax ok, and symbols known to the reference isotopic data), and m_isValid is set to true, otherwise m_isValid is set to false.
Returns the count value associated with key symbol in the symbol / count member map m_symbolCountMap.
Returns the total count of symbols (atoms) in this formula.
The determination is performed by summing up all the count values for all the symbols in the member symbol / count pairs in the member map m_symbolCountMap.
Returns the total count of isotopes in this formula using isotopic_data_csp as the reference isotopic data.
The determination is performed by summing up all the isotope counts for all the symbols keys in the member symbol / count map m_symbolCountMap.
[virtual] bool Formula::validate(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, MsXpS::libXpertMassCore::ErrorList *error_list_p) constReturns true if the formula validates successfully, false otherwise.
The validation uses the isotopic_data_csp reference data and involves:
If errors are encountered, meaningful messages are stored in error_list_p (which is not cleared).
If the validation is successful, m_isValid is set to true, otherwise it is set to false.
[virtual] bool Formula::validate(MsXpS::libXpertMassCore::IsotopicDataCstSPtr isotopic_data_csp, bool store, bool reset, MsXpS::libXpertMassCore::ErrorList *error_list_p)Returns true if the formula validates successfully, false otherwise.
See the other validation function for the validation logic.
This function allows to store in member data the results of the validation process if store is set to true. If reset is true, the member data are first cleared.
If the validation is successful, m_isValid is set to true, otherwise it is set to false.
[virtual] bool Formula::operator!=(const MsXpS::libXpertMassCore::Formula &other) constReturns true if this Formula and other are different, false otherwise.
Returns the negated result of operator==().
[virtual] MsXpS::libXpertMassCore::Formula &Formula::operator=(const MsXpS::libXpertMassCore::Formula &other)Initializes all the member data of this formula by copying to it the data from other.
The copy is deep with all the data from other being copied into this formula.
There is no processing afterwards.
[virtual] bool Formula::operator==(const MsXpS::libXpertMassCore::Formula &other) constReturns true if this Formula and other are identical, false otherwise.
The comparison is only performed on the title and action-formula, not on any other member data that actually derive from the processing of the action-formula.
String representing the action-formula.
This variable holds the m_forceCountIndex tells if when defining a chemical composition formula, the index '1' is required when the count of a symbol is not specified and thus considered to be '1' by default. If true, water should be described as "H2O1", if false, it might be described as "H2O".
This variable holds the status of the formula.
String representing the "minus" component of the main m_minusFormula.
This member datum is set upon parsing of m_actionFormula.
String representing the "plus" component of the main m_actionFormula.
This member datum is set upon parsing of m_actionFormula.
Map relating the symbols (as keys) found in the formula and their counts (atoms, in fact, as values).
Note that the count value type is double, which allows for interesting things to be done with Formula. Also, the count value might be negative if the net mass of an action-formula is negative.
See also Formula::splitActionParts().
String representing the title of the action-formula.
The title is the descriptive string in double quotes that is associated to a formula, like this:
"Acetylation"-H2O+CH3COOH