## Process the pdbs to produce the afm image data
## Usage: pdb2cafeimage.sh cafe_input_file pdb_file_name pdb_path
## First argument is the cafemol input file, 
## second argument is the pdb file name used in cafemol input file
## third argument is pdb folder path

module load R/4.0
module load python/3.5
currentPDB=$2
path_input=`readlink -f $1`
cafe_input=`basename $path_input`
#echo $cafe_input
cd $3
FileList=`ls *.pdb`

## correct the format in each pdb
for name in $FileList
do
  echo $name
  Rscript --vanilla /data/PNAI/yuba/scripts/analysis/format_pdb_cg.R $name
done
temp_dir="temp_pseudo"
mkdir -p $temp_dir && cp $path_input $_

python /data/PNAI/yuba/scripts/analysis/edit_input.py $temp_dir/$cafe_input $temp_dir ./
## edit_input.py will produce a cafemol input file called format.inp
format_input="format.inp"

## run the cafemol to procuce the output image
for name in $FileList
do
  sed -i "s/$currentPDB/$name/g" $format_input
  /data/PNAI/yuba/software/cafemol_imageout/bin/cafemol $format_input
  echo "cafemol run is completed successfully"
  Rscript --vanilla /data/PNAI/yuba/scripts/analysis/read_image_data.R image_info_cafe.txt $format_input $name
  rm "image_info_cafe.txt" 
  sed -i "s/$name/$currentPDB/g" $format_input
done

