# TODO: Add comment
# 
# Author: E.Korsching 15.12.2011
###############################################################################



enumeration.search.dep.boots.GOnly.extf90.distribution <- function(x, refset, testset, method="Pearson", globalSsqINI=1000, sampleCOR=0, ssqflag=1){
	# R: original data set and group size			-->> small version only for ssq values - no order etc.
	# IMPORTANT: initial globalSsqINI must be greater than all results in the permutation process
	# Fortran dll: all other work: enumeration of all permutations of original grouping
	#   do the same for all compositions (combinations) ,
	#   for each: calculate best ssq for each pair of groups -- also sampling on cor table possible --
	# input: x - dataframe - table of raw data, col names: variables, row names: patients, genes, etc.
	#   testset: index of group A, refset: index of group B, nr: number of rows of x, nc: number of cols of x
	#   method: L2 (Euclidean), Pearson  as proximity measure, sampleCOR: 1: cor matrix will be sampled before use
	# output: matrix of results: ssqPG all ssq of enumeration of all groupings, ssqBest: best ssq of enumeration of all permutations
	#   of original grouping, indexB	: index of best test order in the original grouping
	# (!) doing a full enumeration -> check if appropriate
	
	#ini
	if(!(method %in% c("L2","Pearson"))){ cat("\n wrong similarity method "); return() }
	if(method=="Pearson"){ meth <- 1 } else { meth <- 2 }
	
	nr <- length(testset)
	nc <- length(refset)
	nrs <- choose(nr+nc,nc)		# combinations
	cat("\n combinations of groups: ",nrs)
	fact <- factorial(nr)
	cat("\n permutations of test set: ",fact)
	
	ssqPG <- matrix(0,nrs,1)		# allocation, ssq  only
	
	#filter and order x
	y <- x[,c(testset,refset),drop=F]		####
	
	cat("\n\n input (x): \n\n")
	cat(names(x))		#print(x)
	cat("\n\n and ordered input (y): \n\n")
	cat(names(y))		#print(y)
	cat("\n\n col 1 bis ",nr,": Test, col ",nr+1," bis ",nr+nc,": Ref \n")
	
	y <- as.matrix(y)		####
	nry <- dim(y)[1]
	ncy <- dim(y)[2]
	cat("\n nr y: ",nry," -  nc y: ",ncy,"\n\n")
	
	#Fortran definition
	mode(y) <- 'single'
	mode(nry) <- 'integer'
	mode(ncy) <- 'integer'
	mode(nr) <- 'integer'
	mode(nc) <- 'integer'
	mode(nrs) <- 'integer'
	mode(meth) <- 'integer'
	mode(sampleCOR) <- 'integer'
	mode(fact) <- 'integer'
	mode(ssqPG) <- 'single'
	mode(globalSsqINI) <- 'single'
	mode(ssqflag) <- 'integer'					#flag: '1': ssq in x, '2': ssq in y
	
	#Fortran call
	dyn.load("/home/korschi/on3/eclipseR/searchdep11dist/gf/lib_sd11dist_01.so")
	f.back <- .Fortran("workflow",
				x=y,
				nrx=nry,
				ncx=ncy,
				nr=nr,
				nc=nc,
				nrs=nrs,
				meth=meth,
				sampleCOR=sampleCOR,
				fact=fact,
				ssqPG=ssqPG,
				globalSsqINI=globalSsqINI,
				ssqflag=ssqflag
		)
	dyn.unload("/home/korschi/on3/eclipseR/searchdep11dist/gf/lib_sd11dist_01.so")
	
	#check if globalSsqINI value was in all cases greater than all the real global ssq values
	cat("\n Max ssqPG = ",max(f.back$ssqPG)," if equal or greater '",globalSsqINI,"' set a greater 'globalSsqINI' value  and  check the results.\n")
	
	return(f.back$ssqPG)
}

#aa50 <- enumeration.search.dep.boots.GOnly.extf90.distribution(He589.c, refset=c(1,5,3,2), testset=c(4,6,7,8,9,10), method="cor", globalSsqINI=1000, sampleCOR=0, ssqflag=1)
#aa51 <- enumeration.search.dep.boots.GOnly.extf90.distribution(He589.c, refset=c(1,5,3,2), testset=c(4,6,7,8,9,10), method="cor", globalSsqINI=1000, sampleCOR=1, ssqflag=1)

#compare old algo
#aa51 <- enumeration.search.dep.boots.GOnly.extf90(He589.c, refset=c(1,5,3,2), testset=c(4,6,7,8,9,10), method="cor")

#factorial(11)		#39916800
#factorial(10)		#3628800
#factorial(9)			#362880
#factorial(8)			#40320
#choose(16,5)			#4368
#choose(16,6)			#8008
#choose(16,7)			#11440
#choose(16,8)			#12870


