pappsomspp
Library for mass spectrometry
pappso::MsFileAccessor Class Reference

#include <msfileaccessor.h>

Public Member Functions

 MsFileAccessor (const QString &file_name, const QString &xml_prefix)
 
 MsFileAccessor (const MsFileAccessor &other)
 
virtual ~MsFileAccessor ()
 
const QString & getFileName () const
 
MzFormat getFileFormat () const
 get the raw format of mz data More...
 
void setPreferedFileReaderType (MzFormat format, FileReaderType reader_type)
 given an mz format, explicitly set the prefered reader More...
 
std::vector< MsRunIdCstSPtrgetMsRunIds ()
 
MsRunReaderSPtr msRunReaderSp (MsRunIdCstSPtr ms_run_id)
 
MsRunReaderSPtr getMsRunReaderSPtrByRunId (const QString &run_id, const QString &xml_id)
 get an msrun reader by finding the run_id in file More...
 
TimsMsRunReaderMs2SPtr buildTimsMsRunReaderMs2SPtr ()
 if possible, builds directly a dedicated Tims TOF tdf file reader More...
 

Static Public Member Functions

static MsRunReaderSPtr buildMsRunReaderSPtr (MsRunIdCstSPtr ms_run_id)
 get an MsRunReader directly from a valid MsRun ID More...
 

Private Attributes

QString m_fileName
 
const QString m_xmlPrefix
 
MzFormat m_fileFormat = MzFormat::unknown
 
FileReaderType m_fileReaderType
 
std::map< MzFormat, FileReaderTypem_preferedFileReaderTypeMap
 

Detailed Description

Definition at line 34 of file msfileaccessor.h.

Constructor & Destructor Documentation

◆ MsFileAccessor() [1/2]

pappso::MsFileAccessor::MsFileAccessor ( const QString &  file_name,
const QString &  xml_prefix 
)

Definition at line 28 of file msfileaccessor.cpp.

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 }

◆ MsFileAccessor() [2/2]

pappso::MsFileAccessor::MsFileAccessor ( const MsFileAccessor other)

Definition at line 39 of file msfileaccessor.cpp.

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 }

◆ ~MsFileAccessor()

pappso::MsFileAccessor::~MsFileAccessor ( )
virtual

Definition at line 47 of file msfileaccessor.cpp.

48 {
49 }

Member Function Documentation

◆ buildMsRunReaderSPtr()

MsRunReaderSPtr pappso::MsFileAccessor::buildMsRunReaderSPtr ( MsRunIdCstSPtr  ms_run_id)
static

get an MsRunReader directly from a valid MsRun ID

no need to check the file format or filename : all is already part of the msrunid

Definition at line 216 of file msfileaccessor.cpp.

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 }

References pappso::brukerTims, pappso::unknown, and pappso::xy.

◆ buildTimsMsRunReaderMs2SPtr()

TimsMsRunReaderMs2SPtr pappso::MsFileAccessor::buildTimsMsRunReaderMs2SPtr ( )

if possible, builds directly a dedicated Tims TOF tdf file reader

Definition at line 135 of file msfileaccessor.cpp.

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 }

References pappso::TimsMsFileReader::getFileFormat(), pappso::TimsMsFileReader::getMsRunIds(), m_fileFormat, m_fileName, m_fileReaderType, m_xmlPrefix, and pappso::tims_ms2.

◆ getFileFormat()

MzFormat pappso::MsFileAccessor::getFileFormat ( ) const

get the raw format of mz data

Definition at line 60 of file msfileaccessor.cpp.

61 {
62  return m_fileFormat;
63 }

References m_fileFormat.

◆ getFileName()

const QString & pappso::MsFileAccessor::getFileName ( ) const

Definition at line 53 of file msfileaccessor.cpp.

54 {
55  return m_fileName;
56 }

References m_fileName.

◆ getMsRunIds()

std::vector< MsRunIdCstSPtr > pappso::MsFileAccessor::getMsRunIds ( )

Definition at line 67 of file msfileaccessor.cpp.

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 }

References pappso::XyMsFileReader::getFileFormat(), pappso::PwizMsFileReader::getFileFormat(), pappso::TimsMsFileReader::getFileFormat(), pappso::XyMsFileReader::getMsRunIds(), pappso::PwizMsFileReader::getMsRunIds(), pappso::TimsMsFileReader::getMsRunIds(), m_fileFormat, m_fileName, m_fileReaderType, m_preferedFileReaderTypeMap, m_xmlPrefix, pappso::pwiz, pappso::tims, and pappso::xy.

