-
Notifications
You must be signed in to change notification settings - Fork 6
/
vcf_get_val.py
executable file
·73 lines (63 loc) · 2.26 KB
/
vcf_get_val.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
"""Extract values from vcf file
"""
# --- standard library imports
import os, sys
import argparse
# --- third party import
import vcf
def main():
"""main function
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-i", "--vcf",
required="True",
dest="vcf",
help="VCF file to read (- for stdin)")
parser.add_argument("-a", "--all",
dest="ign_filter",
help="Use all, not just passed variants")
choices = ['SNV', 'INDEL']
parser.add_argument("-t", "--type",
dest="type",
choices=choices,
help="Only work on this type of variants (one of %s)" % (','.join(choices)))
parser.add_argument("-v", "--value",
dest="value",
required=True,
help="What to print. Must be 'QUAL'"
" or any field name from vcf INFO.")
args = parser.parse_args()
if args.value == "QUAL":
def extract_func(var):
"""Extract quality value taken case of missing values"""
if not var.QUAL and var.QUAL != 0:
return "."
else:
return var.QUAL
else:
def extract_func(var):
"""Extract value from vcf INFO field"""
val = var.INFO[args.value]
if isinstance(val, list):
return ','.join([str(x) for x in var.INFO[args.value]])
else:
return val
if args.vcf == "-":
vcfreader = vcf.VCFReader(sys.stdin)
else:
assert os.path.exists(args.vcf)
vcfreader = vcf.VCFReader(filename=args.vcf)
for var in vcfreader:
if not args.ign_filter and var.FILTER:
#print "Skipping filtered %s" % str(var)
continue
if args.type == 'INDEL' and not var.is_indel:
#print "Skipping non-INDEL %s" % var
continue
if args.type == 'SNV' and not var.is_snp:
#print "Skipping non-SNV %s " % var
continue
print extract_func(var)
if __name__ == "__main__":
main()