#!/usr/local/Python-2.5.2/bin/python

# Submit jobs to cluster

import os
import os.path
import commands

pbs_template = """\
#!/bin/tcsh 

# This is needed for ICC variables, for some reason.
resoft

# YANK test job
#
#  Submit this script using the command: qsub <script_name>
#
#  Use the "qstat" command to check the status of a job.
#
# The following are embedded QSUB options. The syntax is #PBS (the # does
# _not_  denote that the lines are commented out so do not remove).
#
# walltime : maximum wall clock time (hh:mm:ss)
#PBS -l walltime=12:00:00
#
# nodes: number of 8-core nodes
#   ppn: how many cores per node to use (1 through 8)
#       (you are always charged for the entire node)
#PBS -l nodes=1:ppn=8
#
# export all my environment variables to the job
#PBS -V
#
# job name (default = name of script file)
#PBS -N %(jobname)s
#
# specify queue
#PBS -q lincoln
#
# filename for standard output (default = <job_name>.o<job_id>)
# at end of job, it is in directory from which qsub was executed
# remove extra ## from the line below if you want to name your own file
#PBS -o /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbsa/%(molecule)s/stdout.1
##PBS -o /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbvi/%(molecule)s/stdout.1
##PBS -o /scratch/users/jchodera/yank/test-systems/FKBP/amber-gbsa/%(molecule)s/stdout.3
##PBS -o /scratch/users/jchodera/yank/test-systems/chk1/amber/amber-gbsa/%(molecule)s/stdout.4
#
# filename for standard error (default = <job_name>.e<job_id>)
# at end of job, it is in directory from which qsub was executed
# remove extra ## from the line below if you want to name your own file
#PBS -e /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbsa/%(molecule)s/stderr.1
##PBS -e /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbvi/%(molecule)s/stderr.1
##PBS -e /scratch/users/jchodera/yank/test-systems/FKBP/amber-gbsa/%(molecule)s/stderr.3
##PBS -e /scratch/users/jchodera/yank/test-systems/chk1/amber/amber-gbsa/%(molecule)s/stderr.4
#
# End of embedded QSUB options
#
set echo               # echo commands before execution; use for debugging
#

set JOBID=`echo $PBS_JOBID | cut -d'.' -f1`



#cd $SCR                # change to job scratch directory
                       # use cdjob <jobid> to go to this directory once
                       # the job has started

# Get executable and input files from mass storage
# **** IF THEY ARE STORED ON MSS ****
# otherwise copy your executable to $SCR(the job's scratch directory)
# Ex.   cp ~/subdir01/subdir02/a.out $SCR 
#msscmd cd dir1, get a.out, mget *.input

# mss doesn't keep executable bit set, so need to set it on program
#chmod +x a.out

#./a.out

# save output files back to mass storage
#msscmd mkdir Job.${JOBID},cd Job.${JOBID}, mput *

setenv JOBDIR /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbsa/%(molecule)s
#setenv JOBDIR /scratch/users/jchodera/yank/test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbvi/%(molecule)s
#setenv JOBDIR /scratch/users/jchodera/yank/test-systems/FKBP/amber-gbsa/%(molecule)s
#setenv JOBDIR /scratch/users/jchodera/yank/test-systems/chk1/amber/amber-gbsa/%(molecule)s

setenv YANKDIR /scratch/users/jchodera/yank/proof-of-concept/yank

date

# Change to job directory
cd $JOBDIR

# Clean up old working files
#rm -f stdout stderr
#rm -f $JOBDIR/restraints
#rm -f $JOBDIR/*.nc

# Run yank in current directory
$YANKDIR/yank $JOBDIR

date
"""

def isletter(c):
   if ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z')):
      return True
   return False   

# T4 lysozyme L99A
#molecules = ['1-methylpyrrole', '2-fluorobenzaldehyde', 'benzene', 'ethylbenzene', 'indole', 'n-butylbenzene', 'n-propylbenzene', 'p-xylene', 'thieno_23c_pyridine', '12-dichlorobenzene', '23-benzofuran', 'benzenedithiol', 'indene', 'isobutylbenzene', 'n-methylaniline', 'o-xylene', 'phenol', 'toluene']
molecules = ['p-xylene', 'benzene', 'phenol', 'indole']
molecules = ['benzene', 'indole']

# FKBP
#molecules = ['L12', 'L14', 'L20', 'LG2', 'LG3', 'LG5', 'LG6', 'LG8', 'LG9']

# Chk1 kinase
#molecules = ['molec000001', 'molec000002', 'molec000003']

# Kim's congeneric series
#molecules += ['molec000023', 'molec000034', 'molec000073', 'molec000089', 'molec000096']

for molecule in molecules:
   print molecule

   # Make sure job name begins with a letter
   jobname = molecule
   if not isletter(molecule[0]):
      jobname = 'Q' + jobname
   # Truncate job name to 15 characters for batch queue system
   jobname = jobname[0:15]
   #print jobname
   
   # Form PBS script
   pbs = pbs_template % vars()

   # Construct directory.
   filename = '../../test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbsa/%(molecule)s/run.pbs' % vars()
   #filename = '../../test-systems/T4-lysozyme-L99A/amber-gbsa/amber-gbvi/%(molecule)s/run.pbs' % vars()
   #filename = '../../test-systems/FKBP/amber-gbsa/%(molecule)s/run.pbs' % vars()   
   #filename = '../../test-systems/chk1/amber/amber-gbsa/%(molecule)s/run.pbs' % vars()   
   outfile = open(filename, 'w')
   outfile.write(pbs)
   outfile.close()

   # Submit to PBS
   output = commands.getoutput('qsub %(filename)s' % vars());
   print output
   

   
   
   
