#!/bin/csh -f
#
# Shell script to run Multidock
#
# *************************IMPORTANT****************************
# **** Make sure you have defined the environment variable *****
# **** MULTIDIR which must point to the directory          ***** 
# **** containing the executables and the parameter files. *****
# **** i.e. use the setenv command in your .cshrc file to  *****
# **** define MULTIDIR. e.g.                               *****
# ****                                                     *****
# **** setenv MULTIDIR /home/username/multicopy_directory/ *****
# **************************************************************
#
set nargs = $#argv

if ( $nargs != 3 ) then
  echo  ''
  echo  ' usage: multidock pdb_input pdb_output params.dat'
  echo  ''
  echo  ' The params.dat file should contain the control_parameters'
  echo  ' specific for the particular application. See params.dat_example'
  echo  ' in $MULTIDIR and the MULTIDOCK manual section 2.3 for details.'
  echo  ''
  exit
endif

/bin/rm -f PDB PDB1 PDB2 PDBtor TOR.DAT INTERNAL.DAT
/bin/rm -f $argv[2] $argv[2]'.tor' $argv[2]'.genhyd' $argv[2]'.out'

set PDBFILE = 0
set PARAMSFILE = 0

if ( -e $argv[1] && ( $PDBFILE == 0 ) ) then
  set PDBFILE = $argv[1]
endif

if ( $PDBFILE == 0 ) then
  echo ''
  echo ' *** ERROR: cannot find pdb_input file ***'
  exit
endif

if ( -e $argv[3] && $PARAMSFILE == 0 ) then
  set PARAMSFILE = $argv[3]
endif

if ( $PARAMSFILE == 0 ) then
  echo ''
  echo ' *** ERROR: cannot find PARAMSFILE file ***'
  exit
endif

# uses the dictionary files AATOR.DAT and ROT.LIB in $MULTIDIR 
# check they are there.

if ( -e $MULTIDIR/AATOR.DAT == 0 ) then
  echo ''
  echo ' *** ERROR: unable to assign AATOR.DAT file ***'
  exit
endif

if ( -e $MULTIDIR/ROT.LIB == 0 ) then
  echo ''
  echo ' *** ERROR: unable to assign ROT.LIB file ***'
  exit
endif

if ( -e $MULTIDIR/ROT.OCC == 0 ) then
  echo ''
  echo ' *** ERROR: unable to assign ROT.OCC file ***'
  exit
endif


echo ' '
echo ' Running MULTIDOCK '
echo ' ----------------- '
echo ' '$PDBFILE': assigned as pdb_input file'
echo ' '$argv[2]': assigned as pdb_output file'
echo ' '$argv[2]'.out: assigned as pdb_output.out file'
echo ' '$PARAMSFILE': assigned as parameter file'
echo ' '


# Generating sidechain multilple copies
#
echo ' Generating sidechain multilple copies ...'
echo ' '
$MULTIDIR/mcopy $PDBFILE PDB $PARAMSFILE $MULTIDIR/AATOR.DAT $MULTIDIR/ROT.LIB


# Generating hydrogen atoms using PREPI
#
echo ' Generating hydrogen atoms using PREPI ... '
echo ' '
$MULTIDIR/hydtor -iPDB -oPDB1  -d$MULTIDIR/HYDROGENS.DAT -random > $argv[2]'.genhyd'


# Rearranging the format
#
echo ' Rearranging the format of file generated by PREPI'
echo ' '
$MULTIDIR/rearrange PDB1 PDB2


# Calculate the internal energies of rotomers from their observed population
#
echo ' Calculating rotomer internal energies ... '
echo ' '
$MULTIDIR/torpdb PDB2 PDBtor


# Calculating torsion angles using PREPI
#
$MULTIDIR/hydtor -iPDBtor -oTOR.DAT -tor > $argv[2]'.tor'
$MULTIDIR/rotenergy TOR.DAT INTERNAL.DAT $MULTIDIR/ROT.OCC


# Running ensemble optimisation algorithm
#
echo ' Running ensemble optimisation algorithm ... '
echo ' '
time $MULTIDIR/multidock_main PDB2 $argv[2] $argv[2]'.out' $PARAMSFILE INTERNAL.DAT
