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

fatal_msg "NOTE select = BLASTCLUST is for now disabled"

# get args
if [ $# != 3 ]; then
    echo "Usage $0 <masked fasta> <cleaned fasta> <output>"
    exit 1;
fi

masked="$1"
cleaned="$2"
output="$3"

input="$1"
fasta=$(set_extension "$input" $EXT_FASTA)
fastamasked=$(set_extension "$input" $EXT_FASTA_MASKED)
output=$(set_extension "$input" $EXT_FASTA_SELECTED)

check_file "$0" "$fasta"       || exit 255
check_file "$0" "$fastamasked" || exit 255

percid=$(awk "BEGIN{ print $SELECT_PID/100.0 };")

# check blastclust
check_file_exe "$0" "$EXE_BLASTCLUST" || exit 255

# blastclust common options
bclopts="-S $SELECT_PID -e F"

#cdhit options
cdopts="-p 1 -d 100 -c $percid $OPTS_CDHIT"

# get cdhit and blastclust data type specific options
cdhitcmd=
#if [ "$DATA_TYPE" == "NUCL" ]; then
#    check_file_exe "$0" "$EXE_CDHIT_NUCL" || exit 255
#    cdhitcmd="$EXE_CDHIT_NUCL"
#    bclopts="$bclopts -p F"
#elif [ "$DATA_TYPE" == "PROT" ]; then
check_file_exe "$0" "$EXE_CDHIT_PROT" || exit 255
cdhitcmd="$EXE_CDHIT_PROT"
bclopts="$bclopts -p T"
#else
#    error_msg "${0} : invalid type ${type}"
#    exit 255
#fi


#-----------------------------------------------------------------------------
# run blastclust using the masked fasta

info_msg "BLASTCLUST START"
tmp_make tmpfile blastclust
startdate=$(date)
tA=$(time_msec)
$EXE_BLASTCLUST -i "$fastamasked" -o "$tmpfile" $OPTS_BLASTCLUST
[ $? -eq 0 ] || { error_msg "failed blastclust"; exit 255; }

dT=$(delta_time $tA)
info_msg "BLASTCLUST DONE in $dT seconds"


# get sequences that were clustered ( -S 2 ) from non-masked fasta
tmp_make tmpfasta blastclust
$DIR_ORTHOLOGER_BIN/get_blastclust_fasta.py -b "$tmpfile" -f "$fasta" --size 2 > "$tmpfasta"
[ $? -eq 0 ] || { error_msg "failed processing blastclust result"; exit 255; }
nsel=$(grep "^>" $tmpfasta | wc -l)
info_msg "Selected $nsel sequences for cd-hit"
info_msg "CDHIT START"

# check if output fasta size is non-zero
if [ -s $tmpfasta ]; then
    tmp_make tmpcdhit cdhit
    tmp_add_file "${tmpcdhit}.clstr"
    tmp_add_file "${tmpcdhit}.log"

    tB=$(time_msec)
    # do cdhit on the short list of sequences
    # this will do roughly the same as blastclust but will also give the alignment info
    $cdhitcmd $cdopts -i $tmpfasta -o $tmpcdhit
    [ $? -eq 0 ] || { error_msg "failed cdhit cmd"; exit 255; }

     [ -f ${tmpcdhit}.clstr ] || { error_msg "cannot find .clstr output from cdhit!"; exit 255; }
    dT=$(delta_time $tB)
    info_msg "CDHIT DONE in $dT seconds"

    # copy the clstr
    cp -f ${tmpcdhit}.clstr "${output}.clstr"

    # on older versions of cdhit this file was created
    [ -f ${tmpcdhit}.bak.clstr ] && mv -f ${tmpcdhit}.bak.clstr "${output}.bak.clstr"

    # get the representative fasta file, including all singlet clusters (-S 1)
    $DIR_ORTHOLOGER_BIN/get_blastclust_fasta.py -b $tmpfile -f "$fasta" --size 1 --representative > "$output"
    [ $? -eq 0 ] || { error_msg "failed extracting representative fasta"; exit 255; }

else
    info_msg "NO CLUSTERS FOUND WITH BLASTCLUST"
    # no results - target files
    # output fasta will be the same as the input one
    cp -f "$fasta" "$output"
    rm -f "${output}.clstr"
    echo "# No clusters found" > "${output}.clstr"
fi

dT=$(delta_time $tA)


# add header and footer

add_header_footer $0 "$output" "$output" "$startdate" $dT "blastclust and cd-hit"
[ $? -eq 0 ] || { echo "FAILED!!!"; exit 255; }


# set permissions
[ -f "${output}.bak.clstr" ] && chmod 644 "${output}.bak.clstr"
chmod 644 "$output"
chmod 644 "${output}.clstr"

