-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexac_parser2.py
More file actions
238 lines (181 loc) · 7.31 KB
/
exac_parser2.py
File metadata and controls
238 lines (181 loc) · 7.31 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'''
Missense variant miner:
find missense variants in ExAC
returns csv file.
'''
from __future__ import print_function
import sys, traceback, subprocess, gzip, tarfile, os, signal
import pandas as pd
import csv
from pandas import DataFrame
import zipfile
from collections import OrderedDict
import collections
import string
import json
import pdb
VCF_HEADER = ['CHROM', 'POS', 'ID', 'REF', 'ALT', 'QUAL', 'FILTER', 'INFO']
Chr = sys.argv[1]
ENST = sys.argv[2]
OUT_FILE = "ExAC_OUT.csv"
# Sets transcript ID to search in dataframe
# Chr = "17"
#
# ENST = "ENST00000262410" #MAPT
#OUT_FILE = "MAPT_ExACScores.csv"
# change directory to working with Data
#os.chdir("../Data/")
cwd = os.getcwd()
def count_comments(filename):
"""Count comment lines (those that start with "#") in an optionally
gzipped file.
:param filename: An optionally gzipped file.
modified from: https://gist.github.com/slowkow/6215557
"""
comments = 0
# fn_open = gzip.open if filename.endswith('.gz') else open
# with fn_open(filename) as fh:
for line in filename:
if line.startswith('#'):
comments += 1
#print(line)
else:
break
return comments
def parse(line):
"""Parse a single VCF line and return an OrderedDict.
modified from: https://gist.github.com/slowkow/6215557
"""
result = OrderedDict()
fields = line.rstrip().split('\t')
# Read the values in the first seven columns.
for i, col in enumerate(VCF_HEADER[:7]):
result[col] = _get_value(fields[i])
# INFO field consists of "key1=value;key2=value;...".
infos = fields[7].split(';')
for i, info in enumerate(infos, 1):
# info should be "key=value".
try:
key, value = info.split('=')
# But sometimes it is just "value", so we'll make our own key.
except ValueError:
key = 'INFO{}'.format(i)
value = info
# Set the value to None if there is no value.
result[key] = _get_value(value)
return result
def search_exac(Chr, ENST, OUT_FILE):
"""Open an optionally gzipped VCF file and generate an OrderedDict for
each line.
Modiefied from: https://gist.github.com/slowkow/6215557
"""
# TODO: see if there is a way to first map chromosomes within file and keep this data in a temp file?
#print('opening file')
#fn_open = gzip.open if filename.endswith('.gz') else open
with open('/work/in/ExAC_data/ExAC.r0.3.1.sites.vep.vcf', 'rt') as fh, open(OUT_FILE, 'w') as csvout: #
a = csv.writer(csvout)
first_row = ('GENE_ID', 'TRANSCRIPT', 'TRANSCRIPT CHANGE', 'PROTEIN CHANGE', 'AA_POS', 'AA_CHANGE', 'MUTATION',
'ALLELE COUNT', 'ALLELE FREQUENCY')
a.writerows([first_row])
print ('opened file')
print('looking for chromosome ', Chr)
#items = list(range(1, 23))
#l = len(items)
FinalResults = OrderedDict()
# printProgress(i, l, prefix = 'Progress:', suffix = 'Complete', barLength = 50)
for good_line in find_good_lines(fh):
# parse data line to find protein ID
# dict item ['CQS'] contains ENSP IDs and mutation ID in tricode
query = parse(good_line)['CSQ']
# search for protein coding variants for POI
if any(ENST in s for s in query):
# get the rest of the data for matching lines
p_good_line = parse(good_line)
for k, e in enumerate(query):
for line in e.splitlines():
if ENST in line and "missense_variant" in line:
AlleleCount = ()
AlleleFrequency = ()
#print(':::::')
# print(p_good_line['AF'])
#print(len(p_good_line['AC']))
if len(p_good_line['AC']) > 1:
#print('AC1', (p_good_line['AC']))
#print('AF1', (p_good_line['AF']))
try:
AlleleCount = sum(list(map(int, p_good_line[
'AC']))) # list added to break object, if iterate dont need list(
AlleleFrequency = sum(list(map(float, p_good_line['AF'])))
except ValueError:
#print("error")
AlleleCount = p_good_line['AC']
AlleleFrequency = float(p_good_line['AF'])
#print('AF2:', p_good_line['AF'])
#print('AC2:', p_good_line['AC'])
else:
AlleleCount = int(p_good_line['AC'])
AlleleFrequency = float(p_good_line['AF'])
match = line.split( '|') # GeneSym:match[3], geneID:match[4], ENST:match[6], ENST+change: match[10], ESNP_change:match [11], AApos:match[14], AA_change: match[15]
# print(match[3], match[6],match[10],match[11],match[14], match[15])
aa_change = match[15].split('/')
variant = [aa_change[0], match[14], aa_change[1]]
#print(variant)
# print(':::::::::::::::::::::::::::::::::::::::::::::::')
row_line = [match[3], match[6], match[10], match[11], match[14], match[15],
''.join(variant), AlleleCount, AlleleFrequency]
print(row_line)
a.writerows([row_line])
# print(FinalResults)
output_dict = json.loads(json.dumps(FinalResults))
with open('ExacDUMP.txt', 'w') as outfile:
json.dump(output_dict, outfile)
def find_good_lines(fh):
#TODO: rewrite to account for Chr vs int variables and to reduce unnecessary searching.
i='0'
#print (fh)
for line in fh:
chrom = line[0:2]
#print (chrom, Chr)
if line.startswith('#'):
continue
if chrom == Chr:
yield line
else:
if line[0:2] != i:
#print (line[0:2])
i = line[0:2]
continue
# if chrom > Chr:
# print ('end')
# break
def _get_value(value):
"""Interpret null values and return ``None``. Return a list if the value
contains a comma.
"""
if not value or value in ['', '.', 'NA']:
return None
if ',' in value:
return value.split(',')
return value
def filterExACoutput(file):
#print('reading file')
df = pd.read_csv(file)
col_names = []
col_names = [i for i in string.printable[:len(df.columns)]]
df.columns = [c.rstrip() for c in df.columns]
#print(col_names)
df.columns = [col_names]
# df[1] = [col_names[1].astype(str)]
# df[]
#print('done')
df = df[df['1'] == "missense_variant"]
df = df[df['3'] == GENE]
df = df[df['6'] == ENST]
df.to_csv('Filtered_Exac_OUT.csv')
#print(df.head())
def main():
search_exac(Chr, ENST, OUT_FILE)
#lines('ExAC.r0.3.1.sites.vep.vcf.gz', Chr)
#filterExACoutput('Exac_parse_OUT.csv')
if __name__ == '__main__':
main()