#!/bin/bash
#SBATCH --job-name=r2b_autoclock
#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/autoclock_%A_%a.out
#SBATCH --error=/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION/slurm_logs/autoclock_%A_%a.err
#
# R2b: chronaeon AUTOCLOCK — multi-clock community deconvolution per host HA.
# Replaces R2 single-clock, which produced nonsense on pooled avian sublineages (tMRCA 1332 CE, sub-influenza rates).
# autoclock splits each host into clock communities and dates each separately (Steven's suggestion).

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}.inframe.fasta"   # frame-2 corrected HA
OUT_JSON="${REPRO}/results/autoclock_ha_${HOST}.json"
OUT_CSV="${REPRO}/results/autoclock_ha_${HOST}.csv"
OUTDIR="${REPRO}/results/clock_communities_${HOST}"
echo "[$(date '+%F %T')] host=$(hostname) autoclock host=${HOST}"
if [ -s "$OUT_JSON" ] && [ "$HOST" != "na_avian" ]; then echo "[skip] exists"; exit 0; fi

"${CHRONAEON}" autoclock -a "${ALN}" \
  --date-regex '[0-9]{4}(-[0-9]{2}-[0-9]{2})?' --max-k 6 --manifold auto \
  --output-dir "${OUTDIR}" -o "${OUT_JSON}" -c "${OUT_CSV}"
echo "[$(date '+%F %T')] exit=$? -> ${OUT_JSON}"
exit 0
