pappsomspp
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 
70  // Try the PwizMsFileReader
71 
72  PwizMsFileReader pwiz_ms_file_reader(m_fileName);
73 
74  std::vector<MsRunIdCstSPtr> ms_run_ids =
75  pwiz_ms_file_reader.getMsRunIds(m_xmlPrefix);
76 
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  // try TimsData reader
88  QString tims_dir = m_fileName;
89  if(!QFileInfo(tims_dir).isDir())
90  {
91  tims_dir = QFileInfo(m_fileName).absolutePath();
92  }
93  TimsMsFileReader tims_file_reader(tims_dir);
94 
95  ms_run_ids = tims_file_reader.getMsRunIds(m_xmlPrefix);
96 
97  if(ms_run_ids.size())
98  {
99  // qDebug() << "Might well be handled using the Bruker code";
101  m_fileFormat = tims_file_reader.getFileFormat();
102  m_fileName = tims_dir;
103 
104 
105  auto pref = m_preferedFileReaderTypeMap.find(m_fileFormat);
106  if(pref != m_preferedFileReaderTypeMap.end())
107  {
108  m_fileReaderType = pref->second;
109  }
110 
111  return ms_run_ids;
112  }
113 
114 
115  // At this point try the XyMsFileReader
116 
117  XyMsFileReader xy_ms_file_reader(m_fileName);
118 
119  ms_run_ids = xy_ms_file_reader.getMsRunIds(m_xmlPrefix);
120 
121  if(ms_run_ids.size())
122  {
123  // qDebug() << "Might well be handled using the XY code";
125 
126  m_fileFormat = xy_ms_file_reader.getFileFormat();
127 
128  return ms_run_ids;
129  }
130 
131  return ms_run_ids;
132 }
133 
136 {
137 
138  // try TimsData reader
139  QString tims_dir = m_fileName;
140  if(!QFileInfo(tims_dir).isDir())
141  {
142  tims_dir = QFileInfo(m_fileName).absolutePath();
143  }
144  TimsMsFileReader tims_file_reader(tims_dir);
145 
146  std::vector<MsRunIdCstSPtr> ms_run_ids =
147  tims_file_reader.getMsRunIds(m_xmlPrefix);
148 
149  if(ms_run_ids.size())
150  {
151  // qDebug() << "Might well be handled using the Bruker code";
153  m_fileFormat = tims_file_reader.getFileFormat();
154  m_fileName = tims_dir;
155 
156  return std::make_shared<TimsMsRunReaderMs2>(ms_run_ids.front());
157  }
158  else
159  {
160  throw(ExceptionNotPossible(
161  QObject::tr("Unable to read mz data directory %1 with TimsTOF reader.")
162  .arg(tims_dir)));
163  }
164 }
165 
168 {
169  if(m_fileName != ms_run_id->getFileName())
170  throw(ExceptionNotPossible(
171  QObject::tr("The MsRunId instance must have the name file name as the "
172  "MsFileAccessor.")));
173 
175  {
176  qDebug() << "Returning a PwizMsRunReader.";
177 
178  return std::make_shared<PwizMsRunReader>(ms_run_id);
179  }
181  {
182  qDebug() << "Returning a XyMsRunReader.";
183 
184  return std::make_shared<XyMsRunReader>(ms_run_id);
185  }
186 
188  {
189  qDebug() << "Returning a TimsMsRunReader.";
190 
191  return std::make_shared<TimsMsRunReader>(ms_run_id);
192  }
194  {
195  qDebug() << "Returning a TimsMsRunReaderMs2.";
196 
197  return std::make_shared<TimsMsRunReaderMs2>(ms_run_id);
198  }
199 
201  {
202  if(ms_run_id.get()->getMzFormat() == MzFormat::xy)
203  {
204  return std::make_shared<XyMsRunReader>(ms_run_id);
205  }
206  else
207  {
208  return std::make_shared<PwizMsRunReader>(ms_run_id);
209  }
210  }
211 
212  return nullptr;
213 }
214 
217 {
218 
219  QFile file(ms_run_id.get()->getFileName());
220  if(!file.exists())
221  throw(ExceptionNotFound(
222  QObject::tr("unable to build a reader : file %1 not found.")
223  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
224 
225  MzFormat file_format = ms_run_id.get()->getMzFormat();
226 
227  if(file_format == MzFormat::xy)
228  {
229  qDebug() << "Returning a XyMsRunReader.";
230 
231  return std::make_shared<XyMsRunReader>(ms_run_id);
232  }
233  else if(file_format == MzFormat::unknown)
234  {
235  throw(PappsoException(
236  QObject::tr("unable to build a reader for %1 : unknown file format")
237  .arg(QFileInfo(ms_run_id.get()->getFileName()).absoluteFilePath())));
238  }
239  else if(file_format == MzFormat::brukerTims)
240  {
241  qDebug() << "by default, build a TimsMsRunReader.";
242  return std::make_shared<TimsMsRunReader>(ms_run_id);
243  }
244  else
245  {
246  qDebug() << "Returning a PwizMsRunReader .";
247 
248  return std::make_shared<PwizMsRunReader>(ms_run_id);
249  }
250 }
251 
254  const QString &xml_id)
255 {
256  std::vector<MsRunIdCstSPtr> run_list = getMsRunIds();
257  MsRunReaderSPtr reader_sp;
258 
259  for(MsRunIdCstSPtr &original_run_id : run_list)
260  {
261  if(original_run_id.get()->getRunId() == run_id)
262  {
263  MsRunId new_run_id(*original_run_id.get());
264  new_run_id.setXmlId(xml_id);
265 
266  return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
267  }
268  }
269 
270  if((run_id.isEmpty()) && (run_list.size() == 1))
271  {
272  MsRunId new_run_id(*run_list[0].get());
273  new_run_id.setXmlId(xml_id);
274 
275  return msRunReaderSp(std::make_shared<MsRunId>(new_run_id));
276  }
277 
278 
279  if(reader_sp == nullptr)
280  {
281  throw(
282  ExceptionNotFound(QObject::tr("run id %1 not found in file %2")
283  .arg(run_id)
284  .arg(QFileInfo(m_fileName).absoluteFilePath())));
285  }
286  return reader_sp;
287 }
288 
289 void
291  FileReaderType reader_type)
292 {
293  auto ret = m_preferedFileReaderTypeMap.insert(
294  std::pair<MzFormat, FileReaderType>(format, reader_type));
295  if(!ret.second)
296  {
297  // replace
298  ret.first->second = reader_type;
299  }
300 }
301 
302 } // 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:87
pappso::MzFormat::brukerTims
timsmsfilereader.h
MSrun file reader for native Bruker TimsTOF raw data.
msfileaccessor.h
pappso::FileReaderType::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:290
pappso::MsFileAccessor::m_fileFormat
MzFormat m_fileFormat
Definition: msfileaccessor.h:82
pappso
Definition: aa.cpp:38
pappso::FileReaderType::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:135
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:168
pappso::FileReaderType::pwiz
pappso::MsRunId::setXmlId
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:130
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:63
pappso::XyMsFileReader::getFileFormat
virtual MzFormat getFileFormat() override
Definition: xymsfilereader.cpp:101
pappso::TimsMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: timsmsfilereader.cpp:94
pappso::ExceptionNotPossible
Definition: exceptionnotpossible.h:50
pappso::MzFormat::unknown
unknown format
xymsfilereader.h
pappso::XyMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: xymsfilereader.cpp:108
pappso::MsFileAccessor
Definition: msfileaccessor.h:34
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:71
pappso::MsFileAccessor::m_xmlPrefix
const QString m_xmlPrefix
Definition: msfileaccessor.h:80
pappso::FileReaderType
FileReaderType
Definition: msfileaccessor.h:20
pappso::PwizMsFileReader
Definition: pwizmsfilereader.h:17
pappso::FileReaderType::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:50
pappso::MsFileAccessor::msRunReaderSp
MsRunReaderSPtr msRunReaderSp(MsRunIdCstSPtr ms_run_id)
Definition: msfileaccessor.cpp:167
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:253
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:216
pappso::TimsMsFileReader
Definition: timsmsfilereader.h:56
pappso::MsFileAccessor::m_fileName
QString m_fileName
Definition: msfileaccessor.h:76
pappso::MzFormat
MzFormat
Definition: types.h:125
pappso::MsFileAccessor::getFileFormat
MzFormat getFileFormat() const
get the raw format of mz data
Definition: msfileaccessor.cpp:60
pappso::XyMsFileReader
Definition: xymsfilereader.h:16
pwizmsfilereader.h
pappso::PappsoException
Definition: pappsoexception.h:60