Debian Codegraph @

scripts/k3000/extract_DP_from_vcf.py

raw symbols SCIP
1 import sys
2 """
3 From a vcf file (arg1) and a valid dataset_id (arg2) this script extracts the max, min and average DP for the give dataset.
4 Returns an error if the dataset_id does not exists in the vcf
5
6 Input format (with one unique dataset, else lines would have one or more additional : GT:DP:PL:AD:HQ fields (eg. 0/1:1727:5117,821,20742:1255,472:73,73).
7 # Comments
8 ecoli2kb 599 5 A C . PASS Ty=SNP;Rk=0;UL=19;UR=19;CL=.;CR=.;Genome=A;Sd=-1 GT:DP:PL:AD:HQ 0/1:1727:5117,821,20742:1255,472:73,73
9 ecoli2kb 749 4 A C . PASS Ty=SNP;Rk=0;UL=19;UR=1721;CL=.;CR=.;Genome=G;Sd=1 GT:DP:PL:AD:HQ 0/1:1717:23527,1401,3352:353,1364:73,73
10 ecoli2kb 649 3 A C . PASS Ty=SNP;Rk=0;UL=19;UR=19;CL=.;CR=.;Genome=T;Sd=1 GT:DP:PL:AD:HQ 0/0:1770:1466,2566,28486:1562,208:73,73
11 ecoli2kb 699 2 A C . PASS Ty=SNP;Rk=0;UL=19;UR=19;CL=.;CR=.;Genome=A;Sd=1 GT:DP:PL:AD:HQ 1/1:1712:30704,3553,410:97,1615:73,73
12 ecoli2kb 549 1 A C . PASS Ty=SNP;Rk=0;UL=19;UR=518;CL=.;CR=.;Genome=A;Sd=-1 GT:DP:PL:AD:HQ 0/1:1657:22184,1236,3525:361,1296:73,73
13
14
15 Output format (stdout):
16 param DP_avg := 1716.6
17 param DP_max := 1770;
18 param DP_min := 1657;
19 """
20
21 def extract_DP(vcf_file_name, dataset_id):
22 if dataset_id < 1:
23 sys.stderr.write("Error: dataset id must be > 0\n")
24 return
25 min=sys.maxsize
26 max=0
27 sum=0
28 nb=0
29
30 with open(vcf_file_name) as vcf_file:
31 # SNP_higher_path_9 22 9 C G . . Ty=SNP;Rk=0;UL=2;UR=3;CL=.;CR=.;Genome=.;Sd=. GT:DP:PL:AD:HQ 1/1:540:9155,939,294:48,492:50,50
32 for line in vcf_file:
33 line = line.strip()
34 if line[0] =="#": continue
35 s_line = line.split()
36 field_id = 8+dataset_id
37 try:
38 GT_DP = s_line[field_id]
39 DP = int(GT_DP.split(":")[1])
40 if DP > max: max=DP
41 if DP < min: min=DP
42 sum+=DP
43 nb+=1
44 except IndexError:
45 sys.stderr.write("Error: no field nb "+str(dataset_id)+" in file "+vcf_file_name+"\n")
46 return
47 print(f"param DP_avg := {int(sum/float(nb))};")
48 print(f"param DP_max := {max};")
49 print(f"param DP_min := {min};")
50
51
52
53 def main(vcf_file_name, dataset_id):
54 '''
55 Extraction of DP min, max and average from a VCf file
56 Usage:
57 python ~/workspace/gatb-discosnp/scripts/k3000/extract_DP_from_vcf.py /discoRes_k_31_c_2_D_0_P_3_b_2_coherent.vcf >> graph_5haplotypes_filtered.dat #once graph_5haplotypes_filtered.dat was created using K3000_gfa_to_dat.py
58 '''
59 extract_DP(vcf_file_name, dataset_id)
60
61
62
63 if __name__ == "__main__":
64 main(sys.argv[1], int(sys.argv[2]))