pappsomspp
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 %1@%2 in %3 -- %4(). ")
154  .arg(__FILE__)
155  .arg(__LINE__)
156  .arg(__FUNCTION__)
157  .arg("ERROR trying to set a Resolution precision value of 0."
158  "Program aborted."));
159 
160  MapPpmPrecision::iterator it = m_mapPpm.find(value);
161 
162  if(it == m_mapPpm.end())
163  {
164  // Not found.
165  std::pair<MapPpmPrecision::iterator, bool> insert_res =
166  m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(
167  value, new PpmPrecision(value)));
168  it = insert_res.first;
169  }
170  else
171  {
172  // found
173  }
174  return it->second;
175 }
176 
177 
180 {
181  if(!value)
182  throw ExceptionNotPossible(
183  QObject::tr("Fatal error at %1@%2 in %3 -- %4(). ")
184  .arg(__FILE__)
185  .arg(__LINE__)
186  .arg(__FUNCTION__)
187  .arg("ERROR trying to set a Resolution precision value of 0."
188  "Program aborted."));
189 
190  MapResPrecision::iterator it = m_mapRes.find(value);
191 
192  if(it == m_mapRes.end())
193  {
194  // not found
195  std::pair<MapResPrecision::iterator, bool> insert_res =
196  m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(
197  value, new ResPrecision(value)));
198  it = insert_res.first;
199  }
200  else
201  {
202  // found
203  }
204  return it->second;
205 }
206 
207 
209 {
210 }
211 
213 {
214 }
215 
217 DaltonPrecision::unit() const
218 {
219  return PrecisionUnit::dalton;
220 }
221 
224 {
225  return m_nominal;
226 }
227 
228 QString
230 {
231  return (QString("%1 dalton").arg(m_nominal));
232 }
233 
234 
236 {
237 }
238 
239 
241 {
242 }
243 
245 PpmPrecision::unit() const
246 {
247  return PrecisionUnit::ppm;
248 }
249 
250 
253 {
254  return ((value / ONEMILLION) * m_nominal);
255 }
256 
257 
258 QString
260 {
261  return (QString("%1 ppm").arg(m_nominal));
262 }
263 
264 
266 {
267 }
268 
269 
271 {
272 }
273 
275 ResPrecision::unit() const
276 {
277  return PrecisionUnit::res;
278 }
279 
280 
283 {
284  return (value / m_nominal);
285 }
286 
287 
288 QString
290 {
291  return (QString("%1 res").arg(m_nominal));
292 }
293 
294 } // namespace pappso
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:67
pappso::ResPrecision::ResPrecision
ResPrecision(pappso_double x)
Definition: precision.cpp:284
pappso::PrecisionFactory::m_mapRes
static MapResPrecision m_mapRes
Definition: precision.h:154
pappso::PpmPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:271
pappso::PrecisionFactory::getPpmInstance
static PrecisionPtr getPpmInstance(pappso_double value)
Definition: precision.cpp:168
pappso::PrecisionFactory::MapDaltonPrecision
std::map< pappso_double, DaltonPrecision * > MapDaltonPrecision
Definition: precision.h:147
pappso::DaltonPrecision::~DaltonPrecision
virtual ~DaltonPrecision()
Definition: precision.cpp:231
pappso::PrecisionUnit::dalton
pappso
Definition: aa.cpp:38
pappso::ONEMILLION
const pappso_double ONEMILLION(1000000)
pappso::ResPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:294
pappso::ResPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:308
pappso::PpmPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:278
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:50
pappso::ResPrecision::~ResPrecision
virtual ~ResPrecision()
Definition: precision.cpp:289
pappso::DaltonPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:242
pappso::PeptideIonCter::x
pappso::ResPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:301
pappso::PrecisionUnit::res
pappso::PrecisionFactory::MapPpmPrecision
std::map< pappso_double, PpmPrecision * > MapPpmPrecision
Definition: precision.h:148
pappso::PrecisionFactory::getDaltonInstance
static PrecisionPtr getDaltonInstance(pappso_double value)
Definition: precision.cpp:148
pappso::PrecisionUnit::ppm
pappso::DaltonPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:236
pappso::PrecisionBase::getNominal
virtual pappso_double getNominal() const final
Definition: precision.cpp:83
pappso::PpmPrecision::~PpmPrecision
virtual ~PpmPrecision()
Definition: precision.cpp:259
pappso::DaltonPrecision::DaltonPrecision
DaltonPrecision(pappso_double x)
Definition: precision.cpp:227
pappso::PrecisionPtr
const typedef PrecisionBase * PrecisionPtr
Definition: precision.h:141
pappso::PrecisionFactory::m_mapDalton
static MapDaltonPrecision m_mapDalton
Definition: precision.h:152
pappso::PrecisionUnit
PrecisionUnit
Definition: types.h:81
pappso::PrecisionBase
Definition: precision.h:62
pappso::PrecisionFactory::MapResPrecision
std::map< pappso_double, ResPrecision * > MapResPrecision
Definition: precision.h:149
precision.h
pappso::DaltonPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:248
mzrange.h
pappso::PpmPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:264
pappso::PrecisionBase::m_nominal
const pappso_double m_nominal
Definition: precision.h:85
pappso::PpmPrecision
Definition: precision.h:102
pappso::PrecisionFactory::m_mapPpm
static MapPpmPrecision m_mapPpm
Definition: precision.h:153
pappso::PrecisionFactory::getResInstance
static PrecisionPtr getResInstance(pappso_double value)
Definition: precision.cpp:198
pappso::PrecisionFactory::fromString
static PrecisionPtr fromString(const QString &str)
Definition: precision.cpp:90
exceptionnotpossible.h
pappso::PpmPrecision::PpmPrecision
PpmPrecision(pappso_double x)
Definition: precision.cpp:254