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



enumeration.search.dep.boots.f90 <- function(x, anzSamplings=0, refset, testset, method="pearson", zz=NULL, sampleonly=F, output="boot.raw.col", ssqflag=1){
	# R: original data set + sampls of the data, applying proximity measure on all
	# Fortran dll: enumerationof all permutations, calculate best ssq for each bootstrap / real sample and bring back list of results
	#  avoid special characters in col names
	# input: x - dataframe - table of data, col names: variables, row names: patients, genes, etc.
	#  refset: selection of reference cols, testset: selection of test cols, method: pearson, spearman or euclidean,
	#  zz: existing 'proximity.bootstrap.sampling' dataset for z
	# anzSamplings: Number of samplings/bootstraps to be performed, for the original data set 1 is added appropriately in the following context
	# (!) doing a full enumeration -> check if appropriate
	
	# const
	dynpath <- "../searchdep09/Release_Intel64/searchdep09.so"		# relative to wd
	# check also: libifport.so.5 etc. in /usr/lib
	# ini
	nrx <- dim(x)[1]
	ncx <- dim(x)[2]
	nr <- length(testset)
	nc <- length(refset)
	
	PosPerm <- matrix(0,(anzSamplings+1),(nr+1))
	ssq <- matrix(0,(anzSamplings+1),5)
	
	#compile normal and bootstrap/sampling samples or take an existing data set
	if(is.null(zz)){
		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() }
	}else{		#else use zz as z
		z <- zz
	}
	
	if(sampleonly){ return(z) }		# generate only the sampling/bootstrap matrix as a result; can be later assigned in zz 
	
	#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
	mode(PosPerm) <- 'integer'
	mode(ssq) <- 'single'
	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,
				PosPerm=PosPerm,			#row number for min, test factor order vec
				ssq=ssq,					#min ssq, min ssq second way, max ssq,
											# ssq of bootstrap sample with the order giving the min in the original sample,
											# sum of all ssq results - control must be the same result in each sample by a "no replace" sample
				ssqflag=ssqflag
		)
	dyn.unload(dynpath)
	
	return( list(f.back$PosPerm, f.back$ssq, z) )
}


#aaa1 <- enumeration.search.dep.boots.f90(He589.c,anzSamplings=4,refset=c(1,5,3),testset=c(4,6,14,16),method="cor",zz=aaaz, ssqflag=1)
#aaa2 <- enumeration.search.dep.boots.f90(He589.c,anzSamplings=4,refset=c(1,5,3,2,12,13),testset=c(4,6:11,14:16),method="cor", ssqflag=1)


