pwizmsfilereader.cpp
Go to the documentation of this file.
/////////////////////// StdLib includes
#include <iostream>
#include <iomanip>
/////////////////////// Qt includes
#include <QDebug>
#include <QFile>
#include <QFileInfo>
/////////////////////// libpwiz includes
#include <pwiz/data/msdata/DefaultReaderList.hpp>
/////////////////////// Local includes
#include "pwizmsfilereader.h"
#include "../exception/exceptionnotfound.h"
#include "../utils.h"
#include "../types.h"
#include "../msrun/msrunid.h"
namespace pappso
{
PwizMsFileReader::PwizMsFileReader(const QString &file_name)
: MsFileReader{file_name}
{
}
PwizMsFileReader::~PwizMsFileReader()
{
}
std::size_t
PwizMsFileReader::initialize()
{
pwiz::msdata::DefaultReaderList defaultReaderList;
std::string readerName;
try
{
readerName = defaultReaderList.identify(m_fileName.toStdString());
}
catch(std::runtime_error &error)
{
qDebug() << error.what() << " " << typeid(error).name();
throw PappsoException(
QObject::tr(
"libpwiz ERROR reading MS data file %1 "
"(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
.arg(m_fileName)
.arg(error.what())
.arg(__FILE__)
.arg(__LINE__));
}
catch(std::exception &error)
{
qDebug() << error.what() << " " << typeid(error).name();
throw PappsoException(
QObject::tr(
"libpwiz ERROR reading MS data file %1 "
"(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
.arg(m_fileName)
.arg(error.what())
.arg(__FILE__)
.arg(__LINE__));
}
if(readerName.empty())
{
qDebug() << "Failed to identify the file.";
return 0;
}
// Now convert the string to MzFormat.
if(readerName == "mzML")
m_fileFormat = MzFormat::mzML;
else if(readerName == "mzXML")
m_fileFormat = MzFormat::mzXML;
else if(readerName == "Mascot Generic")
m_fileFormat = MzFormat::MGF;
else if(readerName == "MZ5")
m_fileFormat = MzFormat::mz5;
else if(readerName == "MSn")
m_fileFormat = MzFormat::msn;
else if(readerName == "ABSciex WIFF")
m_fileFormat = MzFormat::abSciexWiff;
else if(readerName == "ABSciex T2D")
m_fileFormat = MzFormat::abSciexT2D;
else if(readerName == "Agilent MassHunter")
m_fileFormat = MzFormat::agilentMassHunter;
else if(readerName == "Thermo RAW")
m_fileFormat = MzFormat::thermoRaw;
else if(readerName == "Water RAW")
m_fileFormat = MzFormat::watersRaw;
else if(readerName == "Bruker FID")
m_fileFormat = MzFormat::brukerFid;
else if(readerName == "Bruker YEP")
m_fileFormat = MzFormat::brukerYep;
else if(readerName == "Bruker BAF")
m_fileFormat = MzFormat::brukerBaf;
else
{
m_fileFormat = MzFormat::unknown;
return 0;
}
// std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
// << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
// << std::endl;
// At this point we know pwiz could be able to read the file. Actually fill-in
// the MSDataPtr vector!
try
{
defaultReaderList.read(Utils::toUtf8StandardString(m_fileName),
m_msDataPtrVector);
}
catch(std::runtime_error &error)
{
qDebug() << error.what() << " " << typeid(error).name();
throw PappsoException(
QObject::tr(
"libpwiz ERROR reading MS data file %1 "
"(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
.arg(m_fileName)
.arg(error.what())
.arg(__FILE__)
.arg(__LINE__));
}
catch(std::exception &error)
{
qDebug() << error.what() << " " << typeid(error).name();
throw PappsoException(
QObject::tr(
"libpwiz ERROR reading MS data file %1 "
"(std::runtime_error):\n%2\nsource file:%3 - source line:%4")
.arg(m_fileName)
.arg(error.what())
.arg(__FILE__)
.arg(__LINE__));
}
qDebug() << "The number of runs is:" << m_msDataPtrVector.size()
<< "The reader type is:" << QString::fromStdString(readerName)
<< "The number of spectra in first run is:"
<< m_msDataPtrVector.at(0)->run.spectrumListPtr->size();
return m_msDataPtrVector.size();
}
MzFormat
PwizMsFileReader::getFileFormat()
{
// std::cout << __FILE__ << " @ " << __LINE__ << " " << __FUNCTION__ << " () "
// << std::setprecision(15) << "m_fileFormat: " << (int)m_fileFormat
// << std::endl;
return m_fileFormat;
}
std::vector<MsRunIdCstSPtr>
PwizMsFileReader::getMsRunIds(const QString &run_prefix)
{
std::vector<MsRunIdCstSPtr> ms_run_ids;
if(!initialize())
return ms_run_ids;
std::size_t iter = 0;
// If the initialization failed, then there is not a single MSDataPtr in the
// vector, so the loop below is not gone through.
for(pwiz::msdata::MSDataPtr ms_data_ptr : m_msDataPtrVector)
{
// For each ms run in the file, we will create a MsRunId instance that
// will hold the file name of the data file
// Finally create the MsRunId with the file name.
MsRunId ms_run_id(m_fileName, QString::fromStdString(ms_data_ptr->id));
ms_run_id.setMzFormat(m_fileFormat);
// We need to set the unambiguous xmlId string.
ms_run_id.setXmlId(QString("%1%2")
.arg(run_prefix)
.arg(Utils::getLexicalOrderedString(iter)));
// Now set the sample name to the run id :
ms_run_id.setSampleName(QString::fromStdString(ms_data_ptr->run.id));
// and if it is possible, the real sample name because this one is for the
// end user to recognize his sample:
if(ms_data_ptr->run.samplePtr != nullptr)
{
ms_run_id.setSampleName(
QString::fromStdString(ms_data_ptr->run.samplePtr->name));
}
qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
<< "Current ms_run_id:" << ms_run_id.toString();
// Finally make a shared pointer out of it and append it to the vector.
ms_run_ids.push_back(std::make_shared<MsRunId>(ms_run_id));
++iter;
}
return ms_run_ids;
}
} // namespace pappso
pappso::MsFileReader
Definition: msfilereader.h:15
pappso::MsFileReader::m_fileFormat
MzFormat m_fileFormat
Definition: msfilereader.h:18
pappso::MsFileReader::m_fileName
QString m_fileName
Definition: msfilereader.h:17
pappso::MsRunId
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition: msrunid.h:53
pappso::MsRunId::toString
QString toString() const
Definition: msrunid.cpp:193
pappso::MsRunId::setMzFormat
void setMzFormat(MzFormat format)
Definition: msrunid.cpp:158
pappso::MsRunId::setXmlId
void setXmlId(const QString &xml_id)
set an XML unique identifier for this MsRunId
Definition: msrunid.cpp:137
pappso::MsRunId::setSampleName
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition: msrunid.cpp:79
pappso::PappsoException
Definition: pappsoexception.h:42
pappso::PwizMsFileReader::getMsRunIds
virtual std::vector< MsRunIdCstSPtr > getMsRunIds(const QString &run_prefix) override
Definition: pwizmsfilereader.cpp:174
pappso::PwizMsFileReader::PwizMsFileReader
PwizMsFileReader(const QString &file_name)
Definition: pwizmsfilereader.cpp:28
pappso::PwizMsFileReader::m_msDataPtrVector
std::vector< pwiz::msdata::MSDataPtr > m_msDataPtrVector
Definition: pwizmsfilereader.h:20
pappso::PwizMsFileReader::initialize
virtual std::size_t initialize()
Definition: pwizmsfilereader.cpp:40
pappso::PwizMsFileReader::~PwizMsFileReader
virtual ~PwizMsFileReader()
Definition: pwizmsfilereader.cpp:34
pappso::PwizMsFileReader::getFileFormat
virtual MzFormat getFileFormat() override
Definition: pwizmsfilereader.cpp:163
pappso::Utils::toUtf8StandardString
static std::string toUtf8StandardString(const QString &text)
Definition: utils.cpp:127
pappso::Utils::getLexicalOrderedString
static const QString getLexicalOrderedString(unsigned int num)
Definition: utils.cpp:52
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::MzFormat
MzFormat
Definition: types.h:107
pappso::MzFormat::msn
@ msn
pappso::MzFormat::agilentMassHunter
@ agilentMassHunter
pappso::MzFormat::abSciexT2D
@ abSciexT2D
pappso::MzFormat::mzXML
@ mzXML
mzXML
pappso::MzFormat::watersRaw
@ watersRaw
pappso::MzFormat::mz5
@ mz5
pappso::MzFormat::brukerBaf
@ brukerBaf
pappso::MzFormat::mzML
@ mzML
mzML
pappso::MzFormat::abSciexWiff
@ abSciexWiff
pappso::MzFormat::brukerFid
@ brukerFid
pappso::MzFormat::unknown
@ unknown
unknown format
pappso::MzFormat::MGF
@ MGF
Mascot format.
pappso::MzFormat::thermoRaw
@ thermoRaw
pappso::MzFormat::brukerYep
@ brukerYep
pwizmsfilereader.h