How to subset a VCF by chromosome and keep the header? 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 &gt; my_new.vcf


or if there's a 'chr' prefix

grep -w '^#\|^chr2' my.vcf &gt; 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?
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
##fileformat=VCFv4.2
##FILTER=&lt;ID=PASS,Description="All filters passed"&gt;
##fileDate=20171222
##source=PLINKv1.90
##contig=&lt;ID=1,length=249167691&gt;
##contig=&lt;ID=2,length=242695901&gt;
##contig=&lt;ID=3,length=197800245&gt;
##contig=&lt;ID=4,length=190915651&gt;
##contig=&lt;ID=5,length=180666277&gt;
##contig=&lt;ID=6,length=170877445&gt;
##contig=&lt;ID=7,length=159086805&gt;
##contig=&lt;ID=8,length=146293415&gt;
##contig=&lt;ID=9,length=141018424&gt;
##contig=&lt;ID=10,length=135434552&gt;
##contig=&lt;ID=11,length=134938471&gt;
##contig=&lt;ID=12,length=133763353&gt;
##contig=&lt;ID=13,length=115045730&gt;
##contig=&lt;ID=14,length=107285438&gt;
##contig=&lt;ID=15,length=102369712&gt;
##contig=&lt;ID=16,length=90141356&gt;
##contig=&lt;ID=17,length=81006630&gt;
##contig=&lt;ID=18,length=78014583&gt;
##contig=&lt;ID=19,length=59071322&gt;
##contig=&lt;ID=20,length=62906515&gt;
##contig=&lt;ID=21,length=48077813&gt;
##contig=&lt;ID=22,length=51156934&gt;
##contig=&lt;ID=23,length=154847490&gt;
##INFO=&lt;ID=PR,Number=0,Type=Flag,Description="Provisional reference allele, may not be based on rea
##FORMAT=&lt;ID=GT,Number=1,Type=String,Description="Genotype"&gt;
##bcftools_filterVersion=1.2-187-g1a55e45+htslib-1.2.1-256-ga356746
##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