pappsomspp
Library for mass spectrometry
pwizmsrunreader.cpp
Go to the documentation of this file.
1 /**
2  * \file pappsomspp/msrun/private/pwizmsrunreader.cpp
3  * \date 29/05/2018
4  * \author Olivier Langella
5  * \brief MSrun file reader base on proteowizard library
6  */
7 
8 /*******************************************************************************
9  * Copyright (c) 2018 Olivier Langella <Olivier.Langella@u-psud.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@u-psud.fr> - initial API and
28  *implementation
29  ******************************************************************************/
30 
31 
32 #include <QDebug>
33 
34 #include "pwizmsrunreader.h"
35 
36 #include <pwiz/data/msdata/DefaultReaderList.hpp>
37 
38 
39 #include "../../utils.h"
40 #include "../../pappsoexception.h"
41 #include "../../exception/exceptionnotfound.h"
42 #include "../../exception/exceptionnotpossible.h"
43 
44 
45 // int pwizMsRunReaderMetaTypeId =
46 // qRegisterMetaType<pappso::PwizMsRunReader>("pappso::PwizMsRunReader");
47 
48 
49 namespace pappso
50 {
51 
52 
54  : MsRunReader(msrun_id_csp)
55 {
56  // The initialization needs to be done immediately so that we get the pwiz
57  // MsDataPtr corresponding to the right ms_run_id in the parameter. That
58  // pointer will be set to msp_msData.
59 
60  initialize();
61 }
62 
63 
64 void
66 {
67  std::string file_name_std =
69 
70  // Make a backup of the current locale
71  std::string env_backup = setlocale(LC_ALL, "");
72  // struct lconv *lc = localeconv();
73 
74  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
75  //<< "env_backup=" << env_backup.c_str() << "lc->decimal_point"
76  //<< lc->decimal_point;
77 
78  // Now actually search the useful MSDataPtr to the member variable.
79 
80  pwiz::msdata::DefaultReaderList defaultReaderList;
81 
82  std::vector<pwiz::msdata::MSDataPtr> msDataPtrVector;
83 
84  try
85  {
86  defaultReaderList.read(file_name_std, msDataPtrVector);
87  }
88  catch(std::exception &error)
89  {
90  qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
91  << QString("Failed to read the data from file %1")
92  .arg(QString::fromStdString(file_name_std));
93  }
94 
95  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
96  //<< "The number of runs is:" << msDataPtrVector.size()
97  //<< "The number of spectra in first run is:"
98  //<< msDataPtrVector.at(0)->run.spectrumListPtr->size();
99 
100  // Single-run file handling here.
101 
102  // Specific case of the MGF data format: we do not have a run id for that kind
103  // of data. In this case there must be a single run!
104 
105  if(mcsp_msRunId->getRunId().isEmpty())
106  {
107  if(msDataPtrVector.size() != 1)
108  throw(
109  ExceptionNotPossible("For the kind of file at hand there can only be "
110  "one run in the file."));
111 
112  // At this point we know the single msDataPtr is the one we are looking
113  // for.
114 
115  msp_msData = msDataPtrVector.front();
116  }
117 
118  else
119  {
120  // Multi-run file handling here.
121  for(auto &msDataPtr : msDataPtrVector)
122  {
123  if(msDataPtr->run.id == mcsp_msRunId->getRunId().toStdString())
124  {
125  msp_msData = msDataPtr;
126 
127  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
128  //<< "Found the right MSDataPtr for run id.";
129 
130  break;
131  }
132  }
133  }
134 
135  if(msp_msData == nullptr)
136  {
137  throw(ExceptionNotPossible(
138  "Could not find a MSDataPtr matching the requested run id."));
139  }
140 
141 
142  // check if this MS run can be used with scan numbers
143  // MS:1000490 Agilent instrument model
144  pwiz::cv::CVID native_id_format =
145  pwiz::msdata::id::getDefaultNativeIDFormat(*msp_msData.get());
146 
147  // msp_msData.get()->getDefaultNativeIDFormat();
148 
149  if(native_id_format == pwiz::cv::CVID::MS_Thermo_nativeID_format)
150  {
151  m_hasScanNumbers = true;
152  }
153  else
154  {
155  m_hasScanNumbers = false;
156  }
157 
158  if(mcsp_msRunId.get()->getMzFormat() == MzFormat::mzXML)
159  {
160  m_hasScanNumbers = true;
161  }
162 }
163 
164 
166 {
167 }
168 
169 
170 pwiz::msdata::SpectrumPtr
171 PwizMsRunReader::getPwizSpectrumPtr(pwiz::msdata::SpectrumList *p_spectrum_list,
172  std::size_t spectrum_index,
173  bool want_binary_data) const
174 {
175  pwiz::msdata::SpectrumPtr native_pwiz_spectrum_sp;
176 
177  try
178  {
179  native_pwiz_spectrum_sp =
180  p_spectrum_list->spectrum(spectrum_index, want_binary_data);
181  }
182  catch(std::runtime_error &error)
183  {
184  qDebug() << "getPwizSpectrumPtr error " << error.what() << " "
185  << typeid(error).name();
186 
187  throw ExceptionNotFound(QObject::tr("Pwiz spectrum index %1 not found in "
188  "MS file std::runtime_error :\n%2")
189  .arg(spectrum_index)
190  .arg(error.what()));
191  }
192  catch(std::exception &error)
193  {
194  qDebug() << "getPwizSpectrumPtr error " << error.what()
195  << typeid(error).name();
196 
197  throw ExceptionNotFound(
198  QObject::tr("Pwiz spectrum index %1 not found in MS file :\n%2")
199  .arg(spectrum_index)
200  .arg(error.what()));
201  }
202 
203  if(native_pwiz_spectrum_sp.get() == nullptr)
204  {
205  throw ExceptionNotFound(
206  QObject::tr(
207  "Pwiz spectrum index %1 not found in MS file : null pointer")
208  .arg(spectrum_index));
209  }
210 
211  return native_pwiz_spectrum_sp;
212 }
213 
214 
215 QualifiedMassSpectrum
217  const MassSpectrumId &massSpectrumId,
218  pwiz::msdata::Spectrum *spectrum_p,
219  bool want_binary_data,
220  bool &ok) const
221 {
222  // qDebug();
223 
224  std::string env;
225  env = setlocale(LC_ALL, "");
226  setlocale(LC_ALL, "C");
227 
228  QualifiedMassSpectrum qualified_mass_spectrum(massSpectrumId);
229 
230  try
231  {
232 
233  // We want to store the ms level for this spectrum
234 
235  int msLevel =
236  (spectrum_p->cvParam(pwiz::msdata::MS_ms_level).valueAs<int>());
237 
238  qualified_mass_spectrum.setMsLevel(msLevel);
239 
240  // qDebug() << "for spectrum at index:" <<
241  // massSpectrumId.getSpectrumIndex()
242  //<< "msLevel:" << msLevel
243  //<< "with number of precursors:" << spectrum_p->precursors.size();
244 
245  // We want to know if this spectrum is a fragmentation spectrum obtained
246  // from a selected precursor ion.
247 
248  if(spectrum_p->precursors.size() > 0)
249  {
250  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
251  //<< "The spectrum has precursor(s).";
252 
253  // Sanity check
254  if(msLevel <= 1)
255  {
256  qDebug() << "Going to throw.";
257 
258  throw(ExceptionNotPossible(
259  "msLevel cannot be less than two for "
260  "a spectrum that has items in its Precursor list."));
261  }
262 
263  // See what is the first precursor in the list.
264 
265  // qDebug() << "Now tring to get the precursor.";
266 
267  pwiz::msdata::Precursor &precursor = spectrum_p->precursors.front();
268 
269  // Set this variable ready as we need that default value in certain
270  // circumstances.
271 
272  std::size_t precursor_spectrum_index =
273  std::numeric_limits<std::size_t>::max();
274 
275  // qDebug() << "First precursor:" << precursor.spectrumID.c_str();
276 
277  if(precursor.spectrumID.empty())
278  {
279  // qDebug() << "The precursor's spectrum ID is empty.";
280 
281  if(mcsp_msRunId.get()->getMzFormat() == MzFormat::MGF)
282  {
283  // qDebug()
284  //<< "Format is MGF, precursor's spectrum ID can be empty.";
285  }
286  else
287  {
288  // When performing Lumos Fusion fragmentation experiments in
289  // Tune mode and with recording, the first spectrum of the
290  // list is a fragmentation spectrum (ms level 2) that has no
291  // identity for the precursor spectrum because there is no
292  // full scan accquisition.
293 
294 #if 0
295  // Let's try to understand if we are in this configuration.
296 
297  if(!massSpectrumId.getSpectrumIndex())
298  {
299  qDebug() << "This MS2 sprectrum is at index 0."
300  "This might indicate a fragmentation "
301  "experiment performed in Tune mode.";
302 
303  // The precursor spectrum index is initialized to max().
304  // So, it is not going to be changed in the present
305  // situation.
306  }
307  else
308  {
309  qDebug() << "This MS2 spectrum has no precursor's "
310  "spectrum index.";
311  }
312  // else
313  //{
314  // throw(
315  // ExceptionNotPossible("Failed to get the id of the "
316  //"precursor ion's spectrum."));
317  //}
318 #endif
319  }
320  }
321  else
322  {
323  // We could get a native precursor spectrum id, so convert that
324  // native id to a spectrum index.
325 
326  qualified_mass_spectrum.setPrecursorNativeId(
327  QString::fromStdString(precursor.spectrumID));
328 
329  if(qualified_mass_spectrum.getPrecursorNativeId().isEmpty())
330  {
331  // qDebug()
332  //<< "The native id of the precursor spectrum is empty !!!";
333  }
334 
335  // Get the spectrum index of the spectrum that contained the
336  // precursor ion.
337 
338  precursor_spectrum_index =
339  msp_msData->run.spectrumListPtr->find(precursor.spectrumID);
340 
341  // Note that the Mascot MGF format has a peculiar handling of the
342  // precursor ion stuff so we cannot throw.
343  if(precursor_spectrum_index ==
344  msp_msData->run.spectrumListPtr->size())
345  {
346  if(mcsp_msRunId.get()->getMzFormat() != MzFormat::MGF)
347  {
348  throw(
349  ExceptionNotPossible("Failed to find the index of the "
350  "precursor ion's spectrum."));
351  }
352  }
353 
354  qualified_mass_spectrum.setPrecursorSpectrumIndex(
355  precursor_spectrum_index);
356 
357  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
358  //<< "Set the precursor spectrum index to:"
359  //<< qualified_mass_spectrum.getPrecursorSpectrumIndex()
360  //<< "for qualified mass spectrum:"
361  //<< &qualified_mass_spectrum;
362  }
363 
364  if(!precursor.selectedIons.size())
365  {
366  throw(
367  ExceptionNotPossible("The spectrum has msLevel > 1 but the "
368  "precursor ions's selected ions "
369  "list is empty."));
370  }
371 
372  pwiz::msdata::SelectedIon &ion = *(precursor.selectedIons.begin());
373 
374  // selected ion m/z
375 
376  pappso_double selected_ion_mz =
377  QString(ion.cvParam(pwiz::cv::MS_selected_ion_m_z).value.c_str())
378  .toDouble();
379 
380  qualified_mass_spectrum.setPrecursorMz(selected_ion_mz);
381 
382  // selected ion peak intensity
383 
384  pappso_double selected_ion_peak_intensity =
385  QString(ion.cvParam(pwiz::cv::MS_peak_intensity).value.c_str())
386  .toDouble();
387 
388  qualified_mass_spectrum.setPrecursorIntensity(
389  selected_ion_peak_intensity);
390 
391  // unsigned int test =
392  // QString(ion.cvParam(pwiz::cv::MS_1200_series_LC_MSD_SL).value.c_str()).toUInt();
393  // qDebug() << " test "<< test;
394 
395  // charge state
396 
397  unsigned int selected_ion_charge_state =
398  QString(ion.cvParam(pwiz::cv::MS_charge_state).value.c_str())
399  .toUInt();
400 
401  if(selected_ion_charge_state > 0)
402  {
403  qualified_mass_spectrum.setPrecursorCharge(
404  selected_ion_charge_state);
405  }
406 
407 
408  // General sum-up
409  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
410  //<< "precursor_scan_number:"
411  //<< "selected_ion_m_z:" << selected_ion_mz
412  //<< "selected_ion_peak_intensity:"
413  //<< selected_ion_peak_intensity << "selected_ion_charge_state"
414  //<< selected_ion_charge_state;
415  }
416  // End of
417  // if(spectrum_p->precursors.size() > 0)
418  else
419  {
420  // Sanity check
421  if(msLevel != 1)
422  {
423  throw(
424  ExceptionNotPossible("msLevel cannot be different than 1 if "
425  "there is not a single precursor ion."));
426  }
427  }
428 
429  // We now have to set the retention time at which this mass spectrum was
430  // acquired. This is the scan start time.
431 
432  if(!spectrum_p->scanList.scans[0].hasCVParam(
433  pwiz::msdata::MS_scan_start_time))
434  {
435  if(mcsp_msRunId.get()->getMzFormat() == MzFormat::MGF)
436  { // MGF could not have scan start time
437  qualified_mass_spectrum.setRtInSeconds(-1);
438  }
439  else
440  {
441  throw(ExceptionNotPossible(
442  "The spectrum has no scan start time value set."));
443  }
444  }
445  else
446  {
447  pwiz::data::CVParam retention_time_cv_param =
448  spectrum_p->scanList.scans[0].cvParam(
449  pwiz::msdata::MS_scan_start_time);
450 
451  // Try to get the units of the retention time value.
452 
453  std::string unit_name = retention_time_cv_param.unitsName();
454  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
455  //<< "Unit name for the retention time:"
456  //<< QString::fromStdString(unit_name);
457 
458  if(unit_name == "second")
459  {
460  qualified_mass_spectrum.setRtInSeconds(
461  retention_time_cv_param.valueAs<double>());
462  }
463  else if(unit_name == "minute")
464  {
465  qualified_mass_spectrum.setRtInSeconds(
466  retention_time_cv_param.valueAs<double>() * 60);
467  }
468  else
469  throw(ExceptionNotPossible(
470  "Could not determine the unit for the scan start time value."));
471  }
472 
473  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
474  //<< "Retention time for spectrum is:"
475  //<< qualified_mass_spectrum.getRtInSeconds();
476 
477  // Old version not checking unit (by default unit is minutes for RT,
478  // not seconds)
479  //
480  // pappso_double retentionTime =
481  // QString(spectrum_p->scanList.scans[0]
482  //.cvParam(pwiz::msdata::MS_scan_start_time)
483  //.value.c_str())
484  //.toDouble();
485  // qualified_mass_spectrum.setRtInSeconds(retentionTime);
486 
487  // Not all the acquisitions have ion mobility data. We need to test
488  // that:
489 
490  if(spectrum_p->scanList.scans[0].hasCVParam(
491  pwiz::msdata::MS_ion_mobility_drift_time))
492  {
493 
494  // qDebug() << "as strings:"
495  //<< QString::fromStdString(
496  // spectrum_p->scanList.scans[0]
497  //.cvParam(pwiz::msdata::MS_ion_mobility_drift_time)
498  //.valueAs<std::string>());
499 
500  pappso_double driftTime =
501  spectrum_p->scanList.scans[0]
502  .cvParam(pwiz::msdata::MS_ion_mobility_drift_time)
503  .valueAs<double>();
504 
505  // qDebug() << "driftTime:" << driftTime;
506 
507  // Old version requiring use of QString.
508  // pappso_double driftTime =
509  // QString(spectrum_p->scanList.scans[0]
510  //.cvParam(pwiz::msdata::MS_ion_mobility_drift_time)
511  //.value.c_str())
512  //.toDouble();
513 
514  // Now make positively sure that the obtained value is correct.
515  // Note that I suffered a lot with Waters Synapt data that
516  // contained apparently correct drift time XML element that in
517  // fact contained either NaN or inf. When such mass spectra were
518  // encountered, the mz,i data were bogus and crashed the data loading
519  // functions. We just want to skip this kind of bogus mass spectrum by
520  // letting the caller know that the drift time was bogus ("I" is
521  // Filippo Rusconi).
522 
523  if(std::isnan(driftTime) || std::isinf(driftTime))
524  {
525  // qDebug() << "detected as nan or inf.";
526 
527  ok = false;
528 
529  return qualified_mass_spectrum;
530  }
531  else
532  {
533  // The mzML standard stipulates that drift times are in
534  // milliseconds.
535  qualified_mass_spectrum.setDtInMilliSeconds(driftTime);
536  }
537  }
538  // End of
539  // if(spectrum_p->scanList.scans[0].hasCVParam(
540  // pwiz::msdata::MS_ion_mobility_drift_time))
541  else
542  {
543  // Not a bogus mass spectrum but also not a drift spectrum, set -1 as
544  // the drift time value.
545  qualified_mass_spectrum.setDtInMilliSeconds(-1);
546  }
547 
548  // for(pwiz::data::CVParam cv_param : ion.cvParams)
549  //{
550  // pwiz::msdata::CVID param_id = cv_param.cvid;
551  // qDebug() << param_id;
552  // qDebug() << cv_param.cvid.c_str();
553  // qDebug() << cv_param.name().c_str();
554  // qDebug() << cv_param.value.c_str();
555  //}
556 
557  if(want_binary_data)
558  {
559 
560  // Fill-in MZIntensityPair vector for convenient access to binary
561  // data
562 
563  std::vector<pwiz::msdata::MZIntensityPair> pairs;
564  spectrum_p->getMZIntensityPairs(pairs);
565 
566  MassSpectrum spectrum;
567  double tic = 0;
568  // std::size_t iterCount = 0;
569 
570  // Iterate through the m/z-intensity pairs
571  for(std::vector<pwiz::msdata::MZIntensityPair>::const_iterator
572  it = pairs.begin(),
573  end = pairs.end();
574  it != end;
575  ++it)
576  {
577  //++iterCount;
578 
579  // qDebug() << "it->mz " << it->mz << " it->intensity" <<
580  // it->intensity;
581  if(it->intensity)
582  {
583  spectrum.push_back(DataPoint(it->mz, it->intensity));
584  tic += it->intensity;
585  }
586  }
587 
588  if(mcsp_msRunId.get()->getMzFormat() == MzFormat::MGF)
589  {
590  // Sort peaks by mz
591  spectrum.sortMz();
592  }
593 
594  // lc = localeconv ();
595  // qDebug() << " env=" << localeconv () << " lc->decimal_point "
596  // << lc->decimal_point;
597  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "() "<<
598  // spectrum.size();
599  MassSpectrumSPtr spectrum_sp = spectrum.makeMassSpectrumSPtr();
600  qualified_mass_spectrum.setMassSpectrumSPtr(spectrum_sp);
601 
602  // double sumY =
603  // qualified_mass_spectrum.getMassSpectrumSPtr()->sumY(); qDebug()
604  // <<
605  // __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
606  //<< "iterCount:" << iterCount << "Spectrum size "
607  //<< spectrum.size() << "with tic:" << tic
608  //<< "and sumY:" << sumY;
609  }
610  else
611  qualified_mass_spectrum.setMassSpectrumSPtr(nullptr);
612  }
613  catch(PappsoException &errorp)
614  {
615  qDebug() << "Going to throw";
616 
618  QObject::tr("Error reading data using the proteowizard library: %1")
619  .arg(errorp.qwhat()));
620  }
621  catch(std::exception &error)
622  {
623  qDebug() << "Going to throw";
624 
626  QObject::tr("Error reading data using the proteowizard library: %1")
627  .arg(error.what()));
628  }
629 
630  // setlocale(LC_ALL, env.c_str());
631 
632  ok = true;
633  return qualified_mass_spectrum;
634 }
635 
636 
637 QualifiedMassSpectrum
639  bool want_binary_data,
640  bool &ok) const
641 {
642 
643  std::string env;
644  env = setlocale(LC_ALL, "");
645  // struct lconv *lc = localeconv();
646 
647  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
648  //<< "env=" << env.c_str()
649  //<< "lc->decimal_point:" << lc->decimal_point;
650 
651  setlocale(LC_ALL, "C");
652 
653  MassSpectrumId massSpectrumId(mcsp_msRunId);
654 
655  if(msp_msData == nullptr)
656  {
657  setlocale(LC_ALL, env.c_str());
658  return (QualifiedMassSpectrum(massSpectrumId));
659  }
660 
661  // const bool want_binary_data = true;
662 
663  pwiz::msdata::SpectrumListPtr spectrum_list_p =
664  msp_msData->run.spectrumListPtr;
665 
666  if(spectrum_index == spectrum_list_p.get()->size())
667  {
668  setlocale(LC_ALL, env.c_str());
669  throw ExceptionNotFound(
670  QObject::tr("The spectrum index cannot be equal to the size of the "
671  "spectrum list."));
672  }
673 
674  // At this point we know the spectrum index might be sane, so store it in
675  // the mass spec id object.
676  massSpectrumId.setSpectrumIndex(spectrum_index);
677 
678  pwiz::msdata::SpectrumPtr native_pwiz_spectrum_sp =
679  getPwizSpectrumPtr(spectrum_list_p.get(), spectrum_index, want_binary_data);
680 
681  setlocale(LC_ALL, env.c_str());
682 
683  massSpectrumId.setNativeId(
684  QString::fromStdString(native_pwiz_spectrum_sp->id));
685 
687  massSpectrumId, native_pwiz_spectrum_sp.get(), want_binary_data, ok);
688 }
689 
690 
691 bool
692 PwizMsRunReader::accept(const QString &file_name) const
693 {
694  // We want to know if we can handle the file_name.
695  pwiz::msdata::ReaderList reader_list;
696 
697  std::string reader_type = reader_list.identify(file_name.toStdString());
698 
699  if(!reader_type.empty())
700  return true;
701 
702  return false;
703 }
704 
705 
707 PwizMsRunReader::massSpectrumSPtr(std::size_t spectrum_index)
708 {
709  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
710  return qualifiedMassSpectrum(spectrum_index, true).getMassSpectrumSPtr();
711 }
712 
714 PwizMsRunReader::massSpectrumCstSPtr(std::size_t spectrum_index)
715 {
716  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
717  return qualifiedMassSpectrum(spectrum_index, true).getMassSpectrumCstSPtr();
718 }
719 
720 QualifiedMassSpectrum
721 PwizMsRunReader::qualifiedMassSpectrum(std::size_t spectrum_index,
722  bool want_binary_data) const
723 {
724 
725  QualifiedMassSpectrum spectrum;
726  bool ok = false;
727 
728  spectrum =
729  qualifiedMassSpectrumFromPwizMSData(spectrum_index, want_binary_data, ok);
730 
731  if(mcsp_msRunId->getMzFormat() == pappso::MzFormat::MGF)
732  {
733  if(spectrum.getRtInSeconds() == 0)
734  {
735  // spectrum = qualifiedMassSpectrumFromPwizMSData(scan_num - 1);
736  }
737  }
738 
739  // if(!ok)
740  // qDebug() << "Encountered a mass spectrum for which the status is bad.";
741 
742  return spectrum;
743 }
744 
745 
746 void
749 {
750 
751  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()";
752 
753  // We want to iterate in the pwiz-spectrum-list and for each pwiz-spectrum
754  // create a pappso-spectrum (QualifiedMassSpectrum). Once the pappso mass
755  // spectrum has been fully qualified (that is, the member data have been
756  // set), it is transferred to the handler passed as parameter to this
757  // function for the consumer to do what it wants with it.
758 
759  // Does the handler consuming the mass spectra read from file want these
760  // mass spectra to hold the binary data arrays (mz/i vectors)?
761 
762  const bool want_binary_data = handler.needPeakList();
763 
764 
765  std::string env;
766  env = setlocale(LC_ALL, "");
767  setlocale(LC_ALL, "C");
768 
769 
770  // We access the pwiz-mass-spectra via the spectrumListPtr that sits in the
771  // run member of msp_msData.
772 
773  pwiz::msdata::SpectrumListPtr spectrum_list_p =
774  msp_msData->run.spectrumListPtr;
775 
776  // We'll need it to perform the looping in the spectrum list.
777  std::size_t spectrum_list_size = spectrum_list_p.get()->size();
778 
779  // qDebug() << "The spectrum list has size:" << spectrum_list_size;
780 
781  // Inform the handler of the spectrum list so that it can handle feedback to
782  // the user.
783  handler.spectrumListHasSize(spectrum_list_size);
784 
785  // Iterate in the full list of spectra.
786 
787  for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
788  {
789 
790  // If the user of this reader instance wants to stop reading the spectra,
791  // then break this loop.
792  if(handler.shouldStop())
793  {
794  qDebug() << "The operation was cancelled. Breaking the loop.";
795  break;
796  }
797 
798  // Get the native pwiz-spectrum from the spectrum list.
799  // Note that this pointer is a shared pointer from pwiz.
800 
801  pwiz::msdata::SpectrumPtr native_pwiz_spectrum_sp =
802  getPwizSpectrumPtr(spectrum_list_p.get(), iter, want_binary_data);
803 
804  /*
805  * we want to load metadata of the spectrum even if it does not contain
806  peaks
807 
808  * if(!native_pwiz_spectrum_sp->hasBinaryData())
809  {
810  // qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "
811  ()"
812  //<< "native pwiz spectrum is empty, continuing.";
813  continue;
814  }
815  */
816 
817  // Instantiate the mass spectrum id that will hold critical information
818  // like the the native id string and the spectrum index.
819 
820  MassSpectrumId massSpectrumId(mcsp_msRunId, iter /* spectrum index*/);
821 
822  // Get the spectrum native id as a QString to store it in the mass
823  // spectrum id class. This is will allow later to refer to the same
824  // spectrum starting back from the file.
825 
826  QString native_id = QString::fromStdString(native_pwiz_spectrum_sp->id);
827  massSpectrumId.setNativeId(native_id);
828 
829  // Finally, instantiate the qualified mass spectrum with its id. This
830  // function will continue performing pappso-spectrum detailed
831  // qualification.
832 
833  bool ok = false;
834 
835  QualifiedMassSpectrum qualified_mass_spectrum =
837  massSpectrumId, native_pwiz_spectrum_sp.get(), want_binary_data, ok);
838 
839  if(!ok)
840  {
841  // qDebug() << "Encountered a mass spectrum for which the returned "
842  //"status is bad.";
843  continue;
844  }
845 
846  // Before handing the mass spectrum out to the handler, see if the native
847  // mass spectrum was empty or not.
848 
849  // if(!native_pwiz_spectrum_sp->defaultArrayLength)
850  // qDebug() << "The mass spectrum has not defaultArrayLength";
851 
852  qualified_mass_spectrum.setEmptyMassSpectrum(
853  !native_pwiz_spectrum_sp->defaultArrayLength);
854 
855  // The handler will receive the index of the mass spectrum in the
856  // current run via the mass spectrum id member datum.
857  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
858  }
859 
860  setlocale(LC_ALL, env.c_str());
861  // End of
862  // for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
863 
864  // Now let the loading handler know that the loading of the data has ended.
865  // The handler might need this "signal" to perform additional tasks or to
866  // cleanup cruft.
867 
868  // qDebug() << "Loading ended";
869  handler.loadingEnded();
870 }
871 
872 
873 std::size_t
875 {
876  return msp_msData->run.spectrumListPtr.get()->size();
877 }
878 
879 bool
881 {
882  return m_hasScanNumbers;
883 }
884 
885 
886 } // namespace pappso
pappso::PwizMsRunReader::initialize
virtual void initialize() override
Definition: pwizmsrunreader.cpp:84
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition: types.h:67
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:74
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition: qualifiedmassspectrum.cpp:140
pappso::PwizMsRunReader::qualifiedMassSpectrumFromPwizMSData
QualifiedMassSpectrum qualifiedMassSpectrumFromPwizMSData(std::size_t spectrum_index, bool want_binary_data, bool &ok) const
Definition: pwizmsrunreader.cpp:657
pappso::SpectrumCollectionHandlerInterface::spectrumListHasSize
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:54
pappso::PwizMsRunReader::m_hasScanNumbers
bool m_hasScanNumbers
Definition: pwizmsrunreader.h:129
pappso::PwizMsRunReader::hasScanNumbers
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
Definition: pwizmsrunreader.cpp:899
pappso::PwizMsRunReader::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition: pwizmsrunreader.cpp:726
pappso
Definition: aa.cpp:38
pappso::SpectrumCollectionHandlerInterface::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
pappso::PwizMsRunReader::qualifiedMassSpectrumFromPwizSpectrumPtr
QualifiedMassSpectrum qualifiedMassSpectrumFromPwizSpectrumPtr(const MassSpectrumId &massSpectrumId, pwiz::msdata::Spectrum *spectrum_p, bool want_binary_data, bool &ok) const
Definition: pwizmsrunreader.cpp:235
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:63
pappso::PwizMsRunReader::~PwizMsRunReader
virtual ~PwizMsRunReader()
Definition: pwizmsrunreader.cpp:184
pappso::SpectrumCollectionHandlerInterface::shouldStop
virtual bool shouldStop()
Definition: msrunreader.cpp:45
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:50
pappso::MsRunReader::mcsp_msRunId
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:224
pappso::SpectrumCollectionHandlerInterface::loadingEnded
virtual void loadingEnded()
Definition: msrunreader.cpp:50
pappso::MzFormat::mzXML
mzXML
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:83
pappso::Utils::toUtf8StandardString
static std::string toUtf8StandardString(const QString &text)
Definition: utils.cpp:134
pappso::SpectrumCollectionHandlerInterface::needPeakList
virtual bool needPeakList() const =0
tells if we need the peak list (if we want the binary data) for each spectrum
pappso::PwizMsRunReader::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition: pwizmsrunreader.cpp:733
pappso::ExceptionNotFound
Definition: exceptionnotfound.h:50
pappso::QualifiedMassSpectrum::getMassSpectrumCstSPtr
MassSpectrumCstSPtr getMassSpectrumCstSPtr() const
Get the MassSpectrumCstSPtr.
Definition: qualifiedmassspectrum.cpp:148
pappso::MsRunReader
class PMSPP_LIB_DECL MsRunReader
Definition: msrunreader.h:167
pappso::MassSpectrumId
Definition: massspectrumid.h:56
pappso::QualifiedMassSpectrum::setEmptyMassSpectrum
void setEmptyMassSpectrum(bool is_empty_mass_spectrum)
Definition: qualifiedmassspectrum.cpp:155
pappso::PwizMsRunReader::accept
virtual bool accept(const QString &file_name) const override
tells if the reader is able to handle this file must be implemented by private MS run reader,...
Definition: pwizmsrunreader.cpp:711
pappso::PwizMsRunReader::qualifiedMassSpectrum
virtual QualifiedMassSpectrum qualifiedMassSpectrum(std::size_t spectrum_index, bool want_binary_data=true) const override
get a QualifiedMassSpectrum class given its scan number
Definition: pwizmsrunreader.cpp:740
pappso::PwizMsRunReader::PwizMsRunReader
PwizMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
Definition: pwizmsrunreader.cpp:72
pappso::PwizMsRunReader::msp_msData
pwiz::msdata::MSDataPtr msp_msData
Definition: pwizmsrunreader.h:109
pappso::PwizMsRunReader::getPwizSpectrumPtr
pwiz::msdata::SpectrumPtr getPwizSpectrumPtr(pwiz::msdata::SpectrumList *p_spectrum_list, std::size_t spectrum_index, bool want_binary_data) const
Definition: pwizmsrunreader.cpp:190
pwizmsrunreader.h
MSrun file reader base on proteowizard library.
pappso::PwizMsRunReader::spectrumListSize
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
Definition: pwizmsrunreader.cpp:893
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:77
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:73
pappso::PappsoException
Definition: pappsoexception.h:60
pappso::MzFormat::MGF
Mascot format.
pappso::PwizMsRunReader::readSpectrumCollection
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
Definition: pwizmsrunreader.cpp:766