######################################################################################
# making Plot/Chart Image
# Syntax: Rscript makingChart.R <input-data-file> 
# Dataset: used comma seperator
# Author:  Beomsoo Han
# Date  :  April 18 2011
######################################################################################

options(warn=-1)  # no print out warning message
options(width=120)

Args <- commandArgs(TRUE)

if (length(Args) < 2) {
   cat("\nError: No input-data-file\n\n")
   cat("\t Syntax:\n\t Rscript makingChart.R <input-data-file> <image-file-name without extension> <barplot option: B or NULL> \n\n") 
#   quit()
} 

infile <- Args[1]
imagefile <- Args[2]
if(length(Args) == 3 & Args[3] == 'B') {
   barplot.option = TRUE
} else {
   barplot.option = FALSE
}

# cat("Args[3]:", Args[3], "  --" , barplot.option, "\n");

# infile <-'1G6J.modelcorr2.txt' 
# imagefile <- 'test.png'

indata <- read.table(file=infile, sep=',', header=TRUE, blank.lines.skip=TRUE, comment.char="#") 
# atomnames <- c('N','CA','CB','CO','HA','HN','ALL')
atomnames <- names(indata)[2:8]

numModel <- length(indata[,1])

# plot save to file
png(filename = paste(imagefile,"-lineplot.png", sep=""), width=750, height=450)

# oldpar <- par()
par( mgp=c(2,0.7,0), mar=c(4,3,3,2)+0.1) 

# cat("numModel:", numModel,"\n")
# print(indata[1,c(2:7)])

modelNO <- as.integer(sub('m','',indata$MODEL))
indata <- cbind(indata, modelNO)
indata <- indata[with(indata, order(as.integer(modelNO))), ]

td <- t(indata)
r <- range(td[2:8,1:length(td[1,])])
r.lowest <- as.numeric(r[1]) - 0.2  
if (r.lowest < 0) r.lowest <- 0

# set color table
LINEColors <- colors()[c(26:62,68:137,142:153)]
MODELColors <- LINEColors[(1:numModel) * 2]

plot(td[c(2:8), 1], col=MODELColors[1], pch=20, type="o", ylim=c(r.lowest,1.0), axes=FALSE, ylab='Correlation Coefficients', xlab='Atoms')
for( i in 2:numModel ) {
   lines(td[c(2:8), i], col=MODELColors[i], pch=20, type='o')
}

# X axis with atomnames
axis(1, at=1:7, lab=atomnames)
axis(2, at=seq(0,1,0.1), lab=c(seq(0,1,0.1)))

title(main='Correlations between Observed CS and Predicted CS\nfor each NMR Model', font.main=2)
legend('bottom', td[1,], col=MODELColors, ncol=5, cex=1, pch=20, lty=1, inset=0.02, title="< Models > ")

invisible(dev.off())

# if Barplot option is on 
if ( barplot.option ) { 
        png(filename = paste(imagefile,"-barplot-%03d.png", sep=""), width=750, height=450)

	modelnames <- td[length(td[,1]),]  ## 1 ... model max no

	# plot ALL first
	barplot(as.real(td[length(atomnames)+1,]), ylim=c(r.lowest,1), names.arg=modelnames
                     , ylab="Correlation Coefficients", xlab="Model Numbers", main="Correlation Coefficients of ALL Atoms"
		     , border=FALSE, col=rainbow(32), axis.lty=1, xpd=FALSE )

	# Plot each atom
	for ( i in 2:length(atomnames) ) {
	   barplot(as.real(td[i,]), ylim=c(0,1), names.arg=modelnames
                     , ylab="Correlation Coefficients", xlab="Model Numbers", main= paste("Correlation Coefficients of", atomnames[i-1])
		     , border=FALSE, col=rainbow(length(modelnames)), axis.lty=1, xpd=FALSE)
	}

	invisible(dev.off())
}


# par(oldpar)

