# Updated on June 19, 2014
# NOTE: Please see 'PLEK_howto_generate_scripts.pdf' to learn about the
# usage of this script.
rm(list=ls(all=TRUE))


#=====================================
## NOTE: please update the following configurations: 

## R_workfolder: where you place this R script, or where you run this R script. 
## On Linux, i.e. /home/user/plek/; on windows, i.e. "E:\\plek\\
R_workfolder="E:\\plek_2nd_revised\\automodel\\" 
 
## ShellScriptRun_workfolder: where you run the Shell/Bash scripts generated by
## this R script. i.e. /home/user/plek/
ShellScriptRun_workfolder="/dat2/plek/modelling/PLEK.1.2/" 

## PLEKModelling_folder:  where PLEKModelling.py locates.
PLEKModelling_folder="/dat2/plek/modelling/PLEK.1.2/" 

## pbs_queue_name: the queue name of PBS/Torque. i.e. small, long, longlong, etc.   
pbs_queue_name="batch" 

## mRNAs_file: mRNA transcript file
mRNAs_file="Zea_mays.AGPv2.17.cdna.all.fa.mRNA" 

## lncRNAs_file: lncRNA transcript file
lncRNAs_file="maize_genomeBiology_2014_LinLi_lncRNA.fa" 

## script_prefix: the files produced by shell scripts will have this prefix, 
## i.e. maize_ens_linli_p01.sh, maize_ens_linli_p02.sh
script_prefix="maize_ens_linli" 

## pbs_run_abb_name: job name that will be displayed on PBS/Torque. 
## i.e. plek_mdlp01, plek_mdlp02.
pbs_run_abb_name="plek_mdl" 

## nfold: n-fold cross-validation
nfold="10" 

## C and Gamma parameters for training model.
log2c_from=0
log2c_to=5
log2c_by=1
log2g_from=0
log2g_to=-5
log2g_by=-1

# k-mers
k=5


#=====================================
setwd(R_workfolder)
str_qsub_pbs=""
str_qsub_pbs=paste(str_qsub_pbs,"cd ", ShellScriptRun_workfolder, " \n",sep="")
str_qsub_pbs=paste(str_qsub_pbs,"dos2unix ", " *.sh ", " \n",sep="")
qsub_pbs_list_file<-paste(script_prefix,"_qsub_file_list.txt",sep="")

log2c_range<-seq(from=log2c_from,to=log2c_to,by=log2c_by) # -2,0,2,4,6,8
log2g_range<-seq(from=log2g_from,to=log2g_to,by=log2g_by) # 2,0,-2,-4,-6,-8
#log2c_range<-seq(from=0,to=6,by=2) # -2,0,2,4,6,8
#log2g_range<-seq(from=0,to=-8,by=-2) # 2,0,-2,-4,-6,-8
yy<-0
for(log2c in log2c_range)
{
	for(log2g in log2g_range)
	{
		yy<-yy+1
		index<-"p0"
		if(yy>=10)
		  index<-"p"
		
		script_file_name=paste(script_prefix, "_", index, yy, ".sh",sep="")

		str_cpcpbs=""
		str_cpcpbs=paste(str_cpcpbs,"#!/bin/sh\n",sep="")
		str_cpcpbs=paste(str_cpcpbs,"#PBS -N ", pbs_run_abb_name, "_", index, yy,"\n", sep="")
		str_cpcpbs=paste(str_cpcpbs,"#PBS -q ", pbs_queue_name,"\n", sep="")
		str_cpcpbs=paste(str_cpcpbs,"#PBS -l nodes=1:ppn=1\n", sep="")
		str_cpcpbs=paste(str_cpcpbs,"#PBS -o ", ShellScriptRun_workfolder, script_file_name, ".out\n",sep="")
		str_cpcpbs=paste(str_cpcpbs,"#PBS -e ", ShellScriptRun_workfolder, script_file_name, ".err\n",sep="")
		

		str_cpcpbs=paste(str_cpcpbs, 'if [[ -z "$PBS_O_PATH" ]] ; then', "\n", sep="")
		str_cpcpbs=paste(str_cpcpbs, '\t echo "Not set PBS_O_PATH"', "\n", sep="")
		str_cpcpbs=paste(str_cpcpbs, "else", "\n", sep="") 
		str_cpcpbs=paste(str_cpcpbs, "\t export PATH=$PBS_O_PATH\n", sep="")
		str_cpcpbs=paste(str_cpcpbs, "fi", "\n", sep="")
		
		
		str_cpcpbs=paste(str_cpcpbs,"cd  ",ShellScriptRun_workfolder,"\n",sep="")

		str_cpcpbs=paste(str_cpcpbs, "python ",PLEKModelling_folder,"PLEKModelling.py ","               \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-mRNA   ", mRNAs_file, "   \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-lncRNA  ", lncRNAs_file, "   \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-prefix ", script_prefix, "_", index, yy,"   \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-thread 1", "                     \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-nfold ", nfold, "                     \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-log2c ",log2c,",",log2c,",1 ","               \\","\n",sep="")
 		str_cpcpbs=paste(str_cpcpbs, "-log2g ",log2g,",",log2g,",-1","               \\","\n",sep="")
		str_cpcpbs=paste(str_cpcpbs, "-k ",k,"               ","\n",sep="")
		str_cpcpbs=paste(str_cpcpbs,"\n",sep="")

		fileConn_cpcpbs<-file(script_file_name)
		writeLines(str_cpcpbs, fileConn_cpcpbs)
		close(fileConn_cpcpbs)

		str_qsub_pbs=paste(str_qsub_pbs,"qsub ",script_file_name,"\n",sep="")

	}
}

fileConn<-file(qsub_pbs_list_file)
writeLines(str_qsub_pbs, fileConn)
close(fileConn)

msg<-paste("If you are on linux, please run [ls ",script_prefix, "_p??.sh] to check the generated shell scripts. \n Please run [more ",qsub_pbs_list_file,"] to check the generated shell scripts.", sep="")

#cat(msg)



