Loading...
Searching...
No Matches
sagereader.cpp
Go to the documentation of this file.
/**
* \file input/sage/sagereader.cpp
* \date 21/08/2024
* \author Olivier Langella
* \brief read data files from Sage output
*/
/*******************************************************************************
* Copyright (c) 2024 Olivier Langella
*<Olivier.Langella@universite-paris-saclay.fr>.
*
* This file is part of i2MassChroQ.
*
* i2MassChroQ 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.
*
* i2MassChroQ 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 i2MassChroQ. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "sagereader.h"
#include <QJsonObject>
#include <QJsonArray>
#include <odsstream/tsvreader.h>
#include <odsstream/odsexception.h>
#include <QUrl>
#include <qcontainerfwd.h>
#include <qfileinfo.h>
#include <qlogging.h>
#include <qobject.h>
#include "sagetsvhandler.h"
#include "pappsomspp/core/utils.h"
#include "pappsomspp/core/exception/exceptionnotfound.h"
#include "pappsomspp/core/pappsoexception.h"
#include "../../../../fasta/fastareader.h"
pappso::cbor::psm::SageReader::SageReader(pappso::UiMonitorInterface *p_monitor,
pappso::cbor::CborStreamWriter *p_output,
const pappso::cbor::psm::SageFileReader &sage_file_reader,
const QString &sage_json_file)
: m_sageFileReader(sage_file_reader)
{
mp_monitor = p_monitor;
mp_cborWriter = p_output;
m_jsonAbsoluteFilePath = sage_json_file;
}
pappso::cbor::psm::SageReader::~SageReader()
{
}
const QString &
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath() const
{
return m_jsonAbsoluteFilePath;
}
void
pappso::cbor::psm::SageReader::read()
{
readTsvFile();
}
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq(SageReader *self)
{
mp_self = self;
m_decoyTag = mp_self->getDecoyTag();
}
void
pappso::cbor::psm::SageReader::FastaSeq::setSequence(const QString &description_in,
const QString &sequence_in)
{
QStringList description_split = description_in.split(" ", Qt::SkipEmptyParts);
QString accession = description_split.at(0);
try
{
const PsmProtein &psm_protein = mp_self->m_psmProteinMap.getByAccession(accession);
psm_protein.protein_sp.get()->setSequence(sequence_in);
psm_protein.protein_sp.get()->setDescription(description_in);
}
catch(pappso::ExceptionNotFound &err)
{
}
try
{
QString rev_accession = accession.prepend(m_decoyTag);
const PsmProtein &psm_protein = mp_self->m_psmProteinMap.getByAccession(rev_accession);
psm_protein.protein_sp.get()->setSequence(sequence_in);
description_split[0] = rev_accession;
psm_protein.protein_sp.get()->setDescription(description_split.join(" "));
psm_protein.protein_sp.get()->reverse();
}
catch(pappso::ExceptionNotFound &err)
{
}
}
pappso::cbor::CborStreamWriter &
pappso::cbor::psm::SageReader::getCborStreamWriter() const
{
return *mp_cborWriter;
}
void
pappso::cbor::psm::SageReader::readTsvFile()
{
extractMzmlPathList(m_sageFileReader.getJsonDocument());
// getTsvFilePath(mp_identificationDataSource->getJsonDocument().object());
QString file_str = getTsvFilePath(m_sageFileReader.getJsonDocument());
QFileInfo tsv_file_info(file_str);
SageTsvHandler handler(mp_monitor, *this, m_psmProteinMap);
try
{
TsvReader tsv_reader(handler);
QFile tsv_file(tsv_file_info.absoluteFilePath());
tsv_reader.parse(tsv_file);
tsv_file.close();
}
catch(OdsException &error_ods)
{
throw pappso::PappsoException(QObject::tr("Error reading %1 file:\n %2")
.arg(tsv_file_info.absoluteFilePath())
.arg(error_ods.qwhat()));
}
// collect protein sequences
QFile fastaFile(getFastaFilePath(m_sageFileReader.getJsonDocument()));
SageReader::FastaSeq seq(this);
pappso::FastaReader reader(seq);
reader.parse(fastaFile);
qDebug();
mp_cborWriter->append("protein_map");
m_psmProteinMap.writeMap(*mp_cborWriter);
mp_cborWriter->append("sample_list");
mp_cborWriter->startArray();
try
{
handler.writeSampleList();
}
catch(OdsException &error_ods)
{
throw pappso::PappsoException(QObject::tr("Error reading %1 file:\n %2")
.arg(tsv_file_info.absoluteFilePath())
.arg(error_ods.qwhat()));
}
mp_cborWriter->endArray();
}
void
pappso::cbor::psm::SageReader::extractMzmlPathList(const QJsonDocument &json_doc)
{
QJsonObject sage_object = json_doc.object();
QJsonValue json_mzml_path_list = sage_object.value("mzml_paths");
if(json_mzml_path_list.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("mzml_paths not found in Sage json document"));
}
m_mzmlPathList.clear();
for(auto path_mzml : json_mzml_path_list.toArray())
{
m_mzmlPathList << convertToLocalFileOrDie(path_mzml.toString());
}
}
const QString &
pappso::cbor::psm::SageReader::getMzmlPath(const QString &file_msrun) const
{
for(auto &file_path : m_mzmlPathList)
{
if(file_path.endsWith(file_msrun))
return file_path;
}
throw pappso::ExceptionNotFound(
QObject::tr("MS run %1 not found in Sage json document").append(file_msrun));
}
QString
pappso::cbor::psm::SageReader::getTsvFilePath(const QJsonDocument &json_doc)
{
QString path;
QJsonObject sage_object = json_doc.object();
QJsonValue output_path = sage_object.value("output_paths");
if(output_path.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("output_paths not found in Sage json document"));
}
if(!output_path.isArray())
{
throw pappso::ExceptionNotFound(QObject::tr("output_paths is not an array"));
}
for(auto element : output_path.toArray())
{
if(element.isString())
{
if(element.toString().endsWith(".tsv"))
{
path = element.toString();
}
}
}
return convertToLocalFileOrDie(path);
}
QString
pappso::cbor::psm::SageReader::getFastaFilePath(const QJsonDocument &json_doc)
{
QString path;
QJsonObject sage_object = json_doc.object();
QJsonValue database = sage_object.value("database");
if(database.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("database not found in Sage json document"));
}
path = database.toObject().value("fasta").toString();
if(path.isEmpty())
{
throw pappso::ExceptionNotFound(QObject::tr("fasta value is empty"));
}
return convertToLocalFileOrDie(path);
}
QString
pappso::cbor::psm::SageReader::convertToLocalFileOrDie(const QString &path) const
{
// if we have an URL : convert it to local file
qDebug() << path;
if(path.startsWith("file:") || path.startsWith("http:") || path.startsWith("https:"))
{
QUrl tsv_url(path);
if(tsv_url.isValid())
{
qDebug() << "tsv_url.isValid()";
if(tsv_url.isLocalFile())
{
qDebug() << "tsv_url.isLocalFile()";
return tsv_url.toLocalFile();
}
else
{
throw pappso::PappsoException(
QObject::tr("Unable to load data from remote URL %1").arg(tsv_url.toString()));
}
}
}
return path;
}
std::vector<pappso::cbor::psm::SageReader::SageModification>
pappso::cbor::psm::SageReader::getStaticModificationList() const
{
std::vector<SageReader::SageModification> list;
QJsonObject sage_object = m_sageFileReader.getJsonDocument().object();
QJsonValue database = sage_object.value("database");
if(database.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("database not found in Sage json document"));
}
QJsonValue static_mods = database.toObject().value("static_mods");
if(static_mods.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("static_mods not found in Sage json document"));
}
for(QString residue_str : static_mods.toObject().keys())
{
SageModification modif;
modif.residue = residue_str.at(0);
modif.modification = pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta(
(Enums::AminoAcidChar)modif.residue.toLatin1(),
static_mods.toObject().value(residue_str).toDouble());
modif.strModification =
QString::number(static_mods.toObject().value(residue_str).toDouble(), 'f', 6);
if(modif.strModification.isEmpty())
{
throw pappso::PappsoException(QObject::tr(" modif.strModification is empty"));
}
if(modif.modification->getMass() < 0)
{
modif.strModification = QString("[%1]").arg(modif.strModification);
}
else
{
modif.strModification = QString("[+%1]").arg(modif.strModification);
}
list.push_back(modif);
}
return list;
}
std::vector<pappso::cbor::psm::SageReader::SageModification>
pappso::cbor::psm::SageReader::getVariableModificationList() const
{
std::vector<SageReader::SageModification> list;
QJsonObject sage_object = m_sageFileReader.getJsonDocument().object();
QJsonValue database = sage_object.value("database");
if(database.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("database not found in Sage json document"));
}
QJsonValue var_mods = database.toObject().value("variable_mods");
if(var_mods.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("static_mods not found in Sage json document"));
}
for(QString residue_str : var_mods.toObject().keys())
{
SageModification modif;
modif.residue = residue_str.at(0);
for(QJsonValue one_mass : var_mods.toObject().value(residue_str).toArray())
{
modif.modification = pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta(
(Enums::AminoAcidChar)modif.residue.toLatin1(), one_mass.toDouble());
modif.strModification = QString::number(one_mass.toDouble(), 'f', 6);
if(modif.strModification.isEmpty())
{
throw pappso::PappsoException(QObject::tr(" modif.strModification is empty"));
}
if(modif.modification->getMass() < 0)
{
modif.strModification = QString("[%1]").arg(modif.strModification);
}
else
{
modif.strModification = QString("[+%1]").arg(modif.strModification);
}
list.push_back(modif);
}
}
return list;
}
QString
pappso::cbor::psm::SageReader::getDecoyTag() const
{
QString path;
QJsonObject sage_object = m_sageFileReader.getJsonDocument().object();
QJsonValue database = sage_object.value("database");
if(database.isUndefined())
{
throw pappso::ExceptionNotFound(QObject::tr("database not found in Sage json document"));
}
path = database.toObject().value("decoy_tag").toString();
if(path.isEmpty())
{
throw pappso::ExceptionNotFound(QObject::tr("decoy_tag value is empty"));
}
return path;
}
const pappso::cbor::psm::SageFileReader &
pappso::cbor::psm::SageReader::getSageFileReader() const
{
return m_sageFileReader;
}
pappso::AaModification::getMass
pappso_double getMass() const
Definition aamodification.cpp:759
pappso::ExceptionNotFound
Definition exceptionnotfound.h:32
pappso::FastaReader
Definition fastareader.h:34
pappso::FastaReader::parse
void parse(QFile &fastaFile)
Definition fastareader.cpp:39
pappso::PappsoException
Definition pappsoexception.h:41
pappso::UiMonitorInterface
Definition uimonitorinterface.h:50
pappso::Utils::guessAaModificationPbyMonoisotopicMassDelta
static AaModificationP guessAaModificationPbyMonoisotopicMassDelta(Enums::AminoAcidChar aa, pappso_double mass)
Definition utils.cpp:677
pappso::cbor::CborStreamWriter
overrides QCborStreamWriter base class to provide convenient functions
Definition cborstreamwriter.h:45
pappso::cbor::psm::SageFileReader
Definition sagefilereader.h:48
pappso::cbor::psm::SageReader::FastaSeq
Definition sagereader.h:87
pappso::cbor::psm::SageReader::FastaSeq::mp_self
SageReader * mp_self
Definition sagereader.h:93
pappso::cbor::psm::SageReader::FastaSeq::setSequence
void setSequence(const QString &description_in, const QString &sequence_in) override
Definition sagereader.cpp:80
pappso::cbor::psm::SageReader::FastaSeq::m_decoyTag
QString m_decoyTag
Definition sagereader.h:94
pappso::cbor::psm::SageReader::FastaSeq::FastaSeq
FastaSeq(SageReader *self)
Definition sagereader.cpp:73
pappso::cbor::psm::SageReader::m_psmProteinMap
PsmProteinMap m_psmProteinMap
Definition sagereader.h:101
pappso::cbor::psm::SageReader::getMzmlPath
const QString & getMzmlPath(const QString &file_msrun) const
Definition sagereader.cpp:188
pappso::cbor::psm::SageReader::SageReader
SageReader(pappso::UiMonitorInterface *p_monitor, pappso::cbor::CborStreamWriter *p_output, const SageFileReader &sage_file_reader, const QString &sage_json_file)
Definition sagereader.cpp:45
pappso::cbor::psm::SageReader::getStaticModificationList
std::vector< SageModification > getStaticModificationList() const
Definition sagereader.cpp:277
pappso::cbor::psm::SageReader::readTsvFile
void readTsvFile()
Definition sagereader.cpp:118
pappso::cbor::psm::SageReader::m_mzmlPathList
QStringList m_mzmlPathList
Definition sagereader.h:103
pappso::cbor::psm::SageReader::mp_cborWriter
pappso::cbor::CborStreamWriter * mp_cborWriter
Definition sagereader.h:100
pappso::cbor::psm::SageReader::getmJsonAbsoluteFilePath
const QString & getmJsonAbsoluteFilePath() const
Definition sagereader.cpp:61
pappso::cbor::psm::SageReader::extractMzmlPathList
void extractMzmlPathList(const QJsonDocument &json_doc)
Definition sagereader.cpp:170
pappso::cbor::psm::SageReader::~SageReader
virtual ~SageReader()
Definition sagereader.cpp:56
pappso::cbor::psm::SageReader::getCborStreamWriter
pappso::cbor::CborStreamWriter & getCborStreamWriter() const
Definition sagereader.cpp:111
pappso::cbor::psm::SageReader::m_jsonAbsoluteFilePath
QString m_jsonAbsoluteFilePath
Definition sagereader.h:102
pappso::cbor::psm::SageReader::m_sageFileReader
const SageFileReader & m_sageFileReader
Definition sagereader.h:98
pappso::cbor::psm::SageReader::getVariableModificationList
std::vector< SageModification > getVariableModificationList() const
Definition sagereader.cpp:319
pappso::cbor::psm::SageReader::getFastaFilePath
QString getFastaFilePath(const QJsonDocument &json_doc)
Definition sagereader.cpp:230
pappso::cbor::psm::SageReader::getDecoyTag
QString getDecoyTag() const
Definition sagereader.cpp:362
pappso::cbor::psm::SageReader::getSageFileReader
const SageFileReader & getSageFileReader() const
Definition sagereader.cpp:380
pappso::cbor::psm::SageReader::getTsvFilePath
QString getTsvFilePath(const QJsonDocument &json_doc)
Definition sagereader.cpp:201
pappso::cbor::psm::SageReader::mp_monitor
pappso::UiMonitorInterface * mp_monitor
Definition sagereader.h:99
pappso::cbor::psm::SageReader::read
void read()
Definition sagereader.cpp:68
pappso::cbor::psm::SageReader::convertToLocalFileOrDie
QString convertToLocalFileOrDie(const QString &file_str) const
Definition sagereader.cpp:250
pappso::cbor::psm::SageTsvHandler
Definition sagetsvhandler.h:50
pappso::cbor::psm::SageTsvHandler::writeSampleList
void writeSampleList()
Definition sagetsvhandler.cpp:64
exceptionnotfound.h
fastareader.h
pappso::Enums::AminoAcidChar
AminoAcidChar
Definition types.h:196
pappsoexception.h
sagereader.h
sagetsvhandler.h
pappso::cbor::psm::PsmProtein
Definition psmproteinmap.h:46
pappso::cbor::psm::PsmProtein::protein_sp
std::shared_ptr< Protein > protein_sp
Definition psmproteinmap.h:47
pappso::cbor::psm::SageReader::SageModification
Definition sagereader.h:60
pappso::cbor::psm::SageReader::SageModification::residue
QChar residue
Definition sagereader.h:62
pappso::cbor::psm::SageReader::SageModification::strModification
QString strModification
Definition sagereader.h:63
pappso::cbor::psm::SageReader::SageModification::modification
pappso::AaModificationP modification
Definition sagereader.h:61
utils.h