# TODO: Add comment
# 
# Author: E.Korsching Jul 24, 2014
###############################################################################



multi.diff <- function(x, gin, groupSt, mtc="none", t.a=0.05, prefix="a", rel.path="/"){
	# multiple differential analyses
	# (all experiments needs to be in one data.frame)
	# x: data.frame with experiments in columns, gin: genome information data.frame  (both with corresponding names/row.names)
	# groupSt: a list with groups of columns (integer numbers)
	# mtc: multiple test correction (method)
	# all versus all group combinations in 'groupSt' will be calculated
	# single results and overview will be returned
	# ! create result path first
	
	# ini
	source(file="/home/korschi/eclipseR/0functions/tdifferential/report.R")
	source(file="/home/korschi/eclipseR/0functions/tdifferential/script.R")
	
	# overview matrix
	groupSt.len <- length(groupSt)
	res.overview <- data.frame(matrix(0,groupSt.len,groupSt.len), row.names=groupSt)
	names(res.overview) <- groupSt
	
	# open file for reporting
	report(name=paste(rel.path,prefix,".log.txt",sep=""))    #switch from standard output to report file
	
	# ini result list
	res.list <- list()	#length 0
	
	for(i in 1:6){		#matrix col -- xcols
		for(j in (i+1):7){		#matrix row -- ycols
			nu.1 <- groupSt[[i]]
			nu.2 <- groupSt[[j]]
			n.1 <- substr(names(x)[ groupSt[[i]] ],1,15)		# limit name to 15
			n.2 <- substr(names(x)[ groupSt[[j]] ],1,15)
			cat("\n\n -------------------------------------------------------------------------------- \n")
			cat("\n experiment : ",n.1," versus ",n.2,"\n")
			cat("\n experiment : ",nu.1," versus ",nu.2,"\n")
			res.list[[(length(res.list)+1)]] <- t.differential(
					x=x,					#signal data
					xcols=nu.1,					#col numbers group 1
					ycols=nu.2,					#col numbers group 2
					gin=gin,					#genome information file , including "Gene.Symbol","label"
					label.col="label",		#name of gene description col
					parametric=T,			#filter by t test else by U test
					Shapiro.W.alpha=t.a,	#decision if normal distribution : >alpha: H0: normal distribution -> set flag, !:outlier sensitive
					F.p.alpha=t.a,			#decision if t-test or Welch variant : F test alpha error, >alpha: H0: no difference in var
					t.p.alpha=t.a,			#filter: t test alpha error, <=alpha: Ha: difference in means
					U.p.alpha=t.a,			#alternative for t test: non parametric: Wilcoxon Mann Whitney U test alpha error, <=alpha: Ha: difference in means
					F.filter=F,				#filter also according to var: keep only: >alpha: H0: no difference in var
					fold.change.k=1,		#filter: ratio
					diff.k=1,				#filter: difference of group means
					sig.out=T,				#save results in data frame
					file.name=paste(prefix,".",paste(nu.1,collapse="."),"-",paste(nu.2,collapse="."),sep=""),		#graphics
					relative.path=rel.path,	#path relative according to getwd() for png type graph sheets + text file
					get.all=F,				#get all calculated values - debug algorithm
					sort="no",				#sort output by "t.p.value", "difference"
					test.corr=mtc,		#multiple testing correction "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"
					plot.it=T,				#plot graphs? - always be printed in a file -
					volcano=T,				#plot volcano graph?
					MARGIN=2,				#margins of plot
					rows.on.page=28,		#adjust number of result rows per plot page
					cex=0.6,				#text size
					lwd=3,					#line width
					font=10,					#font number
					color.set="blueWhiteRed",	#change the color palette - see color.set()
					swscr=F					#T: standard reporting in file , F: to console (or triggered by other functions)
			)
			# print number of differential observations in overview matrix
			tmp <- res.list[[length(res.list)]]
			if(is.data.frame(tmp)){
				res.overview[j,i] <- nrow(tmp)
			}else{
				res.overview[j,i] <- 0
			}
		}
	}
	cat("\n output in subfolder  ",paste(getwd(),rel.path,sep=""),"\n")
	script()    #switch from report file to standard output
	
	cat("\n end - output in subfolder  ",paste(getwd(),rel.path,sep=""),"\n")
	return(list(overview=res.overview,results=res.list))
}


#aa <- list(c(1,5,9),c(2,6,10),c(3,7,11),c(4,8,12),c(13,14,15,16),c(17,18,19,20),c(21,22))
#mh.exp.2.iqrm.diff <- multi.diff(x=mh.exp.2.iqrm1, gin=mh.exp.2.gin, groupSt=aa, mtc="none", t.a=0.05,
#	prefix="mh.diff", rel.path="/diff/")



