-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcvr_split_filter.py
executable file
·127 lines (111 loc) · 5.22 KB
/
cvr_split_filter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Split the Mastermind Cited Variants Reference (CVR) VCF file into separate
VCF files per variant category, and filter out more complex Multiple Nucleotide
Variants (MNVs) with only protein-level citations in the literature.
Usage:
Run the file with the path to the input CVR VCF file on the local filesystem:
./cvr_split_filter.py "/path/to/mastermind_cited_variants_reference-grch37.vcf"
This will produce several separate gzip-compressed VCF output files.
"""
import sys
import re
import gzip
import fnmatch
def open_file(filename):
if fnmatch.fnmatch(filename, '*.gz'):
with gzip.open(filename, "r") as lines:
parse(lines, filename)
else:
with open(filename, "r") as lines:
parse(lines, filename)
def parse(lines, filename):
readlines = False
split = {'non-coding': [], 'coding': [], 'substitution-snvs-cdna': [], 'substitution-snvs-protein': [], 'substitution-mnvs-cdna': [], 'substitution-mnvs-protein': [], 'other': []}
split_substitution_mnv_possibilities = {}
header_lines = []
i = 0
for line in lines:
if readlines:
i+=1
# Only process first 1000 lines of VCF file for faster debugging
# if i > 10000:
# break
out = re.split(r'\t', line)
chrom, pos, ref, alt, data = out[0], out[1], out[3], out[4], out[7]
if ref == '.':
ref = ''
if alt == '.':
alt = ''
data = re.split(';', data)
mmcnt1 = int(re.split('=', data[2])[1])
mmcnt2 = int(re.split('=', data[3])[1])
mmcnt3 = int(re.split('=', data[4])[1])
mmid3 = re.split('=', data[-2])[1].rstrip()
mmid3_list = re.split(',', mmid3)
hgvs = re.split('=', data[0])[1].rstrip()
gene = mmid3.split(':', 1)[0]
if re.search(r":.*fs(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*dup(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*delins(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*del(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*ins(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*inv(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":.*ext(,|$)", mmid3, flags=re.IGNORECASE):
split['coding'].append(line)
elif re.search(r":[ARNDCQEGHILKMFPSTWYVXMU]\d+[ARNDCQEGHILKMFPSTWYVXMU](,|$)", mmid3, flags=re.IGNORECASE):
if len(ref) == 1:
if mmcnt1 > 0:
split['substitution-snvs-cdna'].append(line)
else:
split['substitution-snvs-protein'].append(line)
else:
if mmid3 in split_substitution_mnv_possibilities:
split_substitution_mnv_possibilities[mmid3].append([ref, mmcnt1, line])
else:
split_substitution_mnv_possibilities[mmid3] = [[ref, mmcnt1, line]]
elif re.search(r":.*sa(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*sd(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*3utr(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*5utr(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*int(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*ugv(,|$)", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
elif re.search(r":.*multi-intron", mmid3, flags=re.IGNORECASE):
split['non-coding'].append(line)
else:
split['other'].append(line)
else:
header_lines.append(line)
if line.find("#CHROM") == 0:
readlines = True
for mmid3, values in split_substitution_mnv_possibilities.iteritems():
min_ref = min(map(lambda x: len(x[0]), values))
for ref, mmcnt1, line in values:
if mmcnt1 > 0:
split['substitution-mnvs-cdna'].append(line)
elif len(ref) == min_ref:
split['substitution-mnvs-protein'].append(line)
for cat, lines in split.iteritems():
if len(lines) > 0:
print cat + ': ' + str(len(lines))
output_file_path = re.sub(r"\.vcf(\.gz)?$", "." + cat + ".vcf.gz", filename)
print("\tSaving annotations to " + output_file_path)
with gzip.open(output_file_path, 'wb') as output_file:
output_file.write(''.join(header_lines))
output_file.write(''.join(lines))
def main(args):
open_file(args[-1])
if __name__ == "__main__":
main(sys.argv)