# TODO: Add comment
# 
# Author: E.Korsching Jan 15, 2014
###############################################################################



plot.order.comparison <- function(x, text=T, description="", sq=NULL, mar=c(8,2,8,4), lwd=3, cex=0.8, ssqflag=1)
{
	# draw order plots (2 list elements)
	#  with the same marker panel in the reference and test set (test might be differ in size)
	#  (avoid special characters in col names)
	# using results from  enumeration.search.dep.boots.f90  to fill cor table, order vec  (example at the end)
	# input : x: list containing a list with: 1. cor table, 2. order vec, 3. plot color :
	#         list(list(cor table 1, order 1, color 1), list(cor table 2, order 2, color 2))
	#  text: print data name and global ssq (T/F) and if given a description in next line
	#  sq: plot a transparent (if supported) square at pos c(x1,x2) to indicate a less important part in the graph
	#  mar: margins, lwd: line width (connections), cex: text size
	
	#ini
	if(missing(x)){ cat("\n order vector and color must be provided"); break }
	len.list <- length(x)
	if(len.list!=2){ cat("\n list needs two list objects"); break }
	
	# create meta data list structure
	metadata <- vector("list", len.list)
	for(j in 1:len.list){
		metadata[[j]] <- vector("list", 4)
	}
	
	tmp.r <- c(0,0)		# start value for range
	for(j in 1:len.list){
		tmp <- x[[j]]
		len.element.list <- length(tmp)
		if(len.element.list!=3){ cat("\n internal list element ",j," must contain \n 1. cor table, 2. an order object and 3. a color"); break }
		#meta data from all list elements
		metadata[[j]][[1]] <- nrow(x[[j]][[1]])	# nr
		metadata[[j]][[2]] <- ncol(x[[j]][[1]])	# nc
		metadata[[j]][[3]] <- dimnames(x[[j]][[1]])[[1]]		# rnames - get row names
		metadata[[j]][[4]] <- dimnames(x[[j]][[1]])[[2]]		# cnames - get col names
		#check for order
		cat("\n order ",j," - ",metadata[[j]][[3]][ x[[j]][[2]] ] )
	}
	
	#set range values
	axis.y.positions <- c(-1,1)
	ylim <- round((axis.y.positions+c(-0.1,0.1)), 1)		# define y range for design
	# adjust y range  for spare plot space on top / bottom of axis
	tmp <- abs( (ylim[2]-ylim[1])/1.8 )
	ylim.a <- round( (ylim + c(-tmp,tmp) ), 1)
	
	# x axes offset in y direction
	ylim.off <- abs( (ylim[2]-ylim[1])/30 )
	
	#plot
	par(mar=mar)	#default mar , lines: 5,4,4,2 +0.1
	
	for(j in 1:len.list){				#create one or two regression line(s) / x axis(axes) per nc
		#ascending vector of length nr
		x1 <- (1:metadata[[j]][[1]])
		len.x1 <- length(x1)
		if(j==1){
			plot(x=x1, y=rep(0,len.x1), type="n", ylab="", tck=F, xlab="", ylim=ylim.a, pch=1, axes=F, cex=cex, col=x[[1]][[3]])
			# axis and labels - bottom
			axis(side=1, at=x1, pos=ylim[1]-ylim.off, labels=F, col=x[[j]][[3]])
			text(x=x1+0.05, y=ylim[1]-ylim.off*3.4, labels=metadata[[1]][[3]][ x[[1]][[2]] ], srt=45, adj=c(1,1), xpd=TRUE, cex=cex, col=x[[j]][[3]])
			# reference label and color first experiment
			mtext(side=3, line=2, text=paste(metadata[[1]][[4]],collapse=" - "), adj=0.1, cex=cex+0.2, col="black")
		}else{
			# axis and labels - top
			axis(side=3, at=x1, pos=ylim[2]+ylim.off, labels=F, col=x[[j]][[3]])
			text(x=c(1:length(metadata[[j]][[3]][ x[[j]][[2]] ])), y=ylim[2]+ylim.off*2.7, labels=metadata[[j]][[3]][ x[[j]][[2]] ], srt=45, adj=c(0,0), xpd=TRUE, cex=cex, col=x[[j]][[3]])
		}
	}
	
	# now plot explanatory stuff
	# create triangle
	polygon(x=c(1+0.2,sq[1]-0.2,1+0.2),
		y=c(0+0.2,0,0-0.2),
		density=0, border="black")
	polygon(x=c(sq[2]+0.2,len.x1-0.2,len.x1-0.2),
		y=c(0,0+0.2,0-0.2),
		density=0, border="black")
	
	# now plot connection lines
	# create order structure - o1-o2 pairs build the connection scheme !
	o1 <- metadata[[1]][[3]][ x[[1]][[2]] ]
	o2 <- metadata[[2]][[3]][ x[[2]][[2]] ]
	c1 <- order(o1)		# bottom
	c2 <- order(o2)		# top
	for(j in x1){				#create connection lines
		lines(x=c(c1[j],c2[j]), y=ylim, col="orange", lty=1, lwd=lwd)
	}
	
	# now plot explanatory stuff again
	# create color
	tmp <- c(col2rgb(col="grey", alpha=F), 175)		# last value transparency (0..255 like all values)
	# grey square - less important part of graph
	polygon(x=c(sq[1],sq[2],sq[2],sq[1]),
			y=c(ylim.a[1],ylim.a[1],ylim.a[2],ylim.a[2]),
			density=-1, border=NA, col=rgb(red=tmp[1], green=tmp[2], blue=tmp[3], alpha=tmp[4], names=NULL, maxColorValue=255))

	#text below the last graphic tile
	if(text){
		tmp <- paste( deparse(substitute(x[[]])), collapse="" )	#extract data name
		tmp.len <- nchar(tmp)
		if(tmp.len>35){ tmp <- paste(substr(tmp, start=1, stop=35), ".. ") }	#restrict length of name to 35
		if(description!=""){
			mtext(text=description, side=1, line=3.2, adj=0, cex=cex-0.2)
		}else{
			mtext(text=tmp, side=1, line=3.2, adj=0, cex=cex-0.2)
		}
	}
	
	cat("\n fin \n")
	return()
}



###
#pdf(file=paste(getwd(),"/results/robert/test",".",format(Sys.time(), "%Y%m%d%H%M"),".pdf", sep=""),
#		width=7, height=7, onefile=T, title="MBC.perm.lumi2", pointsize=12)
#plot.order.comparison(x=list(
#				list(rk.Rmale.2.d.1c[[3]][1:8,,drop=F], rk.Rmale.2.d.1c[[1]][1,9:2], "blue"),
#				list(rk.Hfemale.c2.d.1g[[3]][1:8,,drop=F], rk.Hfemale.c2.d.1g[[1]][1,2:9], "black")
#		),
#		text=T, description="1g - male (blue), female (black)", sq=c(3.5,5.5), mar=c(7,2,7,4), lwd=3, cex=0.8, ssqflag=1 )
#dev.off()


