/**
 * \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_4291] -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 "../../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_4291]")
{
  // Set the debugging message formatting pattern.scores to apply in comparisons
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION(
    "..:: Check precise alignment for GRMZM2G083841_P01 on spectrum 4291 "
    "[Q][Q]VMV[G][Y]SDS[G][K] ::..",
    "[spoms_4291]")
  {
    // QQVM[MOD:00425]VGYSDSGK
    // 20120906_balliau_extract_1_A01_urnb-1_4291.0;-1;0;VGYSDSGK;[63,70];[502,22]VGYSDS[G][K];42;8;16,00;QQVMVGYSDSGK;[59,70];[Q][Q]VMV[G][Y]SDS[G][K];42;10

    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "post_treatment", "shut_down_peaks_cleaning", false);
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "post_treatment", "shut_down_non_aligned_mass", false);
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "alignment", "nb_locations_saved", 1);

    pappso::QualifiedMassSpectrum spectrum_simple = readMgf(
      QString(CMAKE_SOURCE_DIR)
        .append("/tests/data/peaklists/"
                "20120906_balliau_extract_1_A01_urnb-1_4291_deisotope_60.mgf"));
    pappso::FilterGreatestY(60).filter(
      *(spectrum_simple.getMassSpectrumSPtr().get()));


    AaCode aa_code;
    aa_code.addAaModification('C', AaModification::getInstance("MOD:00397"));

    pappso::ProteinSp protein_sp = std::make_shared<pappso::Protein>(
      "GRMZM2G083841_P01",
      "MATAPSDVLAVELLQRECGVRQPLPVVPLFERLADLQSAPASVERLFSVDWYMDRIKGKQQVMVGYSDSGKD"
      "AGRLSAAWQLYRAQEEMAQV");

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

    protein_list.addProtein(protein_sp);

    QualifiedMassSpectrumCstSPtr qmsp =
      spectrum_simple.makeQualifiedMassSpectrumCstSPtr();


    auto alignement_list = protein_list.alignQualifiedSpectrum(qmsp);

    REQUIRE(alignement_list.size() == 1);

    auto alignment = alignement_list.at(0).alignment_sp;
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->computeHitModifiedAndRealSequence()
              .at(0)
              .toStdString() == "[502.22]VGYSDS[G][K]");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toInterpretation()
              .toStdString() == "[502.22]VGYSDS[G][K]");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toProForma()
              .toStdString() == "[+502.22]-VGYSDSGK");
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getNBPeaksInCommon() ==
            8);
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getScore() == 42);
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getNonAlignedMass() == 0);
    std::vector<double> nona_mass_list =
      alignment.get()->computePossibleNonAlignedMasses();

    REQUIRE_THAT(
      nona_mass_list,
      Catch::Matchers::Approx(std::vector<double>({371.18105919918002655,
                                                   272.1126452846510233,
                                                   144.05406777800303075,
                                                   15.9954902713550382})));


    REQUIRE(alignement_list.at(0).post_processed_alignment_sp.get() != nullptr);

    REQUIRE(alignement_list.at(0)
              .post_processed_alignment_sp.get()
              ->getNBPeaksInCommon() == 10);
    REQUIRE(alignement_list.at(0)
              .post_processed_alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toInterpretation()
              .toStdString() == "[Q][Q]VMV[G][Y]SDS[G][K]");
    REQUIRE(alignement_list.at(0)
              .post_processed_alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toProForma()
              .toStdString() == "[+15.99]?QQVMVGYSDSGK");
  }
}
