#!/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_checktree.sh <nthreads>,<root>,<child1,child2,...>"
    exit 255
fi

# parse input string
input="$1"
inparr=( $(tokenize "," $input) )
[[ ${#inparr[@]} -ge 2 ]] || { echoerr "usage: run_checktree.sh <nthreads>,<root>[,<child1,child2,...>]"; exit 255; }

# get input
nthreads=${inparr[0]}
rootlbl=${inparr[1]}

cmpopts="mismatch --ec 0 --noexact --tbl"
froot=$DIR_PROJECT_CLUSTER/${rootlbl}.og
check_file "run_checktree.sh" "$froot" || exit 255
cmpopts="$cmpopts --A $froot"
for child in ${inparr[@]:2}; do
    fchild=$DIR_PROJECT_CLUSTER/${child}.og
    check_file "run_checktree.sh" "$fchild" || exit 255
    cmpopts="$cmpopts --B $fchild"
done

ftrg="$DIR_PROJECT_CLUSTER/check_${rootlbl}.log"
# cmpclus tool
cmd=$DIR_BRHCLUS/cmpclus
[[ -x $cmd ]] || fatal_msg "cmpclus cmd not found"

# 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")


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

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

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

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

# end timing
dT=$(delta_time $tstart)

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

mv -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
chmod 644 "$ftrg"
