Selecting 65000 SNPs where AF is close to 0.5 in all or most populations I am evaluating the tool somalier (https://github.com/brentp/somalier) and I need to create a list of about 65,000 SNPs where the allele frequency (AF) is as close to 0.5 as possible across the most representative set of populations possible with today's data.

My alignments are in GRCh38, so I need a vcf input file for somalier also in GRCh38, not the one that is available in the somalier website, which is in GRCh37.

I am starting as input with the vcfs from the 1000genomes project in GRCh38 positions, and I would like to come up with a filtering strategy that:
(1) Sorts all SNPs by the AF being close to 0.5 in as many populations as possible.
(2) Takes the first 65,000 SNPs from that list and produces a vcf file from them.

My simplistic strategy so far has been to take the global AF value if it's between 0.3-0.7 and print it if it's a SNP with this perl one-liner:

gunzip -c my.vcf.gz | grep -v '#' | perl -lne 'if ($_ =~ /\;AF=0\.[34567]/) { if ($_ =~ /\w+\t\d+\t\S+\t\w\t\w\t/) {print $_} }'


Any recommendations?
Just use bcftools view for filtering:

$ bcftools view -i 'AF&gt;0.3 &amp;&amp; AF&lt;0.7' input.vcf.gz &gt; output.vcf


To truncate this list to 65,000 SNPs count the header lines, sum them to the number of SNPs you like to have and use head -n.

$ bcftools view -h input.vcf.gz|wc -l
255

$ bcftools view -i 'AF&gt;0.3 &amp;&amp; AF&lt;0.7' input.vcf.gz | head -n 65255 &gt; output.vcf