# TODO: Add comment
# 
# Author: E.Korsching 10.9.2009
###############################################################################



expression.matrix <- function(x,ctext=NULL,rtext=NULL,title.m="Titel",cex=0.5,
		invers=F,str.p=F,color="cgh"){
	# eine farbige Matrix erzeugen, die die Expressionswerte einer Matrix illustrieren - fix 15 cuts = 16 levels
	# plot of cols: horizontal, rows: vertical
	# für Expressions-Ratios : [-..0..+] : e.g. 'blueWhiteRed' color set has to be choosen
	# für Expressions-Werte : [0..max]  : e.g. 'blueRed' color set has to be choosen
	# invers: TRUE: color gradient will be inverted
	# ctext: col label str vector, rtext: row label str vector, title.m: Titel, f.mar: Rahmenbreiten
	# cuts: Vorgabe der Stufen für x die mit je einer aus 16 Farben belegt werden sollen: 16-1 = 15 cuts = 16 steps
	# str.p: instead of color plot numbers
	# colors: kann mit einem definierten 'color.set' verändert werden
	
	#ini
#	source("/home/korschi/eclipseR/0functions/0general/color.set.R")
	color.set(color=color,example=F,colorcode=F)		#set the predefined colors 1-16 (see color)
	
	nr <- nrow(x)
	nc <- ncol(x)
	y <- x				#produce template for color matrix
	
	#calculate 15 cuts - linear
	step.size <- (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))/15
	start <- min(x, na.rm=TRUE)+(step.size/2)
	end <- max(x, na.rm=TRUE)
	cuts <- seq( start, end, step.size)	#cat("\n start ",start,"\n end ",end,"\n stepsize ",step.size,"\n cuts ",cuts)
	
	#invert col order
	if(invers) {col.set <- seq(16,1,-1)} else {col.set <- seq(1,16,1)}
	
	#transform x values to y color values
	if(sum(is.na(y))>0){
		y[is.na(x)] <- 17
		cat("\n",sum(is.na(x))," NA(s) in the data set - color set to black")
	}
	y[x<cuts[1]] <- col.set[1]								#red if color range cgh
	y[x>=cuts[1] & x<=cuts[2]] <- col.set[2]
	for(i in 2:14){
		y[x>cuts[i] & x<=cuts[i+1]] <- col.set[i+1]
	}
	y[x>cuts[15]] <- col.set[16]							#green
	
	
	## start plotting
	# define plot area
	layout.show(layout(matrix(c(1,2),nrow=1,ncol=2,byrow=FALSE), widths=2.2 )) #,heights=lcm(5)
	
	#open plot area
	#plot dataframe: top to bottom left to right / col wise, first col horizontal on top in plot area
	#plot(c(0, nrow(y)+1), c(0, ncol(y)+1), type="n", axes=F, xlab="", ylab="" )
	plot(c(-(nrow(y)/10), nrow(y)+(nrow(y)/40)), c(-(ncol(y)/40), ncol(y)+(ncol(y)/10)), type="n", axes=F, xlab="", ylab="" )
	title(title.m, cex=cex)
	
	#
	for(i in 1:nc){
		for(j in 1:nr){
			if(str.p){ text(j, nc+1-i, round(x[j,i],1), cex=cex-0.1, crt=0, adj=0)
				}else{
					if(y[j,i]==17){
						plot.bar.point.segment(x=j,height=1,width=1,col="black",density=-1,horiz=F,at=nc+1-i)
					}else{
						plot.bar.point.segment(x=j,height=1,width=1,col=palette()[y[j,i]],density=-1,horiz=F,at=nc+1-i)
					}
			}
		}
	}
	
	# axes
	if(!is.null(ctext)){
		for(i in 1:nc){		# left
			text(0, nc+1-i+0.5, ctext[i], cex=cex, srt=0, adj=1)
		}
	}
	if(!is.null(rtext)){
		for(i in 1:nr){		# top
			text(i, nc+1.2, rtext[i], cex=cex-0.1, srt=45, adj=0)
		}
	}
	
	## color key	
	plot(c(-20, 2), c(-1, 17), type="n", axes=F, xlab="", ylab="" )
	
	plot.bar.point.segment(x=1,height=1,width=1,col=palette()[col.set[1]],density=-1,horiz=F,at=0)			#red if color range cgh
	text(0, 0.5, paste("<",round(cuts[1],1)), cex=cex, srt=0, adj=1)
	
	plot.bar.point.segment(x=1,height=1,width=1,col=palette()[col.set[2]],density=-1,horiz=F,at=1)
	text(0, 1.5, paste(">=",round(cuts[1],1),", <=",round(cuts[2],1)), cex=cex, srt=0, adj=1)
	
	for(i in 2:14){
		plot.bar.point.segment(x=1,height=1,width=1,col=palette()[col.set[i+1]],density=-1,horiz=F,at=i)
		text(0, i+0.5, paste(">",round(cuts[i],1),", <=",round(cuts[i+1],1)), cex=cex, srt=0, adj=1)
	}
	plot.bar.point.segment(x=1,height=1,width=1,col=palette()[col.set[16]],density=-1,horiz=F,at=15)		#green
	text(0, 15.5, paste(">",round(cuts[15],1)), cex=cex, srt=0, adj=1)
	
	#
	palette("default")
}


