## Process a single PDB to produce the afm image data
## Usage: pdb2image_mononmer_unit.sh  pdb_file cafemol_input_file
## First argument is the coarse grained pdb_file, 
## second argument is the cafemol input file
## the path for ninfo file should be properly pointed in cafemol input file
###############################################################################
# Help                                                                         #
################################################################################
Help()
{
   # Display Help
   echo "Process a single PDB to produce the afm image data"
   echo
   echo "Usage: "
   echo "      bash pdb2image_dimer_unit.sh pdb_file cafemol_input_file"
   echo 
   echo "      pdb_file              coarse grained pdb file"
   echo "      cafemol_input_file    input file for cafemol"
   echo
}
# Get the options
while getopts ":h" option; do
   case $option in
      h) # display Help
         Help
         exit;;
   esac
done
# While no arguments
if [[ $# -eq 0 ]] ; then
    Help
    exit 0
fi

module load R/4.0
module load python/3.5
pdb_file=$1
pdb_basename=`basename $pdb_file`
pdb_base2=`basename ${pdb_file%.*}`
cafe_input=$2
pdb_fullpath=`readlink -f $pdb_file`
pdb_path=${pdb_fullpath%/*}

Rscript --vanilla $analysisPath"/analysis/format_pdb_cg.R" $pdb_file

temp_dir="temp_pseudo"
mkdir -p $temp_dir
DIR=${test_path%/*}
cp $pdb_file ./

## introduce chain termination
python $analysisPath"/calculation/chain_ter.py" $pdb_basename
pdb_file_fmt=$pdb_base2"_fmt.pdb"
python $analysisPath"/analysis/edit_input_dunit.py" $cafe_input $pdb_file_fmt $temp_dir ./
## edit_input_unit.py will produce a cafemol input file called format.inp
format_input="format.inp"

## run the cafemol to procuce the output image
echo "********** Running cafemol for image generation **************"
$cafemolImageout $format_input
echo "******* cafemol run is completed successfully ****************"
Rscript --vanilla $analysisPath"/analysis/read_image_data.R" image_info_cafe.txt $format_input $pdb_basename
rm -rf "image_info_cafe.txt" 

