#!/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/>.
#
#
# Run given fasta selection algorithm.
# The command line format is
#   run_select.sh <fasta1>
#
#--- check if environment is ok -----------------
[ "${LOADED_COMMON_PIPELINE_SH+1}" == "" ] && echo "ERROR $0 : main config has not been loaded" 1>&2 && exit 255
#------------------------------------------------

[[ $# -eq 1 ]] || fatal_msg "usage: $0 <label>"

[[ -x "$SELECT_SCRIPT" ]] || fatal_msg "fasta selection script not found - script : $SELECT_SCRIPT"

input="$1"
inparr=( $(tokenize ";" $input) )

nok=0
nerr=0
ec=0

tmpmask=""
tmpclean=""
tmpout=""
tmp_make tmpmask  masker1
tmp_make tmpclean masker2
tmp_make tmpout   masker3

debug_msg "select clusters of similar sequences"
# loop over set of arguments separated by ';'
for args in ${inparr[@]}; do
    echo
    print_separator
    info_msg "Starting SELECT job <$args>"
    print_separator
    echo
    
    fasta="$DIR_PROJECT_SEQUENCES/${args}.$EXT_FASTA"

    # get masked, uncompress and remove comments
    masked=$(set_extension "$fasta" $EXT_FASTA_MASKED)    
    check_footer "$masked" || fatal_msg "run_select : missing end tag (#END) in <$masked>"
    grep -v "^#" "$masked" > "$tmpmask"

    # get fasta clean, uncompress and remove comments
    cleaned=$(set_extension "$fasta" $EXT_FASTA_CLEAN)    
    check_footer "$cleaned" || fatal_msg "run_select : missing end tag (#END) in <$cleaned>"
    grep -v "^#" "$cleaned" > "$tmpclean"

    # now masked contains a file name without compression extension
    tmp_add_file "${tmpout}.clstr"
    
    # run script
    debug_msg "run_select.sh : source $SELECT_SCRIPT $tmpmask $tmpclean $tmpout"
    
    (tmp_setup_trap && source "$SELECT_SCRIPT" "$tmpmask" "$tmpclean" "$tmpout" )
    ec=$?

    debug_msg "Input mask  : $(wc -l $tmpmask)"
    debug_msg "Input clean : $(wc -l $tmpclean)"
    debug_msg "Output      : $(wc -l $tmpout)"
    
    if [[ $ec -eq 0 ]]; then
        nok=$((nok+1))
    else
        nerr=$((nerr+1))
    fi

    # compress files
    if [[ $nerr -eq 0 ]]; then
        output=$(set_extension "$fasta" "$EXT_FASTA_SELECTED")
        cp -f "$tmpout"         "$output"         || ec=1
        cp -f "${tmpout}.clstr" "${output}.clstr" || ec=1
	    (( ec == 0 )) || nerr=$((nerr+1))
    fi
    echo
    print_separator
    info_msg "Done SELECT job <$args>"
    print_separator
done

(( nerr == 0 )) || { info_msg "Completed $nok succesfully and $nerr with errors"; exit 255; }
