#!/bin/bash
#SBATCH --job-name=r1c_memetree
#SBATCH --partition=datamonkey
#SBATCH --cpus-per-task=16
#SBATCH --mem=48G
#SBATCH --time=06:00:00
#SBATCH --output=/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION/slurm_logs/memetree_%A_%a.out
#SBATCH --error=/home/sweaver/programming/axomeme/reviews/moncla_h3nx_hoststrategy_REVIEW/REPRODUCTION/slurm_logs/memetree_%A_%a.err
#
# R1c: TREE-BASED hyphaeon meme on frame-2 HA, using the authors' per-host tree PRUNED to the alignment tips
# (scripts/04_prune_trees.py). Replaces R1b's --no-tree/TN93 pass to resolve the power question left open in
# RESULTS.md: is avian HA genuinely ~0 episodic sites, or was TN93 suppressing power?
# CAVEAT (documented): the .nwk trees are a SHARED per-host backbone (identical across HA/NA/PB1), not
# HA-specific segment trees — so topology is the authors', but HA branch lengths are re-optimized by MEME.
# Separate memetree_ outputs so R1c is COMPARED against R1b (meme_), not overwriting it.

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

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"
TREE="${REPRO}/data/ha/${HOST}.pruned.nwk"
OUT_JSON="${REPRO}/results/memetree_ha_${HOST}.json"
OUT_CSV="${REPRO}/results/memetree_ha_${HOST}.csv"
echo "[$(date '+%F %T')] host=$(hostname) memetree host=${HOST}"
[ -s "$OUT_JSON" ] && [ -s "$OUT_CSV" ] && { echo "[skip] exists"; exit 0; }

"${HYPHAEON}" meme -a "${ALN}" -t "${TREE}" --cpu -o "${OUT_JSON}" -c "${OUT_CSV}"
echo "[$(date '+%F %T')] exit=$? -> ${OUT_CSV}"
exit 0
