#!/bin/bash
set -e

usage() {
    echo; echo "Usage: $0 --fasta=/Path/to/infile.fasta --working=/Path/to/working_directory [--log=/Path/to/run.log]"
    echo "  --fasta     Path to the input NT FASTA file"
    echo "  --working   Path to the output working directory"
    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"
WORKFLOW="initialize"
LOG="" ## Initialize empty in case no log is needed

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

if [ $# -gt 3 ] || [ $# -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;;
    --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}/validate_fasta"
mkdir -p "${WORKING}/${WORKFLOW}/ambig_2_unambig"
mkdir -p "${WORKING}/${WORKFLOW}/six_frame_translation"


## ========================
## Step 0: Validate that this is a NT FASTA file
echo -n "# Verifying that input is NT FASTA file ..." | tee -a "${LOG}" ## If user provides no log file, this tee just won't happen
command time -v -o "${WORKING}/${WORKFLOW}/validate_fasta/validate_fasta.log" "${SCRIPTS}/validate_fasta.pl" -f "${FASTA}" --max_seq_size=1000
status=$?
echo -n " DONE "  | tee -a ${LOG}; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 1: Create UNambiguous versions of the input sequences
echo -n "# Checking for ambiguous bases ............" | tee -a "${LOG}"
command time -v -o "${WORKING}/${WORKFLOW}/ambig_2_unambig/ambig_2_unambig.log" "${SCRIPTS}/ambiguous_2_unambiguous.pl" \
    -f "${FASTA}" \
    -o "${WORKING}/${WORKFLOW}/ambig_2_unambig/${BASE}.unambig.fasta" \
    -n 4
status=$?
echo -n " DONE " | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' |tee -a "${LOG}"

## ========================
## Step 2: Create 6-frame translations of the input sequences for later on
echo -n "# Performing a 6-frame translation ........" | tee -a "${LOG}"
command time -v -o "${WORKING}/${WORKFLOW}/six_frame_translation/six_frame_translation.log" "${SCRIPTS}/six_frame_translation.pl" \
    -f "${WORKING}/${WORKFLOW}/ambig_2_unambig/${BASE}.unambig.fasta" \
    -o "${WORKING}/${WORKFLOW}/six_frame_translation/${BASE}.translated.fasta"
status=$?
echo -n " DONE " | tee -a "${LOG}"; date '+%H:%M:%S %Y-%m-%d' | tee -a "${LOG}"

exit "${status}"
