This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to create a new VCF 1) filtered by samples, and 2) with only selected fields

I've been trying bcftools

bcftools view -s Sample1: Creates splits out Sample1.

bcftools reheader -s Sample1: Creates a new VCF with only Sample1, and the header, but includes all of the fields.

bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\t%GT\n': Creates a table with just the fields I want, but it drops the header.

bcftools view -s Sample1 20201229_PS-19-000796A8.vcf -f '%CHROM\t%POS\t%REF\t%ALT\t%GT\n': This should work, but surprisingly keeps all of the fields despite the format request. EDIT: I gues the -f flag with view is for filtering not formatting.

The command that would do what I want would theoretically be bcftools reheader -s Sample1 --f '%CHROM\t%POS\t%REF\t%ALT\t%GT\n'

But this is not a valid command and throws an error. The -f switch with reheader serves another purpose.

vcf bcftools

However, it drops the header.

The header was kept on my machine.

$ bcftools view -s S3 in.vcf.gz | grep "#"
##fileformat=VCFv4.2
(...)
#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  S3

Sorry. You are correct, and I edited the question.

bcftools query -f '%CHROM\t%POS\t%REF\t%ALT\t%GT\n' Creates a table with just the fields I want, but it drops the hea

the output of bcftools query is not VCF

Yes. I get that. But I WANT a VCF. There is a -H, --print-header, but it does not actually print the header.

1 answer

2) with only selected fields

what is a field ? an INFO attribute ? You have to use bcftools annotate -x '^INFO/FIELD1,INFO/FIELD2,INFO/FIELD3'

Thanks! You sent me in the right direction.

Aha! Indeed this does seem to do the trick:

bcftools view -s Sample1 file.vcf | bcftools annotate -x INFO,^FORMAT/GT

bcftools view -s Sample1 20180323_1803704618.vcf | bcftools annotate -x INFO,^FORMAT/GT - | egrep -v "^##" | head -n2
#CHROM  POS     ID      REF     ALT     QUAL    FILTER  INFO    FORMAT  Sample1
chrM    150     .       TCT     CCC     281.78  MAC     .       GT      ./.

Thanks Pierre!

Log in to answer this question.