#!/usr/bin/env bash
set -euo pipefail
queries=/data/query-paths.txt
references=/data/reference-paths.txt
test -s "$queries"
test -s "$references"
mkdir fastani-pilot-001
root="$PWD/fastani-pilot-001"
cp "$queries" "$root/queries.txt"
cp "$references" "$root/references.txt"
fastANI -h > "$root/help.txt" 2>&1
/usr/bin/time -v -o "$root/resources.txt" \
  fastANI --ql "$queries" --rl "$references" -t 4 \
  -o "$root/ani.tsv" > "$root/stdout.txt" 2> "$root/run.log"
test -f "$root/ani.tsv"
python3 - "$root" <<'PY'
import math,sys
from pathlib import Path
root=Path(sys.argv[1])
def listing(path):
    rows=path.read_text().splitlines()
    assert rows and all(rows) and len(rows)==len(set(rows))
    return set(rows)
queries=listing(root/'queries.txt');refs=listing(root/'references.txt');seen=set()
with (root/'ani.tsv').open() as f:
    for line in f:
        x=line.rstrip('\n').split('\t');assert len(x)==5
        q,r=x[:2];assert q in queries and r in refs and (q,r) not in seen
        value=float(x[2]);matched,total=map(int,x[3:])
        assert math.isfinite(value) and 0<=value<=100 and 0<=matched<=total and total>0
        seen.add((q,r))
print('reported, not reported',len(seen),len(queries)*len(refs)-len(seen))
PY