#!/usr/bin/env bash
#=====================================================
# USER PROJECT CONFIG
#-----------------------------------------------------
# Created by: {{ USER }}
# Date      : {{ DATE }}
#
# NOTE - for detailed help on variables do:
#  ./orthologer.sh -H .variables
#
#-----------------------------------------------------

#01.  Verbose mode
#    0 - none
#    1 - verbose
#   >1 - debug
VERBOSE=0

#02.  # limit the nr of error/warnings per step - set to 0 if no limit
MAX_MESSAGES=10

#03.  Python3
PYTHON3=""

#04.  Pipeline directory
DIR_PIPELINE="{{ DIR_PIPELINE }}"

#05.  Temp files directory - if not set it will use /tmp or $TMPDIR
TMP_DIR_BASE=""

#06.  Todo file - defines all labels to be used
PL_TODO="{{ TODO }}"


#07.  Running: scheduler label
SCHEDULER_LABEL=NONE

#08.  Running: maximum nr of jobs in one batch (running/pending)
OP_NJOBMAX_BATCH=200

#09.  Running: maximum nr of threads available on the local machine
OP_NJOBMAX_LOCAL=4

#10.  Running: keep JOBLOG flag
#    useful flag if the nr of jobs is expected to be very large
#    0 -> delete all logs if no error
#    1 -> keep logs (delete empty error logs)
OP_SAVE_JOBLOG=1

#11.  Running: wait poll time in seconds
#    This only affects how often the progress is updated.
SJ_CHECK_POLLTIME=5

#12.  Running: skip check for remake of step - overrides whatever done flag is set
#    For very large runs the checking can take a significant amount of time.
#    Disable (set to 1) if this is the case.
#    0 -> do remake check if required
#    1 -> NEVER do a remake check, useful when running over large data sets
SKIP_REMAKE_CHECK=0

#13.  Running: skip check of error logs
#    The check assumes there was an error if there is anything in the error log.
#    It may be useful to skip this check as in some cases
#    programs may output to stderr even though it's not an error.
#    0 -> check contents of error logs
#    1 -> skip the check
SKIP_CHECK_ERROR_LOGS=0

#14.  Running: sleep time between steps
#    If set to an integer greater than 0, the process will sleep for that amount of seconds
#    between each step.
#    This is useful if steps fail due to files not found, although they were successfully created in a previous step.
#    It can e.g happen if the NFS lookupcache is not updated sufficiently fast.
STEP_SLEEP=1

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ORTHODB specific setup
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#01.  OrthoDB API root URL
URL_ORTHODB_API_BASE=https://data.orthodb.org
URL_ORTHODB_API_VERSION='v11'

#02.  Orthologer run mode
#    DEFAULT   - more specific, faster
#    XFAST     - least sensitive, fastest
#    SENSITIVE - most sensitive
#    USER      - use parameters in this file
#    This sets the following parameters:
#    * MIN_OVERLAP          - sequence overlap/min length
#    * MIN_COVERAGE         - sequence match coverage
#    * MAX_EVALUE           - max e-value cutoff (alignment)
#    * SELECT_PID           - min percentage id for match in SELECT
#    * MMSEQS_SENSITIVITY   - alignment (mmseqs): sensitivity parameter
#    * MERGEINPAR_FCLUSTMIN - mergeinpar: cluster merge param 1
#    * MERGEINPAR_FSPECMIN  - mergeinpar: cluster merge param 2
#    * MAKEBRH_NALT         - makebrh   : fuzzy BRHs
#    * MAKEBRH_FSEPMAX      - makebrh   : fuzzy BRHs
ODB_RUN_MODE=DEFAULT

#03.  Pair of todo files
#    If both are set, the pairwise steps will run PL_TODO1 against PL_TODO2.
#    Original PL_TODO is used in all non-pairwise steps and is used as project label.
PL_TODO1=""
PL_TODO2=""

# flag how pairs are copnstructed from PL_TODO1 and PL_TODO2
#  0 -> all against all
#  1 -> pairs are one-to-one, first key of each file form the first pair, second key the second pair, etc
#       requires that TODO1 and TODO2 have the same nr of keys
PL_TODO_ONE2ONE=0

#04.  Tool dirs
DIR_BRHCLUS="{{ DIR_BRHCLUS }}"
DIR_BLAST="{{ DIR_BLAST }}"
DIR_SWIPE="{{ DIR_SWIPE }}"
DIR_DIAMOND="{{ DIR_DIAMOND }}"
DIR_MMSEQS="{{ DIR_MMSEQS }}"
DIR_CDHIT="{{ DIR_CDHIT }}"

