#!/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 Bowtie2 and RapSearch2 NT and AA databases"
    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="triage"
LOG="" ## Initialize empty in case no log is needed

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

if [ $# -gt 5 ] || [ $# -lt 2 ]
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}/bowtie2
mkdir -p ${WORKING}/${WORKFLOW}/rapsearch2/
mkdir -p ${WORKING}/${WORKFLOW}/mash/
mkdir -p ${WORKING}/${WORKFLOW}/phmmer/

## Step 0a: Convert ambiguous bases to non-ambiguous bases (if we need to)
if [ ! -f "${WORKING}/initialize/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 0b: Perform 6-frame translation of the input query sequence (if we need to)
if [ ! -f "${WORKING}/initialize/six_frame_translation/${BASE}.translated.fasta" ]; then
    mkdir -p ${WORKING}/initialize/six_frame_translation
    ${SCRIPTS}/six_frame_translation.pl -f ${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta \
	-o ${WORKING}/initialize/six_frame_translation/${BASE}.translated.fasta
    status=$?
fi

## ========================
## Step 1: Bowtie2 search of the NT query against the BSAT database
echo -n "# Running Bowtie2 against the Blacklist ......" | tee -a ${LOG} ## If user provides no log file, this tee just won't happen 
${MODULES}/bowtie2.sh --fasta=${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta \
    --database=${DATABASES}/bowtie2/BLACKLIST \
    --out=${WORKING}/${WORKFLOW}/bowtie2/${BASE}.sam \
    --threads=${THREADS}
status=$?
echo -n " DONE "  | tee -a ${LOG}; date '+%H:%M:%S %Y-%m-%d' |tee -a ${LOG}

## ========================
## Step 2: RapSearch2 search of the translated AA query against the BSAT peptide DB
echo -n "# Running RapSearch2 against the Blacklist ..." | tee -a ${LOG}
${MODULES}/rapsearch2.sh --fasta=${WORKING}/initialize/six_frame_translation/${BASE}.translated.fasta \
    --database=${DATABASES}/rapsearch2/BLACKLIST \
    --out=${WORKING}/${WORKFLOW}/rapsearch2/${BASE}.rapsearch \
    --threads=${THREADS}
status=$?
echo -n " DONE "  | tee -a ${LOG}; date '+%H:%M:%S %Y-%m-%d' |tee -a ${LOG}

## ========================
## Step 3: MASH against all of RefSeq. Still need to make this DB.
echo -n "# Running Mash against RefSeq ................" | tee -a ${LOG}
${MODULES}/mash_dist.sh --fasta=${WORKING}/initialize/ambig_2_unambig/${BASE}.unambig.fasta \
    --database=${DATABASES}/mash/RefSeq_10K_Sketches.msh \
    --out=${WORKING}/${WORKFLOW}/mash/${BASE}.mash.txt \
    --threads=${THREADS} \
    --distance=0.05
status=$?
echo -n " DONE "  | tee -a ${LOG}; date '+%H:%M:%S %Y-%m-%d' |tee -a ${LOG}

## ========================
## Step 4: pHMMER against Swiss-Prot
echo -n "# Running pHMMER against Swiss-Prot .........." | tee -a ${LOG}
${MODULES}/phmmer.sh --fasta=${WORKING}/initialize/six_frame_translation/${BASE}.translated.fasta \
    --out=${WORKING}/${WORKFLOW}/phmmer/${BASE}.phmmer \
    --database=${DATABASES}/phmmer/SWISS_PROT_Oct2017 \
    --threads=${THREADS} \
    --evalue=1e-3
echo -n " DONE "  | tee -a ${LOG}; date '+%H:%M:%S %Y-%m-%d' |tee -a ${LOG}

exit ${status}
