#!/usr/bin/env bash

echo
echo "------ DEPRECATED ------"
echo "use mmseqs or swipe"
echo

exit 255

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

#--------------------------------
#--- 1. PROCESS ARGUMENTS     ---
#--------------------------------
# get args
[ $# -eq 4 ] || fatal_msg "usage: $0 <query file> <DB file> <output> <nthreads>"

query="$1"
dbase="$2"
output="$3"
nthreads=$4

#--------------------------------
#--- 2. CHECK INPUT           ---
#--------------------------------
check_file     "$0" "$query"       || exit 255
check_file     "$0" "$dbase"       || exit 255

# set defaults
gapinsert="3"
gapextend="2"
matr="1"
matq="-1"

#--------------------------------
#--- 3. CHECK REQUIREMENTS    ---
#--------------------------------
# check that formatdb files are there
check_formatdb_files "$query" 0 || exit 255
check_formatdb_files "$dbase" 0 || exit 255

#--------------------------------
#--- 4. SETUP ALIGNMENT CMD     ---
#--------------------------------
# -e : max e-value
# -m : alignment view options, 8 -> tabular
# -G : cost to open gap
# -E : cost to extend gap
# -q : penalty for nucleotide mismatch
# -r : reward for nucleotide mismatch
# -v : ??
# -b : nr of alignments to show
# -a : nr of processors to use
# -W : word size, minimal alignment length, default 11 (N) and 3 for P
# -P : ?? only for P
# -A : ?? only for P
# -z : effective size of database, use 0 for real size
# -S : query strands: top(1), bottom(2) and both (3)   (N)
# -F : ??
optscommon="$OPTS_BLAST -b $ALIGNMENT_NUMALIGNMENTS -e $MAX_EVALUE -m 8 -a $nthreads -v 10"

# effective DB size
effdbsz=$(get_effdbsz)
[ $effdbsz -eq 0 ] || optscommon="$optscommon -z $effdbsz"

if [ "$DATA_TYPE" == "NUCL" ]; then
    # Nucleotides
    BLAST="$EXE_BLASTALL -p blastn $optscommon -G $gapinsert -E $gapextend -q $matq -r $matr -W 11 -S 3"
else
    # Proteins
    BLAST="$EXE_BLASTALL -p blastp $optscommon -P 0 -A 50"
fi

#--------------------------------
#--- 5. RUN ALIGNMENT           ---
#--------------------------------
# run blast
tmp_make tmpfile blast
startdate=$(date)
tA=$(time_sec)
ec=0
if [ "$DATA_TYPE" == "NUCL" ]; then
    ${BLAST} -F "m D" -i "$query" -d "$dbase" >> $tmpfile
    ec=$?
else
    ${BLAST} -i "$query" -d "$dbase" >> $tmpfile
    ec=$?
fi
dT=$(dtime $tA)
if [ $ec -ne 0 ]; then
	error_msg "# Failed BLAST"
    error_msg "# Error  : $ec"
    error_msg "# Time   : $dT"
	error_msg "# Query  : $query"
	error_msg "# DB     : $dbase"
	error_msg "# Output : $(uname -n):$tmpfile"
	tmp_mark_keep "$tmpfile"
    tmp_mark_keep "$query"
    tmp_mark_keep "$dbase"
    # exit trap will remove tmp file
	exit 255
fi

#--------------------------------
#--- 6. POST-PROCESSING       ---
#--------------------------------
# modify format to common fmt
# blast -m 8 format
#  id1  id2  percident  alignment_length  mismatches  gaps  start1  end1  start2  end2  evalue  score
#  
# common output is
#  id1  id2  score  evalue  percident  start1  end1  start2  end2
#
# The command below removes all lines beginning with '#' and reorders the data
tmp_make tmpfile2 blast
grep -v "^#" $tmpfile | awk '{ print $1,$2,$12,$11,$3,$7,$8,$9,$10; }' > $tmpfile2

#--------------------------------
#--- 7. ADD HEADER/FOOTER     ---
#--------------------------------
# add data format header
add_to_top $tmpfile2 $tmpfile2 "$(common_fmt_header)"

# add header and footer
add_header_footer $0 $tmpfile2 "$tmpfile2" "$startdate" $dT "\"${BLAST}\"" || exit $?

# copy file to output
cp -f $tmpfile2 "$output"

#--------------------------------
#--- 8. CLEANUP               ---
#--------------------------------
chmod 644 "$output"

