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
24namespace pappso
25{
26
27
28PwizMsFileReader::PwizMsFileReader(const QString &file_name)
29 : MsFileReader{file_name}
30{
31}
32
33
35{
36}
37
38
39std::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 "
55 "(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
56 .arg(m_fileName)
57 .arg(error.what())
58 .arg(__FILE__)
59 .arg(__LINE__));
60 }
61 catch(std::exception &error)
62 {
63 qDebug() << error.what() << " " << typeid(error).name();
64
65 throw PappsoException(
66 QObject::tr(
67 "libpwiz ERROR reading MS data file %1 "
68 "(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
69 .arg(m_fileName)
70 .arg(error.what())
71 .arg(__FILE__)
72 .arg(__LINE__));
73 }
74
75 if(readerName.empty())
76 {
77 qDebug() << "Failed to identify the file.";
78
79 return 0;
80 }
81
82 // Now convert the string to MzFormat.
83 if(readerName == "mzML")
85 else if(readerName == "mzXML")
87 else if(readerName == "Mascot Generic")
89 else if(readerName == "MZ5")
91 else if(readerName == "MSn")
93 else if(readerName == "ABSciex WIFF")
95 else if(readerName == "ABSciex T2D")
97 else if(readerName == "Agilent MassHunter")
99 else if(readerName == "Thermo RAW")
101 else if(readerName == "Water RAW")
103 else if(readerName == "Bruker FID")
105 else if(readerName == "Bruker YEP")
107 else if(readerName == "Bruker BAF")
109 else
110 {
112 return 0;
113 }
114
115 // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
116 // << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
117 // << std::endl;
118
119 // At this point we know pwiz could be able to read the file. Actually fill-in
120 // the MSDataPtr vector!
121 try
122 {
123 defaultReaderList.read(Utils::toUtf8StandardString(m_fileName),
125 }
126 catch(std::runtime_error &error)
127 {
128 qDebug() << error.what() << " " << typeid(error).name();
129
130 throw PappsoException(
131 QObject::tr(
132 "libpwiz ERROR reading MS data file %1 "
133 "(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
134 .arg(m_fileName)
135 .arg(error.what())
136 .arg(__FILE__)
137 .arg(__LINE__));
138 }
139 catch(std::exception &error)
140 {
141 qDebug() << error.what() << " " << typeid(error).name();
142
143 throw PappsoException(
144 QObject::tr(
145 "libpwiz ERROR reading MS data file %1 "
146 "(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
147 .arg(m_fileName)
148 .arg(error.what())
149 .arg(__FILE__)
150 .arg(__LINE__));
151 }
152
153 //qDebug() << "The number of runs is:" << m_msDataPtrVector.size()
154 //<< "The reader type is:" << QString::fromStdString(readerName)
155 //<< "The number of spectra in first run is:"
156 //<< m_msDataPtrVector.at(0)->run.spectrumListPtr->size();
157
158 return m_msDataPtrVector.size();
159}
160
161
164{
165 // std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
166 // << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
167 // << std::endl;
168
169 return m_fileFormat;
170}
171
172
173std::vector<MsRunIdCstSPtr>
174PwizMsFileReader::getMsRunIds(const QString &run_prefix)
175{
176 std::vector<MsRunIdCstSPtr> ms_run_ids;
177
178 if(!initialize())
179 return ms_run_ids;
180
181 std::size_t iter = 0;
182
183 // If the initialization failed, then there is not a single MSDataPtr in the
184 // vector, so the loop below is not gone through.
185
186 for(pwiz::msdata::MSDataPtr ms_data_ptr : m_msDataPtrVector)
187 {
188 // For each ms run in the file, we will create a MsRunId instance that
189 // will hold the file name of the data file
190
191 // Finally create the MsRunId with the file name.
192 MsRunId ms_run_id(m_fileName, QString::fromStdString(ms_data_ptr->id));
193 ms_run_id.setMzFormat(m_fileFormat);
194
195 // We need to set the unambiguous xmlId string.
196 ms_run_id.setXmlId(QString("%1%2")
197 .arg(run_prefix)
199
200 // Now set the sample name to the run id :
201 ms_run_id.setSampleName(QString::fromStdString(ms_data_ptr->run.id));
202 // and if it is possible, the real sample name because this one is for the
203 // end user to recognize his sample:
204 if(ms_data_ptr->run.samplePtr != nullptr)
205 {
206 ms_run_id.setSampleName(
207 QString::fromStdString(ms_data_ptr->run.samplePtr->name));
208 }
209
210 //qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
211 //<< "Current ms_run_id:" << ms_run_id.toString();
212
213 // Finally make a shared pointer out of it and append it to the vector.
214 ms_run_ids.push_back(std::make_shared<MsRunId>(ms_run_id));
215
216 ++iter;
217 }
218
219 return ms_run_ids;
220}
221
222
223} // namespace pappso
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition: msrunid.h:54
void setMzFormat(MzFormat format)
Definition: msrunid.cpp:158
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:137
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition: msrunid.cpp:79
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
PwizMsFileReader(const QString &file_name)
std::vector< pwiz::msdata::MSDataPtr > m_msDataPtrVector
virtual std::size_t initialize()
virtual MzFormat getFileFormat() override
static std::string toUtf8StandardString(const QString &text)
Definition: utils.cpp:143
static const QString getLexicalOrderedString(unsigned int num)
Definition: utils.cpp:52
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
MzFormat
Definition: types.h:120
@ unknown
unknown format
@ MGF
Mascot format.