#!/usr/bin/env bash
# Convert every BAM in the current directory to bigWig (unnormalized, binSize 50)

# -------- user editable --------
THREADS=8                              
CHROM_SIZES="/BLUES/eric/WGBS/hg38.chrom.sizes"  
# --------------------------------

# check genome file
if [[ ! -f "$CHROM_SIZES" ]]; then
  echo "Chrom-sizes file not found: $CHROM_SIZES"
  exit 1
fi

for bam in *.bam
do
  [[ -e "$bam" ]] || { echo "No BAMs found"; exit 1; }

  base=${bam%.bam}
  bw="${base}.bw"

  echo "▶ Converting $bam  →  $bw"
  bamCoverage \
    --bam "$bam" \
    --outFileName "$bw" \
    --outFileFormat bigwig \
    --binSize 50 \
    --numberOfProcessors "$THREADS" \
    --effectiveGenomeSize $(awk '{sum+=$2} END{print sum}' "$CHROM_SIZES") \
    --normalizeUsing None
done

echo "All done"
