#!/bin/bash
set -e

pkg=hinge

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

# Copy the examples installed by the package: the upstream demo scripts, the
# configuration file and the reduced demo data set.
cp -a "/usr/share/doc/${pkg}/examples/." "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}/demo/ecoli_demo"

# Run the demo pipeline from the upstream README on the shipped subset of the
# E. coli P4 data.  The subset is small enough to fit into a single DALIGNER
# block.
gunzip -c reads.fasta.gz > reads.fasta
hinge correct-head reads.fasta reads.pb.fasta map.txt
fasta2DB ecoli reads.pb.fasta

HPC.daligner -t5 ecoli > daligner.sh
bash -v daligner.sh

# For larger data sets HPC.daligner writes one .las file per block and the
# README's LAmerge step is needed.  A one-block run writes ecoli.las directly.
if [ ! -e ecoli.las ] ; then
    LAmerge ecoli.las ecoli.*.las
fi
rm -f ecoli.*.las

# DASqv needs the coverage track produced by DAScover.
DAScover ecoli.db ecoli.las
DASqv -c100 ecoli ecoli.las

mkdir -p log
hinge filter --db ecoli --las ecoli.las -x ecoli --config ../../utils/nominal.ini
hinge maximal --db ecoli --las ecoli.las -x ecoli --config ../../utils/nominal.ini
hinge layout --db ecoli --las ecoli.las -x ecoli --config ../../utils/nominal.ini -o ecoli
hinge clip ecoli.edges.hinges ecoli.hinge.list demo

# hinge clip writes the graph files that use networkx and matplotlib.
for graph in ecolidemo.G00.graphml ecolidemo.G0.graphml ecolidemo.G1.graphml \
             ecolidemo.G2.graphml ecolidemo.Gs.graphml; do
    test -s "${graph}"
done
