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 
24 namespace pappso
25 {
26 
27 
28 MsFileAccessor::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 
52 const QString &
54 {
55  return m_fileName;
56 }
57 
58 
61 {
62  return m_fileFormat;
63 }
64 
65 
66 std::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 
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  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 
109  auto pref = m_preferedFileReaderTypeMap.find(m_fileFormat);
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  {
168  throw(ExceptionNotPossible(
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())
178  throw(ExceptionNotPossible(
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 {
226 
227  QFile file(ms_run_id.get()->getFileName());
228  if(!file.exists())
229  throw(ExceptionNotFound(
230  QObject::tr("unable to build a reader : file %1 not found.")
231  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
232 
233  MzFormat file_format = ms_run_id.get()->getMzFormat();
234 
235  if(file_format == MzFormat::xy)
236  {
237  qDebug() << "Returning a XyMsRunReader.";
238 
239  return std::make_shared<XyMsRunReader>(ms_run_id);
240  }
241  else if(file_format == MzFormat::unknown)
242  {
243  throw(PappsoException(
244  QObject::tr("unable to build a reader for %1 : unknown file format")
245  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
246  }
247  else if(file_format == MzFormat::brukerTims)
248  {
249  qDebug() << "by default, build a TimsMsRunReader.";
250  return std::make_shared<TimsMsRunReader>(ms_run_id);
251  }
252  else
253  {
254  qDebug() << "Returning a PwizMsRunReader .";
255 
256  return std::make_shared<PwizMsRunReader>(ms_run_id);
257  }
258 }
259 
262  const QString &xml_id)
263 {
264  std::vector<MsRunIdCstSPtr> run_list = getMsRunIds();
265  MsRunReaderSPtr reader_sp;
266 
267  for(MsRunIdCstSPtr &original_run_id : run_list)
268  {
269  if(original_run_id.get()->getRunId() == run_id)
270  {
271  MsRunId new_run_id(*original_run_id.get());
272  new_run_id.setXmlId(xml_id);
273 
274  return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
275  }
276  }
277 
278  if((run_id.isEmpty()) && (run_list.size() == 1))
279  {
280  MsRunId new_run_id(*run_list[0].get());
281  new_run_id.setXmlId(xml_id);
282 
283  return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
284  }
285 
286 
287  if(reader_sp == nullptr)
288  {
289  throw(
290  ExceptionNotFound(QObject::tr("run id %1 not found in file %2")
291  .arg(run_id)
292  .arg(QFileInfo(m_fileName).absoluteFilePath())));
293  }
294  return reader_sp;
295 }
296 
297 void
299  FileReaderType reader_type)
300 {
301  auto ret = m_preferedFileReaderTypeMap.insert(
302  std::pair<MzFormat, FileReaderType>(format, reader_type));
303  if(!ret.second)
304  {
305  // replace
306  ret.first->second = reader_type;
307  }
308 }
309 
310 } // namespace pappso
pappso::MsFileAccessor::m_preferedFileReaderTypeMap
std::map< MzFormat, FileReaderType > m_preferedFileReaderTypeMap
Definition: msfileaccessor.h:87
pappso::TimsMsFileReader::getFileFormat
virtual MzFormat getFileFormat() override
Definition: timsmsfilereader.cpp:89
pappso::MzFormat::brukerTims
@ brukerTims
timsmsfilereader.h
MSrun file reader for native Bruker TimsTOF raw data.
msfileaccessor.h
pappso::FileReaderType::xy
@ xy
pappso::MsFileAccessor::getFileName
const QString & getFileName() const
Definition: msfileaccessor.cpp:53
pappso::PwizMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: pwizmsfilereader.cpp:162
pappso::MsFileAccessor::setPreferedFileReaderType
void setPreferedFileReaderType(MzFormat format, FileReaderType reader_type)
given an mz format, explicitly set the prefered reader
Definition: msfileaccessor.cpp:298
pappso::MsFileAccessor::m_fileFormat
MzFormat m_fileFormat
Definition: msfileaccessor.h:82
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks
Definition: aa.cpp:39
pappso::FileReaderType::tims
@ tims
pappso::MsFileAccessor::m_fileReaderType
FileReaderType m_fileReaderType
Definition: msfileaccessor.h:85
pappso::MsFileAccessor::buildTimsMsRunReaderMs2SPtr
TimsMsRunReaderMs2SPtr buildTimsMsRunReaderMs2SPtr()
if possible, builds directly a dedicated Tims TOF tdf file reader
Definition: msfileaccessor.cpp:143
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:172
pappso::FileReaderType::pwiz
@ pwiz
pappso::MsRunId::setXmlId
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:131
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:65
pappso::XyMsFileReader::getFileFormat
virtual MzFormat getFileFormat() override
Definition: xymsfilereader.cpp:99
pappso::TimsMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: timsmsfilereader.cpp:96
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:53
pappso::MzFormat::unknown
@ unknown
unknown format
xymsfilereader.h
pappso::XyMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: xymsfilereader.cpp:106
pappso::MsFileAccessor
Definition: msfileaccessor.h:35
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:74
pappso::MsFileAccessor::m_xmlPrefix
const QString m_xmlPrefix
Definition: msfileaccessor.h:80
pappso::MzFormat::xy
@ xy
(x,y) format
pappso::FileReaderType
FileReaderType
Definition: msfileaccessor.h:21
pappso::PwizMsFileReader
Definition: pwizmsfilereader.h:18
pappso::FileReaderType::tims_ms2
@ tims_ms2
pappso::MsFileAccessor::getMsRunIds
std::vector< MsRunIdCstSPtr > getMsRunIds()
Definition: msfileaccessor.cpp:67
pappso::MsFileAccessor::~MsFileAccessor
virtual ~MsFileAccessor()
Definition: msfileaccessor.cpp:47
pappso::ExceptionNotFound
Definition: exceptionnotfound.h:53
pappso::MsFileAccessor::msRunReaderSp
MsRunReaderSPtr msRunReaderSp(MsRunIdCstSPtr ms_run_id)
Definition: msfileaccessor.cpp:175
pappso::MsFileAccessor::getMsRunReaderSPtrByRunId
MsRunReaderSPtr getMsRunReaderSPtrByRunId(const QString &run_id, const QString &xml_id)
get an msrun reader by finding the run_id in file
Definition: msfileaccessor.cpp:261
pappso::MsFileAccessor::MsFileAccessor
MsFileAccessor(const QString &file_name, const QString &xml_prefix)
Definition: msfileaccessor.cpp:28
pappso::TimsMsRunReaderMs2SPtr
std::shared_ptr< TimsMsRunReaderMs2 > TimsMsRunReaderMs2SPtr
Definition: msfileaccessor.h:17
pappso::MsFileAccessor::buildMsRunReaderSPtr
static MsRunReaderSPtr buildMsRunReaderSPtr(MsRunIdCstSPtr ms_run_id)
get an MsRunReader directly from a valid MsRun ID
Definition: msfileaccessor.cpp:224
pappso::TimsMsFileReader
Definition: timsmsfilereader.h:59
pappso::MsFileAccessor::m_fileName
QString m_fileName
Definition: msfileaccessor.h:76
pappso::MzFormat
MzFormat
Definition: types.h:128
pappso::MsFileAccessor::getFileFormat
MzFormat getFileFormat() const
get the raw format of mz data
Definition: msfileaccessor.cpp:60
pappso::XyMsFileReader
Definition: xymsfilereader.h:17
pwizmsfilereader.h
pappso::PappsoException
Definition: pappsoexception.h:63