<p>I've tried many ways to do this with bcftools but don't think it's possible to exclude a bed file. What you want is</p>
<p><code>bedtools intersect -v -a sample.vcf -b mask.bed -wa -header</code></p>
<p>The <code>-v</code> option gives you only the parts of file a that are <em>not</em> in file b. <code>-wa</code> only keeps entries from file a, and <code>-header</code> preserves the header from file a.</p>

CC BY-SA 4.0
2021-06-15T00:56:05.093Z
2021-06-15T01:11:29.730Z
2021-06-15T01:11:29.730Z
pgcudahy
<p>If you are familiar with Python, check out the <code>pyvcf.VcfFrame.filter_bed()</code> <a href="https://sbslee-fuc.readthedocs.io/en/latest/api.html#fuc.api.pyvcf.VcfFrame.filter_bed" rel="nofollow noreferrer">method</a> I wrote:</p>
<pre><code>from fuc import pyvcf
vf = pyvcf.VcfFrame.from_file('your_vcf.vcf')
filtered_vf = vf.filter_bed('your_bed.bed', opposite=True)
filtered_vf.to_file('filtered.vcf')
</code></pre>

CC BY-SA 4.0
2021-06-10T20:52:45.430Z
2021-06-10T20:59:48.583Z
2021-06-10T20:59:48.583Z
Seung-been Steven Lee
<p>Have a look at <a href="https://samtools.github.io/bcftools/bcftools.html" rel="nofollow noreferrer">BCFtools</a>, which I think does most of the stuff that VCFtools does. I notice there's a <code>--mask</code> option in <code>BCFtools consensus</code>, which may do what you want:</p>
<pre><code>-m, --mask FILE
 BED file or TAB file with regions to be replaced with N
 (the default) or as specified by the next --mask-with
 option. See discussion of --regions-file in Common
 Options for file format details
</code></pre>

CC BY-SA 4.0
2021-06-01T08:32:52.963Z
So just to make sure that I am using the correct command. It'll be:  bcftools consensus --mask A.fa A.vcf.gz > masked_file.vcf. Is this correct?
John
CC BY-SA 4.0
2021-06-01T13:30:35.280Z
@John Sorry, I don't know anything more than what I've read; I have no experience with using masking in BCFtools or VCFtools. Try it and see if it works!
gringer
CC BY-SA 4.0
2021-05-29T10:46:49.000Z
2021-05-29T10:46:49.000Z
gringer
<p>I need to mask all sites in a vcf file flagged by the 1000 Genomes Project as being unfit for population genetic analyses. The sites for all chromosomes are available at:</p>
<p><a href="http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/data_collections/1000_genomes_project/working/20160622_genome_mask_GRCh38/PilotMask/" rel="nofollow noreferrer">1000Genomes masked sites</a></p>
<p>From above mentioned link you can get both bed files and Fasta files.</p>
<p>I wanted to mask all sites using VCFtools but VCFtools is not longer being updated and therefore, can't me used on the my vcf file because you can only VCFtools upto VCF file format v.2. And file format for my file is v.3. Can someone suggest another tool that I can use. Insights will be appreciated.</p>

<vcf><bedtools><vcftools><bcftools><1000genomes>
Masking sites in a vcf file
# Masking sites in a vcf file

(Asked by: username_0 on 2021-05-28)

I need to mask all sites in a vcf file flagged by the 1000 Genomes Project as being unfit for population genetic analyses. The sites for all chromosomes are available at:

[1000Genomes masked sites](http://ftp.1000genomes.ebi.ac.uk/vol1/ftp/data_collections/1000_genomes_project/working/20160622_genome_mask_GRCh38/PilotMask/)

From above mentioned link you can get both bed files and Fasta files.

I wanted to mask all sites using VCFtools but VCFtools is not longer being updated and therefore, can't me used on the my vcf file because you can only VCFtools upto VCF file format v.2. And file format for my file is v.3. Can someone suggest another tool that I can use. Insights will be appreciated.

**Tags:** vcf, bedtools, vcftools, bcftools, 1000genomes

---

## 3 answers

### Answer by username_1 on 2021-06-15. Score: 2

I've tried many ways to do this with bcftools but don't think it's possible to exclude a bed file. What you want is

`bedtools intersect -v -a sample.vcf -b mask.bed -wa -header`

The `-v` option gives you only the parts of file a that are *not* in file b. `-wa` only keeps entries from file a, and `-header` preserves the header from file a.


---

### Answer by username_2 on 2021-06-10. Score: 1

If you are familiar with Python, check out the pyvcf.VcfFrame.filter_bed() method I wrote:
from fuc import pyvcf
vf = pyvcf.VcfFrame.from_file('your_vcf.vcf')
filtered_vf = vf.filter_bed('your_bed.bed', opposite=True)
filtered_vf.to_file('filtered.vcf')


---

### Answer by username_3 on 2021-05-29. Score: 0

Have a look at BCFtools, which I think does most of the stuff that VCFtools does. I notice there's a --mask option in BCFtools consensus, which may do what you want:
-m, --mask FILE
 BED file or TAB file with regions to be replaced with N
 (the default) or as specified by the next --mask-with
 option. See discussion of --regions-file in Common
 Options for file format details

> **Comments:**
> **username_0**: So just to make sure that I am using the correct command. It'll be:  bcftools consensus --mask A.fa A.vcf.gz > masked_file.vcf. Is this correct?  
> **username_3**: @John Sorry, I don't know anything more than what I've read; I have no experience with using masking in BCFtools or VCFtools. Try it and see if it works!  

---

