## make the swarm file and bash scripts utilized by the swarm file
## Usage: bash make_swarm.sh kappa.txt pdb_cafemol pdb_rmsd atom_type top_percent
## eg: bash make_swarm.sh kappa.txt k1/pdb/RNaseP2_center.pdb k1/pdb/topStr-4799-f5511177.pdb all 1

###############################################################################
# Help                                                                         #
################################################################################
Help()
{
   # Display Help
   echo "Complete energy analysis of all kappa runs in a swarm job mode"
   echo
   echo "Usage: "
   echo "      bash make_swarm.sh Zscale.txt pdb_cafemol pdb_rmsd atom_type top_percent"
   echo 
   echo "      Zscale.txt      file with column of Zscale values"
   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"
   echo "      top_percet     integer to select top X  percent pdbs based on either AFMCC or (normizedAFMCC x totalEnergy)"
   echo
   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

## parse the list of kappa values 
zfile=`readlink -f $1`
pdb_cafemol=`readlink -f $2`
pdb_rmsd=`readlink -f $3`
atom_type=$4
top_percent=$5
swarm_folder="swarm_jobs"
frames_cutoff=10 ## first X frames to escape

## remove the intial swarm_folder if it exists
if [ -d $swarm_folder ]; then rm -Rf $swarm_folder; fi
mkdir -p $swarm_folder && cd "$_"
touch "swarm.cmd"

while IFS= read -r line
do
  job_script="qsub_Zscale_"$line".sh"
  touch $job_script
  echo "cd Zscale_${line}/output" >> $job_script
  ## Generate top X percent pdbs and corresponding simulated afm images
  ## A. based on afm CC alone
  echo "bash $analysisPath"/analysis/analyze_all_monomer.sh" $pdb_cafemol *.dcd *.ts ../input.inp $atom_type $top_percent" >> $job_script
  ## B. based on normCC*totalEnergy
  echo "bash $analysisPath"/analysis/analyze_all_monomer_normCE.sh" $pdb_cafemol *.dcd *.ts ../input.inp $atom_type $top_percent" >> $job_script
  ## generate rmsd
  #echo "cd ../" >> $job_script
  echo "Rscript $analysisPath"/analysis/get_rmsd.R" $pdb_rmsd *.dcd $atom_type" >> $job_script
  ## plot the energy
  echo "Rscript $analysisPath"/analysis2/plot_energy.R" *.ts $top_percent $frames_cutoff" >> $job_script
  ## get stats CE
  echo "Rscript $analysisPath"/analysis/get_stats_CE_newEnergy_mod.R" ./ $top_percent $pdb_cafemol rmsd_all.txt" >> $job_script
  echo "sort -k2 -n rmsd_all.txt > sort_rmsd_all.txt" >> $job_script
  ## add command to the swarm file
  echo "bash ${swarm_folder}/${job_script}" >> "swarm.cmd"

done <$zfile

## get 3D stats
#echo "Rscript $analysisPath"/analysis2/get_stats_3D_short.R" $kfile $top_percent" >> "3d_stats.sh"
#echo "Rscript $analysisPath"/analysis2/get_stats_3D_filter.R" $kfile $top_percent" >> "3d_stats.sh"
echo "Rscript $analysisPath"/analysis2/get_energyTally_3D_Zscale.R" $zfile 10" >> "3d_stats.sh"

echo "bash ${swarm_folder}/3d_stats.sh" >> "swarm.cmd"


## to submit swarm job
## submit from the directory which has all k cafemol runs
## Run: swarm -f swarm_jobs/swarm.cmd -g 20 --partition norm --logdir swarm_jobs --module R/4.0 
## Dry Run: swarm -f swarm_jobs/swarm.cmd -g 20 --logdir swarm_jobs --devel
