Skip to content
Snippets Groups Projects
tandemparametersfile.cpp 4.52 KiB
Newer Older
/**
 * \file /files/tandemparametersfile.cpp
 * \date 19/9/2017
 * \author Olivier Langella
 * \brief handles X!Tandem parameters file (presets)
 */

/*******************************************************************************
* Copyright (c) 2017 Olivier Langella <olivier.langella@u-psud.fr>.
*
* This file is part of XTPcpp.
*
*     XTPcpp 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.
*
*     XTPcpp 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 XTPcpp.  If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
*     Olivier Langella <olivier.langella@u-psud.fr> - initial API and implementation
******************************************************************************/

#include "tandemparametersfile.h"
#include <QXmlSimpleReader>
#include <QXmlStreamWriter>
#include <pappsomspp/pappsoexception.h>
#include "../input/xtandemparamsaxhandler.h"

TandemParametersFile::TandemParametersFile(const QString & fasta_source) : _param_source(fasta_source)
{
    qDebug() << "TandemParametersFile::TandemParametersFile "<< fasta_source;
}
TandemParametersFile::TandemParametersFile(const QFileInfo & fasta_source): _param_source(fasta_source)
{
}
TandemParametersFile::TandemParametersFile(const TandemParametersFile & other) : _param_source(other._param_source)
{

}
TandemParametersFile::~TandemParametersFile()
{

}
const QString TandemParametersFile::getMethodName() const {
    return _param_source.baseName();
}
const QString TandemParametersFile::getFilename() const {
    return _param_source.fileName();
}

const QString TandemParametersFile::getAbsoluteFilePath() const {
    return _param_source.absoluteFilePath();
}

void TandemParametersFile::setTandemParameters(const TandemParameters & parameters) const {

    QFile xml_file(_param_source.absoluteFilePath());
    QXmlStreamWriter * p_out;
    if (xml_file.open(QIODevice::WriteOnly)) {
        p_out = new QXmlStreamWriter();
        p_out->setDevice(&xml_file);
        writeXmlParametersFile(p_out, parameters);
        xml_file.close();
        delete p_out;
    } else
    {
        throw pappso::PappsoException(QObject::tr("error : cannot open the XML X!Tandem parameter file : %1\n").arg(xml_file.fileName()));
    }

}

TandemParameters TandemParametersFile::getTandemParameters() const {
    qDebug() << "TandemParametersFile::getTandemParameters begin";
    TandemParameters parameters;
    XtandemParamSaxHandler * parser = new XtandemParamSaxHandler(&parameters);

    QXmlSimpleReader simplereader;
    simplereader.setContentHandler(parser);
    simplereader.setErrorHandler(parser);

    qDebug() << "TandemParametersFile::getTandemParameters '" << _param_source.absoluteFilePath() << "'";
    parameters.setMethodName(getMethodName());

    QFile qfile(_param_source.absoluteFilePath());
    QXmlInputSource xmlInputSource(&qfile);

    if (simplereader.parse(xmlInputSource)) {

        qfile.close();
    } else {

        qfile.close();
        delete parser;

        throw pappso::PappsoException(QObject::tr("Error reading %1 X!Tandem preset file :\n %2").arg(_param_source.absoluteFilePath()).arg(parser->errorString()));
    }
    delete parser;
    return parameters;
}


void TandemParametersFile::writeXmlParametersFile (QXmlStreamWriter * p_out, const TandemParameters & parameters) const {
    p_out->setAutoFormatting(true);
    p_out->writeStartDocument("1.0");

    //<?xml version="1.0" encoding="UTF-8"?>
    //<bioml label="example api document">
    //<note type="input" label="spectrum, parent monoisotopic mass error units">ppm</note>
    //<note type="input" label="spectrum, parent monoisotopic mass error minus">10</note>
    p_out->writeStartElement("bioml");
    p_out->writeAttribute("label","example api document");
    for (const QString & label :parameters.getMapLabelValue().keys()) {
        p_out->writeStartElement("note");
        p_out->writeAttribute("type","input");
        p_out->writeAttribute("label",label);
        p_out->writeCharacters(parameters.getMapLabelValue().value(label));
        p_out->writeEndElement();
    }
    //</bioml>
    p_out->writeEndElement();
    p_out->writeEndDocument();
}