#!/bin/sh
#set -vx
# Create Amber input files from a pdb file containing only the ligand.
# Scott Brozell, Dave Case, TSRI

# define constants
mopac_output=mopac.out
leap_log=leap.log
on=on

# create usage statement
usage()
{
cat << EOF
Usage: $0 [-k] ligand_name charge [antechamber_charge_option]

Description
       Create Amber input files from a mol2 file containing only the ligand
       and an optional specification of the Antechamber charge method.
       The Amber input files are necessary for the Dock amber_score.

Options
       -k
              keep temporary files; the default is to remove them.
              This option must be the first argument.

Operands
       ligand_name
              the name of the ligand only pdb file without its extension.

       [antechamber_charge_option]
              the antechamber charge options.  The default is -c bcc.
              These options must be the last arguments.
              Consult the antechamber documentation for syntax and available
              charge methods.  See the examples below.
              Depending on the charge method a file of name
              {ligand_name}.$mopac_output may be produced.
Outputs
       {ligand_name}.frcmod
              the force modification Amber parameter file.

       {ligand_name}.gaff.mol2
              the mol2 file with Amber GAFF atom types and charges
              produced via Antechamber.

       {ligand_name}.inpcrd
              the Amber initial coordinates file.

       {ligand_name}.log
              verbose output from LEaP's command processing.

       {ligand_name}.prmtop
              the Amber force field parameters and topology file.

       stdout
              output from LEaP's command processing.
              This should be inspected for errors, warnings, and comments.

Side Effects
       A preexisting $mopac_output will be deleted.
       A preexisting $leap_log will be appended to and renamed.

Examples
       $0 ligand
       $0 lig -c gas
       $0 lig -c rc -cf my.charges

EOF

exit 1;
}

# process command line options
if [ "$1" = '-k' ]
then
    keep_temporary_files=$on
    shift
fi

# process remaining command line arguments
if [ $# -lt 2 ]
then
    usage;
elif [ $# -eq 2 ]
then
    ligand=$1
    charge=$2
    shift
    charge_options='-c bcc'
else
    ligand=$1
    charge=$2
    shift
    charge_options="$@"
fi
echo "Using ligand file prefix: $ligand"
echo "Using total charge: $charge" 
echo "Using antechamber options: $charge_options"

# define input, scratch, and output file names
input_mol2=$ligand.mol2
leapin=`echo $0.leapin.$$ | sed 's@/@_@g'`  # convert pathname into filename
leapin=/tmp/$leapin
output_frcmod=$ligand.frcmod
output_inpcrd=$ligand.inpcrd
output_log=$ligand.log
output_mol2=$ligand.gaff.mol2
output_prmtop=$ligand.prmtop

if [ -f $mopac_output ]
then
    /bin/rm $mopac_output
fi

echo "running: antechamber to generate GAFF mol2 file format with AM1-BCC charges"
if ! (export DOCK_HOME=DOCKHOME; DOCKHOME/bin/antechamber -s 2 -i $input_mol2 -fi mol2 -o $output_mol2 -fo mol2 -nc $charge $charge_options)
then
    echo "Error running antechamber!"
    # usage;
fi
if [ -f $mopac_output ]
then
    mv $mopac_output $ligand.$mopac_output
fi
if [ "$keep_temporary_files" != "$on" ]
then
    /bin/rm -f ANTECH* ATOMTYPE.INF mopac.in SHUTDOWN
fi

if ! (export DOCK_HOME=DOCKHOME; DOCKHOME/bin/parmchk -f mol2 -i $output_mol2 -o $output_frcmod)
then
    echo "Error running parmchk!"
    # usage;
fi

if [ -f $mopac_output ]
then
unit=`
 cat $output_mol2 |
 sed '/^[       ]*$/d' |               # delete blank and whitespace-only lines
 sed -n '/@<TRIPOS>ATOM/{n;p;}' |  # print the line after @<TRIPOS>ATOM
 sed 's/        / /g' |                # convert a tab to a space
 sed 's/  */ /g' |                 # convert multiple spaces to a single space
 sed 's/^ //' |                    # delete a leading space
 cut -d' ' -f8                     # extract field 8 using space as a delimiter
`
fi

cat << EOF > $leapin
 source leaprc.gaff
 frcmod=loadamberparams $output_frcmod
 $ligand=loadmol2 $output_mol2
 check $ligand
 savepdb $ligand $ligand.amber.pdb
 saveamberparm $ligand $output_prmtop $output_inpcrd
 quit
EOF

DOCKHOME/bin/tleap -s -f $leapin
if [ -f $leap_log ]
then
    mv $leap_log $output_log
fi
if [ "$keep_temporary_files" != "$on" ]
then
    /bin/rm $leapin
fi

exit

