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


plot.lsd <- function(
		x,					# lsd / anova.mcp object type
		main=NULL,			# main title
		sub=NULL,			# sub title
		figure=1,			# numeration of plots
		sig=T,				# plot only significant results
		palpha=0.05,			# p alpha level
		plottype="interval",	# plot types ("fold.change","bar","interval")
		cex=1,				# font size
		colset="rainbow",		# colorset
		margin=c(7,5,5,10)		# margins
){
	# plot pvalues and simultaneous 95% confidence limits from paiwise t-test comparisons
	# data type: data.frame:
	# 		pair.diff	lower	upper	pvalue	fc		se		SIG
	# a.b	9.4			5.5		13.3	7e-04	2.6		1.6		***
	# a.c	9.7			5.8		13.6	0.0		2.7		1.6		***
	# b.c	0.3			-3.8	4.4		0.8		1.0		1.6
	
	#ini
	color.set(color=colset, example=F, colorcode=F)
	xname <- deparse(substitute(x))
	nr <- dim(x)[1]
	rnames <- row.names(x)
	
	#work
	ci1 <- x$lower
	ci2 <- x$upper
	
	ci.range <- range(ci1, ci2)
	if(plottype=="fold.change"){ m <- max(x$fc) }else{ m <- max(abs(ci.range)) }
		
	par(mar=margin, cex=cex)
	plot(x=0.5, y=0.5, type="n", ylab="", xlab="", xlim=c(-m, m), ylim=c(0+(nr/10),nr), axes=F, cex=cex)
	
	axis(side=1, line=-0.1, cex=cex, padj=0)
	axis(side=2, line=0.5, at=1:nr, labels=rnames, cex=cex, las=2, adj=0.5)
	if(plottype=="fold.change"){
		axis(4, at=1:nr, labels=paste(abs(round(x$fc, 1))), line=0.5, cex=cex, las=2, adj=0.5)
		axis(4, at=1:nr, labels=as.character(x$SIG), line=5, cex=cex, tick=F, las=2, adj=0.5)
	}else{
		axis(4, at=1:nr, labels=paste(round(x$pvalue, 5)), line=0.5, cex=cex, las=2, adj=0.5)
		axis(4, at=1:nr, labels=as.character(x$SIG), line=5, cex=cex, tick=F, las=2, adj=0.5)
	}
	
	lines(x=c(0, 0), y=c(0, nr), lty=2)		# vertical centre line
	
	for(i in 1:nr){
		if(min(c(ci1[i], ci2[i])) > 0 | max(c(ci1[i], ci2[i])) < 0){
			col1 <- palette()[3]
		}else{
			col1 <- palette()[1]
		}
		if(plottype=="interval"){
			points(x[i, "pair.diff"], i, cex=cex*4, col=col1, pch=16)
			lines(c(ci1[i], ci2[i]), c(i, i), col=col1)
			lines(c(ci1[i], ci1[i]), c(i - 0.15, i + 0.15), col=col1)
			lines(c(ci2[i], ci2[i]), c(i - 0.15, i + 0.15), col=col1)
		}
		if(plottype=="bar"){
			my.barplot( x=i, y=x[i, "pair.diff"], col=c("black", col1), width=0.8, xlab="", tick=F, horiz=T )
			se1 <- ifelse(x[i, "pair.diff"]>0, x[i, "se"],  - x[i, "se"])
			lines(c(x[i, "pair.diff"], x[i, "pair.diff"] + se1), c(i, i), col=col1)
			lines(c(x[i, "pair.diff"] + se1, x[i, "pair.diff"] + se1), c(i - 0.25, i + 0.25), col=col1)
		}
		if(plottype=="fold.change"){
			if(x[i, "fc"]>0){
				my.barplot(x=i, y=x[i, "fc"], col=c("black", "green"), width=0.8, xlab="", tick=F, horiz=T)
			}else{
				my.barplot(x=i, y=x[i, "fc"], col=c("black", "red"), width=0.8, xlab="", tick=F, horiz=T)
			}
		}
	}
	
	#figure legend
	if(is.null(main)){
		main <- paste("Least Significant Difference plot", sep = "")
		mtext(text=main, side=3, line=2, adj=0.5, cex=cex+0.2, font=2)
	}else{
		mtext(text=main, side=3, line=2, adj=0.5, cex=cex+0.2, font=2)
	}
	if(is.null(sub)){
		sub1 <- paste("fig.", figure, ". Non simultaneous 1-", round(palpha/2, 2), " confidence intervals", sep = "")
		sub2 <- paste("and 5% significance level for all pairwise comparisons", sep = "")
		sub3 <- paste("based on Fisher's Protected LSD results", sep = "")
		mtext(text=sub1, side=1, line=3.8, adj=0, cex=cex)
		mtext(text=sub2, side=1, line=4.4, adj=0, cex=cex)
		mtext(text=sub3, side=1, line=5, adj=0, cex=cex)
	}else{
		mtext(text=sub, side=1, line=4, adj=0, cex=cex)
	}
	
	#
	palette("default")
	return()
}


#test2a <- test2
#plot.lsd(
#		x=test2a,					# lsd / anova.mcp object type
#		main="main",			# main title
#		sub=NULL,				# sub title
#		figure=1,			#
#		sig=T,				# plot only significant results
#		palpha=0.05,			# p alpha level
#		plottype="interval",	# plot types ("fold.change","bar","interval")
#		cex=0.7,				# font size
#		colset="rainbow",			# color set
#		mar=c(7,6,5,7)		# margins
#)
	


