BCFTOOLS QUERY

Extract custom tabular data from VCF/BCF files using flexible format strings

Example: %CHROM\t%POS\t%REF\t%ALT\n

BCFTOOLS QUERY

Function

bcftools query extracts specific fields from VCF/BCF files and outputs them in a customizable tab-separated format using format tags.


Input Format

  • VCF files (.vcf, .vcf.gz)
  • BCF files (.bcf)
  • Indexed files required for region queries
  • Format string with tags like %CHROM, %POS, %REF, %ALT

Output Format

  • Tab-separated text (TSV)
  • Custom structured tables for R, Python, or Excel

Common Tags

  • %CHROM — chromosome
  • %POS — position
  • %REF — reference allele
  • %ALT — alternate allele
  • %QUAL — quality score
  • %FILTER — filter status
  • %GT — genotype (sample-level)

Applications

  • Generate variant tables for analysis
  • Create genotype matrices
  • Export data for R / Python / Excel
  • Extract region-specific variant data
  • Build custom reports from VCF files

Example Usage

bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\n' input.vcf.gz
bcftools query -s sample1 -f '[%GT]\n' input.vcf.gz
bcftools query -r chr1:1000-5000 -f '%CHROM\t%POS\t%REF\t%ALT\n' input.vcf.gz

Important Notes:
  • Use square brackets for per-sample fields like [ %GT ].
  • VCF/BCF must be indexed for region queries.
  • Use \t for tabs and \n for new lines.
  • Output is best processed using pandas or R.