/**
 * \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 [spectrum] -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 "../config.h"
#include "../common.h"
#include <pappsomspp/core/massspectrum/massspectrum.h>
#include <pappsomspp/core/massspectrum/qualifiedmassspectrum.h>
#include <pappsomspp/core/precision.h>
#include <pappsomspp/core/processing/filters/filterresample.h>
#include <pappsomspp/core/processing/filters/filterpass.h>
#include <pappsomspp/core/processing/specpeptidoms/spomsspectrum.h>
#include <pappsomspp/core/types.h>
#include <pappsomspp/core/utils.h>
#include <qlogging.h>
#include "../../src/core/spoms/transformedspectrum.h"

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

  SECTION("..:: align mutated sequence AIADGSLLQLLR::..", "[model]")
  {
    QualifiedMassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR)
                .append("/tests/data/peaklists/peaklist_15046.mgf"));

    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->at(1).x ==
            Catch::Approx(87.0558240));
    pappso::FilterGreatestY(60).filter(
      *(spectrum_simple.getMassSpectrumSPtr().get()));
    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->at(1).x ==
            Catch::Approx(87.1000));

    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->size() == 60);

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

    pappso::PrecisionPtr precision_ptr =
      pappso::PrecisionFactory::getDaltonInstance(0.02);

    pappso::specpeptidoms::SpOMSSpectrum spoms_leg_spectrum(
      spectrum_simple, precision_ptr, aa_code);


    auto pos_w = spoms_leg_spectrum
                   .getAaPositions(aa_code.getAaCode(
                     pappso::Enums::AminoAcidChar::tryptophan))
                   .at(0);
    REQUIRE(spoms_leg_spectrum
              .getAaPositions(
                aa_code.getAaCode(pappso::Enums::AminoAcidChar::tryptophan))
              .size() == 6);

    REQUIRE(pos_w.l_support == false);
    REQUIRE(spoms_leg_spectrum.at(pos_w.l_peak).peak_mz ==
            Catch::Approx(175.11868300000000431));
    REQUIRE(spoms_leg_spectrum.at(pos_w.r_peak).peak_mz ==
            Catch::Approx(370.19019399999979214));

    REQUIRE(spoms_leg_spectrum
              .getAaPositions(
                aa_code.getAaCode(pappso::Enums::AminoAcidChar::alanine))
              .size() == 14);
    auto pos_ala = spoms_leg_spectrum
                     .getAaPositions(
                       aa_code.getAaCode(pappso::Enums::AminoAcidChar::alanine))
                     .at(0);

    REQUIRE(pos_ala.l_support == false);
    REQUIRE(spoms_leg_spectrum.at(pos_ala.l_peak).peak_mz ==
            Catch::Approx(19.017841));
    REQUIRE(spoms_leg_spectrum.at(pos_ala.r_peak).peak_mz ==
            Catch::Approx(158.0919489));
    /*
        REQUIRE((spoms_leg_spectrum.at(pos_ala.r_peak).peak_mz -
                 spoms_leg_spectrum.at(pos_ala.l_peak).peak_mz) ==
                Catch::Approx(aa_code.getMass(aa_code.getAaCode(
                                pappso::Enums::AminoAcidChar::alanine)))
                  .margin(0.02));*/
  }
  // Set the debugging message formatting pattern.scores to apply in comparisons
  qSetMessagePattern(QString("%{file}@%{line}, %{function}(): %{message}"));

  SECTION("..:: align mutated sequence AIADGSLLQLLR::..", "[model]")
  {
    QualifiedMassSpectrum spectrum_simple =
      readMgf(QString(CMAKE_SOURCE_DIR)
                .append("/tests/data/peaklists/peaklist_15046.mgf"));

    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->at(1).x ==
            Catch::Approx(87.0558240));
    pappso::FilterGreatestY(60).filter(
      *(spectrum_simple.getMassSpectrumSPtr().get()));
    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->at(1).x ==
            Catch::Approx(87.1000));

    REQUIRE(spectrum_simple.getMassSpectrumSPtr().get()->size() == 60);

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

    QualifiedMassSpectrumCstSPtr qmsp =
      spectrum_simple.makeQualifiedMassSpectrumCstSPtr();
    spoms::TransformedSpectrum tr_spectrum(aa_code, qmsp);

    auto aa_alanine = aa_code.getAaCode(pappso::Enums::AminoAcidChar::alanine);

    REQUIRE(tr_spectrum
              .getAminoAcidsPositions()[aa_code.getAaCode(
                pappso::Enums::AminoAcidChar::alanine)]
              .size() == 14);

    REQUIRE(aa_code.getAaCode(pappso::Enums::AminoAcidChar::tryptophan) == 19);

    REQUIRE(tr_spectrum
              .getAminoAcidsPositions()[aa_code.getAaCode(
                pappso::Enums::AminoAcidChar::tryptophan)]
              .size() == 6);

    REQUIRE(tr_spectrum
              .getAminoAcidsPositions()[aa_code.getAaCode(
                pappso::Enums::AminoAcidChar::alanine)][0]
              .getLeftPeak() == 1);
    REQUIRE(tr_spectrum
              .getAminoAcidsPositions()[aa_code.getAaCode(
                pappso::Enums::AminoAcidChar::alanine)][0]
              .getRightPeak() == 17);
    REQUIRE(tr_spectrum
              .getAminoAcidsPositions()[aa_code.getAaCode(
                pappso::Enums::AminoAcidChar::alanine)][0]
              .toString()
              .toStdString() == "(87.0445 ,158.092)1");


    REQUIRE((tr_spectrum
               .getAminoAcidsPositions()[aa_code.getAaCode(
                 pappso::Enums::AminoAcidChar::alanine)][0]
               .getRightMass() -
             tr_spectrum
               .getAminoAcidsPositions()[aa_code.getAaCode(
                 pappso::Enums::AminoAcidChar::alanine)][0]
               .getLeftMass()) ==
            Catch::Approx(aa_code.getMass(aa_alanine)).margin(0.02));
    qDebug() << tr_spectrum
                  .getAminoAcidsPositions()[aa_code.getAaCode(
                    pappso::Enums::AminoAcidChar::alanine)][0]
                  .toString();
    qDebug() << tr_spectrum
                  .getAminoAcidsPositions()[aa_code.getAaCode(
                    pappso::Enums::AminoAcidChar::alanine)][1]
                  .toString();

    auto pos_first_tryp =
      tr_spectrum.getAminoAcidsPositions()[aa_code.getAaCode(
        pappso::Enums::AminoAcidChar::tryptophan)];

    REQUIRE(pos_first_tryp[0].getLeftPeak() == 60);
    REQUIRE(pos_first_tryp[0].getRightPeak() == 55);
    REQUIRE(pos_first_tryp[0].toString().toStdString() == "(184.129 ,370.19)1");
    REQUIRE(pos_first_tryp[1].toString().toStdString() ==
            "(185.128 ,371.194)1");


    REQUIRE(
      (pos_first_tryp[0].getRightMass() - pos_first_tryp[0].getLeftMass()) ==
      Catch::Approx(aa_code.getMass(aa_code.getAaCode(
                      pappso::Enums::AminoAcidChar::tryptophan)))
        .margin(0.02));
  }
}
