This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract only first individual from vcf files

Dear all,

I have multiple vcf files like this, each containing 3 individuals. I would like to extract only the first individual from each vcf and save each as separate vcf file. Is this possible in batch without specifing the individual name as I have many vcfs?

#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  NAME1   NAME2   NAME3
chr1    1666251 .   G   A   139 MQ  BRF=0.46;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-143.73,0.0,-79.13:20:99:100:60 0/0:0.0,-12.67,-236.1:12:99:117:2   0/0:0.0,-16.47,-244.0:13:99:129:1
chr1    2408213 .   T   G   164 QD  BRF=0.05;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-21.02,0.0,-55.12:44:99:77:24   0/0:0.0,-13.94,-147.2:5:99:58:0 0/0:0.0,-8.73,-90.8:3:87:37:0
chr1    2408232 .   T   G   122 QD      BRF=0.04;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-16.82,0.0,-33.02:51:99:57:23   0/0:0.0,-10.38,-111.1:9:99:37:0 0/0:0.0,-5.92,-60.6:8:59:25:0

Expected output:

   #CHROM   POS ID  REF ALT QUAL    FILTER  INFO    FORMAT  NAME1
chr1    1666251 .   G   A   139 MQ  BRF=0.46;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-143.73,0.0,-79.13:20:99:100:6
chr1    2408213 .   T   G   164 QD  BRF=0.05;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-21.02,0.0,-55.12:44:99:77:24
chr1    2408232 .   T   G   122 QD      BRF=0.04;FR=0.1667  GT:GL:GOF:GQ:NR:NV  1/0:-16.82,0.0,-33.02:51:99:57:23
vcf

Is the first individual's ID always "NAME1"? Something like this:

bcftools view -s NAME1 filename.vcf.gz

no, that's the problem..

I see, then go with Pierre's solution.

simple:

$ cut -f1-10 -d$'\t' input.vcf

For many:

$  parallel --plus --dry-run  cut -f 1-10 -d\$\'\t\' {} ">" {.}_out.vcf ::: *.vcf

sure, but it could be vcf.gz or bcf and bcftools will fix AC.

user can use zcat for gz.

1 answer

find dir1 dir2 -type f -name "*.vcf.gz" | while read F
do
   bcftools query -l "${F}"| head -n 1 > samples.txt
   bcftools view --samples-file samples.txt -O z -o "${F}.firstsample.vcf.gz" "${F}"
done

thanks, could you please explain a bit the code? what is dir1 dir2 i.e?

dir1 and dir2 are your directories where you have VCFs. Find vcfs, loop through them using while, get the first sample ID, then subset.

Log in to answer this question.