#!/bin/bash
#
# Notes from TACC:
#
#   -- Launch this script by executing
#   -- Copy/edit this script as desired.  Launch by executing
#      "sbatch knl.openmp.slurm" on a Stampede2 login node.
#
#   -- OpenMP codes run on a single node (upper case N = 1).
#        OpenMP ignores the value of lower case n,
#        but slurm needs a plausible value to schedule the job.
#
#   -- Default value of OMP_NUM_THREADS is 1; be sure to change it!
#
#   -- Increase thread count gradually while looking for optimal setting.
#        If there is sufficient memory available, the optimal setting
#        is often 68 (1 thread per core) or 136 (2 threads per core).
#
#----------------------------------------------------

#SBATCH -J {job_name}           # Job name
#SBATCH -o {log_dir}/{job_name}.o%j       # Name of stdout output file
#SBATCH -e {log_dir}/{job_name}.e%j       # Name of stderr error file
#SBATCH -p {queue}              # Queue (partition) name
#SBATCH -N 1                    # Total # of nodes (must be 1 for OpenMP)
#SBATCH -n 1                    # Total # of mpi tasks (should be 1 for OpenMP)
#SBATCH -t {time_str}           # Run time (hh:mm:ss)
{email_str}
{email_type_str}


#----------------------------------------------------
# Clone the whole miniconda into /tmp so the snakemake command do not access $WORK
mkdir /tmp/test_{env_dir_random}
tar -xf /work2/05622/lhq/test_conda.tar -C /tmp/test_{env_dir_random}
export CONDA_PREFIX=/tmp/test_{env_dir_random}/test/miniconda3
export CONDA_PYTHON_EXE=/tmp/test_{env_dir_random}/test/miniconda3/bin/python
export CONDA_EXE=/tmp/test_{env_dir_random}/test/miniconda3/bin/conda
export PATH=/dev/shm/bin:/tmp/test_{env_dir_random}/test/miniconda3/envs/mapping/bin:/tmp/test_{env_dir_random}/test/miniconda3/bin:/opt/apps/cmake/3.16.1/bin:/opt/apps/intel18/python2/2.7.15/bin:/opt/apps/autotools/1.1/bin:/opt/apps/git/2.24.1/bin:/opt/apps/libfabric/1.7.0/bin:/opt/apps/intel18/impi/18.0.2/bin:/opt/intel/compilers_and_libraries_2018.2.199/linux/mpi/intel64/bin:/opt/intel/compilers_and_libraries_2018.2.199/linux/bin/intel64:/opt/apps/gcc/6.3.0/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin:.
find /tmp/test_{env_dir_random}/test/miniconda3/ -type f -print0 | sed 's/ /\\ /g; s/(/\\(/g; s/)/\\)/g' | xargs -0 -P 30 -I % sh -c '/bin/sed -i "s/\/tmp\/test\/miniconda3\/envs\/mapping\/bin\/python/\/tmp\/test_{env_dir_random}\/test\/miniconda3\/envs\/mapping\/bin\/python/" %'

# Check the path
which python
which snakemake
which yap
which allcools
which bismark

# Installation finished
#----------------------------------------------------


# ---------------------------------------------------
# actual command

# print some info
date
hostname
pwd
# If you want to profile the job (CPU, MEM usage, etc.)
# load remora with
# "module load remora"
# and change the command to
# "remora {command}"


# Set thread count (default value is 1)...
export OMP_NUM_THREADS=48

{command}

# delete everything in /tmp

rm -rf /tmp/test*
# ---------------------------------------------------
