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



enumeration.search.dep.boots.GOnly.ext2f90 <- function(x, refset, testset, anz=1, method="Pearson", ssqflag=1){
	# R: original data set and group size
	# Is the pre-selected ref(size)-test(size) grouping the best in all possible groupings of ref(size)-test(size)
	# 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	
	# input: x - dataframe - table of raw data, col names: variables, row names: patients, genes, etc.
	#   testset: index of test columns, refset: index of reference columns,  nr: number of rows of x,  nc: number of cols of x
	#   anz: number of exchanges concerning original situation, 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
	cat("\n combinations of groups: ",nrs)
	fact <- factorial(nr)
	cat("\n permutations of test set: ",fact)
	
	if(anz > nr){stop("\n maximal # of replacements > size of test set\n")}		# replacements <= test factors
	
	lines.ssqPG <- (choose(nr,anz)*choose(nc,anz)) + 1		#combinations
	ssqPG <- matrix(0,lines.ssqPG,1+2*nr+nc)		# allocation, ssq,configuration,testc,comb
	
	#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," Number of replacements: ",anz,"\n\n")
	
	#Fortran definition
	mode(y) <- 'single'
	mode(nry) <- 'integer'
	mode(ncy) <- 'integer'
	mode(nr) <- 'integer'
	mode(nc) <- 'integer'
	#mode(nrs) <- 'integer'
	mode(anz) <- 'integer'
	mode(lines.ssqPG) <- 'integer'
	mode(meth) <- 'integer'
	mode(fact) <- 'integer'
	mode(ssqPG) <- 'single'
	mode(ssqflag) <- 'integer'					#flag: '1': ssq in x, '2': ssq in y
	
	#Fortran call

	dyn.load("/home/korschi/on3/eclipseR/searchdep12/gf/lib_sd12_01.so")
	f.back <- .Fortran("workflow",
				x=y,
				nrx=nry,
				ncx=ncy,
				nr=nr,
				nc=nc,
				#nrs=nrs,
				anz=anz,
				linesssqPG=lines.ssqPG,
				meth=meth,
				fact=fact,
				ssqPG=ssqPG,
				ssqflag=ssqflag
		)
	dyn.unload("/home/korschi/on3/eclipseR/searchdep12/gf/lib_sd12_01.so")
	
	return(f.back$ssqPG)
}

#date()		# anz=1 - 5 min
#aa44.test <- enumeration.search.dep.boots.GOnly.ext2f90(He589.c, refset=c(1,5,3,2,12,13), testset=c(4,6,7,8,9,10,11,14,15,16), anz=1, method="cor", ssqflag=1)
#date()

