#!/bin/bash

export PATH=$PWD/../../../util/ext/gdl-1.1/GDL/bin:$PWD/../../../util/ext/gdl-1.1/GDL/include:$PATH
export LD_LIBRARY_PATH=$PWD/../../../util/ext/gdl-1.1/GDL/lib:$LD_LIBRARY_PATH
export PATH=$PWD/../../../util/ext/vowpal_wabbit/build/bin:$PWD/../../../util/ext/vowpal_wabbit/build/include:$PATH
export LD_LIBRARY_PATH=$PWD/../../../util/ext/vowpal_wabbit/build/lib:$LD_LIBRARY_PATH

fasta2skm=../../../util/fasta2skm

# specify reference database
DB=A1.10.1000

# specify parameters #
NBATCHES=2
K=48
L=100

function show_help {
echo This script assumes that it is run from the directory in which it lives.
echo Usage: $0 -d [database] --nbatches [NBATCHES] --kmer [KMER_LENGTH] 
}

while :; do
    case $1 in
        -h|-\?|--help)
            show_help
            exit 0
            ;;
        -d|--database)
            if [ -n "$2" ]; then
                DB=$2
                shift
            else
                printf 'ERROR: "--database" requires a non-empty option argument.\n' >&2
                exit 1
            fi
            ;;
        --database=?*)
            DB=${1#*=}
            ;;
        --database=)
            printf 'ERROR: "--database" requires a non-empty option argument.\n' >&2
            exit 1
            ;;
        -l|--length)
            if [ -n "$2" ]; then
                L=$2
                shift
            else
                printf 'ERROR: "--length" requires a non-empty option argument.\n' >&2
                exit 1
            fi
            ;;
        --nbatches)
            if [ -n "$2" ]; then
                NBATCHES=$2
                shift
            else
                printf 'ERROR: "--nbatches" requires a non-empty option argument.\n' >&2
                exit 1
            fi
            ;;
        --nbatches=?*)
            NBATCHES=${1#*=}
            ;;
        --nbatches=)
            printf 'ERROR: "--nbatches" requires a non-empty option argument.\n' >&2
            exit 1
            ;;
        --test_subdir)
            if [ -n "$2" ]; then
                test_subdir=$2
                shift
            else
                printf 'ERROR: "--test_subdir" requires a non-empty option argument.\n' >&2
                exit 1
            fi
            ;;
        --test_subdir=?*)
            test_subdir=${1#*=}
            ;;
        --test_subdir=)
            printf 'ERROR: "--test_subdir" requires a non-empty option argument.\n' >&2
            exit 1
            ;;

        -k|--kmer)
            if [ -n "$2" ]; then
                K=$2
                shift
            else
                printf 'ERROR: "--kmer" requires a non-empty option argument.\n' >&2
                exit 1
            fi
            ;;
        --kmer=?*)
            K=${1#*=}
            ;;
        --kmer=)
            printf 'ERROR: "--kmer" requires a non-empty option argument.\n' >&2
            exit 1
            ;;
        --)
            shift
            break
            ;;
        -?*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)
            break
    esac
    shift
done


modelDir=../../../output/model/train_$DB-db/opal
model=$modelDir/vw-model_batch-$NBATCHES.model
dico=$modelDir/vw-dico.txt


# make predictions fragments #
if [[ -z ${test_subdir+x} ]]; then 
    test_dir=../../../data/$DB/test/
    outputdir=../../../output/prediction/$DB-db/
else
    test_dir=../../../data/${DB}/test/${test_subdir}
    outputdir=../../../output/prediction/${DB}-db/${test_subdir}
fi
fasta=$test_dir/$DB.test.fasta

mkdir -p $outputdir
prefix=$outputdir/test.fragments.$DB-db

# get vw predictions
$fasta2skm -i $fasta -k $K  -p $modelDir/patterns.txt | vw -t -i $model -p $prefix.preds.vw
python vw-class-to-taxid.py $prefix.preds.vw $dico $prefix.preds.taxid
python eval_full.py -d $DB --pred_dir $outputdir --test_dir $test_dir
