I would like to subset a VCF which only has chromosome 2.

The problem with using various grep commands, e.g.

grep -w '^#\|^2' my.vcf > my_new.vcf

or if there's a 'chr' prefix

grep -w '^#\|^chr2' my.vcf > my_new.vcf

is that this will remove the header.

I've been trying to use vcftools with the following command:

vcftools --vcf input.vcf  --chr 2 --out subset

which does not output a VCF subset.vcf as expected, but rather subset.log:

VCFtools - 0.1.15
(C) Adam Auton and Anthony Marcketta 2009

Parameters as interpreted:
    --vcf input.vcf
    --chr 2
    --out subset

After filtering, kept 2 out of 2 Individuals
After filtering, kept 80 out of a possible 720 Sites
Run Time = 0.00 seconds

Given that the run time is Run Time = 0.00 seconds, I guess there's an error here.

How can I subset a VCF and keep the header? Is there an option with bcftools instead maybe?


3 Answers 3


This seems to work for me with bcftools filter and the -r or -R argument.

   -r, --regions chr|chr:pos|chr:from-to|chr:from-[,...]

Comma-separated list of regions, see also -R, --regions-file. Note that -r cannot be used in combination with -R.

   -R, --regions-file FILE

Regions can be specified either on command line or in a VCF, BED, or tab-delimited file (the default). The columns of the tab-delimited file are: CHROM, POS, and, optionally, POS_TO, where positions are 1-based and inclusive. Uncompressed files are stored in memory, while bgzip-compressed and tabix-indexed region files are streamed. Note that sequence names must match exactly, "chr20" is not the same as "20". Also note that chromosome ordering in FILE will be respected, the VCF will be processed in the order in which chromosomes first appear in FILE. However, within chromosomes, the VCF will always be processed in ascending genomic coordinate order no matter what order they appear in FILE. Note that overlapping regions in FILE can result in duplicated out of order positions in the output. This option requires indexed VCF/BCF files. Note that -R cannot be used in combination with -r.

Here's an example:

$ bcftools filter vcf_nocomp_merge_geno98.vcf.gz -r 4 | head -n 38 | colrm 100 1000000
##FILTER=<ID=PASS,Description="All filters passed">
##INFO=<ID=PR,Number=0,Type=Flag,Description="Provisional reference allele, may not be based on rea
##bcftools_filterCommand=filter -r 4 vcf_nocomp_merge_geno98.vcf.gz
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  1_125   3_5555  4_7565  5_9
4   71566   rs13125929  T   C   .   PASS    PR  GT  0/1 0/1 0/0
4   87113   rs2006748   T   C   .   PASS    PR  GT  0/1 0/0 0/0
4   110646  rs11727494  C   T   .   PASS    PR  GT  0/1 0/0 1/1
4   142550  rs11735742  T   C   .   PASS    PR  GT  0/1 0/0 0/0
4   200631  rs6826124   A   G   .   PASS    PR  GT  0/1 1/1 0/1
4   221623  rs7695945   G   T   .   PASS    PR  GT  0/0 0/0 0/0

You can use grep for this. The problem is the -w flag which means "match entire words", but VCF header lines start with ##, not #. Here are some alternative grep commands:

grep -E '^(#|2[[:space:]])' my.vcf 

Or, if you have GNU grep (default on Linux):

grep -P '^(#|2\t)' my.vcf 

To match either 2 or chr2:

grep -E '^(#|(chr2|2)[[:space:]])' my.vcf 


grep -P '^(#|(chr){0,1}2\t)' my.vcf 

Alternatively, you can use awk:

awk '/^#/ || $1=="2" || $2=="chr2"' my.vcf 

Finally, if you want to use vcftools, you need to also use the --recode option:



These options are used to generate a new file in either VCF or BCF from the input VCF or BCF file after applying the filtering options specified by the user. The output file has the suffix ".recode.vcf" or ".recode.bcf". By default, the INFO fields are removed from the output file, as the INFO values may be invalidated by the recoding

(e.g. the total depth may need to be recalculated if individuals are removed). This behavior may be overriden by the following options. By default, BCF files are written out as BGZF compressed files.

But that will remove the INFO fields as explained above, so just use normal text-parsing tools like the ones I suggested.

  • $\begingroup$ grep (at least the version on Linux) can use multiple -e commands as expressions to search for; lines matching any expression will be displayed. $\endgroup$
    – gringer
    Commented Jan 25, 2018 at 20:25
  • $\begingroup$ @gringer yes, that's POSIX. Personally, I prefer ERE or PCRE syntax over multiple -e, but both work. $\endgroup$
    – terdon
    Commented Jan 25, 2018 at 20:45
  • 1
    $\begingroup$ These are both great answers. I will probably mark @gringer as the accepted as there are less opportunities for readers to make mistakes, but personally, I'm using these grep commands. Thank you! $\endgroup$
    – EB2127
    Commented Jan 26, 2018 at 17:12
  • $\begingroup$ @EB2127 you're very welcome, and please feel very free to mark whichever you prefer. I personally prefer rolling my own parser scripts, but with formats as complex as vcf, it is indeed usually better to use dedicated tools. I would go for gringer's answer as well in your place :) $\endgroup$
    – terdon
    Commented Jan 26, 2018 at 17:33

You can do this more easily with the GATK tool SelectVariants, which allows you to subset variants based on various criteria. Here's the doc page:


For this case (subsetting by chromosome) you would simply do:

gatk SelectVariants \
     -R reference.fasta \
     -V input.vcf \
     -L chr2 \
     -O output.chr2.vcf

If you wanted to do this for several chromosomes, or regions within chromosomes, you would give the tool a file of intervals with -L instead of the chromosome name. Suitable interval file formats are described here:


  • $\begingroup$ In the folder of the command ( ./gatk ), the Fasta index file and the Fasta dict file of the reference.fasta should also be included. $\endgroup$ Commented Sep 3, 2023 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.