#!/bin/csh
#
# batch_stc
#
# The following script can be used to bypass the graphics
# mode of stc and run several pdb files for binding calculations.
#
# The script makes a number of assumptions in how you want to 
# make the calculations. It is your responsibility to verify 
# that this is what you want stc to do. 
#
##############################################################

# Indicate the directory where stc is installed.
set STC_HOME="/Users/rbo/Desktop/stc.app"

# Indicate the directory containing your pdb input files 
# In this example we have the pdb files in the current directory
set DATA_DIR="./"

# Your custom amino acid definitions file (if you don't like the defaults) 
set AMINO_FILE=""

# Indicate the output directory
set OUT_DIR="./stc_out"

# Name for the type of analysis done in the script (eg, delta g binding)
set ANALYSIS="DGbind"

##############################################################
##############################################################

# some introductory messages and setup
# users can probably skip this section
echo ""
echo " STC batch script"
echo " ****************"
echo ""
echo "Using version of stc found in '$STC_HOME'"

# Figure out where the library is
set ostype=`uname -a | grep "Darwin"`
if (x != "x${ostype}") then
	set LIB_DIR=$STC_HOME/Contents/MacOS/lib/stc
else
	set LIB_DIR=$STC_HOME/lib/stc
endif
echo "Library directory should be in '$LIB_DIR'"

# Here is where we find the programs
set BIN_DIR=$LIB_DIR/bin 
echo "Binary directory should be in '$LIB_DIR'"

# Here are the amino acid definitions
if (x$AMINO_FILE == "x" ) then
	set AMINO_DEF=$LIB_DIR/amino.def
else
	if (! -r $AMINO_FILE ) then
		set AMINO_DEF=$LIB_DIR/amino.def
	else
		set AMINO_DEF=$AMINO_FILE
	endif
endif
echo "Residue Definitions: '$AMINO_DEF'"

# Check for the stc installation
if (! -d $STC_HOME ) then
	echo ""
	echo " Error, Cannot find '$STC_HOME'. Have you set up STC_HOME correctly" 
	echo " within this script? "
	echo ""
	exit 
endif

# check output directory
if ( ! -d $OUT_DIR ) then
    mkdir $OUT_DIR
	if ( ! -d $OUT_DIR ) then
		echo ""
		echo " Error, cannot make output directory '$OUT_DIR'"
		echo ""
		exit
	endif	
endif

# final output files we are interested in.
set out_unsorted=$OUT_DIR/out.$ANALYSIS
set out_sorted=$OUT_DIR/out.$ANALYSIS.sort
\rm -rf $out_unsorted $out_sorted

# How to call the main calc_asa script.
set CALC_ASA="$BIN_DIR/calc_asa $BIN_DIR $AMINO_DEF $OUT_DIR"

##############################################################
##############################################################

#
# Process each pdb file 
# Users should follow how the calculations are done.
#
echo ""
set PDB_FILES=`ls -1 ${DATA_DIR}*.pdb`
foreach i ( $PDB_FILES )

	echo "Calculating ASA for $i"

	# Strip off the ".pdb" suffix of the file, what remains is the basic file root.
	set root=`echo $i | sed 's?\.pdb??'`

	# This script assumes that your pdb file contains both ligand and enzyme. 
	set COMPLEX=$OUT_DIR/$root.complex
	set LIGAND=$OUT_DIR/$root.ligand
	set ENZYME=$OUT_DIR/$root.enzyme

	# Generic input pdb files need to be checked first with fix_pdb.
	# Notice I have assumed your ligand is entered first in the complex pdb!
	$BIN_DIR/fix_pdb 2 $i $COMPLEX $LIGAND $ENZYME > $OUT_DIR/$root.fixpdb.log

	# Next, calculate asa for complex, ligand, and enzyme 
	$CALC_ASA $root $COMPLEX $COMPLEX.acc  > $COMPLEX.log
	$CALC_ASA $root $LIGAND $LIGAND.acc  > $LIGAND.log
	$CALC_ASA $root $ENZYME $ENZYME.acc  > $ENZYME.log

	# Here we do thermodynamics calculations for each ASA file. Since the thermo
	# program was designed to be interactive, we need to echo the correct answers to
	# the program. Users can invoke the $BIN_DIR/thermo program on one pdb file and
	# make a list of the interactive inputs required. In this example, I have set
	# sbb to be calculated and sexu = 0.
	echo "Thermodynamics calculations for $i (binding)"
	echo " 1 \
	$OUT_DIR/$root.thermo.basic \
	$OUT_DIR/$root.thermo.detail \
	$AMINO_DEF \
	$COMPLEX.acc \
	$LIGAND.acc \
	$ENZYME.acc \
	n \
	n \
	n \
	3 \
	1 \
	5 \
	0 " | $BIN_DIR/thermo -log $OUT_DIR/$root.thermo.log > /dev/null

	# Next we parse the results, eg look for the line with DGbind
	set val=`grep $ANALYSIS $OUT_DIR/$root.thermo.basic`
	echo "$val"
	echo "$root $val" >> $out_unsorted
	echo " "

	# re-organize the verbose output files for each pdb in its own directory 
	set out_root=$OUT_DIR/$root
	if ( ! -d $out_root ) then
		mkdir $out_root
	endif
	if ( -d $out_root ) then
		\mv -f $out_root.* $out_root
	endif
end

# Finally sort the delta G binding file we have created. 
cat $out_unsorted | sort -n -k 3,3 > $out_sorted
