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

#---------------------------------------------------------------------------
# get args
[[ $# -eq 1 ]] || fatal_msg "usage: $0 <node>,<nthreads>"

# get input CSV
finp="$1"
inparr=( $(tokenize "," $finp) )
[[ ${#inparr[@]} -eq 2 ]] || fatal_msg "invalid input ($finp) to run_weakbrh.sh, expected comma separated list <node,Nthreads>";

# create input/output file names
input="${DIR_PROJECT_CLUSTER}/${inparr[0]}.${EXT_CLUSTER}"
output="${DIR_PROJECT_CLUSTER}/${inparr[0]}.${EXT_WEAK_BRH}"
nthreads="${inparr[1]}"

#---------------------------------------------------------------------------
# set options
# --reqone  : only one is required to be of type given by -t
# --nofuzzy : exclude fuzzy BRHs
opts="-t 1,2,3,4,5 --pwc $DIR_PROJECT_PWC -C $input --reqone --nofuzzy"
[[ $nthreads -gt 1 ]] && opts="$opts --nfiles $((4*$nthreads)) -T $nthreads"

#---------------------------------------------------------------------------
# ready to run the clustering
info_msg "start extracting weak BRHs"
info_msg "cmd: $EXE_EXTRACTPAIRS $opts -C $input -o $output"

# use temp files for output
tmpout=""
tmp_make tmpout "weakbrh" || exit 255
tmplog="${tmpout}.log"
tmp_add_file tmplog


BRHCLUS_USE_HUGEMEM=1
libhugemem=/home/tegenfeldt/libhugetlbfs/obj64/libhugetlbfs.so
if [[ ! -f $libhugemem ]]; then
    warn_msg "HUGEMEM was requested but library is missing at: $libhugemem"
    warn_msg "HUGEMEM running without..."
    BRHCLUS_USE_HUGEMEM=0
fi

#---------------------------------------------------------------------------
# go
startdate=$(date)
tA=$(time_msec)
ec=0
if (( BRHCLUS_USE_HUGEMEM == 0 )); then
    "$EXE_EXTRACTPAIRS" $opts -o $tmpout > $tmplog
    ec=$?
else
    info_msg "using HUGEMEM with library $libhugemem"
    LD_PRELOAD=$libhugemem HUGETLB_MORECORE=yes "$EXE_EXTRACTPAIRS" $opts -o $tmpout > $tmplog
    ec=$?
fi

if [[ $? -ne 0 ]]; then
	error_msg "$0: failed extractpairs for input file $input"
	echoerr "# Output: "
	cat $tmpout 1>&2
    echoerr "#LOG file:"
    cat $tmplog 1>&2
	exit 255
fi
dT=$(delta_time $tA)

#---------------------------------------------------------------------------
# add header and footer and fix access mode
add_header_footer $0 $tmpout "$output" "$startdate" "$dT" "$EXE_EXTRACTPAIRS $opts -C $input -o $output"
if [[ $? -ne 0 ]]; then
	echoerr "# Output: "
	cat $tmpout 1>&2
    echoerr "#LOG file:"
    cat $tmplog 1>&2
    exit 255
fi
# copy log file
mv -f $tmplog ${output}.log

chmod 644 "$output"
chmod 644 "${output}.log"
info_msg "done extracting weak BRHs -> $output"

# if tree is not enabled, skip next step
(( TREE_ENABLED == 1 )) || {
    rm -f $tmplog
    exit 0
}

# DONE
