forked from konradjk/exac_browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.py
296 lines (256 loc) · 10.8 KB
/
parsing.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
Utils for reading flat files that are loaded into database
"""
import re
import traceback
import xbrowse
from utils import *
POPS = {
'AFR': 'African',
'AMR': 'Latino',
'EAS': 'East Asian',
'FIN': 'European (Finnish)',
'NFE': 'European (Non-Finnish)',
'SAS': 'South Asian',
'OTH': 'Other'
}
def get_base_coverage_from_file(base_coverage_file):
"""
Read a base coverage file and return iter of dicts that look like:
{
'xpos': 1e9+1,
'mean': 0.0,
'median': 0.0,
'1': 0.0,
'5': 0.0,
'10': 0.0,
'15': 0.0,
'20': 0.0,
'25': 0.0,
'30': 0.0,
'50': 0.0,
'100': 0.0,
}
"""
float_header_fields = ['mean', 'median', '1', '5', '10', '15', '20', '25', '30', '50', '100']
for line in base_coverage_file:
if line.startswith('#'):
continue
fields = line.strip('\n').split('\t')
d = {
'xpos': xbrowse.get_xpos(fields[0], int(fields[1])),
'pos': int(fields[1]),
}
for i, k in enumerate(float_header_fields):
d[k] = float(fields[i+2])
yield d
def get_variants_from_sites_vcf(sites_vcf):
"""
Parse exac sites VCF file and return iter of variant dicts
sites_vcf is a file (gzipped), not file path
"""
vep_field_names = None
for line in sites_vcf:
try:
line = line.strip('\n')
if line.startswith('##INFO=<ID=CSQ'):
vep_field_names = line.split('Format: ')[-1].strip('">').split('|')
if line.startswith('##INFO=<ID=DP_HIST'):
dp_mids = map(float, line.split('Mids: ')[-1].strip('">').split('|'))
if line.startswith('##INFO=<ID=GQ_HIST'):
gq_mids = map(float, line.split('Mids: ')[-1].strip('">').split('|'))
if line.startswith('#'):
continue
# If we get here, it's a variant line
if vep_field_names is None:
raise Exception("VEP_field_names is None. Make sure VCF header is present.")
# This elegant parsing code below is copied from https://github.com/konradjk/loftee
fields = line.split('\t')
info_field = dict([(x.split('=', 1)) if '=' in x else (x, x) for x in re.split(';(?=\w)', fields[7])])
consequence_array = info_field['CSQ'].split(',') if 'CSQ' in info_field else []
annotations = [dict(zip(vep_field_names, x.split('|'))) for x in consequence_array if len(vep_field_names) == len(x.split('|'))]
coding_annotations = [ann for ann in annotations if ann['Feature'].startswith('ENST')]
alt_alleles = fields[4].split(',')
# different variant for each alt allele
for i, alt_allele in enumerate(alt_alleles):
vep_annotations = [ann for ann in coding_annotations if int(ann['ALLELE_NUM']) == i + 1]
# Variant is just a dict
# Make a copy of the info_field dict - so all the original data remains
# Add some new keys that are allele-specific
pos, ref, alt = get_minimal_representation(fields[1], fields[3], alt_allele)
variant = {}
variant['chrom'] = fields[0]
variant['pos'] = pos
variant['rsid'] = fields[2]
variant['xpos'] = xbrowse.get_xpos(variant['chrom'], variant['pos'])
variant['ref'] = ref
variant['alt'] = alt
variant['xstart'] = variant['xpos']
variant['xstop'] = variant['xpos'] + len(variant['alt']) - len(variant['ref'])
variant['variant_id'] = '{}-{}-{}-{}'.format(variant['chrom'], variant['pos'], variant['ref'], variant['alt'])
variant['orig_alt_alleles'] = [
'{}-{}-{}-{}'.format(variant['chrom'], *get_minimal_representation(fields[1], fields[3], x))
for x in alt_alleles
]
variant['site_quality'] = float(fields[5])
variant['filter'] = fields[6]
variant['vep_annotations'] = vep_annotations
variant['allele_count'] = int(info_field['AC_Adj'].split(',')[i])
if not variant['allele_count'] and variant['filter'] == 'PASS': variant['filter'] = 'AC_Adj0' # Temporary filter
variant['allele_num'] = int(info_field['AN_Adj'])
if variant['allele_num'] > 0:
variant['allele_freq'] = variant['allele_count']/float(info_field['AN_Adj'])
else:
variant['allele_freq'] = None
variant['pop_acs'] = dict([(POPS[x], int(info_field['AC_%s' % x].split(',')[i])) for x in POPS])
variant['pop_ans'] = dict([(POPS[x], int(info_field['AN_%s' % x])) for x in POPS])
variant['pop_homs'] = dict([(POPS[x], int(info_field['Hom_%s' % x].split(',')[i])) for x in POPS])
variant['hom_count'] = sum(variant['pop_homs'].values())
if variant['chrom'] in ('X', 'Y'):
variant['pop_hemis'] = dict([(POPS[x], int(info_field['Hemi_%s' % x].split(',')[i])) for x in POPS])
variant['hemi_count'] = sum(variant['pop_hemis'].values())
variant['quality_metrics'] = dict([(x, info_field[x]) for x in METRICS if x in info_field])
variant['genes'] = list({annotation['Gene'] for annotation in vep_annotations})
variant['transcripts'] = list({annotation['Feature'] for annotation in vep_annotations})
if 'DP_HIST' in info_field:
hists_all = [info_field['DP_HIST'].split(',')[0], info_field['DP_HIST'].split(',')[i+1]]
variant['genotype_depths'] = [zip(dp_mids, map(int, x.split('|'))) for x in hists_all]
if 'GQ_HIST' in info_field:
hists_all = [info_field['GQ_HIST'].split(',')[0], info_field['GQ_HIST'].split(',')[i+1]]
variant['genotype_qualities'] = [zip(gq_mids, map(int, x.split('|'))) for x in hists_all]
yield variant
except Exception:
print("Error parsing vcf line: " + line)
traceback.print_exc()
break
def get_canonical_transcripts(canonical_transcript_file):
for line in canonical_transcript_file:
gene, transcript = line.strip().split()
yield gene, transcript
def get_omim_associations(omim_file):
for line in omim_file:
fields = line.strip().split('\t')
if len(fields) == 4:
yield fields
else:
yield None
def get_genes_from_gencode_gtf(gtf_file):
"""
Parse gencode GTF file;
Returns iter of gene dicts
"""
for line in gtf_file:
if line.startswith('#'):
continue
fields = line.strip('\n').split('\t')
if fields[2] != 'gene':
continue
chrom = fields[0][3:]
start = int(fields[3]) + 1 # bed files are 0-indexed
stop = int(fields[4]) + 1
info = dict(x.strip().split() for x in fields[8].split(';') if x != '')
info = {k: v.strip('"') for k, v in info.items()}
gene_id = info['gene_id'].split('.')[0]
gene = {
'gene_id': gene_id,
'gene_name': info['gene_name'],
'gene_name_upper': info['gene_name'].upper(),
'chrom': chrom,
'start': start,
'stop': stop,
'strand': fields[6],
'xstart': xbrowse.get_xpos(chrom, start),
'xstop': xbrowse.get_xpos(chrom, stop),
}
yield gene
def get_transcripts_from_gencode_gtf(gtf_file):
"""
Parse gencode GTF file;
Returns iter of transcript dicts
"""
for line in gtf_file:
if line.startswith('#'):
continue
fields = line.strip('\n').split('\t')
if fields[2] != 'transcript':
continue
chrom = fields[0][3:]
start = int(fields[3]) + 1 # bed files are 0-indexed
stop = int(fields[4]) + 1
info = dict(x.strip().split() for x in fields[8].split(';') if x != '')
info = {k: v.strip('"') for k, v in info.items()}
transcript_id = info['transcript_id'].split('.')[0]
gene_id = info['gene_id'].split('.')[0]
gene = {
'transcript_id': transcript_id,
'gene_id': gene_id,
'chrom': chrom,
'start': start,
'stop': stop,
'strand': fields[6],
'xstart': xbrowse.get_xpos(chrom, start),
'xstop': xbrowse.get_xpos(chrom, stop),
}
yield gene
def get_exons_from_gencode_gtf(gtf_file):
"""
Parse gencode GTF file;
Returns iter of transcript dicts
"""
for line in gtf_file:
if line.startswith('#'):
continue
fields = line.strip('\n').split('\t')
if fields[2] not in ['exon', 'CDS', 'UTR']:
continue
chrom = fields[0][3:]
feature_type = fields[2]
start = int(fields[3]) + 1 # bed files are 0-indexed
stop = int(fields[4]) + 1
info = dict(x.strip().split() for x in fields[8].split(';') if x != '')
info = {k: v.strip('"') for k, v in info.items()}
transcript_id = info['transcript_id'].split('.')[0]
gene_id = info['gene_id'].split('.')[0]
exon = {
'feature_type': feature_type,
'transcript_id': transcript_id,
'gene_id': gene_id,
'chrom': chrom,
'start': start,
'stop': stop,
'strand': fields[6],
'xstart': xbrowse.get_xpos(chrom, start),
'xstop': xbrowse.get_xpos(chrom, stop),
}
yield exon
def get_dbnsfp_info(dbnsfp_file):
"""
Parse dbNSFP_gene file;
Returns iter of transcript dicts
"""
header = dbnsfp_file.next().split('\t')
fields = dict(zip(header, range(len(header))))
for line in dbnsfp_file:
line = line.split('\t')
other_names = line[fields["Gene_old_names"]].split(';') if line[fields["Gene_old_names"]] != '.' else []
if line[fields["Gene_other_names"]] != '.':
other_names.extend(line[fields["Gene_other_names"]].split(';'))
gene_info = {
'gene_name': line[fields["Gene_name"]],
'ensembl_gene': line[fields["Ensembl_gene"]],
'gene_full_name': line[fields["Gene_full_name"]],
'gene_other_names': other_names
}
yield gene_info
def get_snp_from_dbsnp_file(dbsnp_file):
for line in dbsnp_file:
fields = line.split('\t')
rsid = int(fields[0])
chrom = fields[1].rstrip('T')
if chrom == 'PAR': continue
start = int(fields[2]) + 1
snp = {
'xpos': xbrowse.get_xpos(chrom, start),
'rsid': rsid
}
yield snp