| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* BEGIN software license | ||
| 2 | * | ||
| 3 | * MsXpertSuite - mass spectrometry software suite | ||
| 4 | * ----------------------------------------------- | ||
| 5 | * Copyright (C) 2009--2024 Filippo Rusconi | ||
| 6 | * | ||
| 7 | * http://www.msxpertsuite.org | ||
| 8 | * | ||
| 9 | * This file is part of the MsXpertSuite project. | ||
| 10 | * | ||
| 11 | * The MsXpertSuite project is the successor of the massXpert project. This | ||
| 12 | * project now includes various independent modules: | ||
| 13 | * | ||
| 14 | * - massXpert, model polymer chemistries and simulate mass spectrometric data; | ||
| 15 | * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner; | ||
| 16 | * | ||
| 17 | * This program is free software: you can redistribute it and/or modify | ||
| 18 | * it under the terms of the GNU General Public License as published by | ||
| 19 | * the Free Software Foundation, either version 3 of the License, or | ||
| 20 | * (at your option) any later version. | ||
| 21 | * | ||
| 22 | * This program is distributed in the hope that it will be useful, | ||
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 25 | * GNU General Public License for more details. | ||
| 26 | * | ||
| 27 | * You should have received a copy of the GNU General Public License | ||
| 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 29 | * | ||
| 30 | * END software license | ||
| 31 | */ | ||
| 32 | |||
| 33 | |||
| 34 | #include <QDebug> | ||
| 35 | |||
| 36 | |||
| 37 | #include "Averagine.hpp" | ||
| 38 | |||
| 39 | |||
| 40 | namespace MsXpS | ||
| 41 | { | ||
| 42 | namespace libXpertMass | ||
| 43 | { | ||
| 44 | |||
| 45 | |||
| 46 | ✗ | Averagine::Averagine(IsotopicDataCstSPtr isotopic_data_csp) | |
| 47 | ✗ | : mcsp_isotopicData(isotopic_data_csp) | |
| 48 | { | ||
| 49 | ✗ | setupDefaultConfiguration(); | |
| 50 | ✗ | } | |
| 51 | |||
| 52 | ✗ | Averagine::Averagine(const Averagine &other) | |
| 53 | ✗ | : mcsp_isotopicData(other.mcsp_isotopicData), | |
| 54 | ✗ | m_avg(other.m_avg), | |
| 55 | ✗ | m_symbolContentMap(other.m_symbolContentMap) | |
| 56 | { | ||
| 57 | ✗ | } | |
| 58 | |||
| 59 | ✗ | Averagine::~Averagine() | |
| 60 | { | ||
| 61 | ✗ | } | |
| 62 | |||
| 63 | Averagine & | ||
| 64 | ✗ | Averagine::operator=(const Averagine &other) | |
| 65 | { | ||
| 66 | ✗ | if(&other == this) | |
| 67 | ✗ | return *this; | |
| 68 | |||
| 69 | ✗ | mcsp_isotopicData = other.mcsp_isotopicData; | |
| 70 | ✗ | m_avg = other.m_avg; | |
| 71 | ✗ | m_symbolContentMap = other.m_symbolContentMap; | |
| 72 | |||
| 73 | ✗ | return *this; | |
| 74 | } | ||
| 75 | |||
| 76 | void | ||
| 77 | ✗ | Averagine::setIsotopicData(IsotopicDataCstSPtr isotopic_data_csp) | |
| 78 | { | ||
| 79 | |||
| 80 | ✗ | assert(isotopic_data_csp != nullptr && isotopic_data_csp.get() != nullptr); | |
| 81 | |||
| 82 | ✗ | mcsp_isotopicData = isotopic_data_csp; | |
| 83 | |||
| 84 | ✗ | if(!validate()) | |
| 85 | ✗ | qFatal("The Averagine validation failed after setting new isotopic data."); | |
| 86 | |||
| 87 | ✗ | computeAvgMass(); | |
| 88 | ✗ | } | |
| 89 | |||
| 90 | void | ||
| 91 | ✗ | Averagine::setContent(QString &symbol, double content) | |
| 92 | { | ||
| 93 | ✗ | m_symbolContentMap[symbol] = content; | |
| 94 | |||
| 95 | ✗ | if(!validate()) | |
| 96 | ✗ | qFatal("The Averagine validation failed after setting new symbol content."); | |
| 97 | |||
| 98 | ✗ | computeAvgMass(); | |
| 99 | ✗ | } | |
| 100 | |||
| 101 | double | ||
| 102 | ✗ | Averagine::getContent(QString &symbol) const | |
| 103 | { | ||
| 104 | ✗ | auto iter = m_symbolContentMap.find(symbol); | |
| 105 | ✗ | assert(iter != m_symbolContentMap.end()); | |
| 106 | |||
| 107 | ✗ | return iter->second; | |
| 108 | } | ||
| 109 | |||
| 110 | void | ||
| 111 | ✗ | Averagine::setFormula(const QString formula_string) | |
| 112 | { | ||
| 113 | ✗ | Formula formula(formula_string); | |
| 114 | |||
| 115 | // The formula is asked to validate with storage of the found symbol/count | ||
| 116 | // pairs and with resetting of the previous contents of the symbol/count map. | ||
| 117 | ✗ | if(!formula.validate(mcsp_isotopicData, true, true)) | |
| 118 | ✗ | qFatal("The formula passed as argument did not validate."); | |
| 119 | |||
| 120 | ✗ | m_formula = formula; | |
| 121 | ✗ | } | |
| 122 | |||
| 123 | double | ||
| 124 | ✗ | Averagine::getAvgMass() const | |
| 125 | { | ||
| 126 | ✗ | return m_avg; | |
| 127 | } | ||
| 128 | |||
| 129 | double | ||
| 130 | ✗ | Averagine::computeFormulaAveragineEquivalents(const QString &formula_string) | |
| 131 | { | ||
| 132 | // If something is wrong, qFatal(). | ||
| 133 | ✗ | if(!formula_string.isEmpty()) | |
| 134 | ✗ | setFormula(formula_string); | |
| 135 | |||
| 136 | // At this point, we do have m_formula fine and that has been validated. | ||
| 137 | |||
| 138 | ✗ | double formula_average_mass = 0; | |
| 139 | ✗ | m_formula.accountMasses(mcsp_isotopicData, &formula_average_mass); | |
| 140 | |||
| 141 | // There are many checks in the code that m_avg is not 0. | ||
| 142 | ✗ | double averagine_equivs = formula_average_mass / m_avg; | |
| 143 | |||
| 144 | ✗ | return averagine_equivs; | |
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | double | ||
| 149 | ✗ | Averagine::computeFormulaMonoMass(const QString &formula_string) | |
| 150 | { | ||
| 151 | // If something is wrong, qFatal(). | ||
| 152 | ✗ | double averagine_equivs = computeFormulaAveragineEquivalents(formula_string); | |
| 153 | |||
| 154 | ✗ | double mono_mass = 0; | |
| 155 | |||
| 156 | ✗ | for(std::pair<QString, double> pair : m_symbolContentMap) | |
| 157 | { | ||
| 158 | ✗ | bool ok = false; | |
| 159 | ✗ | mono_mass += mcsp_isotopicData->getMonoMassBySymbol(pair.first, &ok) * | |
| 160 | averagine_equivs; | ||
| 161 | |||
| 162 | ✗ | if(!ok) | |
| 163 | ✗ | qFatal("Failed to get mono mass for symbol."); | |
| 164 | ✗ | } | |
| 165 | |||
| 166 | ✗ | return mono_mass; | |
| 167 | } | ||
| 168 | |||
| 169 | bool | ||
| 170 | ✗ | Averagine::validate() | |
| 171 | { | ||
| 172 | ✗ | if(!m_avg) | |
| 173 | ✗ | qFatal("It is not possible that m_avg of Averagine be 0."); | |
| 174 | |||
| 175 | ✗ | for(std::pair<QString, double> pair : m_symbolContentMap) | |
| 176 | { | ||
| 177 | ✗ | if(!mcsp_isotopicData->containsSymbol(pair.first)) | |
| 178 | ✗ | return false; | |
| 179 | ✗ | } | |
| 180 | |||
| 181 | ✗ | return true; | |
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | void | ||
| 186 | ✗ | Averagine::setupDefaultConfiguration() | |
| 187 | { | ||
| 188 | ✗ | m_symbolContentMap["C"] = 4.9384; | |
| 189 | ✗ | m_symbolContentMap["H"] = 7.7583; | |
| 190 | ✗ | m_symbolContentMap["N"] = 1.3577; | |
| 191 | ✗ | m_symbolContentMap["O"] = 1.4773; | |
| 192 | ✗ | m_symbolContentMap["S"] = 0.0417; | |
| 193 | |||
| 194 | ✗ | computeAvgMass(); | |
| 195 | ✗ | } | |
| 196 | |||
| 197 | |||
| 198 | double | ||
| 199 | ✗ | Averagine::computeAvgMass() | |
| 200 | { | ||
| 201 | |||
| 202 | ✗ | for(std::pair<QString, double> pair : m_symbolContentMap) | |
| 203 | { | ||
| 204 | ✗ | bool ok = false; | |
| 205 | ✗ | m_avg += | |
| 206 | ✗ | mcsp_isotopicData->getAvgMassBySymbol(pair.first, &ok) * pair.second; | |
| 207 | |||
| 208 | ✗ | if(!ok) | |
| 209 | ✗ | qFatal("Failed to get mono mass for symbol."); | |
| 210 | ✗ | } | |
| 211 | |||
| 212 | ✗ | if(!m_avg) | |
| 213 | ✗ | qFatal("It is not possible that m_avg of Averagine be 0."); | |
| 214 | |||
| 215 | ✗ | return m_avg; | |
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | } // namespace libXpertMass | ||
| 220 | |||
| 221 | } // namespace MsXpS | ||
| 222 |