#!/bin/sh
# The re-test, after registration (plan sections 6 to 8), in two steps:
#
#     sh code/run extract <raw dir> <work dir>                  outside the container
#     sh code/run measure <raw dir> <work dir> <results dir>    every statistic, in the pinned container
#
# `extract` reads public data over HTTPS with the extraction's Python (duckdb, pyliftover, and htslib's tabix; PYTHON
# names it): it copies the coded census's mapping and controls into <work dir>/retest as claims.csv and controls.csv,
# finds each claim's neighbours, reads the outcome studies' statistics and 1000 Genomes' genotypes the re-test needs,
# builds the calibration's loci (<work dir>/calibration), and reads the judged loci's. `measure` builds env/Dockerfile's
# image and, with no network, runs the re-test (retest.py), the calibration's measures and summary on the judged loci,
# and the analyses (analysis.py), which write summary.json, whose planned_claims are section 11's, and claims.csv to
# <results dir>. CODED (the coded census, data/coded by default) and CALIBRATION (the loci the calibration runs on,
# judged by default) let the development set run through the same steps.
set -eu
plan=$(cd "$(dirname "$0")/.." && pwd)
coded=$(cd "${CODED:-$plan/data/coded}" && pwd)
which=${CALIBRATION:-judged}
python=${PYTHON:-python3}
usage="usage: sh code/run extract <raw dir> <work dir> | measure <raw dir> <work dir> <results dir>"
[ $# -ge 3 ] || { echo "$usage" >&2; exit 2; }
step=$1
raw=$(cd "$2" && pwd)
mkdir -p "$3/retest" "$3/calibration"
work=$(cd "$3" && pwd)
case $step in
extract)
    cp "$coded/mapped.csv" "$work/retest/claims.csv"
    cp "$coded/controls.csv" "$work/retest/controls.csv"
    "$python" "$plan/code/extract_retest.py" neighbours "$work/retest/claims.csv" "$work/retest/controls.csv" "$raw" "$work/retest"
    "$python" "$plan/code/extract_retest.py" outcomes "$work/retest" "$raw"
    "$python" "$plan/code/extract_retest.py" genotypes "$work/retest" "$raw"
    "$python" "$plan/code/calibration.py" units "$raw" "$work/calibration"
    "$python" "$plan/code/extract_retest.py" outcomes "$work/calibration/$which" "$raw"
    "$python" "$plan/code/extract_retest.py" genotypes "$work/calibration/$which" "$raw"
    ;;
measure)
    [ $# -ge 4 ] || { echo "$usage" >&2; exit 2; }
    mkdir -p "$4"
    results=$(cd "$4" && pwd)
    docker build --load -q -t mr-audit-env "$plan/env" > /dev/null
    box() {
        docker run --rm --network none -v "$plan:/plan:ro" -v "$coded:/coded:ro" -v "$raw/proteins:/raw/proteins:ro" \
            -v "$work:/work" -v "$results:/results" mr-audit-env python "$@"
    }
    box /plan/code/retest.py /work/retest /raw
    box /plan/code/calibration.py measure /work/calibration /raw "$which"
    box /plan/code/calibration.py summary /work/calibration "$which"
    box /plan/code/analysis.py /coded/claims.json /coded/mapped.csv /work/retest /plan/data/seen_pairs.csv /results \
        "/work/calibration/$which/summary.json"
    ;;
*)
    echo "$usage" >&2
    exit 2
    ;;
esac
