## Run kappa scan and energy/pdb calculations  

###############################################################################
# Help                                                                         #
################################################################################
Help()
{
  # Display Help
  echo "Calculate top structures based on kappa filtering and frame filtering"
  echo "Kappa filtering is done based on linear regression and outlier removal by Cooks Distance"
  echo "v1 does kappa filter sorting based on stack + hbond, v2 uses stack + hbond + repulsion"
  echo "Frame filtering is based on sorting by Etot*CC^N or EstackEhbond*CC^N or EstackEhbondErepulsion*CC^N"
  echo "Usage:"
  echo
  echo "      Rscript get_gbStats_v1.sh kappa_file N pdb_cafemol pdb_rmsd atom_type F_cutoff M_cutoff N_kappa N_frame"
  echo "      kappa_file     file with list of kappa values"
  echo "      N              power to which afmCC is raised while multipying with Energy Terms"
  echo "      pdb_cafemol    pdb file listed in the cafemol_input file"
  echo "      pdb_rmsd       reference pdb file for calculating the rmsd against"
  echo "      atom_type      atom type from the list (sugar, phosphate, base, all) for rmsd calculation"
  echo "      F_cutoff       integer value representing the # of beginning frames to cutoff for frame filtering"
  echo "      M_cutoff       integer value representing the number of frames to include for calculation summary statistics for each kappa"
  echo "      N_kappa        integer value representing the number of top kappa runs to write out"
  echo "      N_frame        integer value representing the number of top PDB frames to write out under each kappa"
  echo "      Calculates mean and std dev summary statistics based on lowest 10 energy terms sorted by Etot - Afmfit"
  echo "      Output files are in the folder top_statsStHb_PowN, frame filtering results are under subdirectory topPDB_Etot, topPDB_StckHb and topPDB_StckHbRepul"
  echo "      And also under the output directory of each kappa/scan3D directory (Escore_ccPowN)"
  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

kappa_file=$1
N=$2
pdb_cafemol=$3
pdb_rmsd=$4
atom_type=$5
F_cutoff=$6
M_cutoff=$7
N_kappa=$8
N_frame=$9
top_percent=1

## Creat qsub script for kappa/2D  scan
scan_job="scan.sh"
rm -f $scan_job  ## remove file if exists
echo "#! /bin/tcsh" >> $scan_job
echo "#SBATCH --job-name='kscan'" >> $scan_job
echo "module load R/4.0" >> $scan_job
echo " " >> $scan_job
echo "cd \$SLURM_SUBMIT_DIR" >> $scan_job
echo "Rscript $analysisPath/analysis/get_gbStats_v1.R $kappa_file $N $pdb_cafemol $pdb_rmsd $atom_type $F_cutoff $M_cutoff $N_kappa $N_frame > run.log" >> $scan_job
 
echo "Now submitting the kappa/2D scan run"
sbatch --gres=lscratch:200 --cpus-per-task=2 --mem=5g --time=0-02:00:00 --partition=ccr,norm $scan_job

## Now submit the run for energy calculations
echo "Now setting up the swarm jobs"
bash $analysisPath"/analysis/make_swarm.sh" $kappa_file $pdb_cafemol $pdb_rmsd $atom_type $top_percent 
echo "Now submitting the swarm jobs for energy calculations"
swarm -f swarm_jobs/swarm.cmd -g 20 --partition norm --logdir swarm_jobs --module R/4.0 
 
