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



enumeration.search.dep.boots.GOnly.f90 <- function(x, refset, testset, method="Pearson", ssqflag=1){
	# R: original data set and group size
	# Fortran dll: all other work: enumeration of all permutations of original grouping
	#  calculate best ssq for original pair of groups, mem order of group of best ssq,
	#  calc ssq for that order for all other compositions (combinations) --- random pick !
	# 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
	# 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
	
	## Probleme, wenn in einer Spalte nur gleiche Werte enthalten sind: ssqPG wird NA
	## und zwar in allen Berechnungen wo die Spalte im Ref Set steht - siehe fortran code
	
	
	#ini
	if(!(method %in% c("L2","Pearson"))){ stop("\n wrong similarity method ") }
	if(method=="Pearson"){ meth <- 1 } else { meth <- 2 }
	
	nr <- length(testset)
	nc <- length(refset)
	nrs <- choose(nr+nc,nc)		# combinations, utils:combn : enumeration of all combinations
	cat("\n combinations of groups: ",nrs)
	fact <- factorial(nr)
	#fact <- 55000
	cat("\n permutations of test set: ",fact)
	
	#ssqPG <- matrix(0,nrs,nr+nc+1)		# allocation
	ssqPG <- matrix(0,nrs,1)			# allocation
	ssqBest <- 0.01						# allocation with dummy assignment
	indexB <- matrix(0,1,nr)			# allocation
	corMat <- matrix(0,nr,nc)			# test
	
	#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")
	
	#process original grouping and samples of the grouping
	#Fortran definition
	mode(y) <- 'single'
	mode(nry) <- 'integer'
	mode(ncy) <- 'integer'
	mode(nr) <- 'integer'
	mode(nc) <- 'integer'
	mode(nrs) <- 'integer'
	mode(meth) <- 'integer'
	mode(fact) <- 'integer'
	mode(ssqPG) <- 'single'
	mode(ssqBest) <- 'single'
	mode(indexB) <- 'integer'
	mode(corMat) <- 'single'
	mode(ssqflag) <- 'integer'					#flag: '1': ssq in x, '2': ssq in y
	
	#Fortran call
	dyn.load("/home/korschi/on3/eclipseR/searchdep10/gf/lib_sd10_01.so")
	f.back <- .Fortran("workflow",
				x=y,
				nrx=nry,
				ncx=ncy,
				nr=nr,
				nc=nc,
				nrs=nrs,
				meth=meth,
				fact=fact,
				ssqPG=ssqPG,
				ssqBest=ssqBest,
				indexB=indexB,
				corMat=corMat,
				ssqflag=ssqflag
		)
	dyn.unload("/home/korschi/on3/eclipseR/searchdep10/gf/lib_sd10_01.so")
	
	return(list(f.back$ssqPG, f.back$ssqBest, f.back$indexB, f.back$corMat))
	#return(f.back$corMat)
	#return(list(f.back$indexB, f.back$corMat))
}

#choose(10,5,F)	#combinations
#date()		# 9 s
#aa32 <- enumeration.search.dep.boots.GOnly.f90(He589.c, refset=c(1,5,3,2,12,13), testset=c(4,6,7,8,9,10,11,14,15,16), method="Pearson", ssqflag=1)
#date()


