libpappsomspp
Library for mass spectrometry
Loading...
Searching...
No Matches
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"
21
22
23namespace 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
35void
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
50
51
52bool
53XyMsRunReader::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
105XyMsRunReader::massSpectrumSPtr(std::size_t spectrum_index)
106{
107 return qualifiedMassSpectrum(spectrum_index).getMassSpectrumSPtr();
108}
109
110
112XyMsRunReader::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 =
172
173 if(!regExpMatch.hasMatch())
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)
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)
193 QObject::tr("Failed to create data point with line %1.\n")
194 .arg(line));
195
196 // FIXME: what is this data point for if we use emplace_back
197 // right below ?
198 DataPoint data_point(x, y);
199
200 mass_spectrum.emplace_back(x, y);
201 }
202 }
203
204 file.close();
205
206 MassSpectrumSPtr spectrum_sp = mass_spectrum.makeMassSpectrumSPtr();
207 qualified_mass_spectrum.setMassSpectrumSPtr(spectrum_sp);
208
209 // qDebug() << "the qualified mass spectrum has size:"
210 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
211
212 return qualified_mass_spectrum;
213}
214
215
218 [[maybe_unused]] std::size_t spectrum_index, bool want_binary_data) const
219{
220 // qDebug();
221
222 // In reality there is only one mass spectrum in the file, so we do not use
223 // spectrum_index, but use 0 instead.
224
225 MassSpectrumId massSpectrumId(mcsp_msRunId, 0);
226
227 QualifiedMassSpectrum qualified_mass_spectrum =
229
230 // qDebug() << "qualified mass spectrum has size:"
231 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
232
233 // We also do not abide by the want_binary_data parameter because in this XY
234 // mass spec data file loading process we actually want the data.
235 if(!want_binary_data)
236 {
237 // qualified_mass_spectrum.setMassSpectrumSPtr(nullptr);
238 }
239
240 return qualified_mass_spectrum;
241}
242
243
244void
247{
248 // qDebug();
249
250 // In reality there is only one mass spectrum in the file.
251
252 MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
253
254 QualifiedMassSpectrum qualified_mass_spectrum =
256
257 // qDebug() << "qualified mass spectrum has size:"
258 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
259
260 // The handler will receive the index of the mass spectrum in the
261 // current run via the mass spectrum id member datum.
262 handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
263
264 // qDebug() << "Loading ended";
265 handler.loadingEnded();
266}
267
268
269void
271 SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
272{
273 // qDebug();
274
275 // In reality there is only one mass spectrum in the file.
276
277 MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
278
279 QualifiedMassSpectrum qualified_mass_spectrum =
281
282 // qDebug() << "qualified mass spectrum has size:"
283 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
284
285 // The handler will receive the index of the mass spectrum in the
286 // current run via the mass spectrum id member datum.
287
288 if(qualified_mass_spectrum.getMsLevel() == ms_level)
289 {
290 handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
291 }
292
293 // qDebug() << "Loading ended";
294 handler.loadingEnded();
295}
296
297void
299 [[maybe_unused]] const MsRunReadConfig &config,
301{
302 return readSpectrumCollection(handler);
303}
304
305std::size_t
307{
308 return 1;
309}
310
311bool
313{
314 return true;
315}
316
317bool
319{
320 return true;
321}
322
325 [[maybe_unused]],
326 pappso::PrecisionPtr precision
327 [[maybe_unused]]) const
328{
329 throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
330 .arg(__FILE__)
331 .arg(__FUNCTION__)
332 .arg(__LINE__));
333}
334
337 const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
338 pappso::PrecisionPtr precision [[maybe_unused]]) const
339{
340 throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
341 .arg(__FILE__)
342 .arg(__FUNCTION__)
343 .arg(__LINE__));
344}
345
346} // namespace pappso
Class to represent a mass spectrum.
MassSpectrumSPtr makeMassSpectrumSPtr() const
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition msrunreader.h:63
MsRunIdCstSPtr mcsp_msRunId
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
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
static QRegularExpression xyMassDataFormatRegExp
Definition utils.h:59
static QRegularExpression endOfLineRegExp
Regular expression that tracks the end of line in text files.
Definition utils.h:68
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 void readSpectrumCollection2(const MsRunReadConfig &config, SpectrumCollectionHandlerInterface &handler) override
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:46
double pappso_double
A type definition for doubles.
Definition types.h:50
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
std::shared_ptr< XicCoord > XicCoordSPtr
Definition xiccoord.h:43