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 "precision.h"
32#include "mzrange.h"
34#include <QStringList>
35#include <cmath>
36#include <QDebug>
37#include <QRegularExpression>
38
39namespace pappso
40{
41
42
44 MapDaltonPrecision ret;
45
46 return ret;
47}();
48
49
51 MapPpmPrecision ret;
52
53 return ret;
54}();
55
56
58 MapResPrecision ret;
59
60 return ret;
61}();
62
63
66{
67 return m_nominal;
68}
69
70
73{
74
75 // The format of the string is <number><space *><string> with string either
76 // "ppm" or "dalton" or "res".
77 //
78 // If there only once component, that is, <string> is omitted and charge is
79 // not provided, then "dalton" is considered.
80
81 QStringList list = str.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
82
83 if(list.size() > 0)
84 {
85 bool ok;
86 pappso_double value = list[0].toDouble(&ok);
87 if(!ok)
88 {
90 QObject::tr("ERROR getting precision from string :\nunable to "
91 "convert %1 to number in %2")
92 .arg(value)
93 .arg(str));
94 }
95 if(list.size() == 1)
96 {
98 }
99 else if(list.size() == 2)
100 {
101 if(list[1].toLower() == "dalton")
102 {
104 }
105
106 if(list[1].toLower() == "ppm")
107 {
109 }
110
111 if(list[1].toLower() == "res")
112 {
114 }
115
117 QObject::tr("ERROR getting precision from string :\nprecision "
118 "unit %1 to not known in %2")
119 .arg(list[1])
120 .arg(str));
121 }
122 }
123
124 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string "
125 ":\nunable to convert %1 to precision")
126 .arg(str));
127}
128
131{
132 MapDaltonPrecision::iterator it = m_mapDalton.find(value);
133 if(it == m_mapDalton.end())
134 {
135 // not found
136 std::pair<MapDaltonPrecision::iterator, bool> insert_res =
137 m_mapDalton.insert(std::pair<pappso_double, DaltonPrecision *>(
138 value, new DaltonPrecision(value)));
139 it = insert_res.first;
140 }
141 else
142 {
143 // found
144 }
145 return it->second;
146}
147
148
151{
152 if(!value)
154 QObject::tr("Fatal error at precision.cpp "
155 "-- ERROR trying to set a Resolution precision value of 0. "
156 "Program aborted."));
157
158 MapPpmPrecision::iterator it = m_mapPpm.find(value);
159
160 if(it == m_mapPpm.end())
161 {
162 // Not found.
163 std::pair<MapPpmPrecision::iterator, bool> insert_res =
164 m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(
165 value, new PpmPrecision(value)));
166 it = insert_res.first;
167 }
168 else
169 {
170 // found
171 }
172 return it->second;
173}
174
175
178{
179 // qDebug() << "Requested a Resolution precision instance for value:" <<
180 // value;
181
182 if(!value)
184 QObject::tr("Fatal error at precision.cpp "
185 "-- ERROR trying to set a Resolution precision value of 0. "
186 "Program aborted."));
187
188 MapResPrecision::iterator it = m_mapRes.find(value);
189
190 if(it == m_mapRes.end())
191 {
192 // not found
193 std::pair<MapResPrecision::iterator, bool> insert_res =
194 m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(
195 value, new ResPrecision(value)));
196 it = insert_res.first;
197 }
198 else
199 {
200 // found
201 }
202 return it->second;
203}
204
207 double fraction)
208{
209 PrecisionUnit unit = origin->unit();
210 double value = origin->getNominal() * fraction;
211
212
213 return getPrecisionPtrInstance(unit, value);
214}
215
218{
219
220 switch(unit)
221 {
223 return getDaltonInstance(value);
225 return getPpmInstance(value);
227 return getResInstance(value);
229 return getDaltonInstance(value);
231 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
232
233 default:
234 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
235 break;
236 }
237
238 return nullptr;
239}
240
244
248
251{
253}
254
256DaltonPrecision::delta([[maybe_unused]] pappso_double value) const
257{
258 return m_nominal;
259}
260
261QString
263{
264 return (QString("%1 dalton").arg(m_nominal));
265}
266
267
271
272
276
279{
280 return PrecisionUnit::ppm;
281}
282
283
286{
287 return ((value / ONEMILLION) * m_nominal);
288}
289
290
291QString
293{
294 return (QString("%1 ppm").arg(m_nominal));
295}
296
297
301
302
306
309{
310 return PrecisionUnit::res;
311}
312
313
316{
317 return (value / m_nominal);
318}
319
320
321QString
323{
324 return (QString("%1 res").arg(m_nominal));
325}
326
327} // namespace pappso
virtual QString toString() const override
DaltonPrecision(pappso_double x)
virtual pappso_double delta(pappso_double value) const override
virtual PrecisionUnit unit() const override
PpmPrecision(pappso_double x)
virtual pappso_double delta(pappso_double value) const override
virtual PrecisionUnit unit() const override
virtual QString toString() const override
const pappso_double m_nominal
Definition precision.h:46
virtual pappso_double getNominal() const final
Definition precision.cpp:65
virtual PrecisionUnit unit() const =0
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:72
static PrecisionPtr getPrecisionPtrInstance(PrecisionUnit unit, double value)
get a precision pointer instance
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 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
PrecisionUnit
Definition types.h:76
const pappso_double ONEMILLION(1000000)
double pappso_double
A type definition for doubles.
Definition types.h:50