#!/bin/sh -e
# Adapted from build time test

pkg=minimac4

if [ "$AUTOPKGTEST_TMP" = "" ] ; then
    AUTOPKGTEST_TMP=`mktemp -d /tmp/${pkg}-test.XXXXXX`
    trap "rm -rf $AUTOPKGTEST_TMP" 0 INT QUIT ABRT PIPE TERM
fi
cd $AUTOPKGTEST_TMP
cp -a /usr/share/doc/${pkg}/examples/* $AUTOPKGTEST_TMP

testit() {
d=$1
mkdir -p $d

sampledata=ref_panel.vcf
tarpanel=tar_panel.vcf
ref_vcf=$d/$(basename $2).gz
ref_msav=$d/$(basename $2 .vcf).msav
tar_vcf=$d/$(basename $3).gz
imputed_vcf=$d/imputed.vcf.gz

bcftools view $2 -Oz -o $ref_vcf
bcftools index $ref_vcf
bcftools view $3 -Oz -o $tar_vcf
bcftools index $tar_vcf
minimac4 --compress-reference $ref_vcf > $ref_msav
minimac4 $ref_msav $tar_vcf -f GT -O vcf.gz > $imputed_vcf

gzip -cd $imputed_vcf | grep -v "^#" | cut -f9- > $d/imputed_gt_matrix.tsv
gzip -cd $ref_vcf | grep -v "^#" | cut -f9- > $d/ref_gt_matrix.tsv
diff $d/ref_gt_matrix.tsv $d/imputed_gt_matrix.tsv
}

testit m4_simple_test_output ref_panel.vcf tar_panel.vcf
testit m4_haploid_and_diploid_test_output ref_panel_with_haploid_sample.vcf tar_panel_with_haploid_sample.vcf
