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

# for now asks for nr of threads although it is not used!!
if [ $# -ne 1 ]; then
    echo "usage: $0 <nthreads>"
    exit 255
fi
nthreads=$1

# check require files/dirs
check_file "$0" "$PL_TODO"             || exit 255
check_dir  "$0" "$DIR_PROJECT"         || exit 255
check_dir  "$0" "$DIR_PROJECT_CLUSTER" || exit 255

# make sure output dir is writable
iswriteable $DIR_PROJECT_CLUSTER      || fatal_msg "cluster directory is not writeable by user ($SYS_WHOAMI) ; $DIR_PROJECT_CLUSTER"

# cmpclus tool
cmd=$DIR_BRHCLUS/cmpclus
[ -x $cmd ] || fatal_msg "cmpclus command is not executable $cmd"

# set options
opts=""
ind=0

# loop over all
#for A in $PL_TODO_LBLSTR; do
for A in ${PL_TODO_LABELS[@]}; do
    rpart=PWC/$A/${A}_${PL_TODO_NAME}.relog_part
    ind=$((ind+1))

    # if last species we dont expect to find a relate og partial file (half matrix)
    (( ind == PL_TODO_NLABELS )) && break
    [[ -f $rpart ]] || fatal_msg "missing relog_part file: $rpart"
    opts="$opts -H $rpart"
done
opts="$opts MERGERELGRP"
# run merge

# tmp output
tmp_make tmpout ${PL_TODO_NAME}_relatedog_part || fatal_msg "failed creating tmp file in $tmpout"

echo "# merge command: $cmd $opts"
startdate=$(date)
tstart=$(time_msec)
$cmd $opts >> $tmpout
ec=$?
if [ $ec -ne 0 ]; then
    error_msg "cmpclus failed with ec = $ec"
    error_msg "-> cmd    : $cmd"
    error_msg "-> tmpout : $tmpout"
    error_msg "-> host   : $(hostname)"
    cat $tmpout
    exit $ec
fi

# job end time
dT=$(delta_time $tstart)

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

# target file
trgfile=$DIR_PROJECT_CLUSTER/${PL_TODO_NAME}.relog

# copy file to target
cp -f "$tmpout" "$trgfile"
if [ $? -ne 0 ]; then
    error_msg "failed copying related og tmp file"
    error_msg "-> tmp output : $tmpout"
    error_msg "-> trg output : $trgfile"
    error_msg "-> host       : `hostname`"
    exit 255
fi

chmod 644 "$trgfile"
link_results "_siblings.txt" "$trgfile"
