# swap genotypes for specific fields of a vcf file

Is there a tool already established to "swap" the ref/alt information in the GT, AD and PL fields of a vcf file?

For the GT field, I need to transform: 0/0 -> 1/1 ; 1/1 -> 0/0 ; 0/1 -> 1/0 ; and finally 1/0 -> 0/1

For the AD field, I need to swap the columns: x,y -> y,x

For the PL field, I need to swap the columns: x,y,z -> z,y,x

Sample input:

Contig111   11  Contig111_11_T_G    T   G   4278.22 PASS    AC=8;AF=0.073;AN=110;BaseQRankSum=1.35;DP=1176;ExcessHet=0.1448;FS=1.841;InbreedingCoeff=0.1904;QD=13.41;ReadPosRankSum=0.351;set=Intersection  GT:AD:DP:GQ:PL  0/0:17,0:17:48:0,48,720 0/1:14,8:22:99:167,0,347 .....


Sample output:

    Contig111   11  Contig111_11_T_G    T   G   4278.22 PASS    AC=8;AF=0.073;AN=110;BaseQRankSum=1.35;DP=1176;ExcessHet=0.1448;FS=1.841;InbreedingCoeff=0.1904;QD=13.41;ReadPosRankSum=0.351;set=Intersection  GT:AD:DP:GQ:PL  1/1:0,17:17:48:720,48,0 1/0:8,14:22:99:347,0,167 .....


note: the same operations will need to be carried out for all samples if the VCF file has multiple samples (the ... stand for other potential samples).

Before I dive into scripting ( basically playing with regex), I would like to know if something to process this is already available. I looked into bcftools and fixref but they don't seem to help for this.

• What do you mean by "note: the ... stand for other columns to be swapped as well,"? You mean carry out the same operations on the GT, AD and PL of any other samples in the VCF file? – terdon Jul 3 '19 at 13:54
• yes, better wording here! thanks – aechchiki Jul 3 '19 at 13:54
• How generic does this need to be? I mean, is it enough to give you a solution that works specifically for the order of fields you have (GT:AD:DP:GQ:PL) or do we also need to be able to cope with different orders (so need to infer the position of the AD field from the INFO field)? – terdon Jul 3 '19 at 13:56
• for now, a solution for the order of fields I have is okay. I don't have a lot of experience with vcf file, and I thought that the order of fields was "fixed" for all vcfs :9 – aechchiki Jul 3 '19 at 13:58

If we can always assume your fields will be GT:AD:DP:GQ:PL, you can use this relatively simple, brute approach awk script:

awk -vOFS="\t" -F"\t" '{
## just print the header lines
if(/^#/){
print;
}
else{
## The number of samples will be the extra number of fields
## in addition to the default VCF 9
sampleNum = NF - 9
## Fix each sample
for(i=1; i<=sampleNum; i++){
## get the fields
num=split($$(9+i), fields,":"); ## genotype if(fields[1] ~ "0/0"){ fields[1] = "1/1"} else if(fields[1] == "1/1"){ fields[1] = "0/0" } else if(fields[1] == "0/1"){ fields[1] = "1/0" } else if(fields[1] == "1/0"){ fields[1] = "0/1" } ## AD split(fields[2],values,",") fields[2] = values[2]","values[1] ## PL split(fields[2],values,",") ## Now set these as the sample data$$(9+i) = fields[1];
for(k=2;k<=num;k++){
$$(9+i) =$$(9+i)":"fields[k]
}
}
print;
}
}' file.vcf


On your example input, this returns:

Contig111   11  Contig111_11_T_G    T   G   4278.22 PASS    AC=8;AF=0.073;AN=110;BaseQRankSum=1.35;DP=1176;ExcessHet=0.1448;FS=1.841;InbreedingCoeff=0.1904;QD=13.41;ReadPosRankSum=0.351;set=Intersection  GT:AD:DP:GQ:PL  1/1:0,17:17:48:0,48,720 1/0:8,14:22:99:167,0,347    .....

• thanks @terdon, I guess this means we don't really have a software able to handle it and I'll need to dive into scripting :) your solution works so i'll accept it, although I'd prefer to use some available software in this case. – aechchiki Jul 4 '19 at 7:34
• @aechchiki yeah, I doubt there will be software for this since you are doing something that is very much not standard: you are swapping data around between different alleles. – terdon Jul 4 '19 at 8:11