Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-08T14:31:12.120Z
Answers.LastActivityDate: 2019-02-08T14:31:12.120Z
Answers.UserDisplayName: finswimmer
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-08T19:07:02.287Z
Answers.Text: 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.
Answers.UserDisplayName: finswimmer
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-08T19:10:44.847Z
Answers.Text: @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!
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-08T14:49:49.247Z
Answers.LastActivityDate: 2019-02-08T14:49:49.247Z
Answers.UserDisplayName: terdon
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-08T14:17:36.327Z
Comments.Text: 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.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-08T14:24:30.220Z
Comments.Text: 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.
Comments.UserDisplayName: 719016
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-08T14:33:01.483Z
Comments.Text: 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).*"
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-02-08T14:47:35.960Z
Comments.Text: 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).
Comments.UserDisplayName: 719016
Tags: <vcf><snp><genomics><variation>
Title: Selecting 65000 SNPs where AF is close to 0.5 in all or most populations
ThreadText: # 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](http://www.htslib.org/doc/bcftools.html#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`](https://bioinformatics.stackexchange.com/a/6989/298) 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](https://bioinformatics.stackexchange.com/questions/6988/selecting-65000-snps-where-af-is-close-to-0-5-in-all-or-most-populations#comment10101_6988) 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**: @username_2 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!  
  
  ---
  
  