#!/bin/bash
#SBATCH --job-name=r2_clock
#SBATCH --partition=datamonkey
#SBATCH --cpus-per-task=16
#SBATCH --mem=48G
#SBATCH --time=04:00:00
#SBATCH --output=/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION/slurm_logs/clock_%A_%a.out
#SBATCH --error=/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION/slurm_logs/clock_%A_%a.err
#
# R2: chronaeon date per-host HA molecular clock — tests PEER_REVIEW M4/m5.
# Reassortment RATES are scaled by per-host clock rates; if our clocks disagree with the
# authors' TreeTime rates, the avian:swine rate ordering ("reassortment-dominant in birds") may shift.
# --no-tree (TN93) for consistency with R1 and because supplied trees have ~2x extra tips.

REPRO="/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION"
HOSTS=(na_avian eurasian_avian swine human equine canine)
CHRONAEON="/home/sweaver/.local/bin/chronaeon"

HOST="${HOSTS[$(( SLURM_ARRAY_TASK_ID - 1 ))]}"
[ -z "$HOST" ] && { echo "no host for task ${SLURM_ARRAY_TASK_ID}"; exit 0; }
ALN="${REPRO}/data/ha/${HOST}.fasta"
OUT_JSON="${REPRO}/results/clock_ha_${HOST}.json"
OUT_CSV="${REPRO}/results/clock_ha_${HOST}.csv"
echo "[$(date '+%F %T')] host=$(hostname) clock host=${HOST}"
if [ -s "$OUT_JSON" ]; then echo "[skip] exists"; exit 0; fi

"${CHRONAEON}" date -a "${ALN}" --no-tree \
  --date-regex '[0-9]{4}(-[0-9]{2}-[0-9]{2})?' \
  --method all --clock-model auto --bootstrap 1000 --ci-method residual-boot --loocv \
  --cpu -o "${OUT_JSON}" -c "${OUT_CSV}"
echo "[$(date '+%F %T')] exit=$? -> ${OUT_JSON}"
exit 0
