/***********************************************************************************
AUTHOR: A Carvajal-Rodriguez
DATE:	280310
CLASS: SNPSelTools
VARIABLES: none
METHODS:
PERFORMS: Selection detection C++ tools
************************************************************************************/


#ifndef SNPSelTOOLS_H
#define SNPSelTOOLS_H

#ifdef _WIN32
    #include <direct.h>
#endif

#ifndef macintosh
	#include <sys/types.h>
	#include <sys/stat.h>
#endif

#include "TLibHACSel.hpp"

#include <set>

typedef unordered_map<double,string> HapMapTable;

//const double infinito = numeric_limits<double>::max();
const double MinMAF=0.0;
//const int SnptoChangetoFast=1E6;
const int MinforWindows=3;
const int MaxforWindows=(numeric_limits<int>::max()-1);
const string comment_tag{"#"};
const set<string> nucleotides={"A","C","G","T"};

class SNPSelTools
{
public:
    static void printHelp (ostream& fp, const float& version);

    static void ParseArgs(int argc, char** argv,
                string& tag,
                bool& nogaps,
                bool& store,
                bool& old,
                string& path,
                string& format,
                bool& onepopfile,
                string& infile,
                string& infile2,
				string& outfile,
                bool& userfilename,
                int& numruns,
                int& popforHB,
                vector<double>& selectives,
                int& windowsize,
                int& samplesize,
                int& samplesize2,
                int& minWinSize,
                int& MaxWinSize,
                double& MAF,
                double& SL,
                bool& doEOS,
                bool& soloEPO,
                bool& EPOH,
                double& numdesvs,
                bool& fixseed,
                bool& useblocks,
                bool& verbose,
                bool& writersidInfo,
                bool& onlyrsidInfo,
                const float& version);
	//static bool readBSSNPdata(const string& path,const string& filename0, const int& samplesize,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions);
    static bool readphasedVCF(const string& path,const string& filename1,const string& filename2, vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref);
    static bool readphasedVCF_intersec(const string& path,
                               const string& filename1,
                               const string& filename2,
                               vector<int>& samplesizes,
                               deque< deque<bool> >& data,
                               vector< vector<double> >& snpfreqs,
                               vector< vector<double> >& snpositions,
                               int& pref);
    static bool readplinkdata(const string& path,const string& filename_map,const string& filename_ped, const int& numpops, vector<vector<double>>& positions, vector<int>& samplesizes,vector<vector<vector<float>>>& alfreqs,vector<vector<float>>&majoralfreqs,vector<vector<vector<uint_fast32_t>>>& alelids,const uint_fast8_t diploid, vector<string>& locnames);
    static bool readplinkdata2(const string& path,const string& filename_map,const string& filename_ped, const int& numpops, vector<vector<double>>& positions, vector<int>& samplesizes,vector<vector<vector<float>>>& alfreqs,vector<vector<float>>&majoralfreqs,vector<vector<vector<uint_fast32_t>>>& alelids,const uint_fast8_t diploid, vector<string>& locnames);
	static bool readBSSNPdata(const string& path,const string& filename0,const int& numpops,  vector<vector<vector<float>>>& alfreqs,vector<vector<float>>&majoralfreqs,const uint_fast8_t diploid, vector<string>& locnames);
	static bool readfastadata(const bool& nogaps,const string& path,const string& filename0, vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions);
	static bool readfastadatatag(const string& tag, const bool& store,const bool& nogaps, const string& path,const string& filename0,  vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref);
	static bool readfastadatarand(const bool& store,const bool& nogaps, const string& path,const string& filename0,  vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref);
	static bool readfastadata(const bool& nogaps,const string& path,const string& filename1,const string& filename2, vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref);
	static bool filterfasta(const string& tag,const bool& store,const bool& nogaps, const string& path,const string& filename0,  vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref);
	static bool readhmphased(const string& path,const string& filename0,const string& filename1,const bool onepopfile, vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions,HapMapTable& snpstable, int& pref);
	static bool readfastPHASE(const string& path,const string& filename0,const string& filename1,vector<int>& samplesizes,deque < deque<bool> >& data, vector < vector<double> >& snpfreqs,vector < vector<double> >& snpositions, int& pref, const bool& firsthaplo);
	//static bool readinputdata(const string& path,const string& filename0, const int& samplesize,deque < deque<bool> >& data,vector < vector<double> >& snpositions,bool& bayescformat);
	static bool readmsdata(const string& path,const string& filename0, const int& samplesize,deque < deque<bool> >& data,vector < vector<double> >& snpositions);
    static bool readmspopsdata(const string& path,const string& filename0, const int& samplesize,const int& n1,const int& n2, deque < deque<bool> >& data,vector < vector<double> >& snpositions);
    static bool readgenpopdata(const string& path,const string& filename0, vector<int>& samplesizes,vector<vector<vector<float>>>& alfreqs,vector<vector<float>>&majoralfreqs,vector<vector<vector<uint_fast32_t>>>&  alelids, const uint_fast8_t diploid, vector<string>& locnames);
//	static bool writeMSFormat(const string& filename, const deque < deque<bool> >& data, const int& numpops, const vector < vector<double> >& snpositions, const double& thetaval, const size_t& seed);
//	static bool writeBayeScanFormat(const string& filename, const deque < deque<bool> >& data, const int& numpops, const vector < vector<double> >& snpfreqs);
	static void texterr(const int val,const string error_txt,bool finish);

    //static void ftest(const double & fval, const double& df1, const double& df2, double& prob);

};

struct genot{
    uint_fast32_t allel[2];
    genot(): allel{0,0}{}
    genot(uint_fast32_t a1,uint_fast32_t a2): allel{a1,a2} {}
};

struct ResultP{
    double stat;
    double pval=1;
    double qval=1;
};

struct ResultIndexes{
    size_t candidate; // identifies the canditate number (first,second..) when more than one candidate is selected as putative selective site

    int site; // corresponding order position of the candidate SNP in the data e.g. could be pos = 0.42 and site = 3
    //vector<double> varTest; // variance test of populations
    int nclasses; // the number of classes used when computing JHac
    STAT JHac; // Oct 2023
    double pos; // real genome relative snp position
    vector<int> iniW; // initial snp positon (real genome position) of the window [ for each population
    vector<int> endW; // end snp position in the window, real genome position ) for each population
    int wide; // number of SNPs evaluated on the window
    double fst;
    ResultP fstindex;
};




# endif