#05.  ODB plugin labels
PREPROC_LABEL=CHECKFASTA
ALIGNMENT_LABEL=DIAMOND
MASKER_LABEL=SEGMASKER
SELECT_LABEL=CDHIT
CLUSTER_LABEL=BRHCLUS

#06.  Possibly ignore missing files (if set to 1)
IGNORE_MISSING=0

#07.  Tree parameters
# Enable tree mode by setting TREE_ENABLED=1.
# The tree can be specified in many ways:
#    If both are set, the pairwise steps will run PL_TODO1 against PL_TODO2.
#    1. TREE_INPUT_FILE -> newick file containing the tree
#    2. TREE_ROOT_CLADE -> set to taxid which is extracted from OrthoDB tree (TREE_INPUT_ODB)
#    3. PL_TODO -> label is taxid -> proceed as in 1
#
#    Select what clusters to run using TREE_CLUSTER_RUN
#    - 0 : do all
#    - 1 : do only leaf nodes
#    - 2 : do only internal nodes
TREE_ENABLED=0
TREE_INPUT_FILE=
TREE_ROOT_CLADE=
TREE_CLUSTER_RUN=0

#08.  Homology/sequence match parameters <ODB_RUN_MODE=USER>
#MIN_OVERLAP=30
#MIN_COVERAGE=0.8
#MAX_EVALUE=1.0e-3

#-----------------------------------------------------
# Step SELECT
#-----------------------------------------------------
#09.  min percentage ID when selecting representatives <ODB_RUN_MODE=USER>
#SELECT_PID=97

#10.  cdhit opts
OPTS_CDHIT="-M 4000"

#-----------------------------------------------------
# Step MASKER
#-----------------------------------------------------
#11.  dustmasker options
OPTS_DUSTMASKER=""

#12.  segmasker options
OPTS_SEGMASKER=""

#-----------------------------------------------------
# Step ALIGNMENT
#-----------------------------------------------------
#13.  alignment matrix
#    0 - calculate full matrix
#    1 - calculate half matrix (M11...MN1,M12...MN2,...,MNN)
ALIGNMENT_MATRIX=1

#14.  nr of alignments to show
ALIGNMENT_NUMALIGNMENTS=100

#15.  mmseqs: prefilter sensitivity <ODB_RUN_MODE=USER>
#MMSEQS_SENSITIVITY="3.0"

#16.  mmseqs extra opts
OPTS_MMSEQS=""

#17.  diamond extra opts
OPTS_DIAMOND=""

#18.  swipe extra opts
OPTS_SWIPE=""

#19.  blastplus extra opts
OPTS_BLAST=""

#-----------------------------------------------------
# Step MAKEBRH
#-----------------------------------------------------
#20.  Fuzzy BRHs : max nr of BRH candidates <ODB_RUN_MODE=USER>
#    If NALT>1 then the BRH finder will output similar candidates.
#MAKEBRH_NALT="2"

#21.  Fuzzy BRHs : max separation between the best and the candidates <ODB_RUN_MODE=USER>
#    Only used if NALT>1
#MAKEBRH_FSEPMAX="0.03"

#-----------------------------------------------------
# Step CLUSTER
#-----------------------------------------------------

#22.  set cluster input seed (for mapping)
CLUSTER_INPOG=""

#23.  brhclus: extra options for brhclus
OPTS_BRHCLUS=""

#24.  brhclus: max triangle evalue - if not set, MAX_EVALUE will be used
BRHCLUS_TRIEVAL=1.0e-3

#25.  brhclus: scale factor for max pair e-value (<= 1.0)
BRHCLUS_PAIREVAL_SCALE=0.001

#26.  brhclus: max species ratio in cluster merge attempts [0.0,1.0] <ODB_RUN_MODE=USER>
#    This will prevent merging of clusters with similar species content.
#    A value of 0.0 will only allow merging of clusters with no species overlap.
#BRHCLUS_MAX_SPRATIO=0.5

#27.  brhclus: fraction defining the min nr of extra links required for merge attempts <ODB_RUN_MODE=USER>

#    This counts in both directions for a seed link [A0,B0] in two different clusters.
#    The value is val=0 implies just the seed link.
#    val=N implies N links from A0 and N from B0 resulting in 2N+1 links.
#BRHCLUS_MERGE_FNLINKS=0.3

#28.  mergeinpar: extra options
OPTS_MERGEINPAR=""

#29.  mergeinpar: inparalog resolution - second best vs best candidate
MERGEINPAR_FHITSMAX="0.6"

#30.  mergeinpar: inparalog merge 1 - min nr of linked vs nr of members in smallest cluster <ODB_RUN_MODE=USER>
#MERGEINPAR_FCLUSTMIN="0.5"

#31.  mergeinpar: inparalog merge 2 - min species overlap <ODB_RUN_MODE=USER>
#MERGEINPAR_FSPECMIN="0.4"

#-----------------------------------------------------
# Step POSTPROC
#-----------------------------------------------------
#32.  define script(s)
POSTPROC_LABELS="ogmap"

#33.  Load system ODB common setup
[[ -f "${DIR_PIPELINE}/bin/common_odb.sh" ]] || { echo "ERROR unable to read system common_odb.sh : $DIR_PIPELINE/bin/common_odb.sh" 1>&2; exit 255; }
source "${DIR_PIPELINE}/bin/common_odb.sh"
#

#-----------------------------------------------------
#34.  Pipeline per step config
#-----------------------------------------------------
# Set OP_STEP_xxx variables to defaults
op_step_setup

# OP_STEP_NPARALLEL[label]  : nr of jobs that should be run in parallel
# OP_STEP_NTHREADS[label]   : nr of CPUs to be allocated for each job
# OP_STEP_NMERGE[label]     : nr of jobs merged into one
# OP_STEP_ENABLE[label]     : enable flag; 1->enable, 0->disable
# OP_STEP_RUNMODE[label]    : run mode - either BATCH (run with scheduler) or LOCAL (run on local machine)
# OP_STEP_SCHEDOPTS[label]  : option string for job scheduler, per step

OP_STEP_ENABLE[PREPARETREE]=0
OP_STEP_NPARALLEL[PREPARETREE]=1
OP_STEP_NTHREADS[PREPARETREE]=1
OP_STEP_NMERGE[PREPARETREE]=4
OP_STEP_RUNMODE[PREPARETREE]=LOCAL
OP_STEP_SCHEDOPTS[PREPARETREE]=

OP_STEP_ENABLE[PREPROC]=1
OP_STEP_NPARALLEL[PREPROC]=4
OP_STEP_NTHREADS[PREPROC]=1
OP_STEP_NMERGE[PREPROC]=4
OP_STEP_RUNMODE[PREPROC]=LOCAL
OP_STEP_SCHEDOPTS[PREPROC]=

OP_STEP_ENABLE[MASKER]=1
OP_STEP_NPARALLEL[MASKER]=4
OP_STEP_NTHREADS[MASKER]=1
OP_STEP_NMERGE[MASKER]=4
OP_STEP_RUNMODE[MASKER]=LOCAL
OP_STEP_SCHEDOPTS[MASKER]=

OP_STEP_ENABLE[SELECT]=1
OP_STEP_NPARALLEL[SELECT]=4
OP_STEP_NTHREADS[SELECT]=1
OP_STEP_NMERGE[SELECT]=4
OP_STEP_RUNMODE[SELECT]=LOCAL
OP_STEP_SCHEDOPTS[SELECT]=

OP_STEP_ENABLE[STATS]=1
OP_STEP_NPARALLEL[STATS]=4
OP_STEP_NTHREADS[STATS]=1
OP_STEP_NMERGE[STATS]=4
OP_STEP_RUNMODE[STATS]=LOCAL
OP_STEP_SCHEDOPTS[STATS]=

OP_STEP_ENABLE[FORMATDB]=1
OP_STEP_NPARALLEL[FORMATDB]=4
OP_STEP_NTHREADS[FORMATDB]=1
OP_STEP_NMERGE[FORMATDB]=4
OP_STEP_RUNMODE[FORMATDB]=LOCAL
OP_STEP_SCHEDOPTS[FORMATDB]=

OP_STEP_ENABLE[ALIGNMENT]=1
OP_STEP_NPARALLEL[ALIGNMENT]=4
OP_STEP_NTHREADS[ALIGNMENT]=1
OP_STEP_NMERGE[ALIGNMENT]=1
OP_STEP_RUNMODE[ALIGNMENT]=LOCAL
OP_STEP_SCHEDOPTS[ALIGNMENT]=

