Loading...
Searching...
No Matches
cborscanmapbase.cpp
Go to the documentation of this file.
/**
* \file pappsomspp/processing/cbor/psm/cborscanmapbase.h
* \date 13/07/2025
* \author Olivier Langella
* \brief Base class to handle scan cbor map
*/
/*******************************************************************************
* Copyright (c) 2025 Olivier Langella <Olivier.Langella@universite-paris-saclay.fr>.
*
* This file is part of PAPPSOms-tools.
*
* PAPPSOms-tools 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-tools 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-tools. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "cborscanmapbase.h"
#include "pappsomspp/core/pappsoexception.h"
#include "psmfilescanprocess.h"
#include "psmcborutils.h"
#include <QCborArray>
namespace pappso
{
namespace cbor
{
psm::CborScanMapBase::CborScanMapBase(const PsmFileScanProcess &psm_file_scan_process)
: m_PsmFileScanProcess(psm_file_scan_process)
{
}
psm::CborScanMapBase::~CborScanMapBase()
{
}
void
psm::CborScanMapBase::process()
{
}
QCborMap
pappso::cbor::psm::CborScanMapBase::getCborScanId() const
{
if(!keys().contains("id"))
{
throw pappso::PappsoException(QObject::tr("missing id in scan"));
}
return value("id").toMap();
}
QCborMap
pappso::cbor::psm::CborScanMapBase::getCborScanPrecursor() const
{
if(!keys().contains("precursor"))
{
throw pappso::PappsoException(QObject::tr("missing precursor in scan"));
}
return value("precursor").toMap();
}
QCborArray
pappso::cbor::psm::CborScanMapBase::getCborPsmList() const
{
if(!keys().contains("psm_list"))
{
throw pappso::PappsoException(QObject::tr("missing psm_list in scan"));
}
return value("psm_list").toArray();
}
std::vector<double>::iterator
psm::CborScanMapBase::addPsmEvalVectorDouble(const QString &eval_name,
const QString &eval_value_key,
std::vector<double>::iterator itbegin,
std::vector<double>::const_iterator itend)
{
std::vector<double>::iterator it = itbegin;
if(keys().contains("psm_list"))
{
QCborArray new_psm_arr;
for(QCborValue cbor_psm : value("psm_list").toArray())
{
QCborMap cbor_psm_map = cbor_psm.toMap();
if(!cbor_psm_map.keys().contains("proforma"))
{
throw pappso::PappsoException(
QObject::tr("missing proforma in psm %1").arg(cbor_psm_map.keys().size()));
}
QCborMap cbor_psm_eval_newvalues;
if(it == itend)
{
throw pappso::PappsoException(
QObject::tr("no more values to add in psm %1").arg(cbor_psm_map.keys().size()));
}
cbor_psm_eval_newvalues.insert(eval_value_key, *it);
it++;
QCborMap psm_eval = cbor_psm_map.value("eval").toMap();
psm_eval.remove(eval_name);
psm_eval.insert(eval_name, cbor_psm_eval_newvalues);
cbor_psm_map.remove(QString("eval"));
cbor_psm_map.insert(QString("eval"), psm_eval);
new_psm_arr.push_back(cbor_psm_map);
}
// insert(QString("psm_list"), new_psm_arr);
remove(QString("psm_list"));
insert(QString("psm_list"), new_psm_arr);
}
return it;
}
pappso::QualifiedMassSpectrumSPtr
psm::CborScanMapBase::getCurrentQualifiedMassSpectrumSPtr() const
{
if(isEmpty())
{
throw pappso::PappsoException(QObject::tr("ERROR: cbor scan map is empty"));
}
if(!contains(QString("precursor")))
{
throw pappso::PappsoException(QObject::tr("ERROR: no precursor in scan"));
}
const QCborMap cbor_precursor(value("precursor").toMap());
if(!contains(QString("ms2")))
{
throw pappso::PappsoException(QObject::tr("ERROR: ms2 is empty in scan"));
}
const QCborMap cbor_ms2(value("ms2").toMap());
if(!contains(QString("id")))
{
throw pappso::PappsoException(QObject::tr("ERROR: id is empty in scan"));
}
const QCborMap cbor_id(value("id").toMap());
if(!cbor_id.keys().contains("index"))
{
throw pappso::PappsoException("There is no scan index");
}
if(!cbor_ms2.keys().contains("spectrum"))
{
throw pappso::PappsoException("There is no spectrum in ms2");
}
const QCborMap cbor_spectrum(cbor_ms2.value("spectrum").toMap());
if(!cbor_spectrum.keys().contains("mz"))
{
throw pappso::PappsoException("There is no cbor_spectrum mz values");
}
if(!cbor_spectrum.keys().contains("intensity"))
{
throw pappso::PappsoException("There is no cbor_spectrum intensity values");
}
pappso::MsRunId msrun_id(m_PsmFileScanProcess.m_currentPeaklistFile.name);
msrun_id.setSampleName(m_PsmFileScanProcess.m_currentSampleName);
pappso::MsRunIdCstSPtr msrun_id_sp = std::make_shared<const pappso::MsRunId>(msrun_id);
pappso::MassSpectrumId ms_id(msrun_id_sp);
ms_id.setSpectrumIndex(cbor_id.value("index").toInteger());
// native_id
if(cbor_id.keys().contains("native_id"))
{
ms_id.setNativeId(cbor_id.value("native_id").toString());
}
std::vector<DataPoint> data_point_vector;
std::size_t i = 0;
for(auto cbor_mz_value : cbor_spectrum.value("mz").toArray())
{
data_point_vector.push_back(
{cbor_mz_value.toDouble(), cbor_spectrum.value("intensity").toArray().at(i).toDouble()});
i++;
}
MassSpectrum mass_spectrum(data_point_vector);
pappso::PrecursorIonData precursor_ion_data;
pappso::QualifiedMassSpectrum qualified_mass_spectrum(ms_id);
qualified_mass_spectrum.setMassSpectrumSPtr(mass_spectrum.makeMassSpectrumSPtr());
qualified_mass_spectrum.setMsLevel(2);
if(cbor_precursor.keys().contains("z"))
{
precursor_ion_data.charge = cbor_precursor.value("z").toInteger();
}
if(cbor_precursor.keys().contains("mz"))
{
precursor_ion_data.mz = cbor_precursor.value("mz").toDouble();
}
if(cbor_precursor.keys().contains("intensity"))
{
precursor_ion_data.intensity = cbor_precursor.value("intensity").toDouble();
}
qualified_mass_spectrum.appendPrecursorIonData(precursor_ion_data);
if(cbor_ms2.keys().contains("rt"))
{
qualified_mass_spectrum.setRtInSeconds(cbor_ms2.value("rt").toDouble());
}
return qualified_mass_spectrum.makeQualifiedMassSpectrumSPtr();
}
void
psm::CborScanMapBase::populateProteinMapUsingOldProteinMap(const PsmProteinMap &old_protein_map,
PsmProteinMap &new_protein_map) const
{
if(isEmpty())
{
throw pappso::PappsoException(QObject::tr("ERROR: cbor scan map is empty"));
}
if(!contains(QString("psm_list")))
{
throw pappso::PappsoException(QObject::tr("ERROR: no psm_list in scan"));
}
for(auto it_psm : value("psm_list").toArray())
{
QCborMap psm = it_psm.toMap();
if(!psm.contains(QString("protein_list")))
{
throw pappso::PappsoException(
QObject::tr("ERROR: no protein_list in psm, psm_list in scan"));
}
for(auto it_protein : psm.value("protein_list").toArray())
{
new_protein_map.insert(
old_protein_map.getByAccession(it_protein.toMap().value("accession").toString()));
}
}
}
void
psm::CborScanMapBase::filterAndSortPsmList()
{
// cbor_writer.writeCborMap(*this);
}
void
psm::CborScanMapBase::filterPsmListUniqueUniqueProforma()
{
struct uniquePsmResult
{
QString proforma;
QCborMap psm;
};
QCborArray old_psm_arr = value("psm_list").toArray();
QCborArray new_psm_arr;
// for(QCborValue cbor_psm : old_psm_arr) {
std::vector<uniquePsmResult> all_psm_list;
for(auto it_psm : old_psm_arr)
{
QCborMap psm_map = it_psm.toMap();
all_psm_list.push_back({psm_map.value("proforma").toString(), psm_map});
}
qDebug();
std::sort(all_psm_list.begin(), all_psm_list.end(), [](uniquePsmResult &a, uniquePsmResult &b) {
return a.proforma > b.proforma;
});
qDebug();
std::vector<uniquePsmResult> unique_psm_list;
for(auto it = all_psm_list.begin(); it != all_psm_list.end(); it++)
{
qDebug() << it->proforma;
if(unique_psm_list.size() > 0)
{
if(unique_psm_list.back().proforma == it->proforma)
{
// merge ?
PsmCborUtils::mergePsmProteinRefList(unique_psm_list.back().psm, it->psm);
}
else
{
unique_psm_list.push_back(*it);
}
}
else
{
unique_psm_list.push_back(*it);
}
qDebug();
}
qDebug() << unique_psm_list.size();
for(auto it = unique_psm_list.begin(); it != unique_psm_list.end(); it++)
{
new_psm_arr.append(it->psm);
qDebug();
}
// qWarning() << new_psm_arr.size();
remove(QString("psm_list"));
insert(QString("psm_list"), new_psm_arr);
}
} // namespace cbor
} // namespace pappso
cborscanmapbase.h
pappso::MassSpectrumId
Definition massspectrumid.h:41
pappso::MassSpectrumId::setNativeId
void setNativeId(const QString &native_id)
Definition massspectrumid.cpp:92
pappso::MassSpectrumId::setSpectrumIndex
void setSpectrumIndex(std::size_t index)
Definition massspectrumid.cpp:106
pappso::MassSpectrum
Class to represent a mass spectrum.
Definition massspectrum.h:70
pappso::MassSpectrum::makeMassSpectrumSPtr
MassSpectrumSPtr makeMassSpectrumSPtr() const
Definition massspectrum.cpp:128
pappso::MsRunId
MS run identity MsRunId identifies an MS run with a unique ID (XmlId) and contains eventually informa...
Definition msrunid.h:54
pappso::MsRunId::setSampleName
void setSampleName(const QString &name)
set a sample name for this MsRunId
Definition msrunid.cpp:77
pappso::PappsoException
Definition pappsoexception.h:41
pappso::QualifiedMassSpectrum
Class representing a fully specified mass spectrum.
Definition qualifiedmassspectrum.h:99
pappso::QualifiedMassSpectrum::appendPrecursorIonData
void appendPrecursorIonData(const PrecursorIonData &precursor_ion_data)
Definition qualifiedmassspectrum.cpp:443
pappso::QualifiedMassSpectrum::setMsLevel
void setMsLevel(uint ms_level)
Set the mass spectrum level.
Definition qualifiedmassspectrum.cpp:181
pappso::QualifiedMassSpectrum::makeQualifiedMassSpectrumSPtr
QualifiedMassSpectrumSPtr makeQualifiedMassSpectrumSPtr() const
Definition qualifiedmassspectrum.cpp:107
pappso::QualifiedMassSpectrum::setMassSpectrumSPtr
void setMassSpectrumSPtr(MassSpectrumSPtr massSpectrum)
Set the MassSpectrumSPtr.
Definition qualifiedmassspectrum.cpp:122
pappso::QualifiedMassSpectrum::setRtInSeconds
void setRtInSeconds(pappso_double rt)
Set the retention time in seconds.
Definition qualifiedmassspectrum.cpp:197
pappso::cbor::psm::CborScanMapBase::getCborScanPrecursor
QCborMap getCborScanPrecursor() const
Definition cborscanmapbase.cpp:65
pappso::cbor::psm::CborScanMapBase::process
virtual void process()
Definition cborscanmapbase.cpp:50
pappso::cbor::psm::CborScanMapBase::filterAndSortPsmList
virtual void filterAndSortPsmList()
Definition cborscanmapbase.cpp:261
pappso::cbor::psm::CborScanMapBase::filterPsmListUniqueUniqueProforma
void filterPsmListUniqueUniqueProforma()
Definition cborscanmapbase.cpp:268
pappso::cbor::psm::CborScanMapBase::addPsmEvalVectorDouble
std::vector< double >::iterator addPsmEvalVectorDouble(const QString &eval_name, const QString &eval_value_key, std::vector< double >::iterator begin, std::vector< double >::const_iterator end)
add a new eval key and double values (from a vector) to each PSM
Definition cborscanmapbase.cpp:86
pappso::cbor::psm::CborScanMapBase::m_PsmFileScanProcess
const PsmFileScanProcess & m_PsmFileScanProcess
Definition cborscanmapbase.h:85
pappso::cbor::psm::CborScanMapBase::getCborPsmList
QCborArray getCborPsmList() const
Definition cborscanmapbase.cpp:75
pappso::cbor::psm::CborScanMapBase::CborScanMapBase
CborScanMapBase(const PsmFileScanProcess &psm_file_scan_process)
Definition cborscanmapbase.cpp:40
pappso::cbor::psm::CborScanMapBase::~CborScanMapBase
virtual ~CborScanMapBase()
Definition cborscanmapbase.cpp:45
pappso::cbor::psm::CborScanMapBase::populateProteinMapUsingOldProteinMap
void populateProteinMapUsingOldProteinMap(const PsmProteinMap &old_protein_map, PsmProteinMap &new_protein_map) const
Definition cborscanmapbase.cpp:230
pappso::cbor::psm::CborScanMapBase::getCborScanId
QCborMap getCborScanId() const
Definition cborscanmapbase.cpp:55
pappso::cbor::psm::CborScanMapBase::getCurrentQualifiedMassSpectrumSPtr
pappso::QualifiedMassSpectrumSPtr getCurrentQualifiedMassSpectrumSPtr() const
Definition cborscanmapbase.cpp:136
pappso::cbor::psm::PsmCborUtils::mergePsmProteinRefList
static void mergePsmProteinRefList(QCborMap &cbor_psm_destination, const QCborMap &cbor_psm_source)
Definition psmcborutils.cpp:150
pappso::cbor::psm::PsmFileScanProcess
Basic PSM file reader to process scan (parallelized scan processing).
Definition psmfilescanprocess.h:48
pappso::cbor::psm::PsmProteinMap
store PsmProtein in a map with accession as key
Definition psmproteinmap.h:56
pappso::cbor::psm::PsmProteinMap::getByAccession
const pappso::cbor::psm::PsmProtein & getByAccession(const QString &accession) const
retrieve a PsmProtein with its accession
Definition psmproteinmap.cpp:164
pappso::cbor::psm::PsmProteinMap::insert
std::pair< std::map< QString, PsmProtein >::iterator, bool > insert(const PsmProtein &psm_protein)
standard map insert of a PsmProtein (accession is the key)
Definition psmproteinmap.cpp:50
pappso::cbor::psm
Definition cborscanmapbase.h:40
pappso::cbor
Definition cborstreamreader.cpp:35
pappso
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition aa.cpp:39
pappso::QualifiedMassSpectrumSPtr
std::shared_ptr< QualifiedMassSpectrum > QualifiedMassSpectrumSPtr
Definition qualifiedmassspectrum.h:43
pappso::PeptideIonNter::a
@ a
Definition peptide.h:77
pappso::PeptideIonNter::b
@ b
Definition peptide.h:74
pappso::MsRunIdCstSPtr
std::shared_ptr< const MsRunId > MsRunIdCstSPtr
Definition msrunid.h:46
pappsoexception.h
psmcborutils.h
psmfilescanprocess.h
pappso::PrecursorIonData
Definition qualifiedmassspectrum.h:75
pappso::PrecursorIonData::mz
double mz
Definition qualifiedmassspectrum.h:76
pappso::PrecursorIonData::intensity
double intensity
Definition qualifiedmassspectrum.h:78
pappso::PrecursorIonData::charge
int charge
Definition qualifiedmassspectrum.h:77