#!/bin/bash
set -e
set -o pipefail

pkg=sortmerna

export LC_ALL=C.UTF-8

SRC=$(pwd)

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"
# Only copy the data the tests need instead of the whole (large) test_data
mkdir -p "${AUTOPKGTEST_TMP}/test_data"
for f in test_ref.fasta test_read.fasta \
         silva-arc-16s-database-id95.fasta \
         set7_arc_bac_16S_database_match.fasta ; do
  cp -a "test_data/${f}" "${AUTOPKGTEST_TMP}/test_data/"
done

cd "${AUTOPKGTEST_TMP}"

fail() {
  echo "FAIL: $*" >&2
  exit 1
}

# compare_golden <produced> <golden> <description>
compare_golden() {
  if ! diff -u "$2" "$1" ; then
    fail "$3"
  fi
  echo "PASS: $3"
}

# The arguments below are the ones that produced the upstream golden files,
# as recorded in the @PG line of tests/golden/*/aligned.sam.
echo "=== Test 1: tiny database, FASTA/FASTQ, SAM and BLAST output ==="
sortmerna --ref test_data/test_ref.fasta \
          --reads test_data/test_read.fasta \
          --workdir WORKDIR \
          --aligned WORKDIR/aligned \
          --other WORKDIR/other \
          --threads 1 --num_alignments 1 --blast "1 cigar qcov" \
          --fastx --sam --match 2 --mismatch -3 --gap_open 5 \
          --gap_ext 2 -N -3 -L 18

for f in aligned.fa aligned.blast aligned.sam ; do
  [ -f "WORKDIR/${f}" ] || fail "missing WORKDIR/${f}"
done
compare_golden WORKDIR/aligned.fa \
               "${SRC}/tests/golden/tiny/aligned.fa" \
               "tiny: aligned.fa matches golden output"
compare_golden WORKDIR/aligned.blast \
               "${SRC}/tests/golden/tiny/aligned.blast" \
               "tiny: aligned.blast matches golden output"
# The SAM header contains the command line and is not part of the comparison.
grep -v '^@' WORKDIR/aligned.sam > sam_tiny.got
grep -v '^@' "${SRC}/tests/golden/tiny/aligned.sam" > sam_tiny.exp
compare_golden sam_tiny.got sam_tiny.exp \
               "tiny: aligned.sam matches golden output"

echo
echo "=== Test 2: small database, aligned and non-aligned output ==="
sortmerna --ref test_data/silva-arc-16s-database-id95.fasta \
          --reads test_data/set7_arc_bac_16S_database_match.fasta \
          --workdir WORKDIR2 \
          --aligned WORKDIR2/aligned \
          --other WORKDIR2/other \
          --threads 1 --num_alignments 1 --blast "1 cigar qcov" \
          --fastx --sam --match 2 --mismatch -3 --gap_open 5 \
          --gap_ext 2 -N -3 -L 18

for f in aligned.fa aligned.blast aligned.sam other.fa ; do
  [ -f "WORKDIR2/${f}" ] || fail "missing WORKDIR2/${f}"
done
compare_golden WORKDIR2/aligned.fa \
               "${SRC}/tests/golden/small/aligned.fa" \
               "small: aligned.fa matches golden output"
compare_golden WORKDIR2/aligned.blast \
               "${SRC}/tests/golden/small/aligned.blast" \
               "small: aligned.blast matches golden output"
grep -v '^@' WORKDIR2/aligned.sam > sam_small.got
grep -v '^@' "${SRC}/tests/golden/small/aligned.sam" > sam_small.exp
compare_golden sam_small.got sam_small.exp \
               "small: aligned.sam matches golden output"
compare_golden WORKDIR2/other.fa \
               "${SRC}/tests/golden/small/other.fa" \
               "small: other.fa matches golden output"

echo
echo "=== Test 3: gzip-compressed reads (rapidarchive) ==="
gzip -c test_data/test_read.fasta > reads.fasta.gz
sortmerna --ref test_data/test_ref.fasta \
          --reads reads.fasta.gz \
          --workdir WORKDIR3 \
          --aligned WORKDIR3/aligned \
          --other WORKDIR3/other \
          --threads 1 --num_alignments 1 --blast "1 cigar qcov" \
          --fastx --sam --match 2 --mismatch -3 --gap_open 5 \
          --gap_ext 2 -N -3 -L 18 --zip-out 0

[ -f WORKDIR3/aligned.fa ] || fail "missing WORKDIR3/aligned.fa"
compare_golden WORKDIR3/aligned.fa \
               "${SRC}/tests/golden/tiny/aligned.fa" \
               "gzip: decompressed reads produce the golden alignment"

echo
echo "All tests passed."
