#!/bin/csh
# refine.csh
# script for converting input files
# and running xplor water refinement calculations
#
# version 0.1 010604 BFV bvolkman@mcw.edu

setenv BASE_DIR $PWD 
setenv ORIG_DIR $PWD/orig_files 
setenv CONV_DIR $PWD/conversion 
setenv INPUT_DIR $PWD/input 
setenv CONFIG_DIR $PWD/config

# --- evaluate input arguments ---

if ($#argv == 0) then
  echo "no base filename specified! - terminating"
  exit
else if ($#argv == 1) then
  set basename=$argv[1]
  set renum=0
  echo "== using base filename $basename"
  echo "== using existing residue numbering"
else if ($#argv == 2) then
  set basename=$argv[1]
  set renum=$argv[2]
  echo "== using base filename $basename"
  echo "== adjusting residue numbers by $renum"
endif

# ---  convert cyana files to aqua/xplor format ---
echo "== moving into orig_files dir"
cd $ORIG_DIR

echo "== extracting amino acid sequence from $basename.seq"
cut -c1-3 $basename.seq > 1xxx.seq
cp 1xxx.seq $CONV_DIR

echo "== renumbering PDB file $basename.pdb, preparing for conversion"
pdb_renumber.pl $renum $basename.pdb > 1xxx.pdb
cp 1xxx.pdb $CONV_DIR

echo "== moving to $CONV_DIR"
cd $CONV_DIR

echo "== running generateProtons.csh"
./generateProtons.csh

echo "== moving PDB and PSF files to $INPUT_DIR"
mv 1xxx_*.pdb $INPUT_DIR
mv 1xxx.psf $INPUT_DIR

echo "== moving to $ORIG_DIR"
cd $ORIG_DIR

echo "== converting dihedral constraint file $basename.aco to xplor format"
aco_renumber.pl $renum $basename.aco > 1xxx.aco
convXPLORtorsrestr.awk 1xxx.aco > $INPUT_DIR/1xxx_dihe.tbl

echo "== converting NOE constraints to xplor format (using aqua)" 
upl_renumber.pl $renum $basename.upl > 1xxx.upl
cp $INPUT_DIR/1xxx_1.pdb .
aqua
qproject $basename
qconvert -t 4 1xxx.upl
sed 's/[+-] /  /g' 1xxx.noe > temp
mv temp 1xxx.noe
aq2x xplor 1xxx.noe 1xxx_noe.tbl 1xxx_1.pdb
mv 1xxx_noe.tbl $INPUT_DIR