Referenced by getMsRunReaderSPtrByRunId().

◆ getMsRunReaderSPtrByRunId()

MsRunReaderSPtr pappso::MsFileAccessor::getMsRunReaderSPtrByRunId ( const QString &  run_id,
const QString &  xml_id 
)

get an msrun reader by finding the run_id in file

Parameters
run_ididentifier within file of the MSrun
xml_idXML identifier given by the user to identify this MSrun in our experiment (not in the file)

Definition at line 253 of file msfileaccessor.cpp.

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 }

References getMsRunIds(), m_fileName, msRunReaderSp(), and pappso::MsRunId::setXmlId().

◆ msRunReaderSp()

MsRunReaderSPtr pappso::MsFileAccessor::msRunReaderSp ( MsRunIdCstSPtr  ms_run_id)

Definition at line 167 of file msfileaccessor.cpp.

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 }

References m_fileFormat, m_fileName, m_fileReaderType, pappso::pwiz, pappso::tims, pappso::tims_ms2, pappso::unknown, and pappso::xy.

Referenced by getMsRunReaderSPtrByRunId().

◆ setPreferedFileReaderType()

void pappso::MsFileAccessor::setPreferedFileReaderType ( MzFormat  format,
FileReaderType  reader_type 
)

given an mz format, explicitly set the prefered reader

Definition at line 290 of file msfileaccessor.cpp.

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 }

References m_preferedFileReaderTypeMap.

Member Data Documentation

◆ m_fileFormat

MzFormat pappso::MsFileAccessor::m_fileFormat = MzFormat::unknown
private

◆ m_fileName

QString pappso::MsFileAccessor::m_fileName
private

◆ m_fileReaderType

FileReaderType pappso::MsFileAccessor::m_fileReaderType
private

Definition at line 85 of file msfileaccessor.h.

Referenced by buildTimsMsRunReaderMs2SPtr(), getMsRunIds(), and msRunReaderSp().

◆ m_preferedFileReaderTypeMap

std::map<MzFormat, FileReaderType> pappso::MsFileAccessor::m_preferedFileReaderTypeMap
private

Definition at line 87 of file msfileaccessor.h.

Referenced by getMsRunIds(), and setPreferedFileReaderType().

◆ m_xmlPrefix

const QString pappso::MsFileAccessor::m_xmlPrefix
private

Definition at line 80 of file msfileaccessor.h.

Referenced by buildTimsMsRunReaderMs2SPtr(), and getMsRunIds().


The documentation for this class was generated from the following files:
pappso::MsFileAccessor::m_preferedFileReaderTypeMap
std::map< MzFormat, FileReaderType > m_preferedFileReaderTypeMap
Definition: msfileaccessor.h:87
pappso::MzFormat::brukerTims
pappso::FileReaderType::xy
pappso::MsFileAccessor::m_fileFormat
MzFormat m_fileFormat
Definition: msfileaccessor.h:82
pappso::FileReaderType::tims
pappso::MsFileAccessor::m_fileReaderType
FileReaderType m_fileReaderType
Definition: msfileaccessor.h:85
pappso::MsRunReaderSPtr
std::shared_ptr< MsRunReader > MsRunReaderSPtr
Definition: msrunreader.h:168
pappso::FileReaderType::pwiz
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:63
pappso::MzFormat::unknown
unknown format
pappso::MsFileAccessor::m_xmlPrefix
const QString m_xmlPrefix
Definition: msfileaccessor.h:80
pappso::FileReaderType::tims_ms2
pappso::MsFileAccessor::getMsRunIds
std::vector< MsRunIdCstSPtr > getMsRunIds()
Definition: msfileaccessor.cpp:67
pappso::MsFileAccessor::msRunReaderSp
MsRunReaderSPtr msRunReaderSp(MsRunIdCstSPtr ms_run_id)
Definition: msfileaccessor.cpp:167
pappso::MsFileAccessor::m_fileName
QString m_fileName
Definition: msfileaccessor.h:76
pappso::MzFormat
MzFormat
Definition: types.h:125