#!/bin/bash

pkg=cat-bat

if [ "$AUTOPKGTEST_TMP" = "" ]; then

	AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXX`
	trap "rm -rf $AUTOPKG_TMP" 0 INT QUIT ABRT PIPE TERM

fi

# Copy tests data
cp -a /usr/share/doc/"${pkg}"/examples/data "$AUTOPKGTEST_TMP"

# Change directory into data
cd "$AUTOPKGTEST_TMP"/data/

# Extract fasta
gunzip prepare/small.fa.gz

# Prepare CAT database
CAT prepare \
    --db_fasta ./prepare/small.fa \
    --names ./prepare/names.dmp \
    --nodes ./prepare/nodes.dmp \
    --acc2tax ./prepare/prot2acc.txt \
    --db_dir ./output_db

# Annotate contig set
CAT contigs \
    -c ./prepare/small.fa \
    -d ./output_db/db \
    -t ./output_db/tax

# Check final classification files (output files)
if [ -f "out.CAT.ORF2LCA.txt" ] && [ -f "out.CAT.contig2classification.txt" ]; then
    echo "Final Classification Files Generated"
    rm -rf out.*
else
    echo "One or both classification files not generated"
    rm -rf out.*
    exit 1
fi

# Run BAT on single MAG
CAT bins -b prepare/small.fa \
         -d output_db/db \
         -t output_db/tax 

# Check final classification files (output files)
if [ -f "out.BAT.ORF2LCA.txt" ] && [ -f "out.BAT.bin2classification.txt" ]; then
    echo "Final Classification Files Generated"
    rm -rf out.*
else
    echo "One or both classification files not generated"
    rm -rf out.*
    exit 1
fi
