#!/bin/bash
#SBATCH -p short
#SBATCH -t 0-08:00
#SBATCH --mem 5G
#SBATCH -e mergebam.%j.err

module load conda/miniforge3/24.11.3-0
conda activate /n/groups/neuroduo/Shon/venv/bioenv

############################################################
# Average CREB / IgG replicate BigWigs
############################################################

input_dir="/n/groups/neuroduo/ava/260810_geo_resub/geo/CUTnTag/creb"
output_dir="/n/www/neuroduo.hms.harvard.edu/docroot/bigWig/Ava/221012_ChimpHuman/piN_CREB"

mkdir -p "$output_dir"

# Prevent unmatched globs from being treated as filenames
shopt -s nullglob

############################################################
# Loop through species, cell lines, treatments, antibodies
############################################################

for species in Human Chimp; do

    for line in \
        H1C1b_F10-1 \
        H2C2a_G11-1 \
        H2C2a_G6-2
    do

        for condition in minusKCl 15mKCl; do

            for antibody in CREB IgG; do

                ################################################
                # Find all available replicates for this group
                ################################################

                files=(
                    "$input_dir"/${species}_piN_${line}_${condition}_${antibody}_Rep*.bw
                )

                nfiles=${#files[@]}

                ################################################
                # Output filename
                ################################################

                output="${output_dir}/${species}_piN_${line}_${condition}_${antibody}_merged.bw"

                echo
                echo "============================================================"
                echo "${species} | ${line} | ${condition} | ${antibody}"
                echo "Found ${nfiles} replicate BigWigs"

                ################################################
                # Require at least 2 replicates
                ################################################

                if [[ $nfiles -lt 2 ]]; then
                    echo "WARNING: fewer than 2 replicate files found."
                    echo "Skipping."
                    continue
                fi

                printf '  %s\n' "${files[@]}"
                echo "Output:"
                echo "  $output"

                ################################################
                # Don't overwrite existing output
                ################################################

                if [[ -e "$output" ]]; then
                    echo "WARNING: output already exists. Skipping:"
                    echo "  $output"
                    continue
                fi

                ################################################
                # Average BigWigs
                ################################################

                bigwigAverage \
                    -b "${files[@]}" \
                    -o "$output"

                if [[ $? -eq 0 ]]; then
                    echo "Successfully created:"
                    echo "  $output"
                else
                    echo "ERROR: bigwigAverage failed for this group."
                fi

            done
        done
    done
done

echo
echo "Finished."
