
//! Loading Genotype and Covariates Data and other Informations contained in File
bool LoadData(const bool bbase, INT_VCTR_1D& isBase,DBL_VCTR_2D& C, DBL_VCTR_2D& Xtild, INT_VCTR_2D& QualVar, INT_VCTR_2D& LevelPopSize, INT_VCTR_3D& X, INT_VCTR_1D& recAllele, int ER, int EC, const string& File, const bool& specialFormat, const int& nbQuantVar, const int& nb_var_regr, const bool& isRecAllele, const int& trendDegree)
{
	if (Xtild.size() == 0)
	{
		cerr << "Uninitialized Xtild - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (C.size() == 0)
	{
		cerr << "Uninitialized C - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (X.size() == 0)
	{
		cerr << "Uninitialized X - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (Xtild.size() < X.size())
	{
		cerr << "Mismatched Xtild and X - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	int i, a, l, b, var;
	int begin_quantitative=1;

	ifstream ifs(File.c_str());
	if (!ifs.good())
	{
		cerr << "Cannot open file " << File << " - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	int M = static_cast<int>(Xtild.size());
	int B = static_cast<int>(Xtild[0].size());
	int N = static_cast<int>(X.size());
	int A = static_cast<int>(X[0].size());
	int L = static_cast<int>(X[0][0].size());
	// Number of Qualitative Variable
	int nbQualVar = static_cast<int>(QualVar.size());
	int nLevels, nextCol, currCol; //temp variable
	
	double x,y;
	int nbDigitMax = 0;

	if (M != N)
	{
		cerr << " Wrong dimensions" << " - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	// Load and discard the extra rows.
	char extraRow[10240];
	char testChar;
	for (int er = 0; er < ER; er++)
	{
		ifs.getline(extraRow, 10240);
	}

	int recAl = 1;
	if (isRecAllele)
	{
		for (l = 0; l < L; l ++)
		{
			ifs >> recAl;
			recAllele[l] = recAl;
		}
	}

	if (!specialFormat)
	{
		for (i = 0; i < M; i++)
		{
			for (a = 0; a < A; a++)
			{
				// Load and discard the extra columns.
				string discarded;
				for (int ec = 0; ec < EC; ec++)
				{
					ifs >> discarded;
				}

				//Stock base information
				if (bbase)
				{
					ifs >> isBase[i];
				}

				// Qualitative Variables
				if (nbQualVar > 0)
				{
					nextCol=1;
					currCol=1;
					for (var = 0; var < nbQualVar; var++)
					{
						ifs >> QualVar[var][i];
						QualVar[var][i]--;     // User Writes Classes from 1 to nbQvar corresponding to Labels from 0 to (nbQvar-1) in the algorithm
						if (a==0) // count only once
						{
							LevelPopSize[var][QualVar[var][i]]++;      // Computes the Size of Each Class

							// 1 Qual. Var. with n Levels corresponds to n-1 Columns of the Environmental Data Matrix (cf Saporta p.398)
							// The First column of Xtild is ALWAYS set to 1 (corrsponding to Intercept in th Regression)
							// Level 0 -> all columns set to -1
							// Level i!=0  -> all columns set to 0, the ith, set to 1
							nLevels = static_cast<int>(LevelPopSize[var].size());
							nextCol += nLevels-1;
							
							if (QualVar[var][i] == 0)
							{
								for (b=currCol; b < nextCol; b++)
								{
									Xtild[i][b]=-1;
								}
							}
							else
							{
								for (b=currCol; b < nextCol; b++)
								{
									if ( (b-currCol+1) != QualVar[var][i])
									{
										Xtild[i][b]=0;
									}
									else
									{
										Xtild[i][b]=1;
									}
								}
							}
							currCol = nextCol;
						}
					}
					if (a==0) begin_quantitative = nextCol;          // (if a>0, nextCol is not upadated anymore)
				}
				
				
				int begin_coordinates=begin_quantitative + nbQuantVar;
			
				
				// Quantitative Variable
				for (b = begin_quantitative; b < begin_coordinates; b++)
				{
					ifs >> Xtild[i][b];
				}
				
				// Spatial Coordinates (if given in the datafile)

				if (trendDegree != -1) 
				{  
					ifs >> C[i][0];
					ifs >> C[i][1];

					x = C[i][0];
					y = C[i][1];
										
					if (nb_var_regr == 1)
					{
					  // We keep only y (ignoring x column)
						for (b = begin_coordinates; b < B; b ++)
						{
						  Xtild[i][b] = gsl_pow_int(y,b-begin_coordinates+1);
						}
					}

					if (nb_var_regr == 2)
					{
					 
						if (trendDegree > 0)
						{
							Xtild[i][begin_coordinates] = x;
							Xtild[i][begin_coordinates+1] = y;
						
							if (trendDegree > 1)
							{
								Xtild[i][begin_coordinates+2] = x*x;
								Xtild[i][begin_coordinates+3] = y*y;
								Xtild[i][begin_coordinates+4] = x*y;
								if (trendDegree > 2)
								{
									Xtild[i][begin_coordinates+5] = x*x*x;
									Xtild[i][begin_coordinates+6] = y*y*y;
									Xtild[i][begin_coordinates+7] = x*x*y;
									Xtild[i][begin_coordinates+8] = x*y*y;
								}
							}
						}
					}
				}
				// Genotype Data
				if (i < N)
				{
					for (l = 0; l < L; l++)
					{
						ifs >> X[i][a][l];
					}
				}
				else
				{
					for (l = 0; l < L; l++)
					{
						ifs >> discarded;
					}
				}
			}
		}
	}
	if (specialFormat)
	{
		for (i = 0; i < M; i ++)
		{
			// Load and discard the extra columns.
			string discarded;
			for (int ec = 0; ec < EC; ec++)
			{
				ifs >> discarded;
			}
			// Stock base information
			if (bbase)
			{
				ifs >> isBase[i];
			}

			// Qualitative Variables
			if (nbQualVar > 0)
			{
				nextCol=1;
				currCol=1;
				for (var = 0; var < nbQualVar; var++)
				{
					ifs >> QualVar[var][i];
					QualVar[var][i]--;     // User Writes Classes from 1 to nbQvar corresponding to Labels from 0 to (nbQvar-1) in the algorithm
					
					LevelPopSize[var][QualVar[var][i]]++;      // Computes the Size of Each Class

					// 1 Qual. Var. with n Levels corresponds to n-1 Columns of the Environmental Data Matrix (cf Saporta p.398)
					// The First column of Xtild is ALWAYS set to 1 (corrsponding to Intercept in th Regression)
					// Level 0 -> all columns set to -1
					// Level i!=0  -> all columns set to 0, the ith, set to 1
					nLevels = static_cast<int>(LevelPopSize[var].size());
					nextCol += nLevels-1;
					
					if (QualVar[var][i] == 0)
					{
						for (b=currCol; b < nextCol; b++)
						{
							Xtild[i][b]=-1;
						}
					}
					else
					{
						for (b=currCol; b < nextCol; b++)
						{
							if ( (b-currCol+1) != QualVar[var][i])
							{
								Xtild[i][b]=0;
							}
							else
							{
								Xtild[i][b]=1;
							}
						}
					}
					currCol = nextCol;
				}
				begin_quantitative = nextCol;
			}
			
			int begin_coordinates=begin_quantitative + nbQuantVar;
			// Quantitative Variable
			for (b = begin_quantitative; b < begin_coordinates; b++)
			{
				ifs >> Xtild[i][b];
			}

			// Spatial Coordinates (if given in the datafile)
			if (trendDegree != -1) 
			{
				ifs >> C[i][0] >> C[i][1];
				x = C[i][0];
				y = C[i][1];
				
				if (nb_var_regr == 1)
				{
					// We keep only y (ignoring leftmost column)
					for (b = begin_coordinates; b < B; b ++)
					{
						Xtild[i][b] = gsl_pow_int(y,b-begin_coordinates+1);
					}
				}
				
				if (nb_var_regr == 2)
				{
					
				  if (trendDegree > 0) 
					{
						Xtild[i][begin_coordinates] = x;
						Xtild[i][begin_coordinates+1] = y;
						if (trendDegree > 1) 
						{
							Xtild[i][begin_coordinates+2] = x*x;
							Xtild[i][begin_coordinates+3] = y*y;
							Xtild[i][begin_coordinates+4] = x*y;
							
							if (trendDegree > 2) 
							{
								Xtild[i][begin_coordinates+5] = x*x*x;
								Xtild[i][begin_coordinates+7] = y*y*y;
								Xtild[i][begin_coordinates+8] = x*x*y;
								Xtild[i][begin_coordinates+9] = x*y*y;
							}
						}
					}
				}
			}
			

			//Genotype Data
			if (i < N)
			{
				for (l = 0; l < L; l++)
				{
					for (a = 0; a < A; a ++)
					{
						ifs >> X[i][a][l];
					}
				}
			}
			else
			{
				for (l = 0; l < L; l++)
				{
					for (a = 0; a < A; a ++)
					{
						ifs >> discarded;
					}
				}
			}
		}
	}

	ifs.close();
	
	return true;
}




//! Loading Covariates Data, Training Genotype Data and Validation Genotype Data

/*bool LoadData(const bool bbase, INT_VCTR_1D& isBase, DBL_VCTR_2D& Xtild, INT_VCTR_3D& X, INT_VCTR_3D& Xval, INT_VCTR_2D& QualVar, INT_VCTR_2D& LevelPopSize, int ER, int EC, const string& File)
{
	
	if (isBase.size() == 0)
	{
		cerr << "Uninitialized isBase - LoadData" << endl;
		exit(EXIT_FAILURE);
	}
	
	if (Xtild.size() == 0)
	{
		cerr << "Uninitialized Xtild - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (X.size() == 0)
	{
		cerr << "Uninitialized X - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (Xval.size() == 0)
	{
		cerr << "Uninitialized Xval - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	if (Xtild.size() < X.size() || isBase.size() < X.size())
	{
		cerr << "Mismatched Xtild and X - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	int i, a, l, b;
	int begin_quantitative=1;

	ifstream ifs(File.c_str());
	if (!ifs.good())
	{
		cerr << "Cannot open file " << File << " - LoadData" << endl;
		exit(EXIT_FAILURE);
	}

	int M = static_cast<int>(Xtild.size());
	int B = static_cast<int>(Xtild[0].size());
	int N = static_cast<int>(X.size());
	int Nval = static_cast<int>(Xval.size());
	int A = static_cast<int>(X[0].size());
	int Aval = static_cast<int>(Xval[0].size());
	int L = static_cast<int>(X[0][0].size());
	int Lval = static_cast<int>(Xval[0][0].size());
	int nbQualLevels = static_cast<int>(LevelPopSize.size());

	if ( (N!=Nval) || (A!=Aval) )
	{
		cerr << "Mismatched X and Xval - LoadData" << endl;
		cerr << "N!=Nval or A!=Aval" << endl;
		exit(EXIT_FAILURE);
	}

	// Load and discard the extra rows.
	char extraRow[10240];
	for (int er = 0; er < ER; er++)
	{
		ifs.getline(extraRow, 10240);
	}
	
	for (i = 0; i < M; i++)
	{
		for (a = 0; a < A; a++)
		{
			// Load and discard the extra columns.
			string discarded;
			for (int ec = 0; ec < EC; ec++)
			{
				ifs >> discarded;
			}
			
			//Base 
			if (bbase) 
			{
				ifs >> isBase[i];
			}
			if (nbQualLevels>0)
			{ 
				begin_quantitative=nbQualLevels;
				ifs >> QualVar[i];
				QualVar[i]--; //user  writes classes from 1 to nbQualLevels but we use labels from 0 to (nbQualLevels-1)
				if (a==0) LevelPopSize[QualVar[i]]++;
				//the column for the level 0 is deleted (cf Saporta p.398)
				//new column(level i!=0) = old column(level i) - old column(level 0)
				if (QualVar[i]==0)//level 0
				{
					for (b=1; b<nbQualLevels;b++)
					{
						Xtild[i][b]=-1;
					}
				}
				else
				{
					for (b=1; b<nbQualLevels;b++)
					{
						if (b!=QualVar[i])
						{
							Xtild[i][b]=0;
						}
						else
						{
							//cout<<b<<" = "<<QualVar[i];
							Xtild[i][b]=1;
						}
					}
				}
			}
			for (b = begin_quantitative; b < B; b++)
			{
				ifs >> Xtild[i][b];
			}
			
			if (i < N)
			{
				for (l = 0; l < L; l++)
				{
					ifs >> X[i][a][l];
				}
				for (l = 0; l < Lval; l++)
				{
					ifs >> Xval[i][a][l];
				}
			}
			else
			{
				for (l = 0; l < (L+Lval); l++)
				{
					ifs >> discarded;
				}

			}
		}
	}
	ifs.close();

	return true;
}

*/


#-L/usr/local/lib/ -lgsl -lgslcblas # usr/local/src/gsl-1.9/.libs