#!/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="functional_annotation"
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}/mummer"
mkdir -p "${WORKING}/${WORKFLOW}/megares"
mkdir -p "${WORKING}/${WORKFLOW}/interproscan"
mkdir -p "${WORKING}/${WORKFLOW}/functional_assignments"

## 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: MUMmer search against the restriction enzyme database
echo -n "# Running MUMmer against the RE database ......." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
"${MODULES}/mummer.sh" --fasta="${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta" \
    --database="${DATABASES}/rebase/rebase.fna" \
    --out="${WORKING}/${WORKFLOW}/mummer/${BASE}.mummer_re.txt"
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 2: BLASTn against the MEGARes nt database
echo -n "# Running BLASTn against MEGARes nt database ..." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
"${MODULES}/blastn.sh" --fasta="${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta" \
    --database="${DATABASES}/megares/megares_database_v1.01" \
    --out="${WORKING}/${WORKFLOW}/megares/${BASE}.megares" \
    --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 InterProScan
echo -n "# Running InterProScan ........................." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen 
"${MODULES}/interProScan.sh" --fasta="${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta" \
    --out="${WORKING}/${WORKFLOW}/interproscan/${BASE}.interproscan.txt" \
    --threads="${THREADS}"
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 4: Extract GO terms from UniRef BLASTx
echo -n "# Extracting GO IDs from UniRef BLASTx ........." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
"${SCRIPTS}/btab_2_functional_report.pl" -i "${WORKING}/taxonomic_identification/blastx/${BASE}.ur100.btab" \
    -o "${WORKING}/${WORKFLOW}/functional_assignments/results.txt"
status=$?
echo -n " DONE "  | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

exit "${status}"
