Loading...
Searching...
No Matches
xymsrunreader.cpp
Go to the documentation of this file.
/////////////////////// StdLib includes
/////////////////////// Qt includes
#include <QDebug>
#include <QFileInfo>
/////////////////////// libpwiz includes
#include <pwiz/data/msdata/DefaultReaderList.hpp>
/////////////////////// Local includes
#include "xymsrunreader.h"
#include "../utils.h"
#include "../pappsoexception.h"
#include "../exception/exceptionnotfound.h"
#include "../exception/exceptionnotpossible.h"
#include "../exception/exceptionnotimplemented.h"
namespace pappso
{
XyMsRunReader::XyMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
: pappso::MsRunReader(msrun_id_csp)
{
// Run the initialization function that checks that the file exists!
initialize();
}
void
XyMsRunReader::initialize()
{
// qDebug();
if(!QFileInfo(mcsp_msRunId->getFileName()).exists())
throw ExceptionNotFound(QObject::tr("Xy MS file %1 not found\n")
.arg(mcsp_msRunId->getFileName()));
// qDebug();
}
XyMsRunReader::~XyMsRunReader()
{
}
bool
XyMsRunReader::accept(const QString &file_name) const
{
// Here we just test all the lines of the file to check that they comply with
// the xy format.
std::size_t line_count = 0;
QFile file(file_name);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << __FILE__ << __LINE__ << "Failed to open file" << file_name;
return false;
}
QRegularExpressionMatch regExpMatch;
QString line;
bool file_reading_failed = false;
while(!file.atEnd())
{
line = file.readLine();
++line_count;
if(line.startsWith('#') || line.isEmpty() ||
Utils::endOfLineRegExp.match(line).hasMatch())
continue;
// qDebug() << __FILE__ << __LINE__ << "Current xy format line:" << line;
if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
continue;
else
{
file_reading_failed = true;
break;
}
}
file.close();
if(!file_reading_failed && line_count >= 1)
return true;
return false;
}
pappso::MassSpectrumSPtr
XyMsRunReader::massSpectrumSPtr(std::size_t spectrum_index)
{
return qualifiedMassSpectrum(spectrum_index).getMassSpectrumSPtr();
}
pappso::MassSpectrumCstSPtr
XyMsRunReader::massSpectrumCstSPtr(std::size_t spectrum_index)
{
return qualifiedMassSpectrum(spectrum_index).getMassSpectrumCstSPtr();
}
QualifiedMassSpectrum
XyMsRunReader::qualifiedMassSpectrumFromXyMSDataFile(
MassSpectrumId mass_spectrum_id) const
{
// qDebug();
// This is a file that contains a single spectrum. Just iterate in the various
// lines and convert them to DataPoint objects that are fed to the mass
// spectrum.
QualifiedMassSpectrum qualified_mass_spectrum(mass_spectrum_id);
// Set manually data that are necessary for the correct use of this mass
// spectrum in the MS data set tree node.
qualified_mass_spectrum.setMsLevel(1);
qualified_mass_spectrum.setRtInSeconds(0);
MassSpectrum mass_spectrum;
QFile file(mcsp_msRunId->getFileName());
if(!file.exists())
{
// qDebug() << "File" << mcsp_msRunId->getFileName() << "does not exist.";
return qualified_mass_spectrum;
}
if(!file.open(QFile::ReadOnly | QFile::Text))
{
// qDebug() << "Failed to open file" << mcsp_msRunId->getFileName();
return qualified_mass_spectrum;
}
QRegularExpressionMatch regExpMatch;
QString line;
while(!file.atEnd())
{
line = file.readLine();
if(line.startsWith('#') || line.isEmpty() ||
Utils::endOfLineRegExp.match(line).hasMatch())
continue;
if(Utils::xyMassDataFormatRegExp.match(line).hasMatch())
{
pappso_double x = -1;
pappso_double y = -1;
QRegularExpressionMatch regExpMatch =
Utils::xyMassDataFormatRegExp.match(line);
if(!regExpMatch.hasMatch())
throw ExceptionNotPossible(
QObject::tr("Failed to create data point with line %1.\n")
.arg(line));
bool ok = false;
x = regExpMatch.captured(1).toDouble(&ok);
if(!ok)
throw ExceptionNotPossible(
QObject::tr("Failed to create data point with line %1.\n")
.arg(line));
// Note that group 2 is the separator group.
y = regExpMatch.captured(3).toDouble(&ok);
if(!ok)
throw ExceptionNotPossible(
QObject::tr("Failed to create data point with line %1.\n")
.arg(line));
DataPoint data_point(x, y);
mass_spectrum.emplace_back(x, y);
}
}
file.close();
MassSpectrumSPtr spectrum_sp = mass_spectrum.makeMassSpectrumSPtr();
qualified_mass_spectrum.setMassSpectrumSPtr(spectrum_sp);
// qDebug() << "the qualified mass spectrum has size:"
//<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
return qualified_mass_spectrum;
}
QualifiedMassSpectrum
XyMsRunReader::qualifiedMassSpectrum(
[[maybe_unused]] std::size_t spectrum_index, bool want_binary_data) const
{
// qDebug();
// In reality there is only one mass spectrum in the file, so we do not use
// spectrum_index, but use 0 instead.
MassSpectrumId massSpectrumId(mcsp_msRunId, 0);
QualifiedMassSpectrum qualified_mass_spectrum =
qualifiedMassSpectrumFromXyMSDataFile(massSpectrumId);
// qDebug() << "qualified mass spectrum has size:"
//<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
// We also do not abide by the want_binary_data parameter because in this XY
// mass spec data file loading process we actually want the data.
if(!want_binary_data)
{
// qualified_mass_spectrum.setMassSpectrumSPtr(nullptr);
}
return qualified_mass_spectrum;
}
void
XyMsRunReader::readSpectrumCollection(
SpectrumCollectionHandlerInterface &handler)
{
// qDebug();
// In reality there is only one mass spectrum in the file.
MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
QualifiedMassSpectrum qualified_mass_spectrum =
qualifiedMassSpectrumFromXyMSDataFile(massSpectrumId);
// qDebug() << "qualified mass spectrum has size:"
//<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
// The handler will receive the index of the mass spectrum in the
// current run via the mass spectrum id member datum.
handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
// qDebug() << "Loading ended";
handler.loadingEnded();
}
void
XyMsRunReader::readSpectrumCollectionByMsLevel(
SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)
{
// qDebug();
// In reality there is only one mass spectrum in the file.
MassSpectrumId massSpectrumId(mcsp_msRunId, 0 /* spectrum index*/);
QualifiedMassSpectrum qualified_mass_spectrum =
qualifiedMassSpectrumFromXyMSDataFile(massSpectrumId);
// qDebug() << "qualified mass spectrum has size:"
//<< qualified_mass_spectrum.getMassSpectrumSPtr()->size();
// The handler will receive the index of the mass spectrum in the
// current run via the mass spectrum id member datum.
if(qualified_mass_spectrum.getMsLevel() == ms_level)
{
handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
}
// qDebug() << "Loading ended";
handler.loadingEnded();
}
void
XyMsRunReader::readSpectrumCollection2(
[[maybe_unused]] const MsRunReadConfig &config,
SpectrumCollectionHandlerInterface &handler)
{
return readSpectrumCollection(handler);
}
std::size_t
XyMsRunReader::spectrumListSize() const
{
return 1;
}
bool
XyMsRunReader::releaseDevice()
{
return true;
}
bool
XyMsRunReader::acquireDevice()
{
return true;
}
XicCoordSPtr
XyMsRunReader::newXicCoordSPtrFromSpectrumIndex(std::size_t spectrum_index
[[maybe_unused]],
pappso::PrecisionPtr precision
[[maybe_unused]]) const
{
throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
.arg(__FILE__)
.arg(__FUNCTION__)
.arg(__LINE__));
}
XicCoordSPtr
XyMsRunReader::newXicCoordSPtrFromQualifiedMassSpectrum(
const pappso::QualifiedMassSpectrum &mass_spectrum [[maybe_unused]],
pappso::PrecisionPtr precision [[maybe_unused]]) const
{
throw ExceptionNotImplemented(QObject::tr("Not implemented %1 %2 %3")
.arg(__FILE__)
.arg(__FUNCTION__)
.arg(__LINE__));
}
} // namespace pappso
CardanoResultCase::line
@ line
pappso::ExceptionNotFound
Definition exceptionnotfound.h:32
pappso::ExceptionNotImplemented
Definition exceptionnotimplemented.h:32
pappso::ExceptionNotPossible
Definition exceptionnotpossible.h:29
pappso::MassSpectrumId
Definition massspectrumid.h:40
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition massspectrum.h:73
pappso::MassSpectrum::makeMassSpectrumSPtr
MassSpectrumSPtr makeMassSpectrumSPtr() const
Definition massspectrum.cpp:131
pappso::MsRunReadConfig
Definition msrunreadconfig.h:52
pappso::MsRunReader
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition msrunreader.h:63
pappso::MsRunReader::mcsp_msRunId
MsRunIdCstSPtr mcsp_msRunId
Definition msrunreader.h:175
pappso::PrecisionBase
Definition precision.h:44
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition qualifiedmassspectrum.h:93
pappso::QualifiedMassSpectrum::getMsLevel
uint getMsLevel() const
Get the mass spectrum level.
Definition qualifiedmassspectrum.cpp:187
pappso::QualifiedMassSpectrum::getMassSpectrumCstSPtr
MassSpectrumCstSPtr getMassSpectrumCstSPtr() const
Get the MassSpectrumCstSPtr.
Definition qualifiedmassspectrum.cpp:141
pappso::QualifiedMassSpectrum::setMsLevel
void setMsLevel(uint ms_level)
Set the mass spectrum level.
Definition qualifiedmassspectrum.cpp:179
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition qualifiedmassspectrum.cpp:133
pappso::QualifiedMassSpectrum::setMassSpectrumSPtr
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
Definition qualifiedmassspectrum.cpp:125
pappso::QualifiedMassSpectrum::setRtInSeconds
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
Definition qualifiedmassspectrum.cpp:195
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition spectrumcollectionhandlerinterface.h:56
pappso::SpectrumCollectionHandlerInterface::loadingEnded
virtual void loadingEnded()
Definition spectrumcollectionhandlerinterface.cpp:43
pappso::SpectrumCollectionHandlerInterface::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
pappso::Utils::xyMassDataFormatRegExp
static QRegularExpression xyMassDataFormatRegExp
Definition utils.h:54
pappso::Utils::endOfLineRegExp
static QRegularExpression endOfLineRegExp
Regular expression that tracks the end of line in text files.
Definition utils.h:63
pappso::XyMsRunReader::newXicCoordSPtrFromQualifiedMassSpectrum
virtual pappso::XicCoordSPtr newXicCoordSPtrFromQualifiedMassSpectrum(const pappso::QualifiedMassSpectrum &mass_spectrum, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum
Definition xymsrunreader.cpp:334
pappso::XyMsRunReader::qualifiedMassSpectrumFromXyMSDataFile
QualifiedMassSpectrum qualifiedMassSpectrumFromXyMSDataFile(MassSpectrumId mass_spectrum_id) const
Definition xymsrunreader.cpp:119
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:243
pappso::XyMsRunReader::acquireDevice
virtual bool acquireDevice() override
acquire data back end device
Definition xymsrunreader.cpp:316
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:53
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:304
pappso::XyMsRunReader::readSpectrumCollectionByMsLevel
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
Definition xymsrunreader.cpp:268
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:310
pappso::XyMsRunReader::XyMsRunReader
XyMsRunReader(MsRunIdCstSPtr &msrun_id_csp)
Definition xymsrunreader.cpp:26
pappso::XyMsRunReader::initialize
virtual void initialize() override
Definition xymsrunreader.cpp:36
pappso::XyMsRunReader::newXicCoordSPtrFromSpectrumIndex
virtual pappso::XicCoordSPtr newXicCoordSPtrFromSpectrumIndex(std::size_t spectrum_index, pappso::PrecisionPtr precision) const override
get a xic coordinate object from a given spectrum index
Definition xymsrunreader.cpp:322
pappso::XyMsRunReader::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition xymsrunreader.cpp:105
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:215
pappso::XyMsRunReader::~XyMsRunReader
virtual ~XyMsRunReader()
Definition xymsrunreader.cpp:47
pappso::XyMsRunReader::readSpectrumCollection2
virtual void readSpectrumCollection2(const MsRunReadConfig &config, SpectrumCollectionHandlerInterface &handler) override
Definition xymsrunreader.cpp:296
pappso::XyMsRunReader::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition xymsrunreader.cpp:112
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::PeptideIonCter::y
@ y
pappso::PeptideIonCter::x
@ x
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition msrunid.h:46
pappso::pappso_double
double pappso_double
A type definition for doubles.
Definition types.h:50
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition massspectrum.h:57
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition massspectrum.h:56
pappso::XicCoordSPtr
std::shared_ptr< XicCoord > XicCoordSPtr
Definition xiccoord.h:43
pappso::DataPoint
Definition datapoint.h:22
xymsrunreader.h