/**
 * \file tests/test_sgpeptidemodel.cpp
 * \date 14/11/2023
 * \author Olivier Langella
 * \brief tests for peptide model
 */


/*
 * SpecGlobTool, Spectra to peptide alignment tool
 * Copyright (C) 2023  Olivier Langella
 * <olivier.langella@universite-paris-saclay.fr>
 *
 * This program 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.
 *
 * This program 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
 *   mp_parent-> along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */
// ./tests/spoms/catch2-spoms-tests [DA_5] -s


#include <QDebug>
#include <QString>
#include <QTextStream>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>
#include <pappsomspp/core/precision.h>
#include <pappsomspp/core/protein/protein.h>
#include <pappsomspp/core/processing/filters/filterchargedeconvolution.h>
#include <pappsomspp/core/processing/filters/filterresample.h>
#include <pappsomspp/core/processing/filters/filterpass.h>
#include <qdebug.h>
#include "../../src/core/spoms/protein_list.h"
#include "../../src/core/spoms/spomsutils.h"
#include "../config.h"
#include "../common.h"

TEST_CASE("spoms test peptide model", "[DA_5]")
{
  // Set the debugging message formatting pattern.scores to apply in comparisons
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION("..:: align mutated sequence GKQQVM[MOD:00425]VGYSDSGK::..", "[DA_5]")
  {
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "alignment", "nb_locations_saved", 5);
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "post_treatment", "shut_down_peaks_cleaning", false);
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "post_treatment", "shut_down_non_aligned_mass", false);

    pappso::QualifiedMassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR)
                .append("/tests/data/peaklists/"
                        "DATNVGDEGGFAPNIIENK_5.mgf"));
    pappso::FilterChargeDeconvolution(
      pappso::PrecisionFactory::getDaltonInstance(0.02))
      .filter(*(spectrum_simple.getMassSpectrumSPtr().get()));
    pappso::FilterResampleKeepGreater(150).filter(
      *(spectrum_simple.getMassSpectrumSPtr().get()));
    pappso::FilterGreatestY(60).filter(
      *(spectrum_simple.getMassSpectrumSPtr().get()));

    REQUIRE(spectrum_simple.size() == 60);
    AaCode aa_code;
    aa_code.addAaModification('C', AaModification::getInstance("MOD:00397"));


    pappso::spoms::ProteinList protein_list(aa_code);

    pappso::ProteinSp protein_sp = std::make_shared<pappso::Protein>(
      "sp|P04198|MYCN_HUMAN",
      "HPAAECVDPAVVFPFPVNKREPAPVPAAPASAPAAGPAVASGAGIAAPAGAPGVAPPRPG");
    protein_list.addProtein(protein_sp);
    /*

    protein_sp = std::make_shared<pappso::Protein>(
      "sp|P06733",
      "MSILKIHAREIFDSRGNPTVEVDLFTSKGLFRAAVPSGASTGIYEALELRDNDKTRYMGK"
      "GVSKAVEHINKTIAPALVSKKLNVTEQEKIDKLMIEMDGTENKSKFGANAILGVSLAVCK"
      "AGAVEKGVPLYRHIADLAGNSEVILPVPAFNVINGGSHAGNKLAMQEFMILPVGAANFRE"
      "AMRIGAEVYHNLKNVIKEKYGKDATNVGDEGGFAPNILENKEGLELLKTAIGKAGYTDKV"
      "VIGMDVAASEFFRSGKYDLDFKSPDDPSRYISPDQLADLYKSFIKDYPVVSIEDPFDQDD"
      "WGAWQKFTASAGIQVVGDDLTVTNPKRIAKAVNEKSCNCLLLKVNQIGSVTESLQACKLA"
      "QANGWGVMVSHRSGETEDTFIADLVVGLCTGQIKTGAPCRSERLAKYNQLLRIEEELGSK"
      "AKFAGRNFRNPLAK");
    protein_list.addProtein(protein_sp);
    pappso::ProteinSp protein_sp = std::make_shared<pappso::Protein>(
      "sp|P0DH78|RN224_HUMAN",
      "MQDAAAGGPPGLGGGGPPEERTDCIICCSAYDLSGHLPRRLYCGHTFCQACVRRLDTPAP"
      "EQRWIPCPQCRQSTPTPRGGVAMLDLDLAAFLAVKAEREPARLEPLPLTSLKGSAITRQP"
      "AGLCPALGPQPHFPQPRYCCWGCGSLCCPPLGSPEV");
    */
    protein_list.addProtein(protein_sp);

    QualifiedMassSpectrumCstSPtr qmsp =
      spectrum_simple.makeQualifiedMassSpectrumCstSPtr();

    std::vector<pappso::spoms::LocationBackup> location_list;

    pappso::spoms::TransformedSpectrum transformed_spectrum(aa_code, qmsp);

    REQUIRE(transformed_spectrum.getNbAaFound() == 137);

    location_list =
      protein_list.getProteinList().at(0).get()->preliminaryTreatmentV1(
        transformed_spectrum,
        aa_code,
        10,
        pappso::spoms::SpomsUtils::GetInstance()->getAlignmentScoreGrid());

    // REQUIRE(location_list.size() == 1);
    // REQUIRE(location_list.at(0).toString().toStdString() ==
    //         "[3 ,39], score : 13 , tree 12");
    // REQUIRE(location_list.at(1).toString().toStdString() ==
    //         "[261 ,301], score : 14 , tree 407");

    // auto alignement_list = protein_list.alignQualifiedSpectrum(qmsp);

    // REQUIRE(alignement_list.size() == 2);
    // REQUIRE(alignement_list.at(0)
    //           .alignment_sp.get()
    //           ->getPeptideModel()
    //           .get()
    //           ->toInterpretation()
    //           .toStdString() ==
    //           "[D][A]TNV[G][D][E][G][G]FAPNII[1.00234]LENK");
  }
}
