#!/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
#------------------------------------------------

# make sure tmp file cleaning works
tmp_setup_trap

# get args
[[ $# -eq 1 ]] || fatal_msg "$0 - usage <todo label,nthreads>"
input="$1"
inparr=( $(tokenize "," $input) )
[[ ${#inparr[@]} -eq 2 ]] || fatal_msg "invalid input ($input) to run_splitbrh.sh, expected comma separated list <label,Nthreads>";

# get args
label="${inparr[0]}"    # todo label
nt="${inparr[1]}"       # nr of threads

# nr of threads, cap it to nr of cores available on the machine
nthreads=$(get_nthreads $nt)

# get input/output
inputbrh="${DIR_PROJECT_CLUSTER}/${label}.${EXT_BRH}"
splitbrh="${DIR_PROJECT_CLUSTER}/${label}.${EXT_SPLITS_BRH}"

# split file
split_in_file=""
split_out_file=""
split_in_opts=""
if [[ -n $TREE_ROOT_CLADE ]]; then
    if [[ "$TREE_ROOT_CLADE" == "$label" ]]; then
        # this is the root clade -> generate split file
        split_out_file="${DIR_PROJECT_CLUSTER}/${TREE_ROOT_CLADE}.$EXT_SPLITS"
        info_msg "got split output file $split_out_file"
    else
        # not the root clade -> get input split file
        split_in_file="${SPLIT_FILE}"
        split_in_opts="--splitin $split_in_file"
        info_msg "got split input file $split_out_file"
    fi
else
    # no root clade is set - assume this is root clade -> generate split file
    split_out_file="${DIR_PROJECT_CLUSTER}/${label}.$EXT_SPLITS"
fi
                                                                                         

# check nput file and executable
check_file     "$0" "$inputbrh"        || exit 255
check_file_exe "$0" "$EXE_SPLITGENES"  || exit 255

# run program
opts="-g 0.55 --printsum -c 20"

# temp files
tmpbrh=""
tmpsplits=""
tmp_make tmpbrh     splitbrh

echo "DBG OUTPUT: splitbrh $splitbrh"
echo "DBG OUTPUT: splitout $split_out_file"

opts="$opts -P $inputbrh -o $tmpbrh"
docopts="$opts -P $inputbrh -o $splitbrh" # for the documentation line
if [[ -n $split_in_opts ]]; then
    info_msg "not root clade -> reading split definitions from $split_in_file"
    opts="$opts $split_in_opts"
    docopts="$docopts $split_in_opts"
else
    info_msg "root clade -> generating split definitions to file $split_out_file"
    tmp_make tmpsplits  splitbrh
    opts="$opts -s $tmpsplits"
    docopts="$docopts -s $split_out_file"
fi

info_msg "splitbrh cmd: $EXE_SPLITGENES $opts"

startdate=$(date)
tA=$(time_msec)
$EXE_SPLITGENES $opts
if [[ $? -ne 0 ]]; then
	error_msg "$0 : splitbrh failed"
	echo "input BRH file   : $inputbrhorg"
    echo "machine          : $(uname -n)"
	exit 255
fi
dT=$(delta_time $tA)

echo "DBG ls tmpbrh:"
ls -l $tmpbrh

# add header and footer to file
add_header_footer $0 "$tmpbrh" "$splitbrh" "$startdate" $dT "\"$EXE_SPLITBRH $docopts\""
[[ $? -eq 0 ]] || fatal_msg "failed splitbrh"

# copy split file
[[ -z "$tmpsplits"      ]] || mv -f "$tmpsplits" "$split_out_file"
[[ -z "$split_out_file" ]] || chmod 644 "$split_out_file"
chmod 644 "$splitbrh"

exit 0
