/*
 *  ap_ttest.c
 *
 *  Created by wei sun on Sun Sep 16 2007.
 *
 */

#include "ttest.h"
#include "utility.h"
#include <R.h>
#include <R_ext/Error.h>


/**********************************************************************
 * 
 * t test for one group of gene exrepssion profiles and 
 * one group of binary marke genotype profiles
 *
 **********************************************************************/

int eqtl_ttest(double** me, int** mm, int** ime, int** imm, int ne, int nm,
  int p, int L, int var_eql, double* pmins){
  
  double t, pval, pmin, *e0, *e1;
  int i, j, k, le0, le1;
  
  e0 = (double *)calloc(p, sizeof(double));
  e1 = (double *)calloc(p, sizeof(double));
  
  for (i=0; i<ne; i++){
    pmin = 1.0;
    for(j=0; j<nm; j++){
      le0 = 0;
      le1 = 0;
      for(k=0; k<p; k++){
				if(ime[i][k]==1  && imm[j][k]==1){
          if(mm[j][k]==0){
            e0[le0]=me[i][k];
            le0++;
          }else{
            e1[le1]=me[i][k];
            le1++;
          }
        }
      }
      if(le0>=L && le1>=L){
      	if(var_eql==0){
      	  tutest(e0, &le0, e1, &le1, &t, &pval);
      	}else {
      	  tctest(e0, &le0, e1, &le1, &t, &pval);
      	}
      	
      	if(pval < pmin){
      		pmin = pval;
      	}
      }
    }
    pmins[i] = pmin;
  }
  
  free(e0);
  free(e1);
  return 1;
}

/**********************************************************************
 * 
 * t test for one group of gene exrepssion profiles and 
 * one group of binary marke genotype profiles, with permutations
 *
 * by default
 * np = c(100, 1000, 5000, 10000, 50000, 100000)
 * aim_p = c(0.1, 0.05, 0.02, 0.01, 0.002, 0.001)
 * np.max = 500000
 * 
 **********************************************************************/
int ap_ttest(double* Rme, int* Rmm, int* Rime, int* Rimm, int* dims,
  int *idu, int* var_eql, double* pvals, double* p_pvals){
  
  double *pmins, **me, **p_me;
  int *perm1, **mm, **ime, **imm, **p_ime;
  long idum;
  idum = *idu;
  
  int ne = dims[0];
  int nm = dims[1];
  int p  = dims[2];
  int L  = dims[3];
  int np = dims[4];
  int t, k;
    
  pmins = (double *)calloc(ne, sizeof(double));
	perm1 = (int *)calloc(p, sizeof(int));
	
  reorg(Rme, &me, ne, p);
  reorg_int(Rmm, &mm, nm, p);
  reorg_int(Rime, &ime, ne, p);
  reorg_int(Rimm, &imm, nm, p);
  
	p_me  = (double**) malloc(ne*sizeof(double*));
	p_ime = (int**) malloc(ne*sizeof(int*));

  p_me[0]  = (double*) calloc(ne*p, sizeof(double));
  p_ime[0] = (int*) calloc(ne*p, sizeof(int));

  for(k=1; k<ne; k++){
    p_me[k]  = p_me[k-1] + p;
    p_ime[k] = p_ime[k-1] + p;
  }
  
  eqtl_ttest(me, mm, ime, imm, ne, nm, p, L, *var_eql, pvals);
  // for(k=0; k<ne; k++) Rprintf("%f ", pvals[k]);
  // Rprintf("\n");
  
  for(t=0; t<np; t++){
  	// Rprintf("t=%d\n", t);
  	getPermute(perm1, p, &idum);
    /*
  	if(t==0){
      for(k=0; k<p; k++) Rprintf("%d ", perm1[k]);
      Rprintf("\n");
    }
     */
		permute(me, p_me, ime, p_ime, perm1, ne, p);
	  eqtl_ttest(p_me, mm, p_ime, imm, ne, nm, p, L, *var_eql, pmins);

    for(k=0; k<ne; k++){
      if(pmins[k] <= pvals[k]){ p_pvals[k] += 1.0; }
    }
  }
  
  for(k=0; k<ne; k++){
    p_pvals[k] /= np;
  }
  
  free(pmins);
  free(perm1);
  
  return 1;
}
