#!/bin/bash
set -e

pkg=megadepth

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

CUR_DIR=`pwd`

cp -a ${CUR_DIR}/tests "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

echo -e "\e[93m\e[1mRunning Tests\e[0m"

echo -e "\e[93m\e[1mTest 1\e[0m"

echo "2464c1808143637a273c0b8dbc0aab0f  test.bam.all.bw" >> checksums
echo "6508719c474e530405b1200c66002d52  test.bam.alts.tsv" >> checksums
echo "dcb8a42636a2e2f37182e39029db0409  test.bam.annotation.tsv" >> checksums
echo "ad032a6f7f39c2012313c124c0911d44  test.bam.auc.tsv" >> checksums
echo "58c94002c3a6dc8c641b877a5e1c3ffa  test.bam.ends.tsv" >> checksums
echo "dc5fc63b9783d6298945319dd625cc5e  test.bam.frags.tsv" >> checksums
echo "10bad76a5be373fabdaca9d1584e7988  test.bam.softclip.tsv" >> checksums
echo "f5f736b3b9ba5f45a84d0502328e5df7  test.bam.starts.tsv" >> checksums
echo "0176bb1d0f29a9dd8978076885202782  test.bam.unique.bw" >> checksums
echo "4185ac72cbe1108ea8bc22198eceef9a  test.bam.unique.tsv" >> checksums

megadepth tests/test_noprefix.bam --prefix test.bam --threads 4 --bigwig --auc --min-unique-qual 10 --annotation tests/test_exons.bed --frag-dist --alts --include-softclip --only-polya --read-ends --test-polya --no-annotation-stdout --no-auc-stdout --filter-out 260 --add-chr-prefix human

echo -e "\e[93m\e[1mTest 2\e[0m"

echo "867963297db7bc736b4900ec7c0c25bd  auc.single" >> checksums

megadepth tests/test.bam | fgrep "ALL_READS_ALL_BASES" > auc.single

echo -e "\e[93m\e[1mTest 3\e[0m"

echo "dc536e9f935a5d7e57e562d8a5d78690  test2.bam.all_jxs.tsv" >> checksums
echo "fc827bbd5c0f599b6f5c3a22946dc3db  test2.bam.jxs.tsv" >> checksums

megadepth tests/test2.bam --threads 4 --junctions --all-junctions --prefix test2.bam

echo -e "\e[93m\e[1mTest 4\e[0m"

echo "6061748feff63eebd4139577e8177ac4  test.bam.bw1.annotation.tsv" >> checksums
echo "0241b31419e5e1fe44c2d39cc750e8f2  test.bam.bw1.auc.tsv" >> checksums

megadepth test.bam.all.bw --annotation tests/testbw1.bed --auc --prefix test.bam.bw1 --no-annotation-stdout --no-auc-stdout

echo -e "\e[93m\e[1mTest 5\e[0m"

echo "4d4ba6d95a4bcf67ebf6e1d21639cf55  test.bam.bw2.annotation.tsv" >> checksums
echo "7e164dfedcadda2ddb88223bd6b27c68  test.bam.bw2.auc.tsv" >> checksums

megadepth test.bam.all.bw --annotation tests/testbw2.bed --auc --prefix test.bam.bw2 --no-annotation-stdout --no-auc-stdout

echo -e "\e[93m\e[1mTest 6\e[0m"

echo "ee936450c90f5b73adf27371eee1bd77  bw2.mean.annotation.tsv" >> checksums

megadepth test.bam.all.bw --op mean --annotation tests/testbw2.bed --prefix bw2.mean --no-annotation-stdout

echo -e "\e[93m\e[1mTest 7\e[0m"

echo "c5544cefcb60830169ad46d7504a2ba8  bw2.min.annotation.tsv" >> checksums

megadepth test.bam.all.bw --op min --annotation tests/testbw2.bed --prefix bw2.min --no-annotation-stdout

echo -e "\e[93m\e[1mTest 8\e[0m"

echo "87589fa87988257eeab9a8c06bb492fa  bw2.max.annotation.tsv" >> checksums

megadepth test.bam.all.bw --op max --annotation tests/testbw2.bed --prefix bw2.max --no-annotation-stdout

echo -e "\e[93m\e[1mTest 9\e[0m"

echo "e94687b7576aa684ed9f67b2ff7d2641  t3.auc.tsv" >> checksums
echo "46e50d56513bcec0914dc6b78e9fabe6  t3.tsv" >> checksums

megadepth tests/test3.bam --auc --coverage --prefix t3 --no-auc-stdout > t3.tsv

echo -e "\e[93m\e[1mTest 10\e[0m"

echo "8431fe0549a80d2038bf10f9fefa4a8e  test3.auc.tsv" >> checksums

megadepth tests/test3.bam --coverage  --min-unique-qual 10 --bigwig --auc --prefix test3 --no-auc-stdout

echo -e "\e[93m\e[1mTest 11\e[0m"

echo "08c2ea63dffb643980fc47fb76a00b49  long_reads.bam.jxs.tsv" >> checksums

megadepth tests/long_reads.bam --junctions --prefix long_reads.bam --long-reads

echo -e "\e[93m\e[1mTest 12\e[0m"

echo "7c17a64c132eb67d8f856711f2d44a74  test.bam.bw2.annotation.tsv.1" >> checksums

megadepth test.bam.all.bw --sums-only --annotation tests/testbw2.bed --prefix test.bam.bw2 > test.bam.bw2.annotation.tsv.1

echo -e "\e[93m\e[1mTest 13\e[0m"

echo "bad90f554cc99dd728160a017951b984  test.bw.all_overlap_types.test_output.bed" >> checksums

megadepth tests/bw.all_overlap_types.test_input.bw --annotation tests/gh_bug_9.bed --auc > test.bw.all_overlap_types.test_output.bed

echo -e "\e[93m\e[1mTest 14\e[0m"

echo "c94f5e4d0ecebff1af6e90281944e7f0  TCGA_BLCA_A13J_vs_chr1.61863.62160.annotation.tsv" >> checksums

megadepth tests/TCGA_BLCA_A13J.vcf.gz_cg_cov5.bw.bg.gz.chr1.60379.62229.bw --annotation tests/chr1.61863.62160.bed --no-annotation-stdout --prefix TCGA_BLCA_A13J_vs_chr1.61863.62160

echo -e "\e[93m\e[1mTest 15\e[0m"

echo "a358f4ab8835f1192b43005da03b0440  test.bw1.total_auc" >> checksums

megadepth test.bam.all.bw | grep "AUC" > test.bw1.total_auc

md5sum --check checksums
echo -e "\e[92m\e[1mPASS\e[0m"

#cleanup
rm -f test*tsv test*auc bw2* test3* test2* t3.* long_reads.bam.jxs.tsv test_run_out *null*.unique.tsv test.*.bw auc.single test.bam.mean test.bw.all_overlap_types.test_output.bed TCGA_BLCA_A13J_vs_chr1.61863.62160.annotation.tsv
