This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to sort VCF "chr1, chr2..."

I have a VCF file ordered by "chr1, chr10, chr11 ...". I would like to sort it to "chr1, chr2, chr3 ..."

I tried Picard's SortVcf but it gives me back the orders "chr1, chr10, chr11 ...". It's not something I want.

Q: What's the easiest way to sort my VCF file by "chr1, chr2, chr3 ..."?

vcf

Picard's SortVcf but it gives me the old orders.

Hi student-t,

Just a moment, I'm getting my crystal ball to find out more about those errors! In all seriousness: please be as informative as possible. We are notoriously bad in reading your mind or your unknown error messages!

Cheers,
Wouter

I've edited my question to address your concerns.

1 answer

One way is to use vcf-sort from vcftools http://vcftools.sourceforge.net/

vcf-sort your.vcf > sorted.vcf

The second way is to use grep and sort:

grep "^#" your.vcf > sorted.vcf && grep -v "^#" your.vcf | \
  sort -V -k1,1 -k2,2n >> sorted.vcf

The first grep select header, the second grep select data, sort sorts by the first column and since it is alphanumerical it sorts it -V in version sort order. Some systems do not have -V in sort, then use

grep "^#" your.vcf > sorted.vcf && grep -v "^#" your.vcf | \
  awk '{tmp=$1;sub(/chr/,"",tmp);print tmp,$0}' | \
  sort -k1,1n -k3,3n | \
  awk '{tmp=$0;sub(/([^ ]+ +){1}/,"",tmp);print tmp}' >> sorted.vcf

By the way do you have chrX and chrY and where you want to have it?

Log in to answer this question.