<p>Just use <a href="http://www.htslib.org/doc/bcftools.html#view" rel="noreferrer">bcftools view</a> for filtering:</p>

<pre><code>$ bcftools view -i 'AF&gt;0.3 &amp;&amp; AF&lt;0.7' input.vcf.gz &gt; output.vcf
</code></pre>

<p>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 <code>head -n</code>.</p>

<pre><code>$ 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
</code></pre>

CC BY-SA 4.0
2019-02-08T14:31:12.120Z
2019-02-08T14:31:12.120Z
finswimmer
<p><a href="https://bioinformatics.stackexchange.com/a/6989/298">Using <code>bcftools</code></a> is the right way, but you could also greatly simplify the command you're using. You can do the same thing with a single <code>zgrep</code> call:</p>

<pre><code>zgrep -P '^[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7]' my.vcf.gz
</code></pre>

<p>And to limit to the first 65000 results:</p>

<pre><code>zgrep -m 65000 -P '^[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7]' my.vcf.gz
</code></pre>

<p>Finally, to keep the header of the original vcf unchanged, you could do:</p>

<pre><code>zgrep -m 65000 -P '^(#|[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7])' my.vcf.gz
</code></pre>

<p>I also suggest you use hg19 instead of hg38 data since the best available source for frequencies, the gnomAD database, is based on hg19 and the tool you want to test also provides a site file for hg19. But <a href="https://bioinformatics.stackexchange.com/questions/6988/selecting-65000-snps-where-af-is-close-to-0-5-in-all-or-most-populations#comment10101_6988">that may not be possible</a> for you.</p>

<p>If your VCF file has multiple AF entries for different sub-populations and you want to use those, you'll probably need to script something. We can't really help with that unless you show us some example input lines and start writing the basic script yourself though.</p>

CC BY-SA 4.0
2019-02-08T19:07:02.287Z
I have a different opinion about that. hg19 is outdated sind 2013. Whenever you start something new, you should move to the current reference genome, which is by now hg38. There are lifted over versions of 1000 Genomes, ExAC and gnomAD which provide at least the frequency data by [ensembl](ftp://ftp.ensembl.org/pub/data_files/homo_sapiens/GRCh38/variation_genotype/). As long as the frequency data are given in the INFO column you can easily filter by them using `bcftools`. So there is no need to script something.
finswimmer
CC BY-SA 4.0
2019-02-08T19:10:44.847Z
@finswimmer not sure what you are referring to, but if it's about hg38, I am not saying hg19 is better (it isn't) only that the gnomAD data is based on hg19 and liftover is imperfect. As for the rest, yes of course bcftools is the way to go!
terdon
CC BY-SA 4.0
2019-02-08T14:49:49.247Z
2019-02-08T14:49:49.247Z
terdon
<p>I am evaluating the tool somalier (<a href="https://github.com/brentp/somalier" rel="nofollow noreferrer">https://github.com/brentp/somalier</a>) 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.</p>

<p>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.</p>

<p>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.</p>

<p>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:</p>

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

<p>Any recommendations?</p>

CC BY-SA 4.0
2019-02-08T14:17:36.327Z
If possible, I strongly urge you to use hg19 instead of GRCh38 because there are much better frequency data available. Basically, [GnomAD uses hg19](https://gnomad.broadinstitute.org/faq), so any gnomAD frequencies for hg38 will come from lifting over the positions and that doesn't work too well in my experience.
terdon
CC BY-SA 4.0
2019-02-08T14:24:30.220Z
I haven't been able to reliably liftover the sites.vcf.gz file that can be downloaded from the github.com/brentp/somalier site. I will create another Q for the liftover issue.
719016
CC BY-SA 4.0
2019-02-08T14:33:01.483Z
What do you mean? Somalier's github page says it's for hg19, not hg38: "*where sites is a VCF of variant sites (provided by somalier for hg19).*"
terdon
CC BY-SA 4.0
2019-02-08T14:47:35.960Z
My alignments are in GRCh38, so I need a vcf input file for somalier also in GRCh38, not the one that is in GRCh37 (gh19).
719016
<vcf><snp><genomics><variation>
Selecting 65000 SNPs where AF is close to 0.5 in all or most populations
# Selecting 65000 SNPs where AF is close to 0.5 in all or most populations

(Asked by: username_0 on 2019-02-08)

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?

**Tags:** vcf, snp, genomics, variation
> **Comments:**
> **username_1**: If possible, I strongly urge you to use hg19 instead of GRCh38 because there are much better frequency data available. Basically, [GnomAD uses hg19](https://gnomad.broadinstitute.org/faq), so any gnomAD frequencies for hg38 will come from lifting over the positions and that doesn't work too well in my experience.  
> **username_0**: I haven't been able to reliably liftover the sites.vcf.gz file that can be downloaded from the github.com/brentp/somalier site. I will create another Q for the liftover issue.  
> **username_1**: What do you mean? Somalier's github page says it's for hg19, not hg38: "*where sites is a VCF of variant sites (provided by somalier for hg19).*"  
> **username_0**: My alignments are in GRCh38, so I need a vcf input file for somalier also in GRCh38, not the one that is in GRCh37 (gh19).  

---

## 2 answers

### Answer by username_2 on 2019-02-08. Score: 5

Just use bcftools view for filtering:
$ bcftools view -i 'AF>0.3 && AF<0.7' input.vcf.gz > 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>0.3 && AF<0.7' input.vcf.gz | head -n 65255 > output.vcf


---

### Answer by username_1 on 2019-02-08. Score: 3

Using bcftools is the right way, but you could also greatly simplify the command you're using. You can do the same thing with a single zgrep call:
zgrep -P '^[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7]' my.vcf.gz

And to limit to the first 65000 results:
zgrep -m 65000 -P '^[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7]' my.vcf.gz

Finally, to keep the header of the original vcf unchanged, you could do:
zgrep -m 65000 -P '^(#|[^#]+\t\d+\t\S+\t\w\t\w\t.*;AF=0\.[3-7])' my.vcf.gz

I also suggest you use hg19 instead of hg38 data since the best available source for frequencies, the gnomAD database, is based on hg19 and the tool you want to test also provides a site file for hg19. But that may not be possible for you.
If your VCF file has multiple AF entries for different sub-populations and you want to use those, you'll probably need to script something. We can't really help with that unless you show us some example input lines and start writing the basic script yourself though.

> **Comments:**
> **username_2**: I have a different opinion about that. hg19 is outdated sind 2013. Whenever you start something new, you should move to the current reference genome, which is by now hg38. There are lifted over versions of 1000 Genomes, ExAC and gnomAD which provide at least the frequency data by [ensembl](ftp://ftp.ensembl.org/pub/data_files/homo_sapiens/GRCh38/variation_genotype/). As long as the frequency data are given in the INFO column you can easily filter by them using `bcftools`. So there is no need to script something.  
> **username_1**: @finswimmer not sure what you are referring to, but if it's about hg38, I am not saying hg19 is better (it isn't) only that the gnomAD data is based on hg19 and liftover is imperfect. As for the rest, yes of course bcftools is the way to go!  

---

