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



enumeration.search.dep.ssq.f90 <- function(x, refset, testset, method="pearson", output="boot.raw.col", ssqflag=1){
	# R: original data set, calculating all global ssq for a distribution
	# Fortran dll: enumeration of all permutations, calculate all global ssq, output in files
	# input: x - dataframe - table of data, col names: variables, row names: patients, genes, etc.
	#   refset:  Auswahl der Non-Test-cols, testset: Auswahl der Test-cols, method: pearson, spearman or euclidean
	#   finame:  File name
	# output: text file with results: ssq
	# (!) doing a full enumeration -> check if appropriate
	
	
	###### no longer functional ????? #####
	
	
	# const
	dynpath <- "../searchdep08/Release_Intel64/searchdep08.so"		# relative to wd
	# check also: libifport.so.5 etc. in /usr/lib
	#ini
	nr <- length(testset)
	nc <- length(refset)
	
#	nre <- factorial(nr)	#will be done in Fortran
	
	#compile similarity table
	anzSamplings <- 0	#no sampling/bootstrap	 only original matrix
	z <- proximity.bootstrap.sampling(x,reiheRef=refset,reiheTest=testset,anzSamplings=anzSamplings,method=method,output=output)
	if(is.null(z)){ cat("\n errors in fn: proximity.bootstrap.sampling "); return() }
	erg <- 1	#flag, return value: ok := 0 ; error := 1
	
	#Fortran definitions
	mode(z) <- 'single'				#as.xxx() assignment destroys the matrix structure (results in a vector)
	nr <- as.integer(nr)				#no harm to do so but mode() should work as well
	nc <- as.integer(nc)
	anzSamplings <- as.integer(anzSamplings+1)			#number of samplings/bootstraps plus original set
	erg <- as.integer(erg)
	mode(ssqflag) <- 'integer'					#flag: '1': ssq in x, '2': ssq in y
	
	#Fortran call
	dyn.load(dynpath)
	f.back <- .Fortran("workflow",
				x=z,
				nr=nr,
				nc=nc,
				anzBoots=anzSamplings,
				erg=erg,
				ssqflag=ssqflag
		)
	dyn.unload(dynpath)
	
	return(f.back$erg) 
}

#3628800 rows -works
#enumeration.search.dep.ssq.f90(He589.c,refset=c(1,5,3,2,12,13),testset=c(4,6:11,14:16),method="cor",output="matrix")



#factorial(7);factorial(8);factorial(9);factorial(10)		#5040,40320,362880,3628800 rows



