Sorts a BED file karyotypically. After sorting, it checks if the input and output files have the same content.
#!/usr/bin/env bash
#
# Sorts a BED file karyotypically
# File created by Tabea Kischka at Thu Jun 9 11:15:44 CEST 2016
INFILE="$1"
OUTFILE="$2"
cat "$INFILE" | sed 's/chrM/chr0/' | sort -k1,1V -k2,2n -k3,3n | sed 's/chr0/chrM/' > "$OUTFILE"
echo "Done with sorting."
echo "Now checking if both files are still the same."
INFILEMD5=$(sort "$INFILE" | md5)
OUTFILEMD5=$(sort "$OUTFILE" | md5)
if [ "$INFILEMD5" = "$OUTFILEMD5" ]
then
echo "Sorting went fine!"
else
echo "Sorting failed!"
mv "$OUTFILE" "${OUTFILE}.failed"
fi