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