libpappsomspp
Library for mass spectrometry
qualifiedmassspectrum.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2015 Olivier Langella <olivier.langella@u-psud.fr>.
3  *
4  * This file is part of the PAPPSOms++ library.
5  *
6  * PAPPSOms++ is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * PAPPSOms++ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
18  *
19  ******************************************************************************/
20 
21 /////////////////////// StdLib includes
22 #include <cmath>
23 
24 
25 /////////////////////// Qt includes
26 #include <QDebug>
27 
28 
29 /////////////////////// Local includes
30 #include "qualifiedmassspectrum.h"
31 #include "../utils.h"
32 #include "../pappsoexception.h"
33 
34 
35 namespace pappso
36 {
37 
38 //! Construct an uninitialized QualifiedMassSpectrum.
40 {
41 }
42 
43 
44 //! Construct a QualifiedMassSpectrum using a MassSpectrumId;
46  : m_massSpectrumId(id)
47 {
48 }
49 
50 
52  MassSpectrumSPtr mass_spectrum_SPtr)
53  : msp_massSpectrum{mass_spectrum_SPtr}
54 {
55 }
56 
57 
58 //! Construct a QualifiedMassSpectrum as a copy of \p other.
60  : msp_massSpectrum(other.msp_massSpectrum),
61  m_massSpectrumId(other.m_massSpectrumId),
62  m_isEmptyMassSpectrum(other.m_isEmptyMassSpectrum),
63  m_msLevel(other.m_msLevel),
64  m_rt(other.m_rt),
65  m_dt(other.m_dt),
66  m_precursorSpectrumIndex(other.m_precursorSpectrumIndex),
67  m_precursorNativeId(other.m_precursorNativeId),
68  m_precursorIonData(other.m_precursorIonData),
69  m_paramsMap(other.m_paramsMap)
70 {
71  // qDebug();
72 }
73 
74 
75 //! Destruct this QualifiedMassSpectrum.
77 {
78  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << " ()";
79 }
80 
81 
84 {
88  m_msLevel = other.m_msLevel;
89  m_rt = other.m_rt;
90  m_dt = other.m_dt;
94  m_paramsMap = other.m_paramsMap;
95 
96  return *this;
97 }
98 
99 
100 const QualifiedMassSpectrum &
102 {
103  this->msp_massSpectrum =
104  std::make_shared<MassSpectrum>(*this->msp_massSpectrum.get());
105  return *this;
106 }
107 
108 
111 {
112  return std::make_shared<QualifiedMassSpectrum>(*this);
113 }
114 
115 
118 {
119  return std::make_shared<const QualifiedMassSpectrum>(*this);
120 }
121 
122 
123 //! Set the MassSpectrumSPtr.
124 void
126 {
127  msp_massSpectrum = massSpectrum;
128 }
129 
130 
131 //! Get the MassSpectrumSPtr.
134 {
135  return msp_massSpectrum;
136 }
137 
138 
139 //! Get the MassSpectrumCstSPtr.
142 {
143  return msp_massSpectrum;
144 }
145 
146 
147 //! Set the MassSpectrumId.
148 void
150 {
151  m_massSpectrumId = iD;
152 }
153 
154 
155 //! Get the MassSpectrumId.
156 const MassSpectrumId &
158 {
159  return m_massSpectrumId;
160 }
161 
162 
163 void
165 {
166  m_isEmptyMassSpectrum = is_empty_mass_spectrum;
167 }
168 
169 
170 bool
172 {
173  return m_isEmptyMassSpectrum;
174 }
175 
176 
177 //! Set the mass spectrum level.
178 void
180 {
181  m_msLevel = level;
182 }
183 
184 
185 //! Get the mass spectrum level.
186 unsigned int
188 {
189  return m_msLevel;
190 }
191 
192 
193 //! Set the retention time in seconds.
194 void
196 {
197  m_rt = rt_in_seconds;
198 }
199 
200 
201 //! Get the retention time in seconds.
204 {
205  return m_rt;
206 }
207 
208 
209 //! Get the retention time in minutes.
212 {
213  return m_rt / 60;
214 }
215 
216 
217 //! Set the drift time in milliseconds.
218 void
220 {
221  if(std::isinf(dt_in_milli_seconds))
222  m_dt = -1;
223  else
224  m_dt = dt_in_milli_seconds;
225 }
226 
227 
228 //! Get the drift time in milliseconds.
231 {
232  return m_dt;
233 }
234 
235 
236 //! Get the precursor m/z ratio.
239 {
240  if(!m_precursorIonData.size())
241  {
242  if(ok_p != nullptr)
243  *ok_p = false;
244 
245  return std::numeric_limits<double>::max();
246  }
247 
248  if(ok_p != nullptr)
249  *ok_p = true;
250 
251  // qDebug() << "Returning precursor mz value: " <<
252  // m_precursorIonData.front().mz;
253  return m_precursorIonData.front().mz;
254 }
255 
256 
257 QString
259  const std::vector<PrecursorIonData> &precursor_ion_data_vector,
260  const QString &separator) const
261 {
262  QString text;
263 
264  // We do not want to use the separator if there is only one precursor in the
265  // vector.
266 
267  if(precursor_ion_data_vector.size() == 1)
268  return QString("%1").arg(precursor_ion_data_vector.front().mz, 0, 'f', 6);
269 
270  // If there are more than one precursor, then we should list them joined with
271  // the separator.
272 
273  using iterator = std::vector<pappso::PrecursorIonData>::const_iterator;
274 
275  iterator begin_iterator = precursor_ion_data_vector.begin();
276  iterator pre_end_iterator = std::prev(precursor_ion_data_vector.end());
277 
278  for(; begin_iterator != precursor_ion_data_vector.end(); ++begin_iterator)
279  {
280  if(begin_iterator == pre_end_iterator)
281  // No separator at the end of the string
282  text += QString("%1").arg(begin_iterator->mz, 0, 'f', 6);
283  else
284  text +=
285  QString("%1%2").arg(begin_iterator->mz, 0, 'f', 6).arg(separator);
286  }
287 
288  return text;
289 }
290 
291 
292 QString
294  const QString &separator) const
295 {
297 }
298 
299 
300 QString
302  const QString &separator) const
303 {
304  // Sort the PrecursorIonData instances by increasing mz values.
305  // Then craft the string using the mz values.
306 
307  std::vector<PrecursorIonData> sorted_vector =
309 
310  return getPrecursorDataMzValuesAsString(sorted_vector, separator);
311 }
312 
313 
314 //! Get the precursor charge.
315 unsigned int
317 {
318  if(!m_precursorIonData.size())
319  {
320  return std::numeric_limits<unsigned int>::max();
321 
322  if(ok_p != nullptr)
323  *ok_p = false;
324  }
325 
326  if(ok_p != nullptr)
327  *ok_p = true;
328 
329  return m_precursorIonData.front().charge;
330 }
331 
332 
333 QString
335  const std::vector<PrecursorIonData> &precursor_ion_data_vector,
336  const QString &separator) const
337 {
338  QString text;
339 
340  // We do not want to use the separator if there is only one precursor in the
341  // vector.
342 
343  if(precursor_ion_data_vector.size() == 1)
344  return QString("%1").arg(precursor_ion_data_vector.front().charge);
345 
346  // If there are more than one precursor, then we should list them joined with
347  // the separator.
348  for(auto item : precursor_ion_data_vector)
349  {
350  text += QString("%1%2").arg(item.charge).arg(separator);
351  }
352 
353  return text;
354 }
355 
356 
357 QString
359  const QString &separator) const
360 {
362 }
363 
364 
365 QString
367  const QString &separator) const
368 {
369  // Sort the PrecursorIonData instances by increasing mz values.
370  // Then craft the string using the mz values.
371 
372  std::vector<PrecursorIonData> sorted_vector =
374 
375  return getPrecursorDataChargeValuesAsString(sorted_vector, separator);
376 }
377 
378 
379 //! Get the intensity of the precursor ion.
382 {
383  if(!m_precursorIonData.size())
384  {
385  return std::numeric_limits<double>::max();
386 
387  if(ok_p != nullptr)
388  *ok_p = false;
389  }
390 
391  if(ok_p != nullptr)
392  *ok_p = true;
393 
394  return m_precursorIonData.front().intensity;
395 }
396 
397 
398 //! Set the scan number of the precursor ion.
399 void
401  std::size_t precursor_spectrum_index)
402 {
403  m_precursorSpectrumIndex = precursor_spectrum_index;
404 }
405 
406 
407 //! Get the scan number of the precursor ion.
408 std::size_t
410 {
412 }
413 
414 
415 //! Set the scan native id of the precursor ion.
416 void
418 {
419  m_precursorNativeId = native_id;
420 }
421 
422 const QString &
424 {
425  return m_precursorNativeId;
426 }
427 
428 
429 void
431  const PrecursorIonData &precursor_ion_data)
432 {
433  m_precursorIonData.push_back(precursor_ion_data);
434 }
435 
436 
437 const std::vector<PrecursorIonData> &
439 {
440  return m_precursorIonData;
441 }
442 
443 
444 std::vector<PrecursorIonData>
446 {
447  std::vector<PrecursorIonData> new_vector;
448  new_vector.assign(m_precursorIonData.begin(), m_precursorIonData.end());
449 
450  std::sort(new_vector.begin(),
451  new_vector.end(),
452  [](const PrecursorIonData &a, const PrecursorIonData &b) -> bool {
453  return a.mz < b.mz;
454  });
455 
456  return new_vector;
457 }
458 
459 
460 void
462  QualifiedMassSpectrumParameter parameter, const QVariant &value)
463 {
464 
465  auto ret = m_paramsMap.insert(
466  std::pair<QualifiedMassSpectrumParameter, QVariant>(parameter, value));
467 
468  if(ret.second == false)
469  {
470  ret.first->second = value;
471  }
472 }
473 
474 
475 const QVariant
477  QualifiedMassSpectrumParameter parameter) const
478 {
479  auto it = m_paramsMap.find(parameter);
480  if(it == m_paramsMap.end())
481  {
482  return QVariant();
483  }
484  else
485  {
486  return it->second;
487  }
488 }
489 
490 
491 std::size_t
493 {
494  if(msp_massSpectrum == nullptr)
495  {
496  throw pappso::PappsoException(QObject::tr("msp_massSpectrum == nullptr"));
497  }
498  return msp_massSpectrum.get()->size();
499 }
500 
501 
502 QString
504 {
505  QString text;
506 
507  if(msp_massSpectrum != nullptr && msp_massSpectrum.get() != nullptr)
508  {
509  QString pointer_string =
510  QString("msp_massSpectrum.get(): %1 ")
511  .arg(Utils::pointerToString((void *)msp_massSpectrum.get()));
512 
513  text += pointer_string;
514  }
515  else
516  text += QString("msp_massSpectrum is nullptr ");
517 
518  // qDebug() << text;
519 
520  QString precursor_mz_values_string;
521 
522  if(m_precursorIonData.size())
523  precursor_mz_values_string += "Precursor mz values: ";
524 
525  precursor_mz_values_string += getMzSortedPrecursorDataMzValuesAsString();
526 
527  precursor_mz_values_string += "\n";
528 
529  text +=
530  QString(
531  "; m_massSpectrumId : %1 \n"
532  "m_msLevel: %2 ; m_rt (min): %3 ; m_dt (ms): %4 ; prec. spec. "
533  "index: %5 ; %6")
534  .arg(m_massSpectrumId.toString())
535  .arg(m_msLevel)
536  .arg(getRtInMinutes(), 0, 'f', 2)
537  .arg(m_dt, 0, 'f', 5)
538  .arg(m_precursorSpectrumIndex != std::numeric_limits<std::size_t>::max()
540  : -1)
541  .arg(precursor_mz_values_string);
542 
543  return text;
544 }
545 
546 
547 } // namespace pappso
QString toString() const
Class representing a fully specified mass spectrum.
uint getMsLevel() const
Get the mass spectrum level.
QualifiedMassSpectrum & operator=(const QualifiedMassSpectrum &other)
MassSpectrumCstSPtr getMassSpectrumCstSPtr() const
Get the MassSpectrumCstSPtr.
std::vector< PrecursorIonData > m_precursorIonData
std::vector< PrecursorIonData > getPrecursorIonDataSortedWithMz() const
void setPrecursorNativeId(const QString &native_id)
Set the scan native id of the precursor ion.
std::size_t m_precursorSpectrumIndex
Index of the spectrum of the precusor ion that was fragmented to yield.
pappso_double getDtInMilliSeconds() const
Get the drift time in milliseconds.
const std::vector< PrecursorIonData > & getPrecursorIonData() const
uint getPrecursorCharge(bool *ok=nullptr) const
Get the precursor charge.
pappso_double getRtInMinutes() const
Get the retention time in minutes.
unsigned int m_msLevel
Mass spectrometry level of this mass spectrum.
void setDtInMilliSeconds(pappso_double rt)
Set the drift time in milliseconds.
~QualifiedMassSpectrum()
Destruct this QualifiedMassSpectrum.
const QString & getPrecursorNativeId() const
pappso_double m_rt
Retention time (in seconds) at which this mass spectrum was acquired.
pappso_double m_dt
Drift time (in milliseconds) at which this mass spectrum was acquired.
pappso_double getPrecursorIntensity(bool *ok=nullptr) const
Get the intensity of the precursor ion.
void appendPrecursorIonData(const PrecursorIonData &precursor_ion_data)
std::map< QualifiedMassSpectrumParameter, QVariant > m_paramsMap
map containing any parameter value for this spectrum
MassSpectrumSPtr msp_massSpectrum
Shared pointer to the mass spectrum.
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
QString getPrecursorDataChargeValuesAsString(const std::vector< PrecursorIonData > &precursor_ion_data_vector, const QString &separator="\n") const
void setMassSpectrumId(const MassSpectrumId &iD)
Set the MassSpectrumId.
void setMsLevel(uint ms_level)
Set the mass spectrum level.
void setPrecursorSpectrumIndex(std::size_t precursor_scan_num)
Set the scan number of the precursor ion.
QString getPrecursorDataMzValuesAsString(const std::vector< PrecursorIonData > &precursor_ion_data_vector, const QString &separator="\n") const
QualifiedMassSpectrumCstSPtr makeQualifiedMassSpectrumCstSPtr() const
pappso_double getPrecursorMz(bool *ok=nullptr) const
Get the precursor m/z ratio.
const QualifiedMassSpectrum & cloneMassSpectrumSPtr()
clone the mass spectrum contained in Qualified Mass Spectrum
QString getMzSortedPrecursorDataMzValuesAsString(const QString &separator="\n") const
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
QString getMzSortedPrecursorDataChargeValuesAsString(const QString &separator="\n") const
QString m_precursorNativeId
Native XML id of the spectrum relative to the mz data native file.
QualifiedMassSpectrumSPtr makeQualifiedMassSpectrumSPtr() const
MassSpectrumId m_massSpectrumId
Id of the mass spectrum.
void setParameterValue(QualifiedMassSpectrumParameter parameter, const QVariant &value)
QualifiedMassSpectrum()
Construct an uninitialized QualifiedMassSpectrum.
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
std::size_t getPrecursorSpectrumIndex() const
Get the scan number of the precursor ion.
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
pappso_double getRtInSeconds() const
Get the retention time in seconds.
void setEmptyMassSpectrum(bool is_empty_mass_spectrum)
const QVariant getParameterValue(QualifiedMassSpectrumParameter parameter) const
static QString pointerToString(const void *const pointer)
Definition: utils.cpp:251
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< QualifiedMassSpectrum > QualifiedMassSpectrumSPtr
double pappso_double
A type definition for doubles.
Definition: types.h:49
std::shared_ptr< const QualifiedMassSpectrum > QualifiedMassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54