#!/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/>.
#
#--- check if environment is ok -----------------
[[ "${LOADED_COMMON_PIPELINE_SH+1}" == "" ]] && echo "ERROR $0 : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

#----------------------------------------------------------------------------
# Expects the following vars to be defined"
#   nthreads     nr of threads
#   fAB          AB.align file
#   fBA          optional BA.align file
#   output       output BRH file name
#   fuzzyOpts    optional fuzzy BRH options (MAKEBRH_NALT>1)
#----------------------------------------------------------------------------

# set AB and BA files
opts="--AB \"$fAB\""
[[ -n "$fBA" ]] && opts="$opts --BA \"$fBA\""

# set fuzzy BRH options
opts="$opts $fuzzyOpts"

# run tool
MAKEBRH_EC=0
{
    print_header "$EXE_FINDBRH $opts"  "$(date)"
    echo "# ALIGNMENT_MATRIX = $ALIGNMENT_MATRIX"
    (( MAKEBRH_NALT > 1 )) && echo "# fuzzy BRHs with N=$MAKEBRH_NALT and fsepmax=$MAKEBRH_FSEPMAX"

    common_fmt_header
    tA=$(time_msec)
    eval "$EXE_FINDBRH" "$opts"
    MAKEBRH_EC=$?
    dT=$(delta_time $tA)
    print_footer "$dT"
} > "$output"
(( MAKEBRH_EC == 0 )) || rm -f "$output"
