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 
38 namespace pappso
39 {
40 
41 
43  MapDaltonPrecision ret;
44 
45  return ret;
46 }();
47 
48 
50  MapPpmPrecision ret;
51 
52  return ret;
53 }();
54 
55 
57  MapResPrecision ret;
58 
59  return ret;
60 }();
61 
62 
65 {
66  return m_nominal;
67 }
68 
69 
71 PrecisionFactory::fromString(const QString &str)
72 {
73 
74  // The format of the string is <number><space *><string> with string either
75  // "ppm" or "dalton" or "res".
76  //
77  // If there only once component, that is, <string> is omitted and charge is
78  // not provided, then "dalton" is considered.
79 
80  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
81 
82  if(list.size() > 0)
83  {
84  bool ok;
85  pappso_double value = list[0].toDouble(&ok);
86  if(!ok)
87  {
89  QObject::tr("ERROR getting precision from string :\nunable to "
90  "convert %1 to number in %2")
91  .arg(value)
92  .arg(str));
93  }
94  if(list.size() == 1)
95  {
97  }
98  else if(list.size() == 2)
99  {
100  if(list[1].toLower() == "dalton")
101  {
103  }
104 
105  if(list[1].toLower() == "ppm")
106  {
107  return PrecisionFactory::getPpmInstance(value);
108  }
109 
110  if(list[1].toLower() == "res")
111  {
112  return PrecisionFactory::getResInstance(value);
113  }
114 
115  throw ExceptionNotPossible(
116  QObject::tr("ERROR getting precision from string :\nprecision "
117  "unit %1 to not known in %2")
118  .arg(list[1])
119  .arg(str));
120  }
121  }
122 
123  throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string "
124  ":\nunable to convert %1 to precision")
125  .arg(str));
126 }
127 
130 {
131  MapDaltonPrecision::iterator it = m_mapDalton.find(value);
132  if(it == m_mapDalton.end())
133  {
134  // not found
135  std::pair<MapDaltonPrecision::iterator, bool> insert_res =
136  m_mapDalton.insert(std::pair<pappso_double, DaltonPrecision *>(
137  value, new DaltonPrecision(value)));
138  it = insert_res.first;
139  }
140  else
141  {
142  // found
143  }
144  return it->second;
145 }
146 
147 
150 {
151  if(!value)
152  throw ExceptionNotPossible(
153  QObject::tr("Fatal error at precision.cpp "
154  "-- ERROR trying to set a Resolution precision value of 0. "
155  "Program aborted."));
156 
157  MapPpmPrecision::iterator it = m_mapPpm.find(value);
158 
159  if(it == m_mapPpm.end())
160  {
161  // Not found.
162  std::pair<MapPpmPrecision::iterator, bool> insert_res =
163  m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(
164  value, new PpmPrecision(value)));
165  it = insert_res.first;
166  }
167  else
168  {
169  // found
170  }
171  return it->second;
172 }
173 
174 
177 {
178  if(!value)
179  throw ExceptionNotPossible(
180  QObject::tr("Fatal error at precision.cpp "
181  "-- ERROR trying to set a Resolution precision value of 0. "
182  "Program aborted."));
183 
184  MapResPrecision::iterator it = m_mapRes.find(value);
185 
186  if(it == m_mapRes.end())
187  {
188  // not found
189  std::pair<MapResPrecision::iterator, bool> insert_res =
190  m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(
191  value, new ResPrecision(value)));
192  it = insert_res.first;
193  }
194  else
195  {
196  // found
197  }
198  return it->second;
199 }
200 
203  double fraction)
204 {
205  PrecisionUnit unit = origin->unit();
206  double value = origin->getNominal() * fraction;
207 
208 
209  return getPrecisionPtrInstance(unit, value);
210 }
211 
214 {
215 
216  switch(unit)
217  {
219  return getDaltonInstance(value);
220  case PrecisionUnit::ppm:
221  return getPpmInstance(value);
222  case PrecisionUnit::res:
223  return getResInstance(value);
224  case PrecisionUnit::mz:
225  return getDaltonInstance(value);
226  case PrecisionUnit::none:
227  throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
228 
229  default:
230  throw ExceptionNotPossible(QObject::tr("Unknown precision unit"));
231  break;
232  }
233 
234  return nullptr;
235 }
236 
238 {
239 }
240 
242 {
243 }
244 
247 {
248  return PrecisionUnit::dalton;
249 }
250 
252 DaltonPrecision::delta([[maybe_unused]] pappso_double value) const
253 {
254  return m_nominal;
255 }
256 
257 QString
259 {
260  return (QString("%1 dalton").arg(m_nominal));
261 }
262 
263 
265 {
266 }
267 
268 
270 {
271 }
272 
275 {
276  return PrecisionUnit::ppm;
277 }
278 
279 
282 {
283  return ((value / ONEMILLION) * m_nominal);
284 }
285 
286 
287 QString
289 {
290  return (QString("%1 ppm").arg(m_nominal));
291 }
292 
293 
295 {
296 }
297 
298 
300 {
301 }
302 
305 {
306  return PrecisionUnit::res;
307 }
308 
309 
312 {
313  return (value / m_nominal);
314 }
315 
316 
317 QString
319 {
320  return (QString("%1 res").arg(m_nominal));
321 }
322 
323 } // namespace pappso
virtual QString toString() const override
Definition: precision.cpp:258
DaltonPrecision(pappso_double x)
Definition: precision.cpp:237
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:252
virtual PrecisionUnit unit() const override
Definition: precision.cpp:246
PpmPrecision(pappso_double x)
Definition: precision.cpp:264
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:281
virtual ~PpmPrecision()
Definition: precision.cpp:269
virtual PrecisionUnit unit() const override
Definition: precision.cpp:274
virtual QString toString() const override
Definition: precision.cpp:288
const pappso_double m_nominal
Definition: precision.h:46
virtual pappso_double getNominal() const final
Definition: precision.cpp:64
virtual PrecisionUnit unit() const =0
static PrecisionPtr getResInstance(pappso_double value)
get a resolution precision pointer
Definition: precision.cpp:176
static PrecisionPtr fromString(const QString &str)
get a precision pointer from a string
Definition: precision.cpp:71
static PrecisionPtr getPrecisionPtrInstance(PrecisionUnit unit, double value)
get a precision pointer instance
Definition: precision.cpp:213
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:149
static PrecisionPtr getPrecisionPtrFractionInstance(PrecisionPtr origin, double fraction)
get the fraction of an existing precision pointer
Definition: precision.cpp:202
static PrecisionPtr getDaltonInstance(pappso_double value)
get a Dalton precision pointer
Definition: precision.cpp:129
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:304
virtual ~ResPrecision()
Definition: precision.cpp:299
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:311
virtual QString toString() const override
Definition: precision.cpp:318
ResPrecision(pappso_double x)
Definition: precision.cpp:294
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
PrecisionUnit
Definition: types.h:63
const pappso_double ONEMILLION(1000000)
double pappso_double
A type definition for doubles.
Definition: types.h:48