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

  SECTION(
    "..:: BASIC sequence alignment scan_13970 spectrum easy met on fasta file "
    "RDYVQ[K][-128,10]AQTKEQ ::..",
    "[spoms_13970]")
  {


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

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

    protein_sp = std::make_shared<pappso::Protein>(
      "P35580",
      "MAQRTGLEDPERYLFVDRAVIYNPATQADWTAKKLVWIPSERHGFEAASIKEERGDEVMV"
      "ELAENGKKAMVNKDDIQKMNPPKFSKVEDMAELTCLNEASVLHNLKDRYYSGLIYTYSGL"
      "FCVVINPYKNLPIYSENIIEMYRGKKRHEMPPHIYAISESAYRCMLQDREDQSILCTGES"
      "GAGKTENTKKVIQYLAHVASSHKGRKDHNIPGELERQLLQANPILESFGNAKTVKNDNSS"
      "RFGKFIRINFDVTGYIVGANIETYLLEKSRAVRQAKDERTFHIFYQLLSGAGEHLKSDLL"
      "LEGFNNYRFLSNGYIPIPGQQDKDNFQETMEAMHIMGFSHEEILSMLKVVSSVLQFGNIS"
      "FKKERNTDQASMPENTVAQKLCHLLGMNVMEFTRAILTPRIKVGRDYVQKAQTKEQADFA"
      "VEALAKATYERLFRWLVHRINKALDRTKRQGASFIGILDIAGFEIFELNSFEQLCINYTN"
      "EKLQQLFNHTMFILEQEEYQREGIEWNFIDFGLDLQPCIDLIERPANPPGVLALLDEECW"
      "FPKATDKTFVEKLVQEQGSHSKFQKPRQLKDKADFCIIHYAGKVDYKADEWLMKNMDPLN"
      "DNVATLLHQSSDRFVAELWKDVDRIVGLDQVTGMTETAFGSAYKTKKGMFRTVGQLYKES"
      "LTKLMATLRNTNPNFVRCIIPNHEKRAGKLDPHLVLDQLRCNGVLEGIRICRQGFPNRIV"
      "FQEFRQRYEILTPNAIPKGFMDGKQACERMIRALELDPNLYRIGQSKIFFRAGVLAHLEE"
      "ERDLKITDIIIFFQAVCRGYLARKAFAKKQQQLSALKVLQRNCAAYLKLRHWQWWRVFTK"
      "VKPLLQVTRQEEELQAKDEELLKVKEKQTKVEGELEEMERKHQQLLEEKNILAEQLQAET"
      "ELFAEAEEMRARLAAKKQELEEILHDLESRVEEEEERNQILQNEKKKMQAHIQDLEEQLD"
      "EEEGARQKLQLEKVTAEAKIKKMEEEILLLEDQNSKFIKEKKLMEDRIAECSSQLAEEEE"
      "KAKNLAKIRNKQEVMISDLEERLKKEEKTRQELEKAKRKLDGETTDLQDQIAELQAQIDE"
      "LKLQLAKKEEELQGALARGDDETLHKNNALKVVRELQAQIAELQEDFESEKASRNKAEKQ"
      "KRDLSEELEALKTELEDTLDTTAAQQELRTKREQEVAELKKALEEETKNHEAQIQDMRQR"
      "HATALEELSEQLEQAKRFKANLEKNKQGLETDNKELACEVKVLQQVKAESEHKRKKLDAQ"
      "VQELHAKVSEGDRLRVELAEKASKLQNELDNVSTLLEEAEKKGIKFAKDAASLESQLQDT"
      "QELLQEETRQKLNLSSRIRQLEEEKNSLQEQQEEEEEARKNLEKQVLALQSQLADTKKKV"
      "DDDLGTIESLEEAKKKLLKDAEALSQRLEEKALAYDKLEKTKNRLQQELDDLTVDLDHQR"
      "QVASNLEKKQKKFDQLLAEEKSISARYAEERDRAEAEAREKETKALSLARALEEALEAKE"
      "EFERQNKQLRADMEDLMSSKDDVGKNVHELEKSKRALEQQVEEMRTQLEELEDELQATED"
      "AKLRLEVNMQAMKAQFERDLQTRDEQNEEKKRLLIKQVRELEAELEDERKQRALAVASKK"
      "KMEIDLKDLEAQIEAANKARDEVIKQLRKLQAQMKDYQRELEEARASRDEIFAQSKESEK"
      "KLKSLEAEILQLQEELASSERARRHAEQERDELADEITNSASGKSALLDEKRRLEARIAQ"
      "LEEELEEEQSNMELLNDRFRKTTLQVDTLNAELAAERSAAQKSDNARQQLERQNKELKAK"
      "LQELEGAVKSKFKATISALEAKIGQLEEQLEQEAKERAAANKLVRRTEKKLKEIFMQVED"
      "ERRHADQYKEQMEKANARMKQLKRQLEEAEEEATRANASRRKLQRELDDATEANEGLSRE"
      "VSTLKNRLRRGGPISFSSSRSGRRQLHLEGASLELSDDDTESKTSDVNETQPPQSE");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "Q7Z406",
      "MAAVTMSVPGRKAPPRPGPVPEAAQPFLFTPRGPSAGGGPGSGTSPQVEWTARRLVWVPS"
      "ELHGFEAAALRDEGEEEAEVELAESGRRLRLPRDQIQRMNPPKFSKAEDMAELTCLNEAS"
      "VLHNLRERYYSGLIYTYSGLFCVVINPYKQLPIYTEAIVEMYRGKKRHEVPPHVYAVTEG"
      "AYRSMLQDREDQSILCTGESGAGKTENTKKVIQYLAHVASSPKGRKEPGVPGELERQLLQ"
      "ANPILEAFGNAKTVKNDNSSRFGKFIRINFDVAGYIVGANIETYLLEKSRAIRQAKDECS"
      "FHIFYQLLGGAGEQLKADLLLEPCSHYRFLTNGPSSSPGQERELFQETLESLRVLGFSHE"
      "EIISMLRMVSAVLQFGNIALKRERNTDQATMPDNTAAQKLCRLLGLGVTDFSRALLTPRI"
      "KVGRDYVQKAQTKEQADFALEALAKATYERLFRWLVLRLNRALDRSPRQGASFLGILDIA"
      "GFEIFQLNSFEQLCINYTNEKLQQLFNHTMFVLEQEEYQREGIPWTFLDFGLDLQPCIDL"
      "IERPANPPGLLALLDEECWFPKATDKSFVEKVAQEQGGHPKFQRPRHLRDQADFSVLHYA"
      "GKVDYKANEWLMKNMDPLNDNVAALLHQSTDRLTAEIWKDVEGIVGLEQVSSLGDGPPGG"
      "RPRRGMFRTVGQLYKESLSRLMATLSNTNPSFVRCIVPNHEKRAGKLEPRLVLDQLRCNG"
      "VLEGIRICRQGFPNRILFQEFRQRYEILTPNAIPKGFMDGKQACEKMIQALELDPNLYRV"
      "GQSKIFFRAGVLAQLEEERDLKVTDIIVSFQAAARGYLARRAFQKRQQQQSALRVMQRNC"
      "AAYLKLRHWQWWRLFTKVKPLLQVTRQDEVLQARAQELQKVQELQQQSAREVGELQGRVA"
      "QLEEERARLAEQLRAEAELCAEAEETRGRLAARKQELELVVSELEARVGEEEECSRQMQT"
      "EKKRLQQHIQELEAHLEAEEGARQKLQLEKVTTEAKMKKFEEDLLLLEDQNSKLSKERKL"
      "LEDRLAEFSSQAAEEEEKVKSLNKLRLKYEATIADMEDRLRKEEKGRQELEKLKRRLDGE"
      "SSELQEQMVEQQQRAEELRAQLGRKEEELQAALARAEDEGGARAQLLKSLREAQAALAEA"
      "QEDLESERVARTKAEKQRRDLGEELEALRGELEDTLDSTNAQQELRSKREQEVTELKKTL"
      "EEETRIHEAAVQELRQRHGQALGELAEQLEQARRGKGAWEKTRLALEAEVSELRAELSSL"
      "QTARQEGEQRRRRLELQLQEVQGRAGDGERARAEAAEKLQRAQAELENVSGALNEAESKT"
      "IRLSKELSSTEAQLHDAQELLQEETRAKLALGSRVRAMEAEAAGLREQLEEEAAARERAG"
      "RELQTAQAQLSEWRRRQEEEAGALEAGEEARRRAAREAEALTQRLAEKTETVDRLERGRR"
      "RLQQELDDATMDLEQQRQLVSTLEKKQRKFDQLLAEEKAAVLRAVEERERAEAEGREREA"
      "RALSLTRALEEEQEAREELERQNRALRAELEALLSSKDDVGKSVHELERACRVAEQAAND"
      "LRAQVTELEDELTAAEDAKLRLEVTVQALKTQHERDLQGRDEAGEERRRQLAKQLRDAEV"
      "ERDEERKQRTLAVAARKKLEGELEELKAQMASAGQGKEEAVKQLRKMQAQMKELWREVEE"
      "TRTSREEIFSQNRESEKRLKGLEAEVLRLQEELAASDRARRQAQQDRDEMADEVANGNLS"
      "KAAILEEKRQLEGRLGQLEEELEEEQSNSELLNDRYRKLLLQVESLTTELSAERSFSAKA"
      "ESGRQQLERQIQELRGRLGEEDAGARARHKMTIAALESKLAQAEEQLEQETRERILSGKL"
      "VRRAEKRLKEVVLQVEEERRVADQLRDQLEKGNLRVKQLKRQLEEAEEEASRAQAGRRRL"
      "QRELEDVTESAESMNREVTTLRNRLRRGPLTFTTRTVRQVFRLEEGVASDEEAEEAQPGS"
      "GPSPEPEGSPPAHPQ");
    protein_list.addProtein(protein_sp);

    protein_sp = std::make_shared<pappso::Protein>(
      "P35579",
      "MAQQAADKYLYVDKNFINNPLAQADWAAKKLVWVPSDKSGFEPASLKEEVGEEAIVELVE"
      "NGKKVKVNKDDIQKMNPPKFSKVEDMAELTCLNEASVLHNLKERYYSGLIYTYSGLFCVV"
      "INPYKNLPIYSEEIVEMYKGKKRHEMPPHIYAITDTAYRSMMQDREDQSILCTGESGAGK"
      "TENTKKVIQYLAYVASSHKSKKDQGELERQLLQANPILEAFGNAKTVKNDNSSRFGKFIR"
      "INFDVNGYIVGANIETYLLEKSRAIRQAKEERTFHIFYYLLSGAGEHLKTDLLLEPYNKY"
      "RFLSNGHVTIPGQQDKDMFQETMEAMRIMGIPEEEQMGLLRVISGVLQLGNIVFKKERNT"
      "DQASMPDNTAAQKVSHLLGINVTDFTRGILTPRIKVGRDYVQKAQTKEQADFAIEALAKA"
      "TYERMFRWLVLRINKALDKTKRQGASFIGILDIAGFEIFDLNSFEQLCINYTNEKLQQLF"
      "NHTMFILEQEEYQREGIEWNFIDFGLDLQPCIDLIEKPAGPPGILALLDEECWFPKATDK"
      "SFVEKVMQEQGTHPKFQKPKQLKDKADFCIIHYAGKVDYKADEWLMKNMDPLNDNIATLL"
      "HQSSDKFVSELWKDVDRIIGLDQVAGMSETALPGAFKTRKGMFRTVGQLYKEQLAKLMAT"
      "LRNTNPNFVRCIIPNHEKKAGKLDPHLVLDQLRCNGVLEGIRICRQGFPNRVVFQEFRQR"
      "YEILTPNSIPKGFMDGKQACVLMIKALELDSNLYRIGQSKVFFRAGVLAHLEEERDLKIT"
      "DVIIGFQACCRGYLARKAFAKRQQQLTAMKVLQRNCAAYLKLRNWQWWRLFTKVKPLLQV"
      "SRQEEEMMAKEEELVKVREKQLAAENRLTEMETLQSQLMAEKLQLQEQLQAETELCAEAE"
      "ELRARLTAKKQELEEICHDLEARVEEEEERCQHLQAEKKKMQQNIQELEEQLEEEESARQ"
      "KLQLEKVTTEAKLKKLEEEQIILEDQNCKLAKEKKLLEDRIAEFTTNLTEEEEKSKSLAK"
      "LKNKHEAMITDLEERLRREEKQRQELEKTRRKLEGDSTDLSDQIAELQAQIAELKMQLAK"
      "KEEELQAALARVEEEAAQKNMALKKIRELESQISELQEDLESERASRNKAEKQKRDLGEE"
      "LEALKTELEDTLDSTAAQQELRSKREQEVNILKKTLEEEAKTHEAQIQEMRQKHSQAVEE"
      "LAEQLEQTKRVKANLEKAKQTLENERGELANEVKVLLQGKGDSEHKRKKVEAQLQELQVK"
      "FNEGERVRTELADKVTKLQVELDNVTGLLSQSDSKSSKLTKDFSALESQLQDTQELLQEE"
      "NRQKLSLSTKLKQVEDEKNSFREQLEEEEEAKHNLEKQIATLHAQVADMKKKMEDSVGCL"
      "ETAEEVKRKLQKDLEGLSQRHEEKVAAYDKLEKTKTRLQQELDDLLVDLDHQRQSACNLE"
      "KKQKKFDQLLAEEKTISAKYAEERDRAEAEAREKETKALSLARALEEAMEQKAELERLNK"
      "QFRTEMEDLMSSKDDVGKSVHELEKSKRALEQQVEEMKTQLEELEDELQATEDAKLRLEV"
      "NLQAMKAQFERDLQGRDEQSEEKKKQLVRQVREMEAELEDERKQRSMAVAARKKLEMDLK"
      "DLEAHIDSANKNRDEAIKQLRKLQAQMKDCMRELDDTRASREEILAQAKENEKKLKSMEA"
      "EMIQLQEELAAAERAKRQAQQERDELADEIANSSGKGALALEEKRRLEARIAQLEEELEE"
      "EQGNTELINDRLKKANLQIDQINTDLNLERSHAQKNENARQQLERQNKELKVKLQEMEGT"
      "VKSKYKASITALEAKIAQLEEQLDNETKERQAACKQVRRTEKKLKDVLLQVDDERRNAEQ"
      "YKDQADKASTRLKQLKRQLEEAEEEAQRANASRRKLQRELEDATETADAMNREVSSLKNK"
      "LRRGDLPFVVPRRMARKGAGDGSDEEVDGKADGAEAKPAE");
    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 == 25);
    REQUIRE(location_list.at(0).score == 61);

    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 == 4);
    REQUIRE(location_list.at(0).end_line == 25);
    REQUIRE(location_list.at(0).score == 61);

    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 == 1556);
    REQUIRE(location_list.at(0).end_line == 1577);
    REQUIRE(location_list.at(0).score == 61);

    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 == 1556);
    REQUIRE(location_list.at(0).end_line == 1577);
    REQUIRE(location_list.at(0).score == 61);

    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 == 1547);
    REQUIRE(location_list.at(0).end_line == 1568);
    REQUIRE(location_list.at(0).score == 61);

    auto alignement_list = protein_list.alignQualifiedSpectrum(qmsp);

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

    auto alignment = alignement_list.at(0).alignment_sp;
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->computeHitModifiedAndRealSequence()
              .at(0)
              .toStdString() == "RDYVQ[K][-128.09]AQTKEQ");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toInterpretation()
              .toStdString() == "RDYVQ[K][-128.09]AQTKEQ");
    REQUIRE(alignement_list.at(0)
              .alignment_sp.get()
              ->getPeptideModel()
              .get()
              ->toProForma()
              .toStdString() == "RDYVQK[-128.09]AQTKEQ");
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getScore() == 75);
    REQUIRE(alignement_list.at(0).alignment_sp.get()->getNonAlignedMass() == 0);
    std::vector<double> nona_mass_list =
      alignment.get()->computePossibleNonAlignedMasses();
    REQUIRE(nona_mass_list.size() == 0);
  }
}
