#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"

PYTHON=/home/ubuntu/joulebeat-evolve/outreach/env/bin/python
export OMP_NUM_THREADS=2

mkdir -p data logs figs

SAMPLES="151507 151508 151509 151510 151669 151670 151671 151672 151673 151674 151675 151676"

# 1. Download DLPFC data (idempotent - skips files that already exist)
for s in $SAMPLES; do
  if [ ! -f "data/${s}.h5" ]; then
    curl -s "https://spatial-dlpfc.s3.us-east-2.amazonaws.com/h5/${s}_filtered_feature_bc_matrix.h5" -o "data/${s}.h5" &
  fi
  if [ ! -f "data/${s}_positions.txt" ]; then
    curl -s "https://raw.githubusercontent.com/LieberInstitute/HumanPilot/master/10X/${s}/tissue_positions_list.txt" -o "data/${s}_positions.txt" &
  fi
  while [ "$(jobs -r | wc -l)" -ge 4 ]; do wait -n; done
done
wait
if [ ! -f "data/barcode_level_layer_map.tsv" ]; then
  curl -s "https://raw.githubusercontent.com/LieberInstitute/HumanPilot/master/10X/barcode_level_layer_map.tsv" \
    -o "data/barcode_level_layer_map.tsv"
fi

# 2. Run the full sweep + null-model experiment, writes results.json + logs/*.csv
$PYTHON spatial_domain_experiment.py

# 3. Generate figures from results.json + logs/*.csv
$PYTHON make_figures.py
