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 = {
49
50
52 MapDaltonPrecision ret;
53
54 return ret;
55}();
56
57
59 MapPpmPrecision ret;
60
61 return ret;
62}();
63
64
66 MapResPrecision ret;
67
68 return ret;
69}();
70
71
74{
75 return m_nominal;
76}
77
78
81{
82
83 // The format of the string is <number><space *><string> with string either
84 // "ppm" or "dalton" or "res".
85 //
86 // If there only once component, that is, <string> is omitted and charge is
87 // not provided, then "dalton" is considered.
88
89 QStringList list = str.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
90
91 if(list.size() > 0)
92 {
93 bool ok;
94 pappso_double value = list[0].toDouble(&ok);
95 if(!ok)
96 {
97 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string :\nunable to "
98 "convert %1 to number in %2")
99 .arg(value)
100 .arg(str));
101 }
102 if(list.size() == 1)
103 {
105 }
106 else if(list.size() == 2)
107 {
108 if(list[1].toLower() == "dalton")
109 {
111 }
112
113 if(list[1].toLower() == "ppm")
114 {
116 }
117
118 if(list[1].toLower() == "res")
119 {
121 }
122
123 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string :\nprecision "
124 "unit %1 to not known in %2")
125 .arg(list[1])
126 .arg(str));
127 }
128 }
129
130 throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string "
131 ":\nunable to convert %1 to precision")
132 .arg(str));
133}
134
137{
138 MapDaltonPrecision::iterator it = m_mapDalton.find(value);
139 if(it == m_mapDalton.end())
140 {
141 // not found
142 std::pair<MapDaltonPrecision::iterator, bool> insert_res = m_mapDalton.insert(
143 std::pair<pappso_double, DaltonPrecision *>(value, new DaltonPrecision(value)));
144 it = insert_res.first;
145 }
146 else
147 {
148 // found
149 }
150 return it->second;
151}
152
153
156{
157 if(!value)
159 QObject::tr("Fatal error at precision.cpp "
160 "-- ERROR trying to set a Resolution precision value of 0. "
161 "Program aborted."));
162
163 MapPpmPrecision::iterator it = m_mapPpm.find(value);
164
165 if(it == m_mapPpm.end())
166 {
167 // Not found.
168 std::pair<MapPpmPrecision::iterator, bool> insert_res =
169 m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(value, new PpmPrecision(value)));
170 it = insert_res.first;
171 }
172 else
173 {
174 // found
175 }
176 return it->second;
177}
178
179
182{
183 // qDebug() << "Requested a Resolution precision instance for value:" <<
184 // value;
185
186 if(!value)
188 QObject::tr("Fatal error at precision.cpp "
189 "-- ERROR trying to set a Resolution precision value of 0. "
190 "Program aborted."));
191
192 MapResPrecision::iterator it = m_mapRes.find(value);
193
194 if(it == m_mapRes.end())
195 {
196 // not found
197 std::pair<MapResPrecision::iterator, bool> insert_res =
198 m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(value, new ResPrecision(value)));
199 it = insert_res.first;
200 }
201 else
202 {
203 // found
204 }
205 return it->second;
206}
207
210{
211 Enums::PrecisionUnit unit = origin->unit();
212 double value = origin->getNominal() * fraction;
213
214
215 return getPrecisionPtrInstance(unit, value);
216}
217
220{
221
222 switch(unit)
223 {
225 return getDaltonInstance(value);
227 return getPpmInstance(value);
229 return getResInstance(value);
231 return getDaltonInstance(value);
233 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
234
235 default:
236 throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
237 break;
238 }
239
240 return nullptr;
241}
242
246
250
256
258DaltonPrecision::delta([[maybe_unused]] pappso_double value) const
259{
260 return m_nominal;
261}
262
263QString
265{
266 return (QString("%1 dalton").arg(m_nominal));
267}
268
269
273
274
278
281{
283}
284
285
288{
289 return ((value / ONEMILLION) * m_nominal);
290}
291
292
293QString
295{
296 return (QString("%1 ppm").arg(m_nominal));
297}
298
299
303
304
308
311{
313}
314
315
318{
319 return (value / m_nominal);
320}
321
322
323QString
325{
326 return (QString("%1 res").arg(m_nominal));
327}
328
329} // 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:73
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:80
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:60
const PrecisionBase * PrecisionPtr
Definition precision.h:122
std::map< Enums::PrecisionUnit, QString > precisionUnitMap
Definition precision.cpp:43