msrunreader.cpp
Go to the documentation of this file.
/**
* \file pappsomspp/msrun/msrunreader.cpp
* \date 29/05/2018
* \author Olivier Langella
* \brief base interface to read MSrun files
*/
/*******************************************************************************
* Copyright (c) 2018 Olivier Langella <Olivier.Langella@u-psud.fr>.
*
* This file is part of the PAPPSOms++ library.
*
* PAPPSOms++ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PAPPSOms++ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include <QDebug>
#include "msrunreader.h"
#include "../../pappsomspp/exception/exceptionnotfound.h"
int msRunReaderSPtrMetaTypeId =
qRegisterMetaType<pappso::MsRunReaderSPtr>("pappso::MsRunReaderSPtr");
namespace pappso
{
bool
SpectrumCollectionHandlerInterface::shouldStop()
{
return false;
}
void
SpectrumCollectionHandlerInterface::loadingEnded()
{
}
void
SpectrumCollectionHandlerInterface::spectrumListHasSize(
[[maybe_unused]] std::size_t size)
{
}
void
SpectrumCollectionHandlerInterface::setReadAhead(bool is_read_ahead)
{
m_isReadAhead = is_read_ahead;
}
bool
SpectrumCollectionHandlerInterface::isReadAhead() const
{
return m_isReadAhead;
}
bool
SpectrumCollectionHandlerInterface::needMsLevelPeakList(
unsigned int ms_level) const
{
if(needPeakList() == true)
{
if(ms_level < m_needPeakListByMsLevel.size())
{
return m_needPeakListByMsLevel[ms_level];
}
else
return true;
}
else
{
return false;
}
}
void
SpectrumCollectionHandlerInterface::setNeedMsLevelPeakList(
unsigned int ms_level, bool want_peak_list)
{
if(ms_level < m_needPeakListByMsLevel.size())
{
m_needPeakListByMsLevel[ms_level] = want_peak_list;
}
}
bool
MsRunSimpleStatistics::needPeakList() const
{
return false;
}
void
MsRunSimpleStatistics::loadingEnded()
{
// qDebug() << __FILE__ << "@" << __LINE__ << __FUNCTION__ << "()"
//<< "The data loading process ended.";
}
void
MsRunSimpleStatistics::setQualifiedMassSpectrum(
const QualifiedMassSpectrum &qspectrum)
{
// The vector[0] contains the number of spectra at MS
// The vector[1] contains the number of spectra at MS^2
// The vector[2] contains the number of spectra at MS^3
// ...
unsigned int ms_level = qspectrum.getMsLevel();
if(ms_level == 0)
return;
if(ms_level > m_countMsLevelSpectrum.size())
{
m_countMsLevelSpectrum.resize(ms_level);
}
m_countMsLevelSpectrum[ms_level - 1]++;
}
unsigned long
MsRunSimpleStatistics::getMsLevelCount(unsigned int ms_level) const
{
if(ms_level == 0)
return 0;
if(ms_level > m_countMsLevelSpectrum.size())
return 0;
return (m_countMsLevelSpectrum[ms_level - 1]);
}
unsigned long
MsRunSimpleStatistics::getTotalCount() const
{
unsigned long total = 0;
for(unsigned long count : m_countMsLevelSpectrum)
{
total += count;
}
return total;
}
MsRunReaderScanNumberMultiMap::MsRunReaderScanNumberMultiMap()
{
// qDebug();
}
MsRunReaderScanNumberMultiMap::~MsRunReaderScanNumberMultiMap()
{
// qDebug();
}
bool
MsRunReaderScanNumberMultiMap::needPeakList() const
{
return false;
}
void
MsRunReaderScanNumberMultiMap::setQualifiedMassSpectrum(
const QualifiedMassSpectrum &qspectrum)
{
qDebug() << " " << qspectrum.getMassSpectrumId().getNativeId();
QStringList native_id_list =
qspectrum.getMassSpectrumId().getNativeId().split("=");
if(native_id_list.size() < 2)
{
return;
}
else
{
std::size_t scan_number = native_id_list.back().toULong();
m_mmap_scan2index.insert(std::pair<std::size_t, std::size_t>(
scan_number, qspectrum.getMassSpectrumId().getSpectrumIndex()));
qDebug() << "scan number " << scan_number << "=>"
<< qspectrum.getMassSpectrumId().getSpectrumIndex();
}
}
std::size_t
MsRunReaderScanNumberMultiMap::getSpectrumIndexFromScanNumber(
std::size_t scan_number) const
{
qDebug() << m_mmap_scan2index.size();
auto it = m_mmap_scan2index.find(scan_number);
if(it == m_mmap_scan2index.end())
{
throw ExceptionNotFound(
QObject::tr("scan number %1 not found").arg(scan_number));
}
std::size_t index = it->second;
it++;
if((it != m_mmap_scan2index.end()) && (it->first == scan_number))
{
throw PappsoException(
QObject::tr("scan number %1 found multiple times").arg(scan_number));
}
return index;
}
MsRunReaderRetentionTimeLine::MsRunReaderRetentionTimeLine()
{
// qDebug();
}
MsRunReaderRetentionTimeLine::~MsRunReaderRetentionTimeLine()
{
// qDebug();
}
bool
MsRunReaderRetentionTimeLine::needPeakList() const
{
return false;
}
void
MsRunReaderRetentionTimeLine::setQualifiedMassSpectrum(
const QualifiedMassSpectrum &qspectrum)
{
qDebug() << " " << qspectrum.getMassSpectrumId().getNativeId();
m_retention_time_list.push_back(qspectrum.getRtInSeconds());
}
const std::vector<double> &
MsRunReaderRetentionTimeLine::getRetentionTimeLine() const
{
return m_retention_time_list;
}
MsRunReader::MsRunReader(MsRunIdCstSPtr &ms_run_id) : mcsp_msRunId(ms_run_id)
{
}
MsRunReader::MsRunReader(const MsRunReader &other)
: mcsp_msRunId(other.mcsp_msRunId)
{
mpa_multiMapScanNumber = nullptr;
}
const MsRunIdCstSPtr &
MsRunReader::getMsRunId() const
{
return mcsp_msRunId;
}
MsRunReader::~MsRunReader()
{
if(mpa_multiMapScanNumber == nullptr)
delete mpa_multiMapScanNumber;
}
std::size_t
MsRunReader::scanNumber2SpectrumIndex(std::size_t scan_number)
{
qDebug() << " " << mpa_multiMapScanNumber;
if(mpa_multiMapScanNumber == nullptr)
{
mpa_multiMapScanNumber = new MsRunReaderScanNumberMultiMap();
readSpectrumCollection(*mpa_multiMapScanNumber);
}
try
{
return mpa_multiMapScanNumber->getSpectrumIndexFromScanNumber(
scan_number);
}
catch(ExceptionNotFound &error)
{
throw ExceptionNotFound(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
catch(PappsoException &error)
{
throw PappsoException(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
}
bool
MsRunReader::hasScanNumbers() const
{
return false;
}
std::vector<double>
MsRunReader::getRetentionTimeLine()
{
qDebug();
try
{
MsRunReaderRetentionTimeLine reader_timeline;
readSpectrumCollectionByMsLevel(reader_timeline, 1);
return reader_timeline.getRetentionTimeLine();
}
catch(ExceptionNotFound &error)
{
throw ExceptionNotFound(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
catch(PappsoException &error)
{
throw PappsoException(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
}
Trace
MsRunReader::getTicChromatogram()
{
qDebug();
try
{
MsRunReaderTicChromatogram ms_run_reader;
readSpectrumCollection(ms_run_reader);
return ms_run_reader.getTicChromatogram();
}
catch(ExceptionNotFound &error)
{
throw ExceptionNotFound(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
catch(PappsoException &error)
{
throw PappsoException(QObject::tr("error reading file %1 : %2")
.arg(mcsp_msRunId.get()->getFileName())
.arg(error.qwhat()));
}
}
MsRunReaderTicChromatogram::MsRunReaderTicChromatogram()
{
}
MsRunReaderTicChromatogram::~MsRunReaderTicChromatogram()
{
}
bool
MsRunReaderTicChromatogram::needPeakList() const
{
return true;
}
void
MsRunReaderTicChromatogram::setQualifiedMassSpectrum(
const QualifiedMassSpectrum &qualified_mass_spectrum)
{
// In this specialized reader we want to compute the total ion current
// chromatogram that plot the sum of all the ion intensities in the spectra as
// a function of the retention time.
uint spectrum_ms_level = qualified_mass_spectrum.getMsLevel();
if(spectrum_ms_level != 1)
return;
double sumY = qualified_mass_spectrum.getMassSpectrumSPtr()->sumY();
if(!sumY)
return;
double rt = qualified_mass_spectrum.getRtInMinutes();
using Pair = std::pair<double, double>;
using Map = std::map<double, double>;
using Iterator = Map::iterator;
std::pair<Iterator, bool> res = m_ticChromMapTrace.insert(Pair(rt, sumY));
if(!res.second)
{
// One other same rt value was seen already (like in ion mobility mass
// spectrometry, for example). Only increment the y value.
res.first->second += sumY;
}
}
Trace
MsRunReaderTicChromatogram::getTicChromatogram() const
{
return m_ticChromMapTrace.toTrace();
}
} // namespace pappso
pappso::ExceptionNotFound
Definition: exceptionnotfound.h:32
pappso::MapTrace::toTrace
Trace toTrace() const
Definition: maptrace.cpp:218
pappso::MassSpectrumId::getSpectrumIndex
std::size_t getSpectrumIndex() const
Definition: massspectrumid.cpp:113
pappso::MassSpectrumId::getNativeId
const QString & getNativeId() const
Definition: massspectrumid.cpp:99
pappso::MsRunReaderRetentionTimeLine
collect retention times along MS run
Definition: msrunreader.h:150
pappso::MsRunReaderRetentionTimeLine::getRetentionTimeLine
const std::vector< double > & getRetentionTimeLine() const
Definition: msrunreader.cpp:251
pappso::MsRunReaderRetentionTimeLine::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
Definition: msrunreader.cpp:242
pappso::MsRunReaderRetentionTimeLine::~MsRunReaderRetentionTimeLine
virtual ~MsRunReaderRetentionTimeLine()
Definition: msrunreader.cpp:228
pappso::MsRunReaderRetentionTimeLine::needPeakList
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
Definition: msrunreader.cpp:235
pappso::MsRunReaderRetentionTimeLine::MsRunReaderRetentionTimeLine
MsRunReaderRetentionTimeLine()
Definition: msrunreader.cpp:222
pappso::MsRunReaderRetentionTimeLine::m_retention_time_list
std::vector< double > m_retention_time_list
Definition: msrunreader.h:152
pappso::MsRunReaderScanNumberMultiMap
provides a multimap to find quickly spectrum index from scan number
Definition: msrunreader.h:132
pappso::MsRunReaderScanNumberMultiMap::needPeakList
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
Definition: msrunreader.cpp:167
pappso::MsRunReaderScanNumberMultiMap::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
Definition: msrunreader.cpp:173
pappso::MsRunReaderScanNumberMultiMap::getSpectrumIndexFromScanNumber
std::size_t getSpectrumIndexFromScanNumber(std::size_t scan_number) const
Definition: msrunreader.cpp:196
pappso::MsRunReaderScanNumberMultiMap::m_mmap_scan2index
std::multimap< std::size_t, std::size_t > m_mmap_scan2index
Definition: msrunreader.h:134
pappso::MsRunReaderScanNumberMultiMap::~MsRunReaderScanNumberMultiMap
virtual ~MsRunReaderScanNumberMultiMap()
Definition: msrunreader.cpp:160
pappso::MsRunReaderScanNumberMultiMap::MsRunReaderScanNumberMultiMap
MsRunReaderScanNumberMultiMap()
Definition: msrunreader.cpp:154
pappso::MsRunReaderTicChromatogram
calculate a TIC chromatogram
Definition: msrunreader.h:168
pappso::MsRunReaderTicChromatogram::~MsRunReaderTicChromatogram
virtual ~MsRunReaderTicChromatogram()
Definition: msrunreader.cpp:383
pappso::MsRunReaderTicChromatogram::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &qualified_mass_spectrum) override
Definition: msrunreader.cpp:396
pappso::MsRunReaderTicChromatogram::m_ticChromMapTrace
MapTrace m_ticChromMapTrace
Definition: msrunreader.h:179
pappso::MsRunReaderTicChromatogram::MsRunReaderTicChromatogram
MsRunReaderTicChromatogram()
Definition: msrunreader.cpp:378
pappso::MsRunReaderTicChromatogram::needPeakList
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
Definition: msrunreader.cpp:389
pappso::MsRunReaderTicChromatogram::getTicChromatogram
Trace getTicChromatogram() const
Definition: msrunreader.cpp:432
pappso::MsRunReader
base class to read MSrun the only way to build a MsRunReader object is to use the MsRunReaderFactory
Definition: msrunreader.h:191
pappso::MsRunReader::mcsp_msRunId
MsRunIdCstSPtr mcsp_msRunId
Definition: msrunreader.h:282
pappso::MsRunReader::mpa_multiMapScanNumber
MsRunReaderScanNumberMultiMap * mpa_multiMapScanNumber
Definition: msrunreader.h:283
pappso::MsRunReader::hasScanNumbers
virtual bool hasScanNumbers() const
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
Definition: msrunreader.cpp:314
pappso::MsRunReader::getRetentionTimeLine
virtual std::vector< double > getRetentionTimeLine()
retention timeline get retention times along the MSrun in seconds
Definition: msrunreader.cpp:320
pappso::MsRunReader::~MsRunReader
virtual ~MsRunReader()
Definition: msrunreader.cpp:275
pappso::MsRunReader::scanNumber2SpectrumIndex
virtual std::size_t scanNumber2SpectrumIndex(std::size_t scan_number)
if possible, converts a scan number into a spectrum index This is a convenient function to help trans...
Definition: msrunreader.cpp:283
pappso::MsRunReader::readSpectrumCollection
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler)=0
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
pappso::MsRunReader::getTicChromatogram
virtual Trace getTicChromatogram()
Definition: msrunreader.cpp:350
pappso::MsRunReader::MsRunReader
MsRunReader(MsRunIdCstSPtr &ms_run_id)
Definition: msrunreader.cpp:257
pappso::MsRunReader::readSpectrumCollectionByMsLevel
virtual void readSpectrumCollectionByMsLevel(SpectrumCollectionHandlerInterface &handler, unsigned int ms_level)=0
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler by Ms Levels
pappso::MsRunReader::getMsRunId
const MsRunIdCstSPtr & getMsRunId() const
Definition: msrunreader.cpp:269
pappso::MsRunSimpleStatistics::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum) override
Definition: msrunreader.cpp:112
pappso::MsRunSimpleStatistics::getTotalCount
unsigned long getTotalCount() const
Definition: msrunreader.cpp:143
pappso::MsRunSimpleStatistics::loadingEnded
virtual void loadingEnded() override
Definition: msrunreader.cpp:104
pappso::MsRunSimpleStatistics::m_countMsLevelSpectrum
std::vector< unsigned long > m_countMsLevelSpectrum
Definition: msrunreader.h:115
pappso::MsRunSimpleStatistics::needPeakList
virtual bool needPeakList() const override
tells if we need the peak list (if we want the binary data) for each spectrum
Definition: msrunreader.cpp:97
pappso::MsRunSimpleStatistics::getMsLevelCount
unsigned long getMsLevelCount(unsigned int ms_level) const
Definition: msrunreader.cpp:132
pappso::PappsoException
Definition: pappsoexception.h:42
pappso::PappsoException::qwhat
virtual const QString & qwhat() const
Definition: pappsoexception.h:68
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition: qualifiedmassspectrum.h:85
pappso::QualifiedMassSpectrum::getMsLevel
uint getMsLevel() const
Get the mass spectrum level.
Definition: qualifiedmassspectrum.cpp:187
pappso::QualifiedMassSpectrum::getRtInMinutes
pappso_double getRtInMinutes() const
Get the retention time in minutes.
Definition: qualifiedmassspectrum.cpp:211
pappso::QualifiedMassSpectrum::getMassSpectrumId
const MassSpectrumId & getMassSpectrumId() const
Get the MassSpectrumId.
Definition: qualifiedmassspectrum.cpp:157
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition: qualifiedmassspectrum.cpp:133
pappso::QualifiedMassSpectrum::getRtInSeconds
pappso_double getRtInSeconds() const
Get the retention time in seconds.
Definition: qualifiedmassspectrum.cpp:203
pappso::SpectrumCollectionHandlerInterface::shouldStop
virtual bool shouldStop()
Definition: msrunreader.cpp:43
pappso::SpectrumCollectionHandlerInterface::isReadAhead
virtual bool isReadAhead() const
tells if we want to read ahead spectrum
Definition: msrunreader.cpp:63
pappso::SpectrumCollectionHandlerInterface::needPeakList
virtual bool needPeakList() const =0
tells if we need the peak list (if we want the binary data) for each spectrum
pappso::SpectrumCollectionHandlerInterface::loadingEnded
virtual void loadingEnded()
Definition: msrunreader.cpp:48
pappso::SpectrumCollectionHandlerInterface::needMsLevelPeakList
virtual bool needMsLevelPeakList(unsigned int ms_level) const final
tells if we need the peak list (if we want the binary data) for each spectrum, given an MS level
Definition: msrunreader.cpp:69
pappso::SpectrumCollectionHandlerInterface::setReadAhead
virtual void setReadAhead(bool is_read_ahead) final
use threads to read a spectrum by batch of batch_size
Definition: msrunreader.cpp:57
pappso::SpectrumCollectionHandlerInterface::setNeedMsLevelPeakList
virtual void setNeedMsLevelPeakList(unsigned int ms_level, bool want_peak_list) final
tells if we need the peak list given
Definition: msrunreader.cpp:87
pappso::SpectrumCollectionHandlerInterface::m_needPeakListByMsLevel
std::vector< bool > m_needPeakListByMsLevel
Definition: msrunreader.h:91
pappso::SpectrumCollectionHandlerInterface::spectrumListHasSize
virtual void spectrumListHasSize(std::size_t size)
Definition: msrunreader.cpp:52
pappso::SpectrumCollectionHandlerInterface::m_isReadAhead
bool m_isReadAhead
Definition: msrunreader.h:90
pappso::Trace
A simple container of DataPoint instances.
Definition: trace.h:148
msRunReaderSPtrMetaTypeId
int msRunReaderSPtrMetaTypeId
Definition: msrunreader.cpp:34
msrunreader.h
base interface to read MSrun files
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39
pappso::PrecisionUnit::res
@ res
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition: msrunid.h:44
pappso::DataKind::rt
@ rt
Retention time.
pappso::uint
unsigned int uint
Definition: types.h:55