-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvcfAllSiteParser.py
executable file
·59 lines (49 loc) · 1.52 KB
/
vcfAllSiteParser.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
#!/usr/bin/env python
import sys
import gzip
import re
class MaskGenerator:
def __init__(self, filename, chr):
self.lastCalledPos = -1
self.lastStartPos = -1
self.file = gzip.open(filename, "w")
self.chr = chr
# assume 1-based coordinate, output in bed format
def addCalledPosition(self, pos):
if self.lastCalledPos == -1:
self.lastCalledPos = pos
self.lastStartPos = pos
elif pos == self.lastCalledPos + 1:
self.lastCalledPos = pos
else:
self.file.write("{}\t{}\t{}\n".format(self.chr, self.lastStartPos - 1, self.lastCalledPos))
self.lastStartPos = pos
self.lastCalledPos = pos
if len(sys.argv) < 3:
print("too few arguments:")
print("Usage: ./vcfCaller.py <chrom> <mask_out> > <vcf_out>")
print("Reading VCF with all called sites, including hom-ref from stdin")
sys.exit(1)
chr = sys.argv[1]
mask_filename = sys.argv[2]
mask = MaskGenerator(mask_filename, chr)
lastPos = 0
line_cnt = 0
for line in sys.stdin:
if line[0] == '#':
print line,
continue
fields = line.strip().split('\t')
pos = int(fields[1])
refAllele = fields[3]
altAllele = fields[4]
info = fields[7]
genotypes = fields[9]
if line_cnt % 10000 == 0:
sys.stderr.write("parsing position {}\n".format(pos))
line_cnt += 1
if re.match("^[ACTGactg]$", refAllele) and re.match("^[ACTGactg\.]$", altAllele):
if genotypes[0] != '.' and genotypes[2] != '.':
mask.addCalledPosition(pos)
if genotypes[0] == '1' or genotypes[2] == '1':
print line,