libpappsomspp
Library for mass spectrometry
pwizmsfilereader.cpp
Go to the documentation of this file.
1 /////////////////////// StdLib includes
2 #include <iostream>
3 #include <iomanip>
4 
5 
6 /////////////////////// Qt includes
7 #include <QDebug>
8 #include <QFile>
9 #include <QFileInfo>
10 
11 
12 /////////////////////// libpwiz includes
13 #include <pwiz/data/msdata/DefaultReaderList.hpp>
14 
15 
16 /////////////////////// Local includes
17 #include "pwizmsfilereader.h"
18 #include "../exception/exceptionnotfound.h"
19 #include "../utils.h"
20 #include "../types.h"
21 #include "../msrun/msrunid.h"
22 
23 
24 namespace pappso
25 {
26 
27 
28 PwizMsFileReader::PwizMsFileReader(const QString &file_name)
29  : MsFileReader{file_name}
30 {
31 }
32 
33 
35 {
36 }
37 
38 
39 std::size_t
41 {
42  pwiz::msdata::DefaultReaderList defaultReaderList;
43  std::string readerName;
44  try
45  {
46  readerName = defaultReaderList.identify(m_fileName.toStdString());
47  }
48  catch(std::runtime_error &error)
49  {
50  qDebug() << error.what() << " " << typeid(error).name();
51 
52  throw PappsoException(
53  QObject::tr(
54  "libpwiz ERROR reading MS data file %1 (std::runtime_error):\n%2")
55  .arg(m_fileName)
56  .arg(error.what()));
57  }
58  catch(std::exception &error)
59  {
60  qDebug() << error.what() << " " << typeid(error).name();
61 
62  throw PappsoException(
63  QObject::tr(
64  "libpwiz ERROR reading MS data file %1 (std::exception):\n%2")
65  .arg(m_fileName)
66  .arg(error.what()));
67  }
68 
69 
70  if(readerName.empty())
71  {
72  qDebug() << "Failed to identify the file.";
73 
74  return 0;
75  }
76 
77  // Now convert the string to MzFormat.
78  if(readerName == "mzML")
80  else if(readerName == "mzXML")
82  else if(readerName == "Mascot Generic")
84  else if(readerName == "MZ5")
86  else if(readerName == "MSn")
88  else if(readerName == "ABSciex WIFF")
90  else if(readerName == "ABSciex T2D")
92  else if(readerName == "Agilent MassHunter")
94  else if(readerName == "Thermo RAW")
96  else if(readerName == "Water RAW")
98  else if(readerName == "Bruker FID")
100  else if(readerName == "Bruker YEP")
102  else if(readerName == "Bruker BAF")
104  else
105  {
107  return 0;
108  }
109 
110  // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
111  // << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
112  // << std::endl;
113 
114  // At this point we know pwiz could be able to read the file. Actually fill-in
115  // the MSDataPtr vector!
116  try
117  {
118  defaultReaderList.read(Utils::toUtf8StandardString(m_fileName),
120  }
121  catch(std::runtime_error &error)
122  {
123  qDebug() << error.what() << " " << typeid(error).name();
124 
125  throw PappsoException(
126  QObject::tr(
127  "libpwiz ERROR reading MS data file %1 (std::runtime_error):\n%2")
128  .arg(m_fileName)
129  .arg(error.what()));
130  }
131  catch(std::exception &error)
132  {
133  qDebug() << error.what() << " " << typeid(error).name();
134 
135  throw PappsoException(
136  QObject::tr(
137  "libpwiz ERROR reading MS data file %1 (std::exception):\n%2")
138  .arg(m_fileName)
139  .arg(error.what()));
140  }
141  qDebug() << "The number of runs is:" << m_msDataPtrVector.size()
142  << "The reader type is:" << QString::fromStdString(readerName)
143  << "The number of spectra in first run is:"
144  << m_msDataPtrVector.at(0)->run.spectrumListPtr->size();
145 
146  return m_msDataPtrVector.size();
147 }
148 
149 
150 MzFormat
152 {
153  // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
154  // << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
155  // << std::endl;
156 
157  return m_fileFormat;
158 }
159 
160 
161 std::vector<MsRunIdCstSPtr>
162 PwizMsFileReader::getMsRunIds(const QString &run_prefix)
163 {
164  std::vector<MsRunIdCstSPtr> ms_run_ids;
165 
166  if(!initialize())
167  return ms_run_ids;
168 
169  std::size_t iter = 0;
170 
171  // If the initialization failed, then there is not a single MSDataPtr in the
172  // vector, so the loop below is not gone through.
173 
174  for(pwiz::msdata::MSDataPtr ms_data_ptr : m_msDataPtrVector)
175  {
176  // For each ms run in the file, we will create a MsRunId instance that
177  // will hold the file name of the data file
178 
179  // Finally create the MsRunId with the file name.
180  MsRunId ms_run_id(m_fileName, QString::fromStdString(ms_data_ptr->id));
181  ms_run_id.setMzFormat(m_fileFormat);
182 
183  // We need to set the unambiguous xmlId string.
184  ms_run_id.setXmlId(QString("%1%2")
185  .arg(run_prefix)
186  .arg(Utils::getLexicalOrderedString(iter)));
187 
188  // Now set the sample name to the run id :
189  ms_run_id.setSampleName(QString::fromStdString(ms_data_ptr->run.id));
190  // and if it is possible, the real sample name because this one is for the
191  // end user to recognize his sample:
192  if(ms_data_ptr->run.samplePtr != nullptr)
193  {
194  ms_run_id.setSampleName(
195  QString::fromStdString(ms_data_ptr->run.samplePtr->name));
196  }
197 
198  qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
199  << "Current ms_run_id:" << ms_run_id.toString();
200 
201  // Finally make a shared pointer out of it and append it to the vector.
202  ms_run_ids.push_back(std::make_shared<MsRunId>(ms_run_id));
203 
204  ++iter;
205  }
206 
207  return ms_run_ids;
208 }
209 
210 
211 } // namespace pappso
pappso::MzFormat::agilentMassHunter
@ agilentMassHunter
pappso::PwizMsFileReader::m_msDataPtrVector
std::vector< pwiz::msdata::MSDataPtr > m_msDataPtrVector
Definition: pwizmsfilereader.h:20
pappso::MzFormat::thermoRaw
@ thermoRaw
pappso::MsRunId::setMzFormat
void setMzFormat(MzFormat format)
Definition: msrunid.cpp:152
pappso::PwizMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: pwizmsfilereader.cpp:162
pappso
Definition: aa.cpp:38
pappso::MsRunId::setXmlId
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:131
pappso::MzFormat::brukerYep
@ brukerYep
pappso::MsRunId::setSampleName
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition: msrunid.cpp:73
pappso::MzFormat::abSciexT2D
@ abSciexT2D
pappso::MzFormat::unknown
@ unknown
unknown format
pappso::MzFormat::mzXML
@ mzXML
mzXML
pappso::PwizMsFileReader::getFileFormat
virtual MzFormat getFileFormat() override
Definition: pwizmsfilereader.cpp:151
pappso::MsRunId
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition: msrunid.h:73
pappso::PwizMsFileReader::initialize
virtual std::size_t initialize()
Definition: pwizmsfilereader.cpp:40
pappso::MzFormat::mz5
@ mz5
pappso::Utils::toUtf8StandardString
static std::string toUtf8StandardString(const QString &text)
Definition: utils.cpp:136
pappso::MzFormat::msn
@ msn
pappso::MsFileReader::m_fileFormat
MzFormat m_fileFormat
Definition: msfilereader.h:18
pappso::MzFormat::abSciexWiff
@ abSciexWiff
pappso::MzFormat::brukerFid
@ brukerFid
pappso::MsFileReader::m_fileName
QString m_fileName
Definition: msfilereader.h:17
pappso::MsFileReader
Definition: msfilereader.h:14
pappso::MzFormat::brukerBaf
@ brukerBaf
pappso::PwizMsFileReader::~PwizMsFileReader
virtual ~PwizMsFileReader()
Definition: pwizmsfilereader.cpp:34
pappso::MzFormat::mzML
@ mzML
mzML
pappso::MzFormat
MzFormat
Definition: types.h:127
pappso::PwizMsFileReader::PwizMsFileReader
PwizMsFileReader(const QString &file_name)
Definition: pwizmsfilereader.cpp:28
pappso::Utils::getLexicalOrderedString
static const QString getLexicalOrderedString(unsigned int num)
Definition: utils.cpp:73
pappso::MzFormat::watersRaw
@ watersRaw
pwizmsfilereader.h
pappso::PappsoException
Definition: pappsoexception.h:62
pappso::MzFormat::MGF
@ MGF
Mascot format.
pappso::MsRunId::toString
QString toString() const
Definition: msrunid.cpp:187