#!/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/>.
#
#
# simple script to run SEG and XNU separately,
# store the intermediate results in temporary files,
# then OR the results to stdout with PMERGE
# (for proteins), and clean up the temporary files.
# NOTE: the files seg+xnu and xnu+seg are hard
# links to the same file.
#
# Usage:  seg+xnu todo
#
# SECURITY ALERT:  if infile is open for users
# to specify in a filter option, then tests should
# be added to ensure privacy is not compromised.
#
#--- 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

# --- Check executables ---
# require seg and xnu to be in the same directory
# as this script
[ -d "$DIR_WUBLAST" ]        || fatal_msg "seg+xnu : missing dir $DIR_WUBLAST"
[ -x "$DIR_WUBLAST/seg" ]    || fatal_msg "seg+xnu : missing seg in dir $DIR_WUBLAST"
[ -x "$DIR_WUBLAST/xnu" ]    || fatal_msg "seg+xnu : missing xnu in dir $DIR_WUBLAST"
[ -x "$DIR_WUBLAST/pmerge" ] || fatal_msg "seg+xnu : missing pmerge in dir $DIR_WUBLAST"

filter() {
    local fname="$1"
    [ -f "$fname" ] || return 1

    local outfile="$2"

     # check if source is younger than target and if target exists
###    check_if_newer_source "$fname" "$outfile"

    # update only if target does not exist or is younger than source
###    [ $? -eq 0 ] || return 0

    # cp file to temp
    local tmpdata=""
    tmp_make tmpdata "segxnu_data"
    grep -v "^#" "$fname" > $tmpdata

    # run seg
    local tmpseg=""
    tmp_make tmpseg "seg"
    local startdate=$(date)
    local tA=$(time_msec)
    "$DIR_WUBLAST/seg" - -x > $tmpseg < $tmpdata
    local ec=$?
    if [ $ec -ne 0 ]; then
        error_msg "seg failed with error code $ec"
	    return 5
    fi

    # run xnu
    local tmpxnu=""
    tmp_make tmpxnu "xnu"
    "$DIR_WUBLAST/xnu" - > $tmpxnu < $tmpdata
    ec=$?
    if [ $ec -ne 0 ]; then
        error_msg "xnu failed with error code $ec"
	    return 6
    fi
    
    # merge results
    local tmppm=""
    tmp_make tmppm "pmerge"
    "$DIR_WUBLAST/pmerge" $tmpseg $tmpxnu > $tmppm
    ec=$?
    if [ $ec -ne 0 ]; then
        error_msg "pmerge failed with error code $ec"
	    return 7;
    fi

    $PYTHON3 "$DIR_ORTHOLOGER_BIN/filter_masked_fasta.py" -f $tmppm --maxf 0.95 --minlen $MIN_OVERLAP --type $DATA_TYPE > "$outfile"

    local dT=$(delta_time $tA)

    add_header_footer $0 "$outfile" "$outfile" "$startdate" $dT "\"seg+xnu\""
    chmod 644 "$outfile"

    return 0
}

[ $# -eq 2 ] || fatal_msg "usage: $0 <fasta> <outfile>"

#nthreads="$1"
fasta="$1"
outfile="$2"
check_file "$0" "$fasta" || exit 255

filter $fasta $outfile || exit 255
