This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Sort VCF File by Position?

Previously, I split out a vcf file by chromosome, and for my project, I have combined the X and XY vcf files into a single one. After changing the "XY" chromosome designation to "X" via:

awk '{gsub(/"XY"/, "X"); print;}' Genome_newX.vcf > Genome_newX2.vcf

I'm running into the issue of sorting this new "Genome_newX2.vcf" by position. The idea is that I'll subsequently run the vcf through GenotypeHarmonizer.

Are there any suggestions on how to do this easily? I'm brand new to this style of work, and I'd love some direction on where to read up on it as well. Thank you!

chromosome vcf

2 answers

Edit: 02/23

Just use bcftools sort https://samtools.github.io/bcftools/bcftools.html#sort


Original answer with awk-fu:

cat in.vcf | awk '$1 ~ /^#/ {print $0;next} {print $0 | "sort -k1,1 -k2,2n"}' > out_sorted.vcf

It takes a VCF and prints the sorted file including the header.

excellent, this solved the problem. I really appreciate!

sort -k1,1 -k2,2n

This works well in your case, as you seem to have just on chromosome. For sorting a vcf file I prefer this:

sort -k1,1V -k2,2n my.vcf

This makes sure that your chromosomes are sorted correctly. WIthout the 'V' "2" comes behind "19" for example.

fin simmer

I do not recommend to use natural sorting on genomic data. Most other tools, e.g. samtools (for sorting bam files) do not support this by default. If you ever do operations like intersections with bedtools on two or more files that require files to be sorted, the different sort orders would/could cause conflict, e.g. bedtools intersect with the -sorted option

Hello ATPoint,

funny. This is exact the same reason why I use natural sorting. :) The data I've worked with (human) was always sorted this way and I got problems it a part in the analyse pipeline wasn't.

fin swimmer

too bad vcf-sort is garbage and the -c flag doesnt work even with the newest version

Would there be an equivalent for a BCF? bcftools view | [...] code? Or why not using bcftools sort -Oz output.bcf -o output_sort.vcf.gz?

bcftools sort is absolute the right way and the way I would go today :)

Ok just to double check here because I may be a chop, but dosnt SortVcf from GATK also just use the header formatting as well?

Thank you! I have struck out my comment about not needing the seq-dict dependency (not sure what I was doing to make me think that) Appreciate it!

Log in to answer this question.