#!/bin/bash
# Script: run_juicer_all.sh
# Description: Run juicer.sh on G1_r1, G1_r2, G2_r1, G2_r2

set -euo pipefail  # exit on error, undefined variable, and pipe failure

# Fixed parameters
GENOME_ID="mm10"
GENOME_FASTA="/data/estsoi/G1_G2_hic/genome/mm10.fa"
JUICER_DIR="/home/estsoi/juicer1.6_compact-main"
THREADS="17"
SITE="none"   # -s none : no restriction enzyme (Hi-C data already digested)

# Base directory containing all sample subdirectories
BASE_DIR="/data/estsoi/G1_G2_hic"

# List of samples
samples=(G1_r1 G1_r2 G2_r1 G2_r2)

# Loop over each sample
for sample in "${samples[@]}"; do
    work_dir="${BASE_DIR}/${sample}"
    
    if [[ ! -d "${work_dir}" ]]; then
        echo "ERROR: Working directory ${work_dir} does not exist. Skipping ${sample}."
        continue
    fi
    
    echo "========================================="
    echo "Starting juicer for sample: ${sample}"
    echo "Working directory: ${work_dir}"
    echo "========================================="
    
    # Run juicer.sh
    juicer.sh \
        -g "${GENOME_ID}" \
        -d "${work_dir}" \
        -s "${SITE}" \
        -z "${GENOME_FASTA}" \
        -D "${JUICER_DIR}" \
        -t "${THREADS}"
    
    if [[ $? -eq 0 ]]; then
        echo "Finished ${sample} successfully."
    else
        echo "ERROR: juicer.sh failed for ${sample}."
        # Optionally exit or continue; here we continue to next sample
    fi
done

echo "All samples processed."
