# TODO: Add comment
# 
# Author: E.Korsching Mar 12, 2012
###############################################################################


plot.colgroup.rowgroup <- function(x, y, z=NULL, z.col="DistanceTSS", xlab="", ylab="", label.vec="", p.title=""){
	# plot comparison chart  from a matrix of measurements  one to many groups with one to many cols  with one to many rows
	# x: data.frame / matrix of measurements
	# y: selections in a list object (variable concerning group-col/group-row combinations)
	# z: annotation with position information (matrix order corresponding to x),  z.col: distance to TSS - column name/number
	#   list0:  list01: vector1: col id(s), ... vectorN: col id(s)  list02: vector1: row id(s), ... vectorN: row id(s)
	# label.vec: group labels, p.title: title of page
	
	#ini
	nrx <- nrow(x)
	ncx <- ncol(x)
	len.list01 <- length(y[[1]])
	len.list02 <- length(y[[2]])
	if(len.list01!=len.list02){ cat("\n col group list and row group list must have equal length "); break }
	
	len.range.mat <- matrix(0,len.list01,4)	#contains the length of the list01/02 vectors
	for(i in 1:len.list01){
		len.range.mat[i,1] <- length(y[[1]][[i]])	#col group
		len.range.mat[i,2] <- length(y[[2]][[i]])	#row group
		len.range.mat[i,3] <- min(x[y[[2]][[i]],y[[1]][[i]]])		#signal min
		len.range.mat[i,4] <- max(x[y[[2]][[i]],y[[1]][[i]]])		#signal max
	}
	
	if(is.null(z)){
		col.list <- colors()[c(26,34,49,24,97,147,181,8,566,654)]	#define colors for data segments
	}else{
		col.list.blue <- colors()[c(62,68,26,9,121)]	#define colors for data segments with TSS information: downstream
		col.list.red <- colors()[c(33,53,76,96,148)]	#upstream
	}
	
	#work
	plot(x=0,y=0, type="n", xlim=c(0,sum(len.range.mat[ ,1])+1), ylim=c(-0.5,max(len.range.mat[ ,4])), xlab=xlab, ylab=ylab, axes=F)
	k <- 1	#x couter
	for(i in 1:len.list01){		#process all groups
		kk <- k		#group position start counter
		if(!is.null(z)){	#color code according to TSS position downstream: blue, upstream: red
			tmp1 <- z[ y[[2]][[i]], z.col ]
#			cat("\n tss ",tmp1)
			tmp2 <- tmp1>0	#T >0
			col.list <- vector(mode="character",length=length(tmp2))	#create final color vector
			k1 <- 1 ; k2 <- 1	#counter blue, red
			for(j in 1:length(tmp2)){	#generate a vector with the appropriate binary coloring scheme, recycle if longer than 5
				if(tmp2[j]){ col.list[j] <- col.list.blue[k1]; k1 <- k1+1 }else{ col.list[j] <- col.list.red[k2] ; k2 <- k2+1 }
				if(k1 > 5){ k1 <- 1 }	#reset blue counter for size > 5
				if(k2 > 5){ k2 <- 1 }	#reset red counter for size > 5
			}
		}
		for(j in 1:len.range.mat[i,1]){		#process all cols of a group
			tmp1 <- x[ y[[2]][[i]], y[[1]][[i]][j] ]
			segments(k-0.3, tmp1, k+0.3, tmp1, lty=1, col=col.list)	#data line(s)
			tmp1 <- mean(x[ y[[2]][[i]], y[[1]][[i]][j] ])
			segments(k-0.2, tmp1, k+0.22, tmp1, lty="12", col="black")	#mean line
			if(len.range.mat[i,2]>1){
				tmp2 <- sd(x[ y[[2]][[i]], y[[1]][[i]][j] ])
				segments(k, tmp1-tmp2, k, tmp1+tmp2, lty="14", col="black")	#sd vertical line
				segments(k-0.2, tmp1+tmp2, k+0.22, tmp1+tmp2, lty="12", col="black")	#sd line top
				segments(k-0.2, tmp1-tmp2, k+0.22, tmp1-tmp2, lty="12", col="black")	#sd line bottom
			}
			k <- k+1
		}
		segments(kk-0.3, 0, (k-1)+0.3, 0, lty=1, lwd=1.5, col="black")	#group line
		tmp1 <- (max(len.range.mat[ ,4])-min(len.range.mat[ ,3]))/15
		segments(kk-0.3, 0, kk-0.3, -tmp1, lty=1, lwd=1.5, col="black")	#left tick
		segments((k-1)+0.3, 0, (k-1)+0.3, -tmp1, lty=1, lwd=1.5, col="black")	#right tick
		mtext(text=label.vec[i], side=1, line=0.5, at=(k-1+kk)/2)		#group text , y=-(max(len.range.mat[ ,4])-min(len.range.mat[ ,3]))/15
	}
	axis(2)
	mtext(text=p.title, side=3, line=1)		#page title
	
	#
	return()
}

#aa <- matrix(rnorm(n=50,mean=2,sd=1),10,5)
#plot.colgroup.rowgroup(x=aa, y=list( list( c(1,3),c(2,4,5) ), list( c(5,6,7),c(2,3) ) ), xlab="group1 group2", ylab="signal", label.vec=c("a","b")  )


