#!/bin/sh -e

pkg="sra-toolkit"

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}/test-data.tar.gz .
echo ">>>>>Decompressing test-data archive..."
tar -xzvf *.tar.gz; rm *.tar.gz

echo ">>>>>Running test-sra..."
test-sra > /dev/null

echo ">>>>>Running vdb-config..."
export VDB_CONFIG=$AUTOPKGTEST_TMP
export NCBI_SETTINGS=$AUTOPKGTEST_TMP/meta/u.mkfg
vdb-config --set repository/user/main/public/root=$AUTOPKGTEST_TMP/public

echo ">>>>>Running cache-mgr..."
cache-mgr --report

echo ">>>>>Running srapath..."
ls454=$(srapath SRR2042184)
illumina=$(srapath SRR6650398)
solid=$(srapath SRR7032226)
echo $ls454
echo $illumina
echo $solid

echo ">>>>>Running prefetch..."
prefetch $ls454
prefetch $illumina
prefetch $solid

echo ">>>>>Running fastq-dump..."
fastq-dump -F --split-files SRR2042184
fastq-dump -F --split-files SRR6650398
echo ">>>>>Running latf-load..."
latf-load -p LS454 --quality PHRED_33 -o SRR2042184 SRR2042184_1.fastq
echo ">>>>>Running kar..."
kar -c SRR2042184.sra -d SRR2042184
[ -s SRR2042184.sra ]
# echo ">>>>>Running fastq-load..."
# fastq-load -r meta/SRR6650398_fastq_run.xml -e meta/SRR6650398_experiment.xml -o SRR6650398
rm -rf SRR2042184 SRR6650398 SRR2042184.sra SRR2042184_1.fastq SRR6650398_1.fastq

echo ">>>>>Running abi-dump..."
abi-dump -F SRR7032226
# make sequence descriptor to look like from SOLiD
sed 's/>1/>1_11_1111_F3/' SRR7032226_F3.csfasta > SRR7032226_F3e.csfasta
sed 's/>1/>1_11_1111_F3/' SRR7032226_F3_QV.qual > SRR7032226_F3_QVe.qual
# echo ">>>>>Running abi-load..."
# abi-load -r meta/SRR7032226_run.xml -e meta/SRR7032226_experiment.xml -o SRR7032226
rm -rf SRR7032226 SRR7032226_F3*

echo ">>>>>Running sam-dump..."
sam-dump --output-file SRR6650398.sam SRR6650398
echo ">>>>>Running bam-load..."
bam-load --ref-file meta/reference.fna -o SRR6650398 meta/SRR6650398.bam
rm -r SRR6650398 SRR6650398.sam

echo ">>>>>Running illumina-dump..."
illumina-dump --qseq 1 SRR6650398
# echo ">>>>>Running illumina-load..."
# illumina-load -r meta/SRR6650398_illumina_run.xml -e meta/SRR6650398_experiment.xml -o SRR6650398
rm -rf SRR6650398 SRR6650398_0_0001_qseq.txt

echo ">>>>>Running sff-dump..."
sff-dump SRR2042184
# echo ">>>>>Running sff-load..."
# sff-load -r meta/SRR2042184_run.xml -e meta/SRR2042184_experiment.xml -o SRR2042184

echo ">>>>>Running vdb-dump..."
vdb-dump -f tab -C READ SRR2042184 | awk '{print ">" "SRR2042184." NR "\n" $0}' > SRR2042184.fasta

echo ">>>>>Running sra-pileup..."
sra-pileup --function mismatch --minmismatch 10 -o stat.txt.gz --gzip SRR6650398

echo ">>>>>Running vdb-encrypt..."
cd dbGaP-0   # change directory to the project’s workplace
vdb-encrypt --ngc ../meta/prj_phs710EA_test.ngc ../SRR2042184.fasta ./
echo ">>>>>Running vdb-decrypt..."
vdb-decrypt --ngc ../meta/prj_phs710EA_test.ngc SRR2042184.fasta.ncbi_enc
cd ../

echo ">>>>>Running vdb-validate..."
vdb-validate SRR2042184

echo ">>>>>Running vdb-copy..."
vdb-copy SRR2042184 meta/SRR2042184

echo ">>>>>Running vdb-lock..."
vdb-lock meta/SRR2042184
echo ">>>>>Running vdb-unlock..."
vdb-unlock meta/SRR2042184

echo ">>>>>Running rcexplain..."
rcexplain 3

echo ">>>>>Running kdbmeta..."
kdbmeta SRR2042184 > /dev/null

# Tools that would also be nice to test:
# copycat
# sra-sort ?
# srf-load ?
# helicos-load ?
# cg-load
# pacbio-load
# fasterq-dump (will appear in r.2.9.1)

echo ""; echo "Status: All tests PASSED!"; echo ""
