Loading...
Searching...
No Matches
timsmsrunreaderms2selected.cpp
Go to the documentation of this file.
/**
* \file pappsomspp/msrun/private/timsmsrunreaderms2.cpp
* \date 10/09/2019
* \author Olivier Langella
* \brief MSrun file reader for native Bruker TimsTOF specialized for MS2
* purpose
*/
/*******************************************************************************
* Copyright (c) 2019 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 "pappsomspp/core/msrun/private/timsmsrunreaderms2selected.h"
#include "pappsomspp/core/exception/exceptionnotimplemented.h"
#include "../../vendors/tims/timsddaprecursors.h"
#include <QDebug>
#include <QtConcurrent>
using namespace pappso;
TimsMsRunReaderMs2Selected::TimsMsRunReaderMs2Selected(MsRunIdCstSPtr &msrun_id_csp)
: TimsMsRunReaderMs2(msrun_id_csp)
{
initialize();
}
TimsMsRunReaderMs2Selected::~TimsMsRunReaderMs2Selected()
{
if(mpa_timsData != nullptr)
{
delete mpa_timsData;
}
}
void
pappso::TimsMsRunReaderMs2Selected::initialize()
{
mpa_timsData = new TimsData(mcsp_msRunId.get()->getFileName());
mp_timsDdaPrecursors = mpa_timsData->getTimsDdaPrecursorsPtr();
}
void
TimsMsRunReaderMs2Selected::setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
{
if(mp_timsDdaPrecursors != nullptr)
{
mp_timsDdaPrecursors->setMs2FilterCstSPtr(filter);
}
else
{
throw PappsoException(
QObject::tr("ERROR in TimsMsRunReaderMs2Selected::setMs2FilterCstSPtr "
"mp_timsDdaPrecursors is null"));
}
}
void
TimsMsRunReaderMs2Selected::setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
{
if(mp_timsDdaPrecursors != nullptr)
{
mp_timsDdaPrecursors->setMs1FilterCstSPtr(filter);
}
else
{
throw PappsoException(
QObject::tr("ERROR in TimsMsRunReaderMs2Selected::setMs1FilterCstSPtr "
"mp_timsDdaPrecursors is null"));
}
}
bool
TimsMsRunReaderMs2Selected::accept(const QString &file_name) const
{
qDebug() << file_name;
return true;
}
pappso::MassSpectrumSPtr
TimsMsRunReaderMs2Selected::massSpectrumSPtr(std::size_t spectrum_index)
{
QualifiedMassSpectrum mass_spectrum = qualifiedMassSpectrum(spectrum_index, true);
return mass_spectrum.getMassSpectrumSPtr();
}
pappso::MassSpectrumCstSPtr
TimsMsRunReaderMs2Selected::massSpectrumCstSPtr(std::size_t spectrum_index)
{
QualifiedMassSpectrum mass_spectrum = qualifiedMassSpectrum(spectrum_index, true);
return mass_spectrum.getMassSpectrumSPtr();
}
QualifiedMassSpectrum
TimsMsRunReaderMs2Selected::qualifiedMassSpectrum(std::size_t spectrum_index,
bool want_binary_data) const
{
std::size_t precursor_index = (spectrum_index / 2) + 1;
TimsDdaPrecursors::SpectrumDescr spectrum_descr =
mp_timsDdaPrecursors->getSpectrumDescrWithPrecursorId(precursor_index);
if(spectrum_index % 2 == 0)
{
qDebug();
// this is an MS1 spectrum
QualifiedMassSpectrum mass_spectrum_ms1;
mp_timsDdaPrecursors->getQualifiedMs1MassSpectrumBySpectrumDescr(
getMsRunId(), mass_spectrum_ms1, spectrum_descr, want_binary_data);
qDebug(); // << mass_spectrum_ms1.toString();
// qDebug() << mass_spectrum_ms1.getMassSpectrumSPtr().get()->toString();
return mass_spectrum_ms1;
}
else
{
qDebug();
QualifiedMassSpectrum mass_spectrum_ms2;
if(spectrum_descr.ms2_index != spectrum_index)
{
qDebug();
throw PappsoException(
QObject::tr("ERROR in %1 %2 %3 spectrum_descr.ms2_index != spectrum_index")
.arg(__FILE__)
.arg(__FUNCTION__)
.arg(__LINE__));
}
mp_timsDdaPrecursors->getQualifiedMs2MassSpectrumBySpectrumDescr(
getMsRunId(), mass_spectrum_ms2, spectrum_descr, want_binary_data);
qDebug(); // << mass_spectrum_ms2.toString();
// qDebug() << mass_spectrum_ms2.getMassSpectrumSPtr().get()->toString();
return mass_spectrum_ms2;
}
}
void
TimsMsRunReaderMs2Selected::readSpectrumCollection(SpectrumCollectionHandlerInterface &handler)
{
const bool want_binary_data = handler.needPeakList();
// const bool want_binary_data = false;
// We'll need it to perform the looping in the spectrum list.
std::size_t spectrum_list_size = spectrumListSize();
// qDebug() << "The spectrum list has size:" << spectrum_list_size;
// Inform the handler of the spectrum list so that it can handle feedback to
// the user.
handler.spectrumListHasSize(spectrum_list_size);
std::vector<size_t> selected_precursor{63905,
73549,
105675,
130439,
177297,
177369,
177483,
190453,
196967,
246691,
271215,
289423,
310669};
// QFile temp_output("/data/temp.txt");
// temp_output.open(QIODevice::WriteOnly | QIODevice::Text);
// QTextStream out(&temp_output);
// Iterate in the full list of spectra.
bool readAhead = handler.isReadAhead();
if(readAhead)
{
std::size_t process_list_size = 300;
struct tmp_item
{
QualifiedMassSpectrum qualified_mass_spectrum;
std::size_t iter;
bool want_binary_data;
};
for(std::size_t i = 0; i < spectrum_list_size; i += process_list_size)
{
// QTextStream out(&temp_output);
qDebug();
// If the user of this reader instance wants to stop reading the
// spectra, then break this loop.
if(handler.shouldStop())
{
qDebug() << "The operation was cancelled. Breaking the loop.";
break;
}
std::vector<tmp_item> item_list;
for(std::size_t iter = 0; (iter < process_list_size) && ((iter + i) < spectrum_list_size);
iter++)
{
if(std::find(selected_precursor.begin(), selected_precursor.end(), iter + i) !=
selected_precursor.end())
{
bool get_data = want_binary_data;
if((iter + i) % 2 == 0)
{ // MS1
get_data = handler.needMsLevelPeakList(1);
}
else
{
get_data = handler.needMsLevelPeakList(2);
}
item_list.push_back({QualifiedMassSpectrum(), iter + i, get_data});
}
}
qDebug() << item_list.size();
// Use QtConcurrentBlocking::mapped to apply the scale function to all
// the images in the list.
QtConcurrent::blockingMap(item_list.begin(), item_list.end(), [this](tmp_item &one_item) {
qDebug() << one_item.iter;
one_item.qualified_mass_spectrum =
qualifiedMassSpectrum(one_item.iter, one_item.want_binary_data);
// qDebug() << one_item.qualified_mass_spectrum.size() << " " <<
// one_item.qualified_mass_spectrum.getMassSpectrumSPtr().get()->toString();
});
qDebug() << item_list.size();
for(auto &item : item_list)
{
// qDebug() <<
// item.qualified_mass_spectrum.getMassSpectrumSPtr()
// .get()
// ->toString();
handler.setQualifiedMassSpectrum(item.qualified_mass_spectrum);
qDebug();
}
}
}
else
{
for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
{
qDebug();
// If the user of this reader instance wants to stop reading the
// spectra, then break this loop.
if(handler.shouldStop())
{
qDebug() << "The operation was cancelled. Breaking the loop.";
break;
}
bool get_data = want_binary_data;
if(iter % 2 == 0)
{ // MS1
if(!handler.needMsLevelPeakList(1))
{
get_data = false;
}
}
QualifiedMassSpectrum qualified_mass_spectrum = qualifiedMassSpectrum(iter, get_data);
handler.setQualifiedMassSpectrum(qualified_mass_spectrum);
qDebug();
}
}
// End of
// for(std::size_t iter = 0; iter < spectrum_list_size; iter++)
// Now let the loading handler know that the loading of the data has
// ended. The handler might need this "signal" to perform additional tasks
// or to cleanup cruft.
// qDebug() << "Loading ended";
// temp_output.close();
handler.loadingEnded();
}
void
TimsMsRunReaderMs2Selected::readSpectrumCollection2([[maybe_unused]] const MsRunReadConfig &config,
SpectrumCollectionHandlerInterface &handler)
{
return readSpectrumCollection(handler);
}
std::size_t
TimsMsRunReaderMs2Selected::spectrumListSize() const
{
return (mp_timsDdaPrecursors->getTotalPrecursorCount() * 2);
}
bool
TimsMsRunReaderMs2Selected::hasScanNumbers() const
{
return false;
}
std::size_t
TimsMsRunReaderMs2Selected::spectrumStringIdentifier2SpectrumIndex(
const QString &spectrum_identifier [[maybe_unused]])
{
throw pappso::ExceptionNotImplemented(
QObject::tr("%1 %2 %3 not implemented").arg(__FILE__).arg(__FUNCTION__).arg(__LINE__));
}
pappso::ExceptionNotImplemented
Definition exceptionnotimplemented.h:32
pappso::MsRunReadConfig
Definition msrunreadconfig.h:62
pappso::MsRunReader::mcsp_msRunId
MsRunIdCstSPtr mcsp_msRunId
Definition msrunreader.h:194
pappso::MsRunReader::getMsRunId
const MsRunIdCstSPtr & getMsRunId() const
Definition msrunreader.cpp:55
pappso::PappsoException
Definition pappsoexception.h:41
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition qualifiedmassspectrum.h:99
pappso::QualifiedMassSpectrum::getMassSpectrumSPtr
MassSpectrumSPtr getMassSpectrumSPtr() const
Get the MassSpectrumSPtr.
Definition qualifiedmassspectrum.cpp:130
pappso::SpectrumCollectionHandlerInterface
interface to collect spectrums from the MsRunReader class
Definition spectrumcollectionhandlerinterface.h:52
pappso::SpectrumCollectionHandlerInterface::shouldStop
virtual bool shouldStop()
Definition spectrumcollectionhandlerinterface.cpp:38
pappso::SpectrumCollectionHandlerInterface::isReadAhead
virtual bool isReadAhead() const
tells if we want to read ahead spectrum
Definition spectrumcollectionhandlerinterface.cpp:57
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 spectrumcollectionhandlerinterface.cpp:43
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 spectrumcollectionhandlerinterface.cpp:63
pappso::SpectrumCollectionHandlerInterface::spectrumListHasSize
virtual void spectrumListHasSize(std::size_t size)
Definition spectrumcollectionhandlerinterface.cpp:47
pappso::SpectrumCollectionHandlerInterface::setQualifiedMassSpectrum
virtual void setQualifiedMassSpectrum(const QualifiedMassSpectrum &spectrum)=0
pappso::TimsData
Definition timsdata.h:68
pappso::TimsMsRunReaderMs2Selected::massSpectrumCstSPtr
virtual MassSpectrumCstSPtr massSpectrumCstSPtr(std::size_t spectrum_index) override
Definition timsmsrunreaderms2selected.cpp:107
pappso::TimsMsRunReaderMs2Selected::spectrumListSize
virtual std::size_t spectrumListSize() const override
get the totat number of spectrum conained in the MSrun data file
Definition timsmsrunreaderms2selected.cpp:306
pappso::TimsMsRunReaderMs2Selected::readSpectrumCollection2
virtual void readSpectrumCollection2(const MsRunReadConfig &config, SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
Definition timsmsrunreaderms2selected.cpp:299
pappso::TimsMsRunReaderMs2Selected::~TimsMsRunReaderMs2Selected
virtual ~TimsMsRunReaderMs2Selected()
Definition timsmsrunreaderms2selected.cpp:44
pappso::TimsMsRunReaderMs2Selected::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 timsmsrunreaderms2selected.cpp:91
pappso::TimsMsRunReaderMs2Selected::setMs2FilterCstSPtr
void setMs2FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition timsmsrunreaderms2selected.cpp:61
pappso::TimsMsRunReaderMs2Selected::setMs1FilterCstSPtr
void setMs1FilterCstSPtr(pappso::FilterInterfaceCstSPtr filter)
Definition timsmsrunreaderms2selected.cpp:76
pappso::TimsMsRunReaderMs2Selected::mpa_timsData
TimsData * mpa_timsData
Definition timsmsrunreaderms2selected.h:77
pappso::TimsMsRunReaderMs2Selected::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 timsmsrunreaderms2selected.cpp:115
pappso::TimsMsRunReaderMs2Selected::massSpectrumSPtr
virtual MassSpectrumSPtr massSpectrumSPtr(std::size_t spectrum_index) override
get a MassSpectrumSPtr class given its spectrum index
Definition timsmsrunreaderms2selected.cpp:99
pappso::TimsMsRunReaderMs2Selected::TimsMsRunReaderMs2Selected
TimsMsRunReaderMs2Selected(MsRunIdCstSPtr &msrun_id_csp)
Definition timsmsrunreaderms2selected.cpp:38
pappso::TimsMsRunReaderMs2Selected::hasScanNumbers
virtual bool hasScanNumbers() const override
tells if spectra can be accessed using scan numbers by default, it returns false. Only overrided func...
Definition timsmsrunreaderms2selected.cpp:313
pappso::TimsMsRunReaderMs2Selected::spectrumStringIdentifier2SpectrumIndex
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...
Definition timsmsrunreaderms2selected.cpp:320
pappso::TimsMsRunReaderMs2Selected::initialize
virtual void initialize() override
Definition timsmsrunreaderms2selected.cpp:53
pappso::TimsMsRunReaderMs2Selected::readSpectrumCollection
virtual void readSpectrumCollection(SpectrumCollectionHandlerInterface &handler) override
function to visit an MsRunReader and get each Spectrum in a spectrum collection handler
Definition timsmsrunreaderms2selected.cpp:160
pappso::TimsMsRunReaderMs2::TimsMsRunReaderMs2
TimsMsRunReaderMs2(MsRunIdCstSPtr &msrun_id_csp)
Definition timsmsrunreaderms2.cpp:39
pappso::TimsMsRunReaderMs2::mp_timsDdaPrecursors
TimsDdaPrecursors * mp_timsDdaPrecursors
Definition timsmsrunreaderms2.h:114
exceptionnotimplemented.h
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::ProjectParamCategory::filter
@ filter
concerning filters (psm, peptide, protein validation)
Definition projectparameters.h:48
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition msrunid.h:46
pappso::MassSpectrumCstSPtr
std::shared_ptr< const MassSpectrum > MassSpectrumCstSPtr
Definition massspectrum.h:54
pappso::FilterInterfaceCstSPtr
std::shared_ptr< const FilterInterface > FilterInterfaceCstSPtr
Definition filterinterface.h:47
pappso::MassSpectrumSPtr
std::shared_ptr< MassSpectrum > MassSpectrumSPtr
Definition massspectrum.h:53
pappso::TimsDdaPrecursors::SpectrumDescr
Definition timsddaprecursors.h:63
pappso::TimsDdaPrecursors::SpectrumDescr::ms2_index
std::size_t ms2_index
Definition timsddaprecursors.h:69
timsddaprecursors.h
timsmsrunreaderms2selected.h