/**
 * \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 [spoms_3107] -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/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", "[spoms_3107]")
{
  // 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::..",
          "[spoms_3107]")
  {

    // /home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // ERROR reading spectrum index 3364
    // :/home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // Spectral (index=3107) alignment failed with std exception
    // /home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // protein list final treatment failed with std exception:
    // /home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // precise alignment failed for protein AC211698.5_FGP003 with std
    // exception:
    // /home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // for each amino acid failed with std exception:
    // /home/langella/developpement/git/pappsomspp/src/pappsomspp/core/pappsoexception.h@PappsoException:
    // checking positions failed with std exception: vector::_M_range_check: __n
    // (which is 6) >= this->size() (which is 0)

    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "alignment", "min_scenario_score", 10);
    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/"
                        "20120906_balliau_extract_1_A01_urnb-1_3107.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()));


    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>(
      "AC211698.5_FGP003",
      "MELKPWRRRRIGELGGATGNGQEAQPRAPGHRLGHKLTRMELPEIYTPVRLFLLPLLPLS"
      "LETTASPSRHQQLRSPAAGPYYTHDGMATGNHSHAPRDSTPSVRSFSKNLTSLWMMGNLL"
      "SRGSSCTATGSGGGSSGEGSQRLTNPKA");
    protein_list.addProtein(protein_sp);


    // unknown@0, unknown(): pl 201  "GRMZM2G080274_P01"   0 "[38 ,61], score :
    // 25 , tree 148"   "AAAKSKKAAAAKPKKAAAAGTKRK"


    QualifiedMassSpectrumCstSPtr qmsp =
      spectrum_simple.makeQualifiedMassSpectrumCstSPtr();

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

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

    auto alignement_list = protein_list.alignQualifiedSpectrum(qmsp);

    // with "min_scenario_score": 10,
    REQUIRE(alignement_list.size() == 1);

    auto alignment = alignement_list.at(0).alignment_sp;
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toInterpretation()
              .toStdString() == "[1230.46]YYTH");
  }
}
