Skip to content

Instantly share code, notes, and snippets.

@edawson
Forked from arq5x/example.sh
Last active March 5, 2019 20:42
Show Gist options
  • Save edawson/3ceb7c0fae0fae8ed070dfec6a1e667e to your computer and use it in GitHub Desktop.
Save edawson/3ceb7c0fae0fae8ed070dfec6a1e667e 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
# Natural sorts a VCF using linux sort's
# built-in parallelization
[ $# -eq 0 ] && { echo "Sorts a VCF file in natural chromosome order";\
echo "Usage: $0 [my.vcf | my.vcf.gz]"; exit 1;
}
if (zless $1 | grep ^#; zless $1 | grep -v ^# | LC_ALL=C sort -S 80% --parallel=4 -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
# 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