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"
21
22
23namespace pappso
24{
25
27{
28 // Run the initialization function that checks that the file exists!
29
30 initialize();
31}
32
33
34void
36{
37 // qDebug();
38
39 if(!QFileInfo(mcsp_msRunId->getFileName()).exists())
41 QObject::tr("Xy MS file %1 not found\n").arg(mcsp_msRunId->getFileName()));
42 // qDebug();
43}
44
45
49
50
51bool
52XyMsRunReader::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() || Utils::endOfLineRegExp.match(line).hasMatch())
80 continue;
81
82 // qDebug() << __FILE__ << __LINE__ << "Current xy format line:" << line;
83
84 if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
85 continue;
86 else
87 {
88 file_reading_failed = true;
89 break;
90 }
91 }
92
93 file.close();
94
95 if(!file_reading_failed && line_count >= 1)
96 return true;
97
98 return false;
99}
100
101
103XyMsRunReader::massSpectrumSPtr(std::size_t spectrum_index)
104{
105 return qualifiedMassSpectrum(spectrum_index).getMassSpectrumSPtr();
106}
107
108
110XyMsRunReader::massSpectrumCstSPtr(std::size_t spectrum_index)
111{
112 return qualifiedMassSpectrum(spectrum_index).getMassSpectrumCstSPtr();
113}
114
115
118{
119 // qDebug();
120
121 // This is a file that contains a single spectrum. Just iterate in the various
122 // lines and convert them to DataPoint objects that are fed to the mass
123 // spectrum.
124
125 QualifiedMassSpectrum qualified_mass_spectrum(mass_spectrum_id);
126
127 // Set manually data that are necessary for the correct use of this mass
128 // spectrum in the MS data set tree node.
129 qualified_mass_spectrum.setMsLevel(1);
130 qualified_mass_spectrum.setRtInSeconds(0);
131
132 MassSpectrum mass_spectrum;
133
134 QFile file(mcsp_msRunId->getFileName());
135
136 if(!file.exists())
137 {
138 // qDebug() << "File" << mcsp_msRunId->getFileName() << "does not exist.";
139
140 return qualified_mass_spectrum;
141 }
142
143 if(!file.open(QFile::ReadOnly | QFile::Text))
144 {
145 // qDebug() << "Failed to open file" << mcsp_msRunId->getFileName();
146
147 return qualified_mass_spectrum;
148 }
149
150 QRegularExpressionMatch regExpMatch;
151
152 QString line;
153
154 while(!file.atEnd())
155 {
156 line = file.readLine();
157
158 if(line.startsWith('#') || line.isEmpty() || Utils::endOfLineRegExp.match(line).hasMatch())
159 continue;
160
161 if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
162 {
163 pappso_double x = -1;
164 pappso_double y = -1;
165
166 QRegularExpressionMatch regExpMatch = Utils::xyMassDataFormatRegExp.match(line);
167
168 if(!regExpMatch.hasMatch())
170 QObject::tr("Failed to create data point with line %1.\n").arg(line));
171
172 bool ok = false;
173
174 x = regExpMatch.captured(1).toDouble(&ok);
175
176 if(!ok)
178 QObject::tr("Failed to create data point with line %1.\n").arg(line));
179
180 // Note that group 2 is the separator group.
181
182 y = regExpMatch.captured(3).toDouble(&ok);
183
184 if(!ok)
186 QObject::tr("Failed to create data point with line %1.\n").arg(line));
187
188 // FIXME: what is this data point for if we use emplace_back
189 // right below ?
190 DataPoint data_point(x, y);
191
192 mass_spectrum.emplace_back(x, y);
193 }
194 }
195
196 file.close();
197
198 MassSpectrumSPtr spectrum_sp = mass_spectrum.makeMassSpectrumSPtr();
199 qualified_mass_spectrum.setMassSpectrumSPtr(spectrum_sp);
200
201 // qDebug() << "the qualified mass spectrum has size:"
202 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
203
204 return qualified_mass_spectrum;
205}
206
207
209XyMsRunReader::qualifiedMassSpectrum([[maybe_unused]] std::size_t spectrum_index,
210 bool want_binary_data) const
211{
212 // qDebug();
213
214 // In reality there is only one mass spectrum in the file, so we do not use
215 // spectrum_index, but use 0 instead.
216
217 MassSpectrumId massSpectrumId(mcsp_msRunId, 0);
218
219 QualifiedMassSpectrum qualified_mass_spectrum =
221
222 // qDebug() << "qualified mass spectrum has size:"
223 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
224
225 // We also do not abide by the want_binary_data parameter because in this XY
226 // mass spec data file loading process we actually want the data.
227 if(!want_binary_data)
228 {
229 // qualified_mass_spectrum.setMassSpectrumSPtr(nullptr);
230 }
231
232 return qualified_mass_spectrum;
233}
234
235
236void
238{
239 // qDebug();
240
241 // In reality there is only one mass spectrum in the file.
242
243 MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
244
245 QualifiedMassSpectrum qualified_mass_spectrum =
247
248 // qDebug() << "qualified mass spectrum has size:"
249 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
250
251 // The handler will receive the index of the mass spectrum in the
252 // current run via the mass spectrum id member datum.
253 handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
254
255 // qDebug() << "Loading ended";
256 handler.loadingEnded();
257}
258
259
260void
262 unsigned int ms_level)
263{
264 // qDebug();
265
266 // In reality there is only one mass spectrum in the file.
267
268 MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
269
270 QualifiedMassSpectrum qualified_mass_spectrum =
272
273 // qDebug() << "qualified mass spectrum has size:"
274 //<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
275
276 // The handler will receive the index of the mass spectrum in the
277 // current run via the mass spectrum id member datum.
278
279 if(qualified_mass_spectrum.getMsLevel() == ms_level)
280 {
281 handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
282 }
283
284 // qDebug() << "Loading ended";
285 handler.loadingEnded();
286}
287
288void
291{
292 return readSpectrumCollection(handler);
293}
294
295std::size_t
297{
298 return 1;
299}
300
301bool
303{
304 return true;
305}
306
307bool
309{
310 return true;
311}
312
314XyMsRunReader::newXicCoordSPtrFromSpectrumIndex(std::size_t spectrum_index [[maybe_unused]],
315 pappso::PrecisionPtr precision
316 [[maybe_unused]]) const
317{
319 QObject::tr("Not implemented %1 %2 %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
320}
321
324 const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
325 pappso::PrecisionPtr precision [[maybe_unused]]) const
326{
328 QObject::tr("Not implemented %1 %2 %3").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
329}
330
331
332std::size_t
334 [[maybe_unused]])
335{
337 QObject::tr("%1 %2 %3 not implemented").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
338}
339
340} // namespace pappso
Class to represent a mass spectrum.
MassSpectrumSPtr makeMassSpectrumSPtr() const
MsRunIdCstSPtr mcsp_msRunId
MsRunReader(const MsRunIdCstSPtr &ms_run_id)
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:60
static QRegularExpression endOfLineRegExp
Regular expression that tracks the end of line in text files.
Definition utils.h:69
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 spectrumStringIdentifier2SpectrumIndex(const QString &spectrum_identifier) override
if possible, get the spectrum index given a string identifier throw a not found exception if spectrum...
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:61
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
const PrecisionBase * PrecisionPtr
Definition precision.h:122
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
std::shared_ptr< XicCoord > XicCoordSPtr
Definition xiccoord.h:44