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  if(ms_run_ids.size())
78  {
79  // qDebug() << "Might well be handled using the Pwiz code.";
80  m_fileReaderType = FileReaderType::pwiz;
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  TimsMsFileReader tims_file_reader(tims_dir);
97 
98  ms_run_ids = tims_file_reader.getMsRunIds(m_xmlPrefix);
99 
100  if(ms_run_ids.size())
101  {
102  // qDebug() << "Might well be handled using the Bruker code";
103  m_fileReaderType = FileReaderType::tims;
104  m_fileFormat = tims_file_reader.getFileFormat();
105  m_fileName = tims_dir;
106 
107 
108  auto pref = m_preferedFileReaderTypeMap.find(m_fileFormat);
109  if(pref != m_preferedFileReaderTypeMap.end())
110  {
111  m_fileReaderType = pref->second;
112  }
113 
114  return ms_run_ids;
115  }
116 
117  // qDebug() << "The Tims reader did not work.";
118 
119  // At this point try the XyMsFileReader
120 
121  XyMsFileReader xy_ms_file_reader(m_fileName);
122 
123  ms_run_ids = xy_ms_file_reader.getMsRunIds(m_xmlPrefix);
124 
125  if(ms_run_ids.size())
126  {
127  // qDebug() << "Might well be handled using the XY code";
128  m_fileReaderType = FileReaderType::xy;
129 
130  m_fileFormat = xy_ms_file_reader.getFileFormat();
131 
132  return ms_run_ids;
133  }
134 
135  // qDebug() << "The XY reader did not work.";
136 
137  return ms_run_ids;
138 }
139 
140 
143 {
144 
145  // try TimsData reader
146  QString tims_dir = m_fileName;
147  if(!QFileInfo(tims_dir).isDir())
148  {
149  tims_dir = QFileInfo(m_fileName).absolutePath();
150  }
151  TimsMsFileReader tims_file_reader(tims_dir);
152 
153  std::vector<MsRunIdCstSPtr> ms_run_ids =
154  tims_file_reader.getMsRunIds(m_xmlPrefix);
155 
156  if(ms_run_ids.size())
157  {
158  // qDebug() << "Might well be handled using the Bruker code";
160  m_fileFormat = tims_file_reader.getFileFormat();
161  m_fileName = tims_dir;
162 
163  return std::make_shared<TimsMsRunReaderMs2>(ms_run_ids.front());
164  }
165  else
166  {
167  throw(ExceptionNotPossible(
168  QObject::tr("Unable to read mz data directory %1 with TimsTOF reader.")
169  .arg(tims_dir)));
170  }
171 }
172 
175 {
176  if(m_fileName != ms_run_id->getFileName())
177  throw(ExceptionNotPossible(
178  QObject::tr("The MsRunId instance must have the name file name as the "
179  "MsFileAccessor.")));
180 
181  if(m_fileReaderType == FileReaderType::pwiz)
182  {
183  qDebug() << "Returning a PwizMsRunReader.";
184 
185  return std::make_shared<PwizMsRunReader>(ms_run_id);
186  }
187  else if(m_fileReaderType == FileReaderType::xy)
188  {
189  qDebug() << "Returning a XyMsRunReader.";
190 
191  return std::make_shared<XyMsRunReader>(ms_run_id);
192  }
193 
194  else if(m_fileReaderType == FileReaderType::tims)
195  {
196  qDebug() << "Returning a TimsMsRunReader.";
197 
198  return std::make_shared<TimsMsRunReader>(ms_run_id);
199  }
201  {
202  qDebug() << "Returning a TimsMsRunReaderMs2.";
203 
204  return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
205  }
206 
207  if(m_fileFormat == MzFormat::unknown)
208  {
209  if(ms_run_id.get()->getMzFormat() == MzFormat::xy)
210  {
211  return std::make_shared<XyMsRunReader>(ms_run_id);
212  }
213  else
214  {
215  return std::make_shared<PwizMsRunReader>(ms_run_id);
216  }
217  }
218 
219  return nullptr;
220 }
221 
224 {
225 
226  QFile file(ms_run_id.get()->getFileName());
227  if(!file.exists())
228  throw(ExceptionNotFound(
229  QObject::tr("unable to build a reader : file %1 not found.")
230  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
231 
232  MzFormat file_format = ms_run_id.get()->getMzFormat();
233 
234  if(file_format == MzFormat::xy)
235  {
236  qDebug() << "Returning a XyMsRunReader.";
237 
238  return std::make_shared<XyMsRunReader>(ms_run_id);
239  }
240  else if(file_format == MzFormat::unknown)
241  {
242  throw(PappsoException(
243  QObject::tr("unable to build a reader for %1 : unknown file format")
244  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
245  }
246  else if(file_format == MzFormat::brukerTims)
247  {
248  qDebug() << "by default, build a TimsMsRunReader.";
249  return std::make_shared<TimsMsRunReader>(ms_run_id);
250  }
251  else
252  {
253  qDebug() << "Returning a PwizMsRunReader .";
254 
255  return std::make_shared<PwizMsRunReader>(ms_run_id);
256  }
257 }
258 
261  const QString &xml_id)
262 {
263  qDebug() << "GGGGGGGGG1";
264  std::vector<MsRunIdCstSPtr> run_list = getMsRunIds();
265  MsRunReaderSPtr reader_sp;
266  qDebug() << "GGGGGGGGG";
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:71
timsmsfilereader.h
MSrun file reader for native Bruker TimsTOF raw data.
msfileaccessor.h
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 and changes multichar...
Definition: aa.cpp:39
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:142
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:151
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:44
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:78
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:29
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:53
pappso::MsFileAccessor::m_xmlPrefix
const QString m_xmlPrefix
Definition: msfileaccessor.h:80
pappso::FileReaderType
FileReaderType
Definition: msfileaccessor.h:21
pappso::PwizMsFileReader
Definition: pwizmsfilereader.h:18
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:32
pappso::MsFileAccessor::msRunReaderSp
MsRunReaderSPtr msRunReaderSp(MsRunIdCstSPtr ms_run_id)
Definition: msfileaccessor.cpp:174
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:260
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:223
pappso::TimsMsFileReader
Definition: timsmsfilereader.h:41
pappso::MsFileAccessor::m_fileName
QString m_fileName
Definition: msfileaccessor.h:76
pappso::MzFormat
MzFormat
Definition: types.h:107
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:42