#!/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 run_evolrate.sh : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

if [ $# -ne 1 ]; then
    echoerr "usage: run_evolrate.sh <nthreads>,<fclus>,<fpair,ftrg>"
    exit 255
fi

# parse input string
input="$1"
inparr=( $(tokenize "," $input) )
[[ ${#inparr[@]} -eq 4 ]] || { echoerr "usage: run_evolrate.sh <nthreads>,<fclus>,<fpair,ftrg>"; exit 255; }

# set input
nthreads=${inparr[0]}
fclus="${inparr[1]}"
fpair="${inparr[2]}"
ftrg="${inparr[3]}"

# check input files
check_file "run_evolrate.sh" "$fclus" || exit 255
check_file "run_evolrate.sh" "$fpair"  || exit 255

# make sure output dir is writeable
iswriteable "$DIR_PROJECT_CLUSTER" || fatal_msg "output directory is not writeable by user $SYS_WHOAMI -> $DIR_PROJECT_CLUSTER"

# get label of cluster file
label=$(file_basename "$fclus")

# evolrate tool
cmd=$DIR_BRHCLUS/evolrate
[[ -x $cmd ]] || fatal_msg "evolrate cmd not found"

# temporary output file
tmp_make tmpout ${label}_evolrate
[[ $? -eq 0 ]] || fatal_msg "failed creating tmp file in $tmpout"

# set nr of threads and pool size
(( nthreads > 0 )) || nthreads=1
npooled=$((64*nthreads))

# full options
####~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#### Fix added in order to correctly compute evolrates.
#### A bug in 'findbrh' saved the wrong 'PID' in the BRH files (it used a data structure that did NOT contain PID).
#### The option --alicorr reads the corresponding .align file for each .brh and retrieves the PID.
####~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
###info_msg "--- RUNNING WITH ALICORR OPTION ---"
###opts="-C $fclus -P $fpair -N $nthreads -p $npooled --output $tmpout --pwc $DIR_PROJECT_PWC --alicorr"

# normal options
opts="-C $fclus -P $fpair -N $nthreads -p $npooled --output $tmpout --pwc $DIR_PROJECT_PWC"

# start timing
startdate=$(date)
tstart=$(time_msec)

# run evolrate
$cmd $opts >& ${tmpout}.log
ec=$?
if [[ $ec -ne 0 ]]; then
    error_msg "evolrate failed with ec = $ec"
    error_msg "-> tmpout : $tmpout"
    error_msg "-> host   : $(hostname)"
    error_msg "-> cmd    : $cmd $opts"
    cat $tmpout
    exit $ec
fi

# end timing
dT=$(delta_time $tstart)

# add header/footer
add_header_footer run_evolrate.sh $tmpout "$tmpout" "$startdate" $dT "\"${cmd} $opts\"" || fatal_msg "failed adding final header and footer of file!"

cp -f $tmpout $ftrg
if [[ $? -ne 0 ]]; then
    error_msg "failed copying evolrate tmp file"
    error_msg "-> tmp output : $tmpout"
    error_msg "-> trg output : $ftrg"
    error_msg "-> host       : `hostname`"
    exit 255
fi
mv -f ${tmpout}.log ${ftrg}.log

chmod 644 "$ftrg"
chmod 644 "${ftrg}.log"
