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

  SECTION(
    "..:: BASIC sequence alignment scan_52144 spectrum easy met on fasta file "
    "[V][A]EEIQDEVDELLQKEQ ::..",
    "[spoms_52144]")
  {

    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "spectrum", "deisotope", false);
    spoms::SpomsUtils::GetInstance()->setParameterValue(
      "spectrum", "minimum_mz", 0);

    pappso::QualifiedMassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR)
                .append("/tests/data/peaklists/"
                        "52144.mgf"));
    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>(
      "peptide649", "AGQVAEEIQDEVDELLQKEQAIATKKII");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "peptide840", "TVEDEKLQVAEEIQDEVDELLQKADNFLLE");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "peptide841", "TVEDEKLQVAEEIQDEVDELLQKADNFLLEN");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "peptide842", "QAQLAAGQVAEEIQDEVDELLQKEQAIATKKII");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "P05067",
      "MLPGLALLLLAAWTARALEVPTDGNAGLLAEPQIAMFCGRLNMHMNVQNGKWDSDPSGTK"
      "TCIDTKEGILQYCQEVYPELQITNVVEANQPVTIQNWCKRGRKQCKTHPHFVIPYRCLVG"
      "EFVSDALLVPDKCKFLHQERMDVCETHLHWHTVAKETCSEKSTNLHDYGMLLPCGIDKFR"
      "GVEFVCCPLAEESDNVDSADAEEDDSDVWWGGADTDYADGSEDKVVEVAEEEEVAEVEEE"
      "EADDDEDDEDGDEVEEEAEEPYEEATERTTSIATTTTTTTESVEEVVREVCSEQAETGPC"
      "RAMISRWYFDVTEGKCAPFFYGGCGGNRNNFDTEEYCMAVCGSAMSQSLLKTTQEPLARD"
      "PVKLPTTAASTPDAVDKYLETPGDENEHAHFQKAKERLEAKHRERMSQVMREWEEAERQA"
      "KNLPKADKKAVIQHFQEKVESLEQEAANERQQLVETHMARVEAMLNDRRRLALENYITAL"
      "QAVPPRPRHVFNMLKKYVRAEQKDRQHTLKHFEHVRMVDPKKAAQIRSQVMTHLRVIYER"
      "MNQSLSLLYNVPAVAEEIQDEVDELLQKEQNYSDDVLANMISEPRISYGNDALMPSLTET"
      "KTTVELLPVNGEFSLDDLQPWHSFGADSVPANTENEVEPVDARPAADRGLTTRPGSGLTN"
      "IKTEEISEVKMDAEFRHDSGYEVHHQKLVFFAEDVGSNKGAIIGLMVGGVVIATVIVITL"
      "VMLKKKQYTSIHHGVVEVDAAVTPEERHLSKMQQNGYENPTYKFFEQMQN");
    protein_list.addProtein(protein_sp);

    QualifiedMassSpectrumCstSPtr qmsp =
      spectrum_simple.makeQualifiedMassSpectrumCstSPtr();

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

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

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

    qDebug() << location_list.size() << location_list.at(0).toString()
             << location_list.at(0).getSubSequence();
    REQUIRE(location_list.size() == 1);
    REQUIRE(location_list.at(0).start_line == 4);
    REQUIRE(location_list.at(0).end_line == 30);
    REQUIRE(location_list.at(0).score == 135);

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

    // qDebug() << location_list.size() << location_list.at(0).toString()
    //          << location_list.at(0).getSubSequence();
    // REQUIRE(location_list.size() == 1);
    // REQUIRE(location_list.at(0).start_line == 291);
    // REQUIRE(location_list.at(0).end_line == 314);
    // REQUIRE(location_list.at(0).score == 128);

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

    // qDebug() << location_list.size() << location_list.at(0).toString()
    //          << location_list.at(0).getSubSequence();
    // REQUIRE(location_list.size() == 1);
    // REQUIRE(location_list.at(0).start_line == 291);
    // REQUIRE(location_list.at(0).end_line == 314);
    // REQUIRE(location_list.at(0).score == 128);

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

    qDebug() << location_list.size() << location_list.at(0).toString()
             << location_list.at(0).getSubSequence();
    REQUIRE(location_list.size() == 1);
    REQUIRE(location_list.at(0).start_line == 4);
    REQUIRE(location_list.at(0).end_line == 30);
    REQUIRE(location_list.at(0).score == 135);

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

    qDebug() << location_list.size() << location_list.at(0).toString()
             << location_list.at(0).getSubSequence();
    REQUIRE(location_list.size() == 1);
    REQUIRE(location_list.at(0).start_line == 196);
    REQUIRE(location_list.at(0).end_line == 222);
    REQUIRE(location_list.at(0).score == 135);

    auto alignement_list = protein_list.alignQualifiedSpectrum(qmsp);

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

    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->computeHitModifiedAndRealSequence()
              .at(0)
              .toStdString() == "[V][A]EEIQDEVDELLQKEQ");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toInterpretation()
              .toStdString() == "[V][A]EEIQDEVDELLQKEQ");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toProForma()
              .toStdString() == "VAEEIQDEVDELLQKEQ");
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getScore() == 135);
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getNonAlignedMass() == 0);
    std::vector<double> nona_mass_list = alignement_list.at(0)
                                           .alignment_sp.get()
                                           ->computePossibleNonAlignedMasses();
    REQUIRE(nona_mass_list.size() == 0);

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