## Process a single PDB to produce the afm image data
## Usage: pdb2image_mononmer_unit.sh  pixel_size max_dim pdb_file cafemol_input_file
## First argument is the pixel size along x and y (integer or decimal)
## Second argument is maximum dimension along x and y (integer or decimal)
## Third argument is the coarse grained pdb_file, 
## Fourth argument is the cafemol input file
## the path for ninfo file should be properly pointed in cafemol input file
###############################################################################
# Help                                                                         #
################################################################################
Help()
{
   # Display Help
   echo "Calculate the AFM image for individual coarse grained PDB file based on user defined image size"
   echo
   echo "Usage: "
   echo "      bash pdb2image_monomer_setField.sh pixel_size max_dim pdb_file cafemol_inputfile"
   echo 
   echo "      pixel_size            pixel size along x and y (integer or decimal)"
   echo "      max_dim               maximum dimension along x and y (integer or decimal)"
   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
pixel_size=$1
max_dim=$2
pdb_file=$3
pdb_basename=`basename $pdb_file`
pdb_noExt=`basename ${pdb_file%.*}`
pdb_recenter=$pdb_noExt"_center.pdb"
cafe_input=$4
cafe_custom=`basename ${cafe_input%.*}`"_customField.inp"
pdb_fullpath=`readlink -f $pdb_file`
pdb_path=${pdb_fullpath%/*}
#echo $pdb_basename
#echo $pdb_recenter
#echo $cafe_custom

echo "*** Now running set_afmField.R to generate cafemol input with a custom image Field ***"
Rscript $analysisPath"/analysis2/set_afmField.R" $pixel_size $max_dim $cafe_input
echo "******** Now recentering the given cg pdb in the custom image field ******************"
Rscript $analysisPath"/calculation/process_pdb_cg_recenter.R" $pdb_file $cafe_custom
#Rscript --vanilla $analysisPath"/analysis/format_pdb_cg.R" $pdb_file

temp_dir="temp_pseudo"
mkdir -p $temp_dir


python $analysisPath"/analysis/edit_input_munit.py" $cafe_custom $pdb_recenter $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" 

