libpappsomspp
Library for mass spectrometry
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 
39 namespace 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 
72 PrecisionFactory::fromString(const QString &str)
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  {
108  return PrecisionFactory::getPpmInstance(value);
109  }
110 
111  if(list[1].toLower() == "res")
112  {
113  return PrecisionFactory::getResInstance(value);
114  }
115 
116  throw ExceptionNotPossible(
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)
153  throw ExceptionNotPossible(
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  if(!value)
180  throw ExceptionNotPossible(
181  QObject::tr("Fatal error at precision.cpp "
182  "-- ERROR trying to set a Resolution precision value of 0. "
183  "Program aborted."));
184 
185  MapResPrecision::iterator it = m_mapRes.find(value);
186 
187  if(it == m_mapRes.end())
188  {
189  // not found
190  std::pair<MapResPrecision::iterator, bool> insert_res =
191  m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(
192  value, new ResPrecision(value)));
193  it = insert_res.first;
194  }
195  else
196  {
197  // found
198  }
199  return it->second;
200 }
201 
204  double fraction)
205 {
206  PrecisionUnit unit = origin->unit();
207  double value = origin->getNominal() * fraction;
208 
209 
210  return getPrecisionPtrInstance(unit, value);
211 }
212 
215 {
216 
217  switch(unit)
218  {
220  return getDaltonInstance(value);
221  case PrecisionUnit::ppm:
222  return getPpmInstance(value);
223  case PrecisionUnit::res:
224  return getResInstance(value);
225  case PrecisionUnit::mz:
226  return getDaltonInstance(value);
227  case PrecisionUnit::none:
228  throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
229 
230  default:
231  throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
232  break;
233  }
234 
235  return nullptr;
236 }
237 
239 {
240 }
241 
243 {
244 }
245 
248 {
249  return PrecisionUnit::dalton;
250 }
251 
253 DaltonPrecision::delta([[maybe_unused]] pappso_double value) const
254 {
255  return m_nominal;
256 }
257 
258 QString
260 {
261  return (QString("%1 dalton").arg(m_nominal));
262 }
263 
264 
266 {
267 }
268 
269 
271 {
272 }
273 
276 {
277  return PrecisionUnit::ppm;
278 }
279 
280 
283 {
284  return ((value / ONEMILLION) * m_nominal);
285 }
286 
287 
288 QString
290 {
291  return (QString("%1 ppm").arg(m_nominal));
292 }
293 
294 
296 {
297 }
298 
299 
301 {
302 }
303 
306 {
307  return PrecisionUnit::res;
308 }
309 
310 
313 {
314  return (value / m_nominal);
315 }
316 
317 
318 QString
320 {
321  return (QString("%1 res").arg(m_nominal));
322 }
323 
324 } // namespace pappso
virtual QString toString() const override
Definition: precision.cpp:259
DaltonPrecision(pappso_double x)
Definition: precision.cpp:238
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:253
virtual PrecisionUnit unit() const override
Definition: precision.cpp:247
PpmPrecision(pappso_double x)
Definition: precision.cpp:265
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:282
virtual ~PpmPrecision()
Definition: precision.cpp:270
virtual PrecisionUnit unit() const override
Definition: precision.cpp:275
virtual QString toString() const override
Definition: precision.cpp:289
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
Definition: precision.cpp:177
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
Definition: precision.cpp:214
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
Definition: precision.cpp:150
static PrecisionPtr getPrecisionPtrFractionInstance(PrecisionPtr origin, double fraction)
get the fraction of an existing precision pointer
Definition: precision.cpp:203
static PrecisionPtr getDaltonInstance(pappso_double value)
get a Dalton precision pointer
Definition: precision.cpp:130
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
Definition: precision.cpp:305
virtual ~ResPrecision()
Definition: precision.cpp:300
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:312
virtual QString toString() const override
Definition: precision.cpp:319
ResPrecision(pappso_double x)
Definition: precision.cpp:295
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
PrecisionUnit
Definition: types.h:64
const pappso_double ONEMILLION(1000000)
double pappso_double
A type definition for doubles.
Definition: types.h:49