libpappsomspp
Library for mass spectrometry
massspectrumcombinerfactory.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 
64  MapMzPrecision ret;
65 
66  return ret;
67 }();
68 
69 
72 {
73  return m_nominal;
74 }
75 
76 
78 PrecisionFactory::fromString(const QString &str)
79 {
80 
81  // The format of the string is <number><space *><string> with string either
82  // "ppm" or "dalton" or "res" or "mz".
83  //
84  // If there only once component, that is, <string> is omitted and charge is
85  // not provided, then "dalton" is considered.
86 
87  QStringList list = str.split(QRegExp("\\s+"), QString::SkipEmptyParts);
88 
89  if(list.size() > 0)
90  {
91  bool ok;
92  pappso_double value = list[0].toDouble(&ok);
93  if(!ok)
94  {
95  throw ExceptionNotPossible(
96  QObject::tr("ERROR getting precision from string :\nunable to "
97  "convert %1 to number in %2")
98  .arg(value)
99  .arg(str));
100  }
101  if(list.size() == 1)
102  {
104  }
105  else
106  {
107  if(list.size() == 2)
108  {
109  if(list[1].toLower() == "dalton")
110  {
112  }
113 
114  if(list[1].toLower() == "ppm")
115  {
116  return PrecisionFactory::getPpmInstance(value);
117  }
118 
119  if(list[1].toLower() == "res")
120  {
121  return PrecisionFactory::getResInstance(value);
122  }
123 
124  throw ExceptionNotPossible(
125  QObject::tr("ERROR getting precision from string :\nprecision "
126  "unit %1 to not known in %2")
127  .arg(list[1])
128  .arg(str));
129  }
130  else if(list.size() == 3)
131  {
132  // We are handling a mz precision instance.
133  // Thus, first item is the value, the second item has to be
134  // charge.
135 
136  bool ok;
137  int charge = list[1].toInt(&ok);
138 
139  if(!ok || list[2] != "mz")
140  {
141  throw ExceptionNotPossible(
142  QObject::tr("ERROR getting charge from string :\nunable to "
143  "convert %1 to int in %2, or string is not mz")
144  .arg(charge)
145  .arg(str));
146  }
147 
148  return PrecisionFactory::getMzInstance(value, charge);
149  }
150  }
151  }
152 
153  throw ExceptionNotPossible(QObject::tr("ERROR getting precision from string "
154  ":\nunable to convert %1 to precision")
155  .arg(str));
156 }
157 
160 {
161  MapDaltonPrecision::iterator it = m_mapDalton.find(value);
162  if(it == m_mapDalton.end())
163  {
164  // not found
165  std::pair<MapDaltonPrecision::iterator, bool> insert_res =
166  m_mapDalton.insert(std::pair<pappso_double, DaltonPrecision *>(
167  value, new DaltonPrecision(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  MapPpmPrecision::iterator it = m_mapPpm.find(value);
191 
192  if(it == m_mapPpm.end())
193  {
194  // Not found.
195  std::pair<MapPpmPrecision::iterator, bool> insert_res =
196  m_mapPpm.insert(std::pair<pappso_double, PpmPrecision *>(
197  value, new PpmPrecision(value)));
198  it = insert_res.first;
199  }
200  else
201  {
202  // found
203  }
204  return it->second;
205 }
206 
207 
210 {
211  if(!value)
212  throw ExceptionNotPossible(
213  QObject::tr("Fatal error at %1@%2 in %3 -- %4(). ")
214  .arg(__FILE__)
215  .arg(__LINE__)
216  .arg(__FUNCTION__)
217  .arg("ERROR trying to set a Resolution precision value of 0."
218  "Program aborted."));
219 
220  MapResPrecision::iterator it = m_mapRes.find(value);
221 
222  if(it == m_mapRes.end())
223  {
224  // not found
225  std::pair<MapResPrecision::iterator, bool> insert_res =
226  m_mapRes.insert(std::pair<pappso_double, ResPrecision *>(
227  value, new ResPrecision(value)));
228  it = insert_res.first;
229  }
230  else
231  {
232  // found
233  }
234  return it->second;
235 }
236 
237 
240 {
241  // Special find_if search because we need to confirm the presence of the
242  // value, charge pair, not only the value.
243 
244  MapMzPrecision::iterator it = std::find_if(
245  std::begin(m_mapMz),
246  std::end(m_mapMz),
247  [value, charge](const std::pair<pappso_double, MzPrecision *> &pair) {
248  return ((pair.first == value) && (pair.second->charge() == charge));
249  });
250 
251  if(it == m_mapMz.end())
252  {
253  // Not found.
254  std::pair<MapMzPrecision::iterator, bool> insert_res =
255  m_mapMz.insert(std::pair<pappso_double, MzPrecision *>(
256  value, new MzPrecision(value, charge)));
257 
258  it = insert_res.first;
259  }
260  else
261  {
262  // Found.
263  }
264 
265  return it->second;
266 }
267 
268 
270 {
271 }
272 
274 {
275 }
276 
278 DaltonPrecision::unit() const
279 {
280  return PrecisionUnit::dalton;
281 }
282 
285 {
286  return m_nominal;
287 }
288 
289 QString
291 {
292  return (QString("%1 dalton").arg(m_nominal));
293 }
294 
295 
296 PpmPrecision::PpmPrecision(pappso_double x) : PrecisionBase(x)
297 {
298 }
299 
300 
302 {
303 }
304 
306 PpmPrecision::unit() const
307 {
308  return PrecisionUnit::ppm;
309 }
310 
311 
314 {
315  return ((value / ONEMILLION) * m_nominal);
316 }
317 
318 
319 QString
321 {
322  return (QString("%1 ppm").arg(m_nominal));
323 }
324 
325 
326 ResPrecision::ResPrecision(pappso_double x) : PrecisionBase(x)
327 {
328 }
329 
330 
332 {
333 }
334 
336 ResPrecision::unit() const
337 {
338  return PrecisionUnit::res;
339 }
340 
341 
344 {
345  return (value / m_nominal);
346 }
347 
348 
349 QString
351 {
352  return (QString("%1 res").arg(m_nominal));
353 }
354 
355 
357  : PrecisionBase(value), m_charge(charge)
358 {
359 }
360 
361 
363 {
364 }
365 
366 
369 {
370  return PrecisionUnit::mz;
371 }
372 
373 
376 {
377  if(!m_charge)
378  return m_nominal;
379  else
380  return m_nominal / m_charge;
381 }
382 
383 
384 int
386 {
387  return m_charge;
388 };
389 
390 
391 QString
393 {
394  return (QString("%1 %2 mz").arg(m_nominal).arg(m_charge));
395 };
396 
397 } // namespace pappso
pappso::MzPrecision::MzPrecision
MzPrecision(pappso_double value, int charge)
Definition: massspectrumcombinerfactory.cpp:356
pappso::PrecisionFactory::getMzInstance
static PrecisionPtr getMzInstance(pappso_double value, int charge)
Definition: massspectrumcombinerfactory.cpp:239
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:48
pappso::ResPrecision::ResPrecision
ResPrecision(pappso_double x)
Definition: precision.cpp:259
pappso::PrecisionFactory::m_mapRes
static MapResPrecision m_mapRes
Definition: precision.h:135
pappso::PpmPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:246
pappso::MzPrecision
Definition: massspectrumcombinerfactory.h:122
pappso::PrecisionFactory::getPpmInstance
static PrecisionPtr getPpmInstance(pappso_double value)
Definition: precision.cpp:149
pappso::PrecisionFactory::MapDaltonPrecision
std::map< pappso_double, DaltonPrecision * > MapDaltonPrecision
Definition: precision.h:128
pappso::DaltonPrecision::~DaltonPrecision
virtual ~DaltonPrecision()
Definition: precision.cpp:206
pappso::PrecisionFactory::MapMzPrecision
std::map< pappso_double, MzPrecision * > MapMzPrecision
Definition: massspectrumcombinerfactory.h:151
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::ONEMILLION
const pappso_double ONEMILLION(1000000)
pappso::MzPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: massspectrumcombinerfactory.cpp:375
pappso::ResPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:269
pappso::ResPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:283
pappso::PeptideIonCter::y
@ y
pappso::PpmPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:253
pappso::ResPrecision::~ResPrecision
virtual ~ResPrecision()
Definition: precision.cpp:264
pappso::DaltonPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: massspectrumcombinerfactory.cpp:284
pappso::ResPrecision::delta
virtual pappso_double delta(pappso_double value) const override
Definition: precision.cpp:276
pappso::PrecisionFactory::MapPpmPrecision
std::map< pappso_double, PpmPrecision * > MapPpmPrecision
Definition: precision.h:129
pappso::MzPrecision::~MzPrecision
virtual ~MzPrecision()
Definition: massspectrumcombinerfactory.cpp:362
pappso::MzPrecision::m_charge
const int m_charge
Definition: massspectrumcombinerfactory.h:126
pappso::MzPrecision::toString
virtual QString toString() const override
Definition: massspectrumcombinerfactory.cpp:392
pappso::PrecisionFactory::getDaltonInstance
static PrecisionPtr getDaltonInstance(pappso_double value)
Definition: precision.cpp:129
pappso::PrecisionFactory::m_mapMz
static MapMzPrecision m_mapMz
Definition: massspectrumcombinerfactory.h:157
pappso::DaltonPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:211
pappso::PrecisionBase::getNominal
virtual pappso_double getNominal() const final
Definition: precision.cpp:64
pappso::PpmPrecision::~PpmPrecision
virtual ~PpmPrecision()
Definition: precision.cpp:234
pappso::PrecisionPtr
const PrecisionBase * PrecisionPtr
Definition: precision.h:122
pappso::DaltonPrecision::DaltonPrecision
DaltonPrecision(pappso_double x)
Definition: precision.cpp:202
pappso::PrecisionFactory::m_mapDalton
static MapDaltonPrecision m_mapDalton
Definition: precision.h:133
pappso::PrecisionUnit
PrecisionUnit
Definition: types.h:63
pappso::PrecisionBase
Definition: precision.h:44
pappso::PrecisionFactory::MapResPrecision
std::map< pappso_double, ResPrecision * > MapResPrecision
Definition: precision.h:130
precision.h
pappso::MzPrecision::unit
virtual PrecisionUnit unit() const override
Definition: massspectrumcombinerfactory.cpp:368
pappso::DaltonPrecision::toString
virtual QString toString() const override
Definition: precision.cpp:223
mzrange.h
pappso::PpmPrecision::unit
virtual PrecisionUnit unit() const override
Definition: precision.cpp:239
pappso::PrecisionUnit::mz
@ mz
pappso::PrecisionBase::m_nominal
const pappso_double m_nominal
Definition: precision.h:46
pappso::PrecisionFactory::m_mapPpm
static MapPpmPrecision m_mapPpm
Definition: precision.h:134
pappso::PrecisionFactory::getResInstance
static PrecisionPtr getResInstance(pappso_double value)
Definition: precision.cpp:176
pappso::PrecisionFactory::fromString
static PrecisionPtr fromString(const QString &str)
Definition: precision.cpp:71
exceptionnotpossible.h
pappso::PpmPrecision::PpmPrecision
PpmPrecision(pappso_double x)
Definition: precision.cpp:229
pappso::MzPrecision::charge
int charge() const
Definition: massspectrumcombinerfactory.cpp:385