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 "../utils.h"
17#include "../msrun/msrunid.h"
18#include "../msrun/private/pwizmsrunreader.h"
19#include "../msrun/private/timsmsrunreader.h"
20#include "../msrun/private/timsmsrunreaderms2.h"
21#include "../msrun/xymsrunreader.h"
22
23
24namespace pappso
25{
26
27
28MsFileAccessor::MsFileAccessor(const QString &file_name,
29 const QString &xml_prefix)
30 : m_fileName(file_name), m_xmlPrefix(xml_prefix)
31{
32 QFile file(file_name);
33 if(!file.exists())
34 throw(ExceptionNotFound(QObject::tr("File %1 not found.")
35 .arg(QFileInfo(file_name).absoluteFilePath())));
36}
37
38
40 : m_fileName(other.m_fileName),
41 m_xmlPrefix(other.m_xmlPrefix),
42 m_fileFormat(other.m_fileFormat),
43 m_fileReaderType(other.m_fileReaderType)
44{
45}
46
48{
49}
50
51
52const QString &
54{
55 return m_fileName;
56}
57
58
61{
62 return m_fileFormat;
63}
64
65
66std::vector<MsRunIdCstSPtr>
68{
69 // qDebug();
70
71 // Try the PwizMsFileReader
72
73 PwizMsFileReader pwiz_ms_file_reader(m_fileName);
74
75 std::vector<MsRunIdCstSPtr> ms_run_ids =
76 pwiz_ms_file_reader.getMsRunIds(m_xmlPrefix);
77 if(ms_run_ids.size())
78 {
79 // qDebug() << "Might well be handled using the Pwiz code.";
81
82 m_fileFormat = pwiz_ms_file_reader.getFileFormat();
83
84 return ms_run_ids;
85 }
86
87 // qDebug() << "The Pwiz reader did not work.";
88
89 // Try the TimsData reader
90
91 QString tims_dir = m_fileName;
92 if(!QFileInfo(tims_dir).isDir())
93 {
94 tims_dir = QFileInfo(m_fileName).absolutePath();
95 }
96
97 TimsMsFileReader tims_file_reader(tims_dir);
98
99 ms_run_ids = tims_file_reader.getMsRunIds(m_xmlPrefix);
100
101 if(ms_run_ids.size())
102 {
103 // qDebug() << "Might well be handled using the Bruker code";
105 m_fileFormat = tims_file_reader.getFileFormat();
106 m_fileName = tims_dir;
107
108
110 if(pref != m_preferedFileReaderTypeMap.end())
111 {
112 m_fileReaderType = pref->second;
113 }
114
115 return ms_run_ids;
116 }
117
118 // qDebug() << "The Tims reader did not work.";
119
120 // At this point try the XyMsFileReader
121
122 XyMsFileReader xy_ms_file_reader(m_fileName);
123
124 ms_run_ids = xy_ms_file_reader.getMsRunIds(m_xmlPrefix);
125
126 if(ms_run_ids.size())
127 {
128 // qDebug() << "Might well be handled using the XY code";
130
131 m_fileFormat = xy_ms_file_reader.getFileFormat();
132
133 return ms_run_ids;
134 }
135
136 // qDebug() << "The XY reader did not work.";
137
138 return ms_run_ids;
139}
140
141
144{
145
146 // try TimsData reader
147 QString tims_dir = m_fileName;
148 if(!QFileInfo(tims_dir).isDir())
149 {
150 tims_dir = QFileInfo(m_fileName).absolutePath();
151 }
152 TimsMsFileReader tims_file_reader(tims_dir);
153
154 std::vector<MsRunIdCstSPtr> ms_run_ids =
155 tims_file_reader.getMsRunIds(m_xmlPrefix);
156
157 if(ms_run_ids.size())
158 {
159 // qDebug() << "Might well be handled using the Bruker code";
161 m_fileFormat = tims_file_reader.getFileFormat();
162 m_fileName = tims_dir;
163
164 return std::make_shared<TimsMsRunReaderMs2>(ms_run_ids.front());
165 }
166 else
167 {
169 QObject::tr("Unable to read mz data directory %1 with TimsTOF reader.")
170 .arg(tims_dir)));
171 }
172}
173
176{
177 if(m_fileName != ms_run_id->getFileName())
179 QObject::tr("The MsRunId instance must have the name file name as the "
180 "MsFileAccessor.")));
181
183 {
184 qDebug() << "Returning a PwizMsRunReader.";
185
186 return std::make_shared<PwizMsRunReader>(ms_run_id);
187 }
189 {
190 qDebug() << "Returning a XyMsRunReader.";
191
192 return std::make_shared<XyMsRunReader>(ms_run_id);
193 }
194
196 {
197 qDebug() << "Returning a TimsMsRunReader.";
198
199 return std::make_shared<TimsMsRunReader>(ms_run_id);
200 }
202 {
203 qDebug() << "Returning a TimsMsRunReaderMs2.";
204
205 return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
206 }
207
209 {
210 if(ms_run_id.get()->getMzFormat() == MzFormat::xy)
211 {
212 return std::make_shared<XyMsRunReader>(ms_run_id);
213 }
214 else
215 {
216 return std::make_shared<PwizMsRunReader>(ms_run_id);
217 }
218 }
219
220 return nullptr;
221}
222
225{
227}
228
231 MsRunIdCstSPtr ms_run_id, pappso::FileReaderType prefered_file_reader_type)
232{
233
234 QFile file(ms_run_id.get()->getFileName());
235 if(!file.exists())
236 throw(ExceptionNotFound(
237 QObject::tr("unable to build a reader : file %1 not found.")
238 .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
239
240 MzFormat file_format = ms_run_id.get()->getMzFormat();
241
242 if(file_format == MzFormat::xy)
243 {
244 qDebug() << "Returning a XyMsRunReader.";
245
246 return std::make_shared<XyMsRunReader>(ms_run_id);
247 }
248 else if(file_format == MzFormat::unknown)
249 {
250 throw(PappsoException(
251 QObject::tr("unable to build a reader for %1 : unknown file format")
252 .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
253 }
254 else if(file_format == MzFormat::brukerTims)
255 {
256 if(prefered_file_reader_type == pappso::FileReaderType::tims)
257 {
258 return std::make_shared<TimsMsRunReader>(ms_run_id);
259 }
260 else if(prefered_file_reader_type == pappso::FileReaderType::tims_ms2)
261 {
262 qDebug();
263 return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
264 }
265
266 qDebug() << "by default, build a TimsMsRunReader.";
267 return std::make_shared<TimsMsRunReader>(ms_run_id);
268 }
269 else
270 {
271 qDebug() << "Returning a PwizMsRunReader .";
272
273 return std::make_shared<PwizMsRunReader>(ms_run_id);
274 }
275}
276
279 const QString &xml_id)
280{
281 qDebug() << "GGGGGGGGG1";
282 std::vector<MsRunIdCstSPtr> run_list = getMsRunIds();
283 MsRunReaderSPtr reader_sp;
284 qDebug() << "GGGGGGGGG";
285 for(MsRunIdCstSPtr &original_run_id : run_list)
286 {
287 if(original_run_id.get()->getRunId() == run_id)
288 {
289 MsRunId new_run_id(*original_run_id.get());
290 new_run_id.setXmlId(xml_id);
291
292 return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
293 }
294 }
295
296 if((run_id.isEmpty()) && (run_list.size() == 1))
297 {
298 MsRunId new_run_id(*run_list[0].get());
299 new_run_id.setXmlId(xml_id);
300
301 return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
302 }
303
304
305 if(reader_sp == nullptr)
306 {
307 throw(
308 ExceptionNotFound(QObject::tr("run id %1 not found in file %2")
309 .arg(run_id)
310 .arg(QFileInfo(m_fileName).absoluteFilePath())));
311 }
312 return reader_sp;
313}
314
315void
317 FileReaderType reader_type)
318{
319 auto ret = m_preferedFileReaderTypeMap.insert(
320 std::pair<MzFormat, FileReaderType>(format, reader_type));
321 if(!ret.second)
322 {
323 // replace
324 ret.first->second = reader_type;
325 }
326}
327
330{
331 return m_fileReaderType;
332}
333} // 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:184
std::shared_ptr< TimsMsRunReaderMs2 > TimsMsRunReaderMs2SPtr
MzFormat
Definition: types.h:107
@ 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.