#!/usr/bin/env bash
#
# orthologer - Pipeline for deducing orthology from fasta files.
# Copyright (C) 2005-2023 E. Zdobnov lab: F. Tegenfeldt
# <fredrik.tegenfeldt@unige.ch> based on code by E. Kriventseva
#
# This file is part of orthologer.
#
# orthologer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# orthologer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with orthologer.  If not, see <http://www.gnu.org/licenses/>.
#
if [[ $# -ge 1 ]]; then
    [[ -f $1 ]] || { echo "error: input file is not readable - should be a common.sh"; exit 255; }
    source $1
fi
#--- check if environment is ok -----------------
[ "${LOADED_COMMON_ORTHODB_SH+1}" == "" ] && echo "ERROR $0 : main common_odb.sh has not been loaded" 1>&2 && exit 255
#------------------------------------------------
set -u

source $DIR_ORTHOLOGER/bin/check_setup_common.sh
odb_check_setup() {
    local col="$COLOR_GREEN"
    local status=OK
    print_header "OrthoDB specific"

    #print_header "Data type"
    #check_var_setup VAR  DATA_TYPE 0 $(inrange_set "PROT NUCL" )

    print_header "Tool dirs"
    check_var_setup DIR DIR_BRHCLUS

    check_var_setup DIR DIR_BLAST     $(tst_var "ALIGNMENT_LABEL" "BLAST")
    check_var_setup DIR DIR_SWIPE     $(tst_var "ALIGNMENT_LABEL" "SWIPE")
    check_var_setup DIR DIR_MMSEQS    $(tst_var "ALIGNMENT_LABEL" "MMSEQS")
    check_var_setup DIR DIR_CDHIT     $(tst_var "SELECT_LABEL"    "CDHIT")

    print_header "Tool executables"
    need_cdhit=0
    [[ "$SELECT_LABEL" == "CDHIT" ]]      && need_cdhit=1
    ####[[ "$SELECT_LABEL" == "BLASTCLUST" ]] && need_cdhit=1
    check_var_setup EXE EXE_SEGMASKER  $(tst_var "MASKER_LABEL"  "SEGMASKER")  #  "check_DIR_BLAST"
    check_var_setup EXE EXE_DUSTMASKER $(tst_var "MASKER_LABEL"  "DUSTMASKER") #  "check DIR_BLAST"
    
    #check_var_setup EXE EXE_CDHIT_NUCL $(eval [[ $need_cdhit -eq 1 ]] && [[ "$DATA_TYPE" == "NUCL" ]] || echo 2) #"check DIR_CDHIT"
    #check_var_setup EXE EXE_CDHIT_PROT $(eval [[ $need_cdhit -eq 1 ]] && [[ "$DATA_TYPE" == "PROT" ]] || echo 2) #"check DIR_CDHIT"
    check_var_setup EXE EXE_CDHIT_PROT 0

    check_var_setup EXE EXE_MAKEBLASTDB $(eval [[ -d "$DIR_BLAST" ]] && echo 0 || echo 2) # "check DIR_BLAST"
    
    check_var_setup EXE EXE_BLASTP      $(tst_var "ALIGNMENT_LABEL" "BLAST") #"check DIR_BLAST"
    ####check_var_setup EXE EXE_BLASTN      $(tst_var "ALIGNMENT_LABEL" "BLAST") #"check DIR_BLAST"
    
    check_var_setup EXE EXE_SWIPE       $(tst_var "ALIGNMENT_LABEL" "SWIPE")    #"check DIR_SWIPE"
    check_var_setup EXE EXE_MMSEQS      $(tst_var "ALIGNMENT_LABEL" "MMSEQS")   #"check DIR_MMSEQS"
    
    check_var_setup EXE EXE_MERGEINPAR 0 #"check DIR_BRHCLUS"
    check_var_setup EXE EXE_GETINPAR   0 #"check DIR_BRHCLUS"
    check_var_setup EXE EXE_BRHCLUS    0 #"check DIR_BRHCLUS"
    check_var_setup EXE EXE_FINDBRH    0 #"check DIR_BRHCLUS"
    
    print_header "General parameters"
    check_var_setup VAR MIN_OVERLAP 0 "inrange_int $MIN_OVERLAP 0 x" "NEGATIVE NR"
    check_var_setup VAR MIN_COVERAGE 0 "inrange_float $MIN_COVERAGE 0.0 1.0" "NOT IN [0,1]"
    check_var_setup VAR SELECT_PID  0 "inrange_int $SELECT_PID 90 100" "NOT IN [90,100]"
    check_var_setup VAR MAX_EVALUE  0 "inrange_float $MAX_EVALUE 1.0e-100 x" "TOO SMALL <1.0e-100"
    check_var_setup VAR IGNORE_MISSING  0 "inrange_int $IGNORE_MISSING 0 1" "NOT IN {0,1}"

    print_header "Parameters for aligner"
    check_var_setup VAR ALIGNMENT_NUMALIGNMENTS  0 "inrange_int $ALIGNMENT_NUMALIGNMENTS 0 x" "NEGATIVE NR"
    check_var_setup VAR ALIGNMENT_MATRIX         0 "inrange_int $ALIGNMENT_MATRIX 0 1" "NOT IN [0,1]"

    print_header "Parameters for BRH"
    check_var_setup VAR MAKEBRH_NALT             0 "inrange_int $MAKEBRH_NALT 0 x" "NEGATIVE NR"
    check_var_setup VAR MAKEBRH_FSEPMAX          0 "inrange_float $MAKEBRH_FSEPMAX 0 1.0" "NOT IN [0,1]"
    
    print_header "Parameters for BRHCLUS"
    check_var_setup VAR BRHCLUS_PAIREVAL_SCALE   0 "inrange_float $BRHCLUS_PAIREVAL_SCALE 0.0 1.0" "NOT IN [0,1]"
    check_var_setup VAR BRHCLUS_TRIEVAL          0 "inrange_float $BRHCLUS_TRIEVAL $BRHCLUS_PAIREVAL x" "NOT > PAIREVAL"
    check_var_setup VAR BRHCLUS_MAX_SPRATIO      0 "inrange_float $BRHCLUS_MAX_SPRATIO 0 1" "NOT IN [0,1]"
    check_var_setup VAR BRHCLUS_MERGE_FNLINKS    0 "inrange_float $BRHCLUS_MERGE_FNLINKS 0 1" "NOT IN [0,1]"
    
    print_header "Parameters for MERGEINPAR"
    check_var_setup VAR MERGEINPAR_FHITSMAX  0 "inrange_float $MERGEINPAR_FHITSMAX 0 1.0" "NOT IN [0,1]"
    check_var_setup VAR MERGEINPAR_FCLUSTMIN 0 "inrange_float $MERGEINPAR_FCLUSTMIN 0 1.0" "NOT IN [0,1]"
    check_var_setup VAR MERGEINPAR_FSPECMIN  0 "inrange_float $MERGEINPAR_FSPECMIN 0 1.0" "NOT IN [0,1]"
    
    print_header "General options for various programs"
    check_var_setup VAR OPTS_BRHCLUS    1
    check_var_setup VAR OPTS_MERGEINPAR 1
    check_var_setup VAR OPTS_CDHIT      1
    ###check_var_setup VAR OPTS_BLASTCLUST 1
    check_var_setup VAR OPTS_DUSTMASKER 1
    check_var_setup VAR OPTS_SEGMASKER  1
    
    print_header "File extensions"
    check_var_setup VAR EXT_FASTA
    check_var_setup VAR EXT_FASTA_CLEAN
    check_var_setup VAR EXT_FASTA_MASKED
    check_var_setup VAR EXT_FASTA_SELECTED
    check_var_setup VAR EXT_FASTA_SELCLSTR
    check_var_setup VAR EXT_FASTA_STATS
    check_var_setup VAR EXT_FASTA_SEQLEN    

    check_var_setup VAR EXT_ALIGN
    check_var_setup VAR EXT_BRH

    check_var_setup VAR EXT_INPAR
    check_var_setup VAR EXT_INPAR_SELECTED

    check_var_setup VAR EXT_PAIRS
    check_var_setup VAR EXT_SPLITS
    check_var_setup VAR EXT_SPLITS_BRH

    check_var_setup VAR EXT_CLUSTER
    check_var_setup VAR EXT_CLUSTER_RAW
    check_var_setup VAR EXT_CLUSTER_INPAR
    
    print_header "Pairwise matrix selection"
    check_var_setup VAR OP_LABELA_FIRST $(eval [[ ${#OP_LABELA_FIRST} -gt 0 ]] || echo 2)
    check_var_setup VAR OP_LABELA_LAST   $(eval [[ ${#OP_LABELA_LAST}   -gt 0 ]] || echo 2)

    check_var_setup VAR OP_LABELB_FIRST $(eval [[ ${#OP_LABELB_FIRST} -gt 0 ]] || echo 2)
    check_var_setup VAR OP_LABELB_LAST   $(eval [[ ${#OP_LABELB_LAST}   -gt 0 ]] || echo 2)
    return 0
}

[[ "$0" = "$BASH_SOURCE" ]] && {
    setup_environment
    check_setup
    odb_check_setup
}
