#!/bin/bash

for file in *.bed
do
  [ -e "$file" ] || { echo "No .bed files found in current folder"; exit 1; }

  echo "Sorting $file"
  sort -k1,1 -k2,2n "$file" > "${file}.sorted"

  echo "Compressing ${file}.sorted"
  bgzip -f "${file}.sorted"

  echo "Indexing ${file}.sorted.gz"
  tabix -p bed "${file}.sorted.gz"
done

echo "All BED files sorted, compressed and indexed."

