#!/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
#------------------------------------------------
if isfunc make_tmp; then
    tmp_make() {
        make_tmp $*
    }
fi

# containers to store stats per tax id
declare -x -A TM_STATS_FASTA_LEN
declare -x -A TM_STATS_FASTA_NELEM
export TM_STATS_FASTA_TOTLEN=0
export TM_MAX_NPOINTS=${TM_MAX_NPOINTS=1000}
export TM_FIT_RESULT="1 0"

# fill fasta length statistics
tm_fill_stats_fastalen() {
    [[ -d $DIR_PROJECT_SEQUENCES ]] || { echo "error: unable to find Sequences dir of project $DIR_PROJECT_SEQUENCES"; exit 255; }
    local t=
    local N=0
    TM_STATS_FASTA_TOTLEN=0
              
    #for t in $PL_TODO_LBLSTR; do
    for t in ${PL_TODO_LABELS[@]}; do
        local f=$DIR_PROJECT_SEQUENCES/${t}.${EXT_FASTA_STATS}
        [[ -f $f ]] || continue
        local len=$(grep "^#TOTLEN" $f | cut -d ":" -f 2)
        local nelem=$(grep "^#NTOT" $f | cut -d ":" -f 2)
        TM_STATS_FASTA_LEN[$t]=$len
        TM_STATS_FASTA_NELEM[$t]=$nelem
        TM_STATS_FASTA_TOTLEN=$(calc "${TM_STATS_FASTA_TOTLEN}+${len}")
        N=$((N+1))
    done
    [[ $N -gt 0 ]] || fatal_msg "no fasta stats files found - make sure step STATS is run!"
}

# get fasta length for a given species ID
tm_get_fastalen() {
    [ $# -eq 1 ] || return 255

    local spec=$1
    set +u
    [ ${#TM_STATS_FASTA_LEN[${spec}]} -eq 0 ] || { echo  ${TM_STATS_FASTA_LEN[$spec]}; set -u; return 0; }
    set -u
    return 255
}

tm_get_fastanelem() {
    [ $# -eq 1 ] || return 255

    local spec=$1
    set +u
    [ ${#TM_STATS_FASTA_NELEM[${spec}]} -eq 0 ] || { echo  ${TM_STATS_FASTA_NELEM[$spec]}; set -u; return 0; }
    set -u
    return 255
}

# extract time from file, look for a TIME tag at the end
tm_extract_time() {
    [ $# -eq 1 ] || return 255
    local fdata="$1"
    local tstr=($(tail -n 10 $fdata | grep TIME | cut -d ":" -f 2))
    if [ ${#tstr[@]} -eq 1 ]; then
        echo ${tstr[0]}
    else
        echo ${tstr[1]}
    fi
}

tm_extract_fulltime() {
    [ $# -eq 1 ] || return 255
    local fdata="$1"
    local tstr=($(tail -n 10 $fdata | grep TIME | cut -d ":" -f 2))
    # return <machine> <time> <normalised time>
    echo ${tstr[@]}
}

# extract machine name
tm_extract_machine() {
    [ $# -eq 1 ] || return 255
    local fdata="$1"
    head -n 5 $fdata | grep "^# System" | cut -d ":" -f 2 | cut -d " " -f 3
}

# predict time
tm_predict() {
    [ $# -eq 3 ] || return 255
    local X=$1
    local A=$2
    local B=$3
    calc "10^($A*$X + $B)"
}

tm_filename_data() {
    [ $# -eq 1 ] || return 255
    local stepid=$1
    local label=$(basename "$PL_TODO" .todo)
    echo "$DIR_PROJECT/Cluster/${label}_ALIGNMENT_timing.dat"
}

tm_filename_fit() {
    [ $# -eq 1 ] || return 255
    local stepid=$1
    local label=$(basename "$PL_TODO" .todo)
    echo "$DIR_PROJECT/Cluster/${label}_ALIGNMENT_timing_fit.dat"
}

tm_clear_data() {
    [ $# -eq 1 ] || return 0
    local stepid=$1
    local f=$(tm_filename_data "$stepid")
    rm -f "$f"

    f=$(tm_filename_fit "$stepid")
    rm -f "$f"
}

#tm_step_<step>() {
#    loop over all output files in step:
#       find TIME tag
#       estimate time
#
#    <step id > <N output> <total time> <average time> <total actual time> <estimated total time>
#}

tm_fill_stats_fastalen