OP_STEP_ENABLE[MAKEBRH]=1
OP_STEP_NPARALLEL[MAKEBRH]=4
OP_STEP_NTHREADS[MAKEBRH]=1
OP_STEP_NMERGE[MAKEBRH]=4
OP_STEP_RUNMODE[MAKEBRH]=LOCAL
OP_STEP_SCHEDOPTS[MAKEBRH]=

OP_STEP_ENABLE[MAKEINPAR]=1
OP_STEP_NPARALLEL[MAKEINPAR]=4
OP_STEP_NTHREADS[MAKEINPAR]=1
OP_STEP_NMERGE[MAKEINPAR]=4
OP_STEP_RUNMODE[MAKEINPAR]=LOCAL
OP_STEP_SCHEDOPTS[MAKEINPAR]=

OP_STEP_ENABLE[MAKEINPARSEL]=1
OP_STEP_NPARALLEL[MAKEINPARSEL]=4
OP_STEP_NTHREADS[MAKEINPARSEL]=1
OP_STEP_NMERGE[MAKEINPARSEL]=4
OP_STEP_RUNMODE[MAKEINPARSEL]=LOCAL
OP_STEP_SCHEDOPTS[MAKEINPARSEL]=

OP_STEP_ENABLE[SETBRH]=1
OP_STEP_NPARALLEL[SETBRH]=1
OP_STEP_NTHREADS[SETBRH]=1
OP_STEP_NMERGE[SETBRH]=1
OP_STEP_RUNMODE[SETBRH]=LOCAL
OP_STEP_SCHEDOPTS[SETBRH]=

OP_STEP_ENABLE[SETINPAR]=1
OP_STEP_NPARALLEL[SETINPAR]=1
OP_STEP_NTHREADS[SETINPAR]=1
OP_STEP_NMERGE[SETINPAR]=1
OP_STEP_RUNMODE[SETINPAR]=LOCAL
OP_STEP_SCHEDOPTS[SETINPAR]=

OP_STEP_ENABLE[SETINPARSEL]=1
OP_STEP_NPARALLEL[SETINPARSEL]=1
OP_STEP_NTHREADS[SETINPARSEL]=1
OP_STEP_NMERGE[SETINPARSEL]=1
OP_STEP_RUNMODE[SETINPARSEL]=LOCAL
OP_STEP_SCHEDOPTS[SETINPARSEL]=

OP_STEP_ENABLE[CLUSTER]=1
OP_STEP_NPARALLEL[CLUSTER]=1
OP_STEP_NTHREADS[CLUSTER]=1
OP_STEP_NMERGE[CLUSTER]=1
OP_STEP_RUNMODE[CLUSTER]=LOCAL
OP_STEP_SCHEDOPTS[CLUSTER]=

OP_STEP_ENABLE[MERGEINPAR]=1
OP_STEP_NPARALLEL[MERGEINPAR]=1
OP_STEP_NTHREADS[MERGEINPAR]=1
OP_STEP_NMERGE[MERGEINPAR]=1
OP_STEP_RUNMODE[MERGEINPAR]=LOCAL
OP_STEP_SCHEDOPTS[MERGEINPAR]=

OP_STEP_ENABLE[MERGEINPARSEL]=1
OP_STEP_NPARALLEL[MERGEINPARSEL]=1
OP_STEP_NTHREADS[MERGEINPARSEL]=1
OP_STEP_NMERGE[MERGEINPARSEL]=1
OP_STEP_RUNMODE[MERGEINPARSEL]=LOCAL
OP_STEP_SCHEDOPTS[MERGEINPARSEL]=

OP_STEP_ENABLE[POSTPROC]=1
OP_STEP_NPARALLEL[POSTPROC]=1
OP_STEP_NTHREADS[POSTPROC]=1
OP_STEP_NMERGE[POSTPROC]=1
OP_STEP_RUNMODE[POSTPROC]=LOCAL
OP_STEP_SCHEDOPTS[POSTPROC]=


# Select slice/block of the pairwise steps (ALIGNMENT,MAKEBRH and MAKEINPAR
# If left empty, the full matrix is calculated.
# Note that the convention for half-matrix is that
#   labelA <(=) labelB
# This is a case insensitive string comparison (using locale LANG=C).
OP_LABELA_FIRST=1278
OP_LABELA_LAST=1892
OP_LABELB_FIRST=
OP_LABELB_LAST=

# set/check variables based on ODB_RUN_MODE
set_run_mode

#--- END OF USER CONFIG ---

