#!/bin/bash
set -e

pkg=freebayes

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

#do_stuff_to_test_package#
ls -R

echo -e "\e[93m\e[1mTest 1\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 2\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam --region ref -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 3\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam --region ref:4-5 --region ref:6-7 -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 4\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam --region ref:4 --region ref:6 -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 5\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam --targets test.bed -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 6\e[0m"
freebayes --haplotype-length 0 --min-alternate-count 1 --min-alternate-fraction 0 --pooled-continuous --ploidy 1 -f test.ref test.bam --region ref:1,0-1,1 -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 7\e[0m"
freebayes -f tiny/q.fa tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 8\e[0m"
for region in q:180-191 q:1002-1013 q:3250-3268 q:5003-5014 q:5632-5646 q:6412-6423 q:8840-8851 q:10526-10537 q:12119-12130;
do 
    freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam -r $region -v test.vcf
done
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 9\e[0m"
freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 10\e[0m"
freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam -t targets.bed -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 11\e[0m"
freebayes -f tiny/q.fa tiny/NA12878.chr22.tiny.bam  -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 12\e[0m"
freebayes -f tiny/q.fa -l@ tiny/q.vcf.gz tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 13\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 14\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz tiny/NA12878.chr22.tiny.bam -l  -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 15\e[0m"
freebayes -f tiny/q.fa -@ tiny/q.vcf.gz -l tiny/1read.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 16\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz -r q:1-10000 tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 17\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz -r q:1-10000 tiny/NA12878.chr22.tiny.bam -l -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 18\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz --stdin < tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 19\e[0m"
freebayes -f tiny/q.fa -@ tiny/q_spiked.vcf.gz -l --stdin < tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 20\e[0m"
freebayes -f tiny/q.fa tiny/NA12878.chr22.tiny.bam  -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 21\e[0m"
freebayes -f 'tiny/q with spaces.fa' tiny/NA12878.chr22.tiny.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 22\e[0m"
freebayes -f tiny/hla.fa -@ tiny/hla.vcf.gz -r HLA-DRB1*16:02:01:1-10000 tiny/NA12878.chr22.tiny.hla.bam -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 23\e[0m"
freebayes -f splice/1:883884-887618.fa splice/1:883884-887618.bam -F 0.05 -C 1 -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 24\e[0m"
freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam --gvcf -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 25\e[0m"
freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam --gvcf --gvcf-chunk 50 -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo

echo -e "\e[93m\e[1mTest 26\e[0m"
freebayes -f tiny/q.fa -F 0.2 tiny/NA12878.chr22.tiny.bam --gvcf --gvcf-dont-use-chunk true -v test.vcf
echo -e "\e[92m\e[1mPassed\e[0m"
echo
