# TODO: Add comment
# 
# Author: E.Korsching Jul 2, 2012, GPL licence
###############################################################################



tins.plot.dependencies <- function(z, 
		ylim=NULL, y.at=NULL, ylab="correlation", text=T, description="", cex=0.8, mfrow=c(2,3))
{
	# draw regression plots
	# using the data structure given by the tma.inspiration.import() function
	# only the start situation will be plotted
	# two examples of a function call can be found at the end
	
	# input :
	#  z: return object of  tma.inspiration.import()
	#  ylim: y axis range: c(min,max) given or generated from the data
	#  y.at: tick positions,  ylab: y label at tick position
	#  text (T/F): print data name and global ssq and if given a 'description' this text will be placed in a following line
	#  cex: text size,  mfrow: layout of plot page
	# Note: special characters in marker names might cause problems
	
	# return value: none
	# output: graphic
	
	#ini
	if(missing(z)){ cat("\n provide data object from function  tma.inspiration.import() "); stop() }
	if(mode(z)!="list"){ cat("\n data object seems not to be from function  tma.inspiration.import() "); stop() }
	if(length(z)!=10){ cat("\n data object seems not to be from function  tma.inspiration.import() "); stop() }
	
	# get the Fortran based correlation table
	x <- z$corTableF
	# get the start row (row.names do contain the original order - here position 1 is the start position)
	tmp.row <- which(row.names(z$result)==1)
	#  make decision if z is coming from sampling or combinations approach
	if((length(z$marker)-z$nref+1)==ncol(z$result)){
		tmp.col <- c(2:(length(z$marker)-z$nref+1))
		o <- unlist( z$result[tmp.row, tmp.col] )
	}else{
		tmp.col <- c( (length(z$marker)+2) : (length(z$marker)+(length(z$marker)-z$nref)+1) )
		o <- unlist( z$result[tmp.row, tmp.col] )
	}
	# get the corresponding ssqg
	ssqg <- z$result[tmp.row, 1]
	
	# pick some additional information
	nr <- nrow(x)
	nc <- ncol(x)
	rnames <- dimnames(x)[[1]]		# get row names
	cnames <- dimnames(x)[[2]]		# get col names
	
	#plot
	par(mfrow=mfrow)
	if(is.null(ylim)){ ylim <- range(x) }
	
	x1 <- (1:nr)		#make ascending vector of length nr
	res <- 0
	for(i in 1:nc){		#create nc plots
		loess <- lm(x[o,i]~x1)					#fit for abline
		plot(x=x1, y=x[o,i], ylab="", tck=F, xlab="", ylim=ylim, axes=F, cex=cex)	#plot points
		abline(coef=loess$coefficients)		#plot fit line
		# customize axis
		axis(side=1, at=x1, labels=F)
		text(x=c(1:length(rnames[o])), y=par("usr")[3]+( (par("usr")[3])/10 ), labels=rnames[o], srt=45, adj=1, xpd=TRUE, cex=cex)
		if(is.null(y.at)){ axis(side=2) } else { axis(side=2, at=y.at) }
		# customize additional text
		mtext(text=cnames[i], side=3, line=0.5, outer=F, srt=0, adj=0, cex=cex)
		mtext(text=ylab, side=2, line=3.2, outer=F, srt=90, adj=0.5, cex=cex)
	}
	
	if(text){		#text below the last graphic tile
		tmp <- paste( deparse(substitute(x)), collapse="" )		#extract data name
		tmp.len <- nchar(tmp)
		if(tmp.len>15){ tmp <- paste(substr(tmp, start=1, stop=15), ".. ") }	#restrict length of name to 15
		
		mtext(paste(tmp,",   global ssq = ", round(ssqg, 2), sep=""), side=1, line=3.0, adj=0, cex=cex-0.2)
		if(description!=""){ mtext(text=description, side=1, line=3.6, adj=0, cex=cex-0.2) }
	}
	
	cat("\n order :  ", rnames[o], "\n  min. global ssq = ", ssqg,"\n")	#output some status information for control purpose
	return()
}



