libpappsomspp
Library for mass spectrometry
xymsrunreader.cpp
Go to the documentation of this file.
1 
2 /////////////////////// StdLib includes
3 
4 
5 /////////////////////// Qt includes
6 #include <QDebug>
7 #include <QFileInfo>
8 
9 
10 /////////////////////// libpwiz includes
11 #include <pwiz/data/msdata/DefaultReaderList.hpp>
12 
13 
14 /////////////////////// Local includes
15 #include "xymsrunreader.h"
16 #include "../utils.h"
17 #include "../pappsoexception.h"
18 #include "../exception/exceptionnotfound.h"
19 #include "../exception/exceptionnotpossible.h"
20 #include "../exception/exceptionnotimplemented.h"
21 
22 
23 namespace pappso
24 {
25 
27  : pappso::MsRunReader(msrun_id_csp)
28 {
29  // Run the initialization function that checks that the file exists!
30 
31  initialize();
32 }
33 
34 
35 void
37 {
38  // qDebug();
39 
40  if(!QFileInfo(mcsp_msRunId->getFileName()).exists())
41  throw ExceptionNotFound(QObject::tr("Xy MS file %1 not found\n")
42  .arg(mcsp_msRunId->getFileName()));
43  // qDebug();
44 }
45 
46 
48 {
49 }
50 
51 
52 bool
53 XyMsRunReader::accept(const QString &file_name) const
54 {
55 
56  // Here we just test all the lines of the file to check that they comply with
57  // the xy format.
58 
59  std::size_t line_count = 0;
60 
61  QFile file(file_name);
62 
63  if(!file.open(QFile::ReadOnly | QFile::Text))
64  {
65  qDebug() << __FILE__ << __LINE__ << "Failed to open file" << file_name;
66 
67  return false;
68  }
69 
70  QRegularExpressionMatch regExpMatch;
71 
72  QString line;
73  bool file_reading_failed = false;
74 
75  while(!file.atEnd())
76  {
77  line = file.readLine();
78  ++line_count;
79 
80  if(line.startsWith('#') || line.isEmpty() ||
81  Utils::endOfLineRegExp.match(line).hasMatch())
82  continue;
83 
84  // qDebug() << __FILE__ << __LINE__ << "Current xy format line:" << line;
85 
86  if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
87  continue;
88  else
89  {
90  file_reading_failed = true;
91  break;
92  }
93  }
94 
95  file.close();
96 
97  if(!file_reading_failed && line_count >= 1)
98  return true;
99 
100  return false;
101 }
102 
103 
105 XyMsRunReader::massSpectrumSPtr(std::size_t spectrum_index)
106 {
107  return qualifiedMassSpectrum(spectrum_index).getMassSpectrumSPtr();
108 }
109 
110 
112 XyMsRunReader::massSpectrumCstSPtr(std::size_t spectrum_index)
113 {
114  return qualifiedMassSpectrum(spectrum_index).getMassSpectrumCstSPtr();
115 }
116 
117 
120  MassSpectrumId mass_spectrum_id) const
121 {
122  // qDebug();
123 
124  // This is a file that contains a single spectrum. Just iterate in the various
125  // lines and convert them to DataPoint objects that are fed to the mass
126  // spectrum.
127 
128  QualifiedMassSpectrum qualified_mass_spectrum(mass_spectrum_id);
129 
130  // Set manually data that are necessary for the correct use of this mass
131  // spectrum in the MS data set tree node.
132  qualified_mass_spectrum.setMsLevel(1);
133  qualified_mass_spectrum.setRtInSeconds(0);
134 
135  MassSpectrum mass_spectrum;
136 
137  QFile file(mcsp_msRunId->getFileName());
138 
139  if(!file.exists())
140  {
141  // qDebug() << "File" << mcsp_msRunId->getFileName() << "does not exist.";
142 
143  return qualified_mass_spectrum;
144  }
145 
146  if(!file.open(QFile::ReadOnly | QFile::Text))
147  {
148  // qDebug() << "Failed to open file" << mcsp_msRunId->getFileName();
149 
150  return qualified_mass_spectrum;
151  }
152 
153  QRegularExpressionMatch regExpMatch;
154 
155  QString line;
156 
157  while(!file.atEnd())
158  {
159  line = file.readLine();
160 
161  if(line.startsWith('#') || line.isEmpty() ||
162  Utils::endOfLineRegExp.match(line).hasMatch())
163  continue;
164 
165  if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
166  {
167  pappso_double x = -1;
168  pappso_double y = -1;
169 
170  QRegularExpressionMatch regExpMatch =
171  Utils::xyMassDataFormatRegExp.match(line);
172 
173  if(!regExpMatch.hasMatch())
174  throw ExceptionNotPossible(
175  QObject::tr("Failed to create data point with line %1.\n")
176  .arg(line));
177 
178  bool ok = false;
179 
180  x = regExpMatch.captured(1).toDouble(&ok);
181 
182  if(!ok)
183  throw ExceptionNotPossible(
184  QObject::tr("Failed to create data point with line %1.\n")
185  .arg(line));
186 
187  // Note that group 2 is the separator group.
188 
189  y = regExpMatch.captured(3).toDouble(&ok);
190 
191  if(!ok)
192  throw ExceptionNotPossible(
193  QObject::tr("Failed to create data point with line %1.\n")
194  .arg(line));
195 
196  DataPoint data_point(x, y);
197 
198  mass_spectrum.emplace_back(x, y);
199  }
200  }
201 
202  file.close();
203 
204  MassSpectrumSPtr spectrum_sp = mass_spectrum.makeMassSpectrumSPtr();
205  qualified_mass_spectrum.setMassSpectrumSPtr(spectrum_sp);
206 
207  // qDebug() << "the qualified mass spectrum has size:"
208  //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
209 
210  return qualified_mass_spectrum;
211 }
212 
213 
216  [[maybe_unused]] std::size_t spectrum_index, bool want_binary_data) const
217 {
218  // qDebug();
219 
220  // In reality there is only one mass spectrum in the file, so we do not use
221  // spectrum_index, but use 0 instead.
222 
223  MassSpectrumId massSpectrumId(mcsp_msRunId, 0);
224 
225  QualifiedMassSpectrum qualified_mass_spectrum =
227 
228  // qDebug() << "qualified mass spectrum has size:"
229  //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
230 
231  // We also do not abide by the want_binary_data parameter because in this XY
232  // mass spec data file loading process we actually want the data.
233  if(!want_binary_data)
234  {
235  // qualified_mass_spectrum.setMassSpectrumSPtr(nullptr);
236  }
237 
238  return qualified_mass_spectrum;
239 }
240 
241 
242 void
245 {
246  // qDebug();
247 
248  // In reality there is only one mass spectrum in the file.
249 
250  MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
251 
252  QualifiedMassSpectrum qualified_mass_spectrum =
254 
255  // qDebug() << "qualified mass spectrum has size:"
256  //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
257 
258  // The handler will receive the index of the mass spectrum in the
259  // current run via the mass spectrum id member datum.
260  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
261 
262  // qDebug() << "Loading ended";
263  handler.loadingEnded();
264 }
265 
266 
267 void
269  SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
270 {
271  // qDebug();
272 
273  // In reality there is only one mass spectrum in the file.
274 
275  MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
276 
277  QualifiedMassSpectrum qualified_mass_spectrum =
279 
280  // qDebug() << "qualified mass spectrum has size:"
281  //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
282 
283  // The handler will receive the index of the mass spectrum in the
284  // current run via the mass spectrum id member datum.
285 
286  if(qualified_mass_spectrum.getMsLevel() == ms_level)
287  {
288  handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
289  }
290 
291  // qDebug() << "Loading ended";
292  handler.loadingEnded();
293 }
294 
295 std::size_t
297 {
298  return 1;
299 }
300 
301 bool
303 {
304  return true;
305 }
306 
307 bool
309 {
310  return true;
311 }
312 
315  [[maybe_unused]],
316  pappso::PrecisionPtr precision
317  [[maybe_unused]]) const
318 {
319  throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
320  .arg(__FILE__)
321  .arg(__FUNCTION__)
322  .arg(__LINE__));
323 }
324 
327  const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
328  pappso::PrecisionPtr precision [[maybe_unused]]) const
329 {
330  throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
331  .arg(__FILE__)
332  .arg(__FUNCTION__)
333  .arg(__LINE__));
334 }
335 
336 } // namespace pappso
Class to represent a mass spectrum.
Definition: massspectrum.h:71
MassSpectrumSPtr makeMassSpectrumSPtr() const
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:192
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:301
Class representing a fully specified mass spectrum.
uint getMsLevel() const
Get the mass spectrum level.
MassSpectrumCstSPtr getMassSpectrumCstSPtr() const
Get the MassSpectrumCstSPtr.
void setMsLevel(uint ms_level)
Set the mass spectrum level.
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
interface to collect spectrums from the MsRunReader class
Definition: msrunreader.h:56
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
static QRegularExpression xyMassDataFormatRegExp
Definition: utils.h:53
static QRegularExpression endOfLineRegExp
Regular expression that tracks the end of line in text files.
Definition: utils.h:62
virtual pappso::XicCoordSPtr newXicCoordSPtrFromQualifiedMassSpectrum(const pappso::QualifiedMassSpectrum &mass_spectrum, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum
QualifiedMassSpectrum qualifiedMassSpectrumFromXyMSDataFile(MassSpectrumId mass_spectrum_id) const
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
virtual bool acquireDevice() override
acquire data back end device
virtual bool accept(const QString &file_name) const override
tells if the reader is able to handle this file must be implemented by private MS run reader,...
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
virtual void readSpectrumCollectionByMsLevel(SpectrumCollectionHandlerInterface &handler, unsigned int ms_level) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler by Ms Levels
virtual bool releaseDevice() override
release data back end device if a the data back end is released, the developper has to use acquireDev...
XyMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
virtual void initialize() override
virtual pappso::XicCoordSPtr newXicCoordSPtrFromSpectrumIndex(std::size_t spectrum_index, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum index
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
virtual QualifiedMassSpectrum qualifiedMassSpectrum(std::size_t spectrum_index, bool want_binary_data=true) const override
get a QualifiedMassSpectrum class given its scan number
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
double pappso_double
A type definition for doubles.
Definition: types.h:49
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition: massspectrum.h:55
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition: massspectrum.h:54
std::shared_ptr< XicCoord > XicCoordSPtr
Definition: xiccoord.h:41