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