#!/bin/bash
# Run the pyhmmer unit tests and a small end-to-end search example.
set -eu

pkg="pyhmmer"

if [ -z "${AUTOPKGTEST_TMP:-}" ] ; then
    AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
fi
cd "${AUTOPKGTEST_TMP}"

echo "==> running the pyhmmer unit tests"
python3 -m unittest pyhmmer.tests -v

echo "==> checking that the extensions use the Debian-packaged libraries"
python3 - <<'EOF'
import subprocess
import pyhmmer.plan7

linked = subprocess.check_output(["ldd", pyhmmer.plan7.__file__], text=True)
for library in ("libhmmer.so.3", "libeasel.so.0"):
    assert library in linked, f"{library} not linked:\n{linked}"
print("plan7 extension is dynamically linked against libhmmer and libeasel")
EOF

echo "==> running a small hmmsearch example"
python3 - <<'EOF'
from importlib.resources import files

import pyhmmer
from pyhmmer.easel import SequenceFile
from pyhmmer.plan7 import HMMFile

data = files("pyhmmer.tests").joinpath("data")
with HMMFile(str(data.joinpath("hmms", "txt", "Thioesterase.hmm"))) as hmm_file:
    hmm = hmm_file.read()
with SequenceFile(str(data.joinpath("seqs", "938293.PRJEB85.HG003687.faa")), digital=True) as seqs_file:
    sequences = seqs_file.read_block()

hits = list(pyhmmer.hmmsearch(hmm, sequences, cpus=1))[0]
assert len(hits) == 1, f"expected 1 hit, got {len(hits)}"
assert hits[0].name == "938293.PRJEB85.HG003687_113"
print(f"hmmsearch found {hits[0].name}: score={hits[0].score:.1f} bits")
EOF
