#!/bin/bash
set -e

usage() {
    echo; echo "Usage: $0 --fasta=/Path/to/infile.fasta --databases=/Path/to/databases --working=/Path/to/working_directory [--threads=4] [--log=/Path/to/run.log]"
    echo "  --fasta     Path to the input NT FASTA file"
    echo "  --databases Path to the database directory"
    echo "  --working   Path to the output working directory"
    echo "  --threads   Number of threads to use (Default=1)"
    echo "  --log       Log file that the status can be tee'd to (Default=Don't save the log)"
    echo "  -h, --help  Print this help message out"; echo;
    exit 1;
}

## ========================
## Globals
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MODULES="${DIR}/../modules"
SCRIPTS="${DIR}/../scripts"
THREADS=1
WORKFLOW="ultra_sensitive_protein_search"
LOG="" ## Initialize empty in case no log is needed

## ========================
## Process the input parameters

if [ $# -gt 5 ] || [ $# -lt 3 ]
then
    usage
fi

while true
do
    case $1 in
    --help|-h)
	    usage
	    exit;;
    --fasta=?*)
	    FASTA=${1#*=};;
    --fasta|fasta=)
	    echo "$0: missing argument for '$1' option"
	    usage
	    exit 1;;
    --working=?*)
	    WORKING=${1#*=};;
    --working|working=)
	    echo "$0: missing argument for '$1' option"
	    usage
	    exit 1;;
    --databases=?*)
	    DATABASES=${1#*=};;
    --databases|databases=)
	    echo "$0: missing argument for '$1' option"
            usage
            exit 1;;
    --threads=?*)
            THREADS=${1#*=};;
    --threads|threads=)
            echo "$0: missing argument for '$1' option"
            usage
            exit 1;;
    --log=?*)
	    LOG=${1#*=};;
    --log|log=)
	    echo "$0: missing argument for '$1' option"
	    usage
	    exit 1;;
    --)
	    shift
	    break;;
    -?*)
	    echo "$0: invalid option: $1"
	    usage
	    exit 1;;
    *)
	    break
    esac
    shift
done

## ========================
## Create working directories

BASE=$(basename "${FASTA}")
mkdir -p "${WORKING}"
mkdir -p "${WORKING}/${WORKFLOW}/extract_sequences"
mkdir -p "${WORKING}/${WORKFLOW}/tblastx"
mkdir -p "${WORKING}/${WORKFLOW}/module8"

## Step 0a: Convert ambiguous bases to non-ambiguous bases (if we need to)
if [ ! -f "${WORKING}/ambig_2_unambig/${BASE}.unambig.fasta" ]; then
    mkdir -p "${WORKING}/initialize/ambig_2_unambig"
    "${SCRIPTS}/ambiguous_2_unambiguous.pl" -f "${FASTA}" -o "${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta" -n 4
    status=$?
fi

## ========================
## Step 1: Extract only sequences that didn't have a BLASTx hit
echo -n "# Extracting sequences that have no hits ..........." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
"${SCRIPTS}/extract_sequences.pl" --fasta="${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta" \
    --lookup="${WORKING}/taxonomic_identification/blastx/${BASE}.ur100.btab" \
    --out="${WORKING}/${WORKFLOW}/extract_sequences/no_hits.fasta" \
    --inverse
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 2: tBLASTx against all black list genes
echo -n "# Running tBLASTx against the black list gene DB ..." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
"${MODULES}/tblastx.sh" --fasta="${WORKING}/${WORKFLOW}/extract_sequences/no_hits.fasta" \
    --database="${DATABASES}/blast/BLACKLIST_GENES" \
    --out="${WORKING}/${WORKFLOW}/tblastx/${BASE}" \
    --threads="${THREADS}" \
    --evalue=1e-3
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 3: Run the protein motif detection module8
echo -n "# Running protein motif detection Module8 .........." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen 
"${MODULES}/module8.sh" --fasta="${WORKING}/${WORKFLOW}/extract_sequences/no_hits.fasta" \
    --out="${WORKING}/${WORKFLOW}/module8/${BASE}.txt"
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

exit "${status}"
