####  AFMcc, rmsd, rmsf, residue residue correlation, and generate related text files.
####  Calculation of PDB files that score top x percent in AFMcc, and generate their pseudo images
####  Usage: bash analyze_all.sh pdb_file dcd_file timestep_file cafe_infile atom_type top_percent
####  atom_type = choose one (sugar, phosphate, base)
####  The pdb file should be the one originally used in the cafe_infile

pdb_file=`readlink -f $1`
pdb_base=`basename $pdb_file`
dcd_file=`readlink -f $2`
timestep_file=`readlink -f $3`
cafe_infile=`readlink -f $4`
atom_type=$5
top_percent=$6
pdb_dir="top_"$top_percent"%_CC_pdbs"

mkdir -p $pdb_dir
top_pdb_path=`readlink -f $pdb_dir`
cd $pdb_dir
module load R/4.0
module load python/3.5
echo "########## Now generating analysis plots and data files ################"
Rscript /data/PNAI/yuba/scripts/analysis/analysis_bio3d.R $pdb_file $dcd_file $timestep_file $atom_type
Rscript /data/PNAI/yuba/scripts/analysis/plot_timestep.R $timestep_file
echo "########## Now generating the PDB files that score top $top_percent percent in the AFMcc ###############"
Rscript /data/PNAI/yuba/scripts/analysis/write_dcd2pdb.R $pdb_file $dcd_file $timestep_file $top_percent
#echo "######### Now generating pseudo images from the top $top_percent percent PDBs ##############"
#bash /data/PNAI/yuba/scripts/analysis/map_pdb2image_dimer.sh $cafe_infile $pdb_base $top_pdb_path
echo "######### Done with the analysis, check folder $pdb_dir ############"
