libpappsomspp
Library for mass spectrometry
msfileaccessor.cpp
Go to the documentation of this file.
1//#include <proteowizard/pwiz/data/msdata/DefaultReaderList.hpp>
2
3#include <QDebug>
4#include <QFile>
5#include <QFileInfo>
6
7
8#include "msfileaccessor.h"
9#include "pwizmsfilereader.h"
10#include "timsmsfilereader.h"
11#include "xymsfilereader.h"
12
13
14#include "../exception/exceptionnotfound.h"
15#include "../exception/exceptionnotpossible.h"
16#include "../msrun/msrunid.h"
17#include "../msrun/private/timsframesmsrunreader.h"
18
19#include "../msrun/private/pwizmsrunreader.h"
20#include "../msrun/private/timsmsrunreader.h"
21#include "../msrun/private/timsmsrunreaderms2.h"
22#include "../msrun/xymsrunreader.h"
23
24
25namespace pappso
26{
27
28
29MsFileAccessor::MsFileAccessor(const QString &file_name,
30 const QString &xml_prefix)
31 : m_fileName(file_name), m_xmlPrefix(xml_prefix)
32{
33 QFile file(file_name);
34 if(!file.exists())
35 throw(ExceptionNotFound(QObject::tr("File %1 not found.")
36 .arg(QFileInfo(file_name).absoluteFilePath())));
37}
38
39
41 : m_fileName(other.m_fileName),
42 m_xmlPrefix(other.m_xmlPrefix),
43 m_fileFormat(other.m_fileFormat),
44 m_fileReaderType(other.m_fileReaderType)
45{
46}
47
49{
50}
51
52
53const QString &
55{
56 return m_fileName;
57}
58
59
62{
63 return m_fileFormat;
64}
65
66
67std::vector<MsRunIdCstSPtr>
69{
70 // qDebug();
71
72 // Try the PwizMsFileReader
73
74 PwizMsFileReader pwiz_ms_file_reader(m_fileName);
75
76 std::vector<MsRunIdCstSPtr> ms_run_ids =
77 pwiz_ms_file_reader.getMsRunIds(m_xmlPrefix);
78 if(ms_run_ids.size())
79 {
80 // qDebug() << "Might well be handled using the Pwiz code.";
82
83 m_fileFormat = pwiz_ms_file_reader.getFileFormat();
84
85 return ms_run_ids;
86 }
87
88 // qDebug() << "The Pwiz reader did not work.";
89
90 // Try the TimsData reader
91
92 QString tims_dir = m_fileName;
93 if(!QFileInfo(tims_dir).isDir())
94 {
95 tims_dir = QFileInfo(m_fileName).absolutePath();
96 }
97
98 TimsMsFileReader tims_file_reader(tims_dir);
99
100 ms_run_ids = tims_file_reader.getMsRunIds(m_xmlPrefix);
101
102 if(ms_run_ids.size())
103 {
104 // qDebug() << "Might well be handled using the Bruker code";
106 m_fileFormat = tims_file_reader.getFileFormat();
107 m_fileName = tims_dir;
108
109
111 if(pref != m_preferedFileReaderTypeMap.end())
112 {
113 m_fileReaderType = pref->second;
114 }
115
116 //qDebug() << "Returning Bruker::tims ms run(s).";
117
118 return ms_run_ids;
119 }
120
121 // qDebug() << "The Tims reader did not work.";
122
123 // At this point try the XyMsFileReader
124
125 XyMsFileReader xy_ms_file_reader(m_fileName);
126
127 ms_run_ids = xy_ms_file_reader.getMsRunIds(m_xmlPrefix);
128
129 if(ms_run_ids.size())
130 {
131 // qDebug() << "Might well be handled using the XY code";
133
134 m_fileFormat = xy_ms_file_reader.getFileFormat();
135
136 return ms_run_ids;
137 }
138
139 // qDebug() << "The XY reader did not work.";
140
141 return ms_run_ids;
142}
143
144
147{
148
149 // try TimsData reader
150 QString tims_dir = m_fileName;
151 if(!QFileInfo(tims_dir).isDir())
152 {
153 tims_dir = QFileInfo(m_fileName).absolutePath();
154 }
155 TimsMsFileReader tims_file_reader(tims_dir);
156
157 std::vector<MsRunIdCstSPtr> ms_run_ids =
158 tims_file_reader.getMsRunIds(m_xmlPrefix);
159
160 if(ms_run_ids.size())
161 {
162 // qDebug() << "Might well be handled using the Bruker code";
164 m_fileFormat = tims_file_reader.getFileFormat();
165 m_fileName = tims_dir;
166
167 return std::make_shared<TimsMsRunReaderMs2>(ms_run_ids.front());
168 }
169 else
170 {
172 QObject::tr("Unable to read mz data directory %1 with TimsTOF reader.")
173 .arg(tims_dir)));
174 }
175}
176
177
180{
181 if(m_fileName != ms_run_id->getFileName())
183 QObject::tr("The MsRunId instance must have the name file name as the "
184 "MsFileAccessor.")));
185
187 {
188 //qDebug() << "Returning a PwizMsRunReader.";
189
190 return std::make_shared<PwizMsRunReader>(ms_run_id);
191 }
193 {
194 //qDebug() << "Returning a XyMsRunReader.";
195
196 return std::make_shared<XyMsRunReader>(ms_run_id);
197 }
199 {
200 //qDebug() << "Returning a TimsMsRunReader.";
201
202 return std::make_shared<TimsMsRunReader>(ms_run_id);
203 }
205 {
206 //qDebug() << "Returning a TimsFramesMsRunReader.";
207
208 return std::make_shared<TimsFramesMsRunReader>(ms_run_id);
209 }
211 {
212 //qDebug() << "Returning a TimsMsRunReaderMs2.";
213
214 return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
215 }
217 {
218 if(ms_run_id.get()->getMzFormat() == MzFormat::xy)
219 {
220 return std::make_shared<XyMsRunReader>(ms_run_id);
221 }
222 else
223 {
224 return std::make_shared<PwizMsRunReader>(ms_run_id);
225 }
226 }
227
228 return nullptr;
229}
230
231
234{
236}
237
240 MsRunIdCstSPtr ms_run_id, pappso::FileReaderType prefered_file_reader_type)
241{
242
243 QFile file(ms_run_id.get()->getFileName());
244 if(!file.exists())
245 throw(ExceptionNotFound(
246 QObject::tr("unable to build a reader : file %1 not found.")
247 .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
248
249 MzFormat file_format = ms_run_id.get()->getMzFormat();
250
251 if(file_format == MzFormat::xy)
252 {
253 //qDebug() << "Returning a XyMsRunReader.";
254
255 return std::make_shared<XyMsRunReader>(ms_run_id);
256 }
257 else if(file_format == MzFormat::unknown)
258 {
259 throw(PappsoException(
260 QObject::tr("unable to build a reader for %1 : unknown file format")
261 .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
262 }
263
264 else if(file_format == MzFormat::brukerTims)
265 {
266 if(prefered_file_reader_type == pappso::FileReaderType::tims)
267 {
268 return std::make_shared<TimsMsRunReader>(ms_run_id);
269 }
270 else if(prefered_file_reader_type == pappso::FileReaderType::tims_ms2)
271 {
272 return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
273 }
274
275 //qDebug() << "by default, build a TimsMsRunReader.";
276 return std::make_shared<TimsMsRunReader>(ms_run_id);
277 }
278 else
279 {
280 //qDebug() << "Returning a PwizMsRunReader .";
281
282 return std::make_shared<PwizMsRunReader>(ms_run_id);
283 }
284}
285
286
289 const QString &xml_id)
290{
291 std::vector<MsRunIdCstSPtr> run_list = getMsRunIds();
292 MsRunReaderSPtr reader_sp;
293 for(MsRunIdCstSPtr &original_run_id : run_list)
294 {
295 if(original_run_id.get()->getRunId() == run_id)
296 {
297 MsRunId new_run_id(*original_run_id.get());
298 new_run_id.setXmlId(xml_id);
299
300 return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
301 }
302 }
303
304 if((run_id.isEmpty()) && (run_list.size() == 1))
305 {
306 MsRunId new_run_id(*run_list[0].get());
307 new_run_id.setXmlId(xml_id);
308
309 return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
310 }
311
312
313 if(reader_sp == nullptr)
314 {
315 throw(
316 ExceptionNotFound(QObject::tr("run id %1 not found in file %2")
317 .arg(run_id)
318 .arg(QFileInfo(m_fileName).absoluteFilePath())));
319 }
320 return reader_sp;
321}
322
323void
325 FileReaderType reader_type)
326{
327 auto ret = m_preferedFileReaderTypeMap.insert(
328 std::pair<MzFormat, FileReaderType>(format, reader_type));
329
330 if(!ret.second)
331 {
332 // replace
333 ret.first->second = reader_type;
334 }
335}
336
339{
340 return m_fileReaderType;
341}
342} // namespace pappso
MzFormat getFileFormat() const
get the raw format of mz data
MsRunReaderSPtr msRunReaderSp(MsRunIdCstSPtr ms_run_id)
const QString m_xmlPrefix
void setPreferedFileReaderType(MzFormat format, FileReaderType reader_type)
given an mz format, explicitly set the prefered reader
FileReaderType getFileReaderType() const
get the file reader type
std::map< MzFormat, FileReaderType > m_preferedFileReaderTypeMap
std::vector< MsRunIdCstSPtr > getMsRunIds()
FileReaderType m_fileReaderType
MsRunReaderSPtr getMsRunReaderSPtrByRunId(const QString &run_id, const QString &xml_id)
get an msrun reader by finding the run_id in file
MsFileAccessor(const QString &file_name, const QString &xml_prefix)
static MsRunReaderSPtr buildMsRunReaderSPtr(MsRunIdCstSPtr ms_run_id)
get an MsRunReader directly from a valid MsRun ID
TimsMsRunReaderMs2SPtr buildTimsMsRunReaderMs2SPtr()
if possible, builds directly a dedicated Tims TOF tdf file reader
const QString & getFileName() const
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition: msrunid.h:53
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:137
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
virtual MzFormat getFileFormat() override
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
virtual MzFormat getFileFormat() override
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
virtual MzFormat getFileFormat() override
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:185
std::shared_ptr< TimsMsRunReaderMs2 > TimsMsRunReaderMs2SPtr
MzFormat
Definition: types.h:108
@ xy
(x,y) format
@ unknown
unknown format
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:45
MSrun file reader for native Bruker TimsTOF raw data.