# TODO: Add comment
# 
# Author: E.Korsching  2020
###############################################################################



expr.boxplot <- function(
		x,			# data.frame  or  list - before normalization
		y,			# data.frame  or  list - after normalization
		title="",
		logscale="",		# y axis with log scale: "y"
		outlier=F,			# show outlier
		mfrow=c(2,1),
		mar=c(7,5,4,4),
		srt=90,
		cex=0.6,
		line=0,
		adj=1,
		col.f=c("blue","green"),
		col.b="black"
)
{
	# boxplot of expression matrix before and after a transformation
	
	# ini   df or list
	ch.df <- is.data.frame(x)
	if(ch.df){
		nc <- dim(x)[2]
	}else{
		nc <- length(x)
	}
	cnames <- names(x)
	x.name=deparse(substitute(x))
	
	mar1 <- c(1,5,4,1)
	mar2 <- c(8,5,1,1)
	
	## before
	# layout
	layout(
			matrix(c(1,2), nrow=2, ncol=1, byrow=TRUE),
			widths=c(34),				# relative proportions
			heights=c(9,13),				# relative proportions
			respect=TRUE
	)
#	layout.show(2)	# check the layout
	
	cex.sav <- par()$cex
	adj.sav <- par()$adj
	par(cex=cex,adj=adj)
	
	par(mar=mar1)
	if(logscale=="y"){
		if(ch.df){			# +1: to get the log scale running if '0'
			xx <- x+1
		}else{
			xx <- vector("list",0)
			for(i in 1:nc){
				xx[[i]] <- x[[i]]+1
			}
		}
	}else{
		xx <- x
	}
	boxplot(x=xx,				# wisker : 1.5 * IQR range top/bottom
			outline=outlier,
#			outwex= .4,
			notch=F,
			ylim=range(boxplot(xx,plot=F)$stats),
			ylab=if(logscale=="y"){"intensity (+1)"}else{"intensity"},
			cex.lab=1.1,
			boxwex= .7,
			log=logscale,
			plot=T,
			axes=F,
			col=col.f[1],
			border=col.b
	)
	axis( side=2, cex.axis=1.1, cex.lab=1.1 )
	tmp <- paste(x.name,", before and after",title,"\n",sep=" ")
	mtext(text=tmp, side=3, line=line, outer=F, adj=1)
	
	## after
	par(mar=mar2)
	if(logscale=="y"){
		if(ch.df){			# +1: to get the log scale running if '0'
			xx <- y+1
		}else{
			xx <- vector("list",0)
			for(i in 1:nc){
				xx[[i]] <- y[[i]]+1
			}
		}
	}else{
		xx <- y
	}
	boxplot(x=xx,
			outline=outlier,
#			outwex= .4,
			notch=F,
			ylim=range(boxplot(xx,plot=F)$stats),
			ylab=if(logscale=="y"){"intensity (+1)"}else{"intensity"},
			cex.lab=1.1,
			boxwex= .7,
			log=logscale,
			plot=T,
			axes=F,
			col=col.f[2],
			border=col.b
	)
	axis( side = 1 , at=c(1:length(names(x))) , labels=names(x), tick=TRUE, las=2, srt=srt )
	axis( side = 2 , cex.axis=1.1, cex.lab=1.1)
	
	# restore
	par(mar=c(5,4,4,2),cex=cex.sav,adj=adj.sav)
	#
	return()
}



