#!/bin/bash

# pipsa 3.0 script
# Computes electroststic potential grids
#
# - needs 3 parameters: 
# $1 = pipsa_distr_dir = pipsa distribution directory
# $2 = pipsa_wrk_dir  = the directory data is stored 
# $3 = pdb2pqr executable = location of pdb2pqr script
#
# - assumes pdb2pqr sreadable pdb files in  pipsa_wrk_dir/pdbs/
# - will go to directory pipsa_wrk_dir/pqrs and compute a pqr file for each pdb
echo $@
if [[ -n $3 ]]; then
	echo "Setting PDB2PQR"
	export PDB2PQR=$3	
else
	echo "Checking PDB2PQR"
	export PDB2PQR=`which pdb2pqr`
	echo "Using: ${PDB2PQR}"
fi;

if [ ! -d $2/pdbs ]; then
	echo "$2/pdbs does not exist, cannot create pqr files"
	exit 1
fi;
cd $2/pdbs 
ls -C -w 1 *.pdb > pdbnames
cat pdbnames | cut -f1 -d"." > names
cd $2
mkdir -p $2/pqrs
cp $2/pdbs/names $2/pqrs/names
cd $2/pqrs

# run pdb2pqr
for i in `cat names`
do
echo ============================= >>  pqr.ou
echo $i                            >>  pqr.ou
echo ============================= >>  pqr.ou
echo "${PDB2PQR} --ff AMBER --drop-water $2/pdbs/${i}.pdb $2/pqrs/${i}.pqr" >> pqr.ou
${PDB2PQR} --ff AMBER --drop-water $2/pdbs/${i}.pdb $2/pqrs/${i}.pqr
done
cd $2/pqrs
ls -C -w 1 *.pqr > pqrnames
cd $2

