libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
precision.cpp
Go to the documentation of this file.
1/**
2 * \file pappsomspp/mass_range.cpp
3 * \date 4/3/2015
4 * \author Olivier Langella
5 * \brief object to handle a mass range (an mz value + or - some delta)
6 */
7
8/*******************************************************************************
9 * Copyright (c) 2015 Olivier Langella <Olivier.Langella@moulon.inra.fr>.
10 *
11 * This file is part of the PAPPSOms++ library.
12 *
13 * PAPPSOms++ is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * PAPPSOms++ is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
25 *
26 * Contributors:
27 * Olivier Langella <Olivier.Langella@moulon.inra.fr> - initial API and
28 *implementation
29 ******************************************************************************/
30
31#include "types.h"
32#include "precision.h"
33#include "mzrange.h"
35#include <QStringList>
36#include <cmath>
37#include <QDebug>
38#include <QRegularExpression>
39
40namespace pappso
41{
42
43std::map<Enums::PrecisionUnit, QString> precisionUnitMap = {{Enums::PrecisionUnit::none, "none"},
48
49
51 MapDaltonPrecision ret;
52
53 return ret;
54}();
55
56
58 MapPpmPrecision ret;
59
60 return ret;
61}();
62
63
65 MapResPrecision ret;
66
67 return ret;
68}();
69
70
73{
74 return m_nominal;
75}
76
77
80{
81
82 // The format of the string is <number><space *><string> with string either
83 // "ppm" or "dalton" or "res".
84 //
85 // If there only once component, that is, <string> is omitted and charge is
86 // not provided, then "dalton" is considered.
87
88 QStringList list = str.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
89
90 if(list.size() > 0)
91 {
92 bool ok;
93 pappso_double value = list[0].toDouble(&ok);
94 if(!ok)
95 {
96 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string :\nunable to "
97 "convert %1 to number in %2")
98 .arg(value)
99 .arg(str));
100 }
101 if(list.size() == 1)
102 {
104 }
105 else if(list.size() == 2)
106 {
107 if(list[1].toLower() == "dalton")
108 {
110 }
111
112 if(list[1].toLower() == "ppm")
113 {
115 }
116
117 if(list[1].toLower() == "res")
118 {
120 }
121
122 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string :\nprecision "
123 "unit %1 to not known in %2")
124 .arg(list[1])
125 .arg(str));
126 }
127 }
128
129 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string "
130 ":\nunable to convert %1 to precision")
131 .arg(str));
132}
133
136{
137 MapDaltonPrecision::iterator it = m_mapDalton.find(value);
138 if(it == m_mapDalton.end())
139 {
140 // not found
141 std::pair<MapDaltonPrecision::iterator, bool> insert_res = m_mapDalton.insert(
142 std::pair<pappso_double, DaltonPrecision *>(value, new DaltonPrecision(value)));
143 it = insert_res.first;
144 }
145 else
146 {
147 // found
148 }
149 return it->second;
150}
151
152
155{
156 if(!value)
158 QObject::tr("Fatal error at precision.cpp "
159 "-- ERROR trying to set a Resolution precision value of 0. "
160 "Program aborted."));
161
162 MapPpmPrecision::iterator it = m_mapPpm.find(value);
163
164 if(it == m_mapPpm.end())
165 {
166 // Not found.
167 std::pair<MapPpmPrecision::iterator, bool> insert_res =
168 m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(value, new PpmPrecision(value)));
169 it = insert_res.first;
170 }
171 else
172 {
173 // found
174 }
175 return it->second;
176}
177
178
181{
182 // qDebug() << "Requested a Resolution precision instance for value:" <<
183 // value;
184
185 if(!value)
187 QObject::tr("Fatal error at precision.cpp "
188 "-- ERROR trying to set a Resolution precision value of 0. "
189 "Program aborted."));
190
191 MapResPrecision::iterator it = m_mapRes.find(value);
192
193 if(it == m_mapRes.end())
194 {
195 // not found
196 std::pair<MapResPrecision::iterator, bool> insert_res =
197 m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(value, new ResPrecision(value)));
198 it = insert_res.first;
199 }
200 else
201 {
202 // found
203 }
204 return it->second;
205}
206
209{
210 Enums::PrecisionUnit unit = origin->unit();
211 double value = origin->getNominal() * fraction;
212
213
214 return getPrecisionPtrInstance(unit, value);
215}
216
219{
220
221 switch(unit)
222 {
224 return getDaltonInstance(value);
226 return getPpmInstance(value);
228 return getResInstance(value);
230 return getDaltonInstance(value);
232 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
233
234 default:
235 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
236 break;
237 }
238
239 return nullptr;
240}
241
245
249
255
257DaltonPrecision::delta([[maybe_unused]] pappso_double value) const
258{
259 return m_nominal;
260}
261
262QString
264{
265 return (QString("%1 dalton").arg(m_nominal));
266}
267
268
272
273
277
280{
282}
283
284
287{
288 return ((value / ONEMILLION) * m_nominal);
289}
290
291
292QString
294{
295 return (QString("%1 ppm").arg(m_nominal));
296}
297
298
302
303
307
310{
312}
313
314
317{
318 return (value / m_nominal);
319}
320
321
322QString
324{
325 return (QString("%1 res").arg(m_nominal));
326}
327
328} // namespace pappso
virtual QString toString() const override
virtual Enums::PrecisionUnit unit() const override
DaltonPrecision(pappso_double x)
virtual pappso_double delta(pappso_double value) const override
PpmPrecision(pappso_double x)
virtual pappso_double delta(pappso_double value) const override
virtual Enums::PrecisionUnit unit() const override
virtual QString toString() const override
const pappso_double m_nominal
Definition precision.h:46
PrecisionBase(pappso_double nominal)
Definition precision.h:48
virtual pappso_double getNominal() const final
Definition precision.cpp:72
virtual Enums::PrecisionUnit unit() const =0
static PrecisionPtr getPrecisionPtrInstance(Enums::PrecisionUnit unit, double value)
get a precision pointer instance
static PrecisionPtr getResInstance(pappso_double value)
get a resolution precision pointer
static PrecisionPtr fromString(const QString &str)
get a precision pointer from a string
Definition precision.cpp:79
static MapResPrecision m_mapRes
Definition precision.h:135
static MapPpmPrecision m_mapPpm
Definition precision.h:134
std::map< pappso_double, PpmPrecision * > MapPpmPrecision
Definition precision.h:129
static PrecisionPtr getPpmInstance(pappso_double value)
get a ppm precision pointer
static PrecisionPtr getPrecisionPtrFractionInstance(PrecisionPtr origin, double fraction)
get the fraction of an existing precision pointer
static PrecisionPtr getDaltonInstance(pappso_double value)
get a Dalton precision pointer
std::map< pappso_double, DaltonPrecision * > MapDaltonPrecision
Definition precision.h:128
static MapDaltonPrecision m_mapDalton
Definition precision.h:133
std::map< pappso_double, ResPrecision * > MapResPrecision
Definition precision.h:130
virtual Enums::PrecisionUnit unit() const override
virtual pappso_double delta(pappso_double value) const override
virtual QString toString() const override
ResPrecision(pappso_double x)
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
const pappso_double ONEMILLION(1000000)
double pappso_double
A type definition for doubles.
Definition types.h:61
const PrecisionBase * PrecisionPtr
Definition precision.h:122
std::map< Enums::PrecisionUnit, QString > precisionUnitMap
Definition precision.cpp:43