This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to add INFO field in VCFgz file

Hi I have a vcfgz file having the following header

#CHROM  POS     ID      REF     ALT     QUAL    FILTER  FORMAT  Sample1        Sample2        Sample3

I want to split it by samples but by using various tools it gives error that there is no INFO field. How can i add the INFO field in vcfgz file?

vcf next-gen

2 answers

not tested.

awk '/^##/{print;next;} /^#CHROM/ {for(i=1;i<=NF;i++) {printf("%s%s",(i>1?"\t":""),$i);if(i==7) printf("\tINFO");}printf("\n");next;} {for(i=1;i<=NF;i++) {printf("%s%s",(i>1?"\t":""),$i);if(i==7) printf("\t.");}printf("\n");}' input.vcf

it gives error: awk: read error (Bad address)

read error (Bad address)

it's not a problem with the awk script.

This should work, although you'll still end up with INFO-less vcf files:

for i in 9 10 11; do
 sample=$(zgrep -m1 ^#CHROM multisample.vcf.gz | cut -f$i)
 zgrep ^## multisample.vcf.gz > $sample.vcf
 zgrep -v ^## multisample.vcf.gz | cut -f1-8,$i >> $sample.vcf
done

how to split if i have a list of samples and just want to get those samples i have in a list?

Not tested:

iMaxSample=$(zgrep -m1 ^#CHROM multisample.vcf.gz | wc -w)
zgrep ^## multisample.vcf.gz > header.txt
for i in $(seq 1 $iMaxSample); do
 sample=$(zgrep -m1 ^#CHROM multisample.vcf.gz | cut -f$i)
 if grep -w $sample list.of.samples.txt &>/dev/null; then
  { cat header.txt; zgrep -v ^## multisample.vcf.gz | cut -f1-8,$i; } > $sample.vcf
 fi
done

Again, if you start with a malformed multi-sample vcf, this snippet will generate individual malformed vcf files. I would strongly recommend to repair the original vcf file format (maybe forcing an empty INFO column?) instead of using this code.

Log in to answer this question.