Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arq5x
Last active January 24, 2019 13:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save arq5x/ab78b7be4d4f2a989a31 to your computer and use it in GitHub Desktop.
Save arq5x/ab78b7be4d4f2a989a31 to your computer and use it in GitHub Desktop.
Natural sort a VCF
chmod a+x vcfsort.sh
vcfsort.sh trio.trim.vep.vcf.gz
#!/bin/bash
# Faster, but can't handle streams
[ $# -eq 0 ] && { echo "Sorts a VCF file in natural chromosome order";\
echo "Usage: $0 [my.vcf | my.vcf.gz]"; exit 1;
}
# cheers, @michaelhoffman
if (zless $1 | grep ^#; zless $1 | grep -v ^# | LC_ALL=C sort -k1,1V -k2,2n);
then
exit 0
else
printf 'sort failed. Does your version of sort support the -V option?\n'
printf 'If not, you should update sort with the latest from GNU coreutils:\n'
printf 'git clone git://git.sv.gnu.org/coreutils'
fi
#!/bin/bash
# Slower, but handles streams.
[ $# -eq 0 ] && { echo "Sorts a VCF file in natural chromosome order";\
echo "Usage: $0 [my.vcf | my.vcf.gz]"; \
echo "Usage: cat [my.vcf | my.vcf.gz] | $0"; \
exit 1;
}
# cheers, @colbychiang
if zless $1 | awk '$0~"^#" { print $0; next } { print $0 | "LC_ALL=C sort -k1,1V -k2,2n" }';
then
exit 0
else
printf 'sort failed. Does your version of sort support the -V option?\n'
printf 'If not, you should update sort with the latest from GNU coreutils:\n'
printf 'git clone git://git.sv.gnu.org/coreutils'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment