### Takes a BioR catalog file consisting of 4 columns: chrom, start, end, DATA
### converts string chromosomes to integers (UNKNOWN=>27, MT=>26, M=>26, XY=>25, Y=>24, X=>23)
### then sorts lines by the first 3 columns: chrom, start, end
### converts the integer chromosomes back to strings as needed
### and outputs to STDOUT

file=$1

if [ ! -f "$file" ] ; then
  echo "File does not exist: $file"
  exit 1;
fi

###set -x
cat $file  \
  | sed 's/^UNKNOWN/27/' | sed 's/^MT/26/' | sed 's/^M/26/' | sed 's/^XY/25/' | sed 's/^Y/24/' | sed 's/^X/23/' \
  | sort -k1,1n -k2,2n -k3,3n \
  | sed 's/^27/UNKNOWN/' | sed 's/^26/M/' | sed 's/^25/XY/' | sed 's/^24/Y/' | sed 's/^23/X/' 

