/
exac.py
351 lines (291 loc) · 11.8 KB
/
exac.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import json
import os
import pymongo
import gzip
from parsing import get_variants_from_sites_vcf, get_genotype_data_from_full_vcf, \
get_genes_from_gencode_gtf, get_transcripts_from_gencode_gtf, get_exons_from_gencode_gtf, \
get_base_coverage_from_file
import lookups
import xbrowse
from utils import *
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, jsonify
from flask import Response
from collections import defaultdict
app = Flask(__name__)
EXAC_FILES_DIRECTORY = '../exac_test_data/'
# Load default config and override config from an environment variable
app.config.update(dict(
DB_HOST='localhost',
DB_PORT=27017,
DB_NAME='exac',
DEBUG=True,
SECRET_KEY='development key',
SITES_VCF=os.path.join(os.path.dirname(__file__), EXAC_FILES_DIRECTORY, 'sites_file.vcf.gz'),
FULL_VCF=os.path.join(os.path.dirname(__file__), EXAC_FILES_DIRECTORY, 'genotype_data.vcf.gz'),
GENCODE_GTF=os.path.join(os.path.dirname(__file__), EXAC_FILES_DIRECTORY, 'gencode.gtf.gz'),
BASE_COVERAGE_FILES=[
os.path.join(os.path.dirname(__file__), EXAC_FILES_DIRECTORY, 'coverage.txt.gz'),
],
))
import math
app.jinja_env.globals.update(log=math.log)
def connect_db():
"""
Connects to the specific database.
"""
client = pymongo.MongoClient(host=app.config['DB_HOST'], port=app.config['DB_PORT'])
return client[app.config['DB_NAME']]
def load_db():
"""
Load the database
"""
db = get_db()
# Initialize database
# Don't need to explicitly create tables with mongo, just indices
db.variants.remove()
db.variants.ensure_index('xpos')
db.variants.ensure_index('rsid')
db.variants.ensure_index('genes')
db.variants.ensure_index('transcripts')
db.genes.remove()
db.genes.ensure_index('gene_id')
db.genes.ensure_index('gene_name')
db.transcripts.remove()
db.transcripts.ensure_index('transcript_id')
db.transcripts.ensure_index('gene_id')
db.exons.remove()
db.exons.ensure_index('exon_id')
db.exons.ensure_index('transcript_id')
db.exons.ensure_index('gene_id')
db.base_coverage.remove()
db.base_coverage.ensure_index('xpos')
# load coverage first; variant info will depend on coverage
for filepath in app.config['BASE_COVERAGE_FILES']:
size = os.path.getsize(filepath)
progress = xbrowse.utils.get_progressbar(size, 'Parsing coverage: {}'.format(os.path.basename(filepath)))
coverage_file = gzip.open(filepath)
for base_coverage in get_base_coverage_from_file(coverage_file):
progress.update(coverage_file.fileobj.tell())
db.base_coverage.insert(base_coverage)
progress.finish()
# grab variants from sites VCF
sites_vcf = gzip.open(app.config['SITES_VCF'])
size = os.path.getsize(app.config['SITES_VCF'])
progress = xbrowse.utils.get_progressbar(size, 'Loading Variants')
for variant in get_variants_from_sites_vcf(sites_vcf):
db.variants.insert(variant)
progress.update(sites_vcf.fileobj.tell())
progress.finish()
# parse full VCF and append other stuff to variants
full_vcf = gzip.open(app.config['FULL_VCF'])
size = os.path.getsize(app.config['FULL_VCF'])
progress = xbrowse.utils.get_progressbar(size, 'Parsing full VCF')
for genotype_info_container in get_genotype_data_from_full_vcf(full_vcf):
# not the most efficient, but let's keep it simple for now
variant = db.variants.find_one({
'xpos': genotype_info_container['xpos'],
'ref': genotype_info_container['ref'],
'alt': genotype_info_container['alt'],
})
if not variant:
continue # :(
#raise Exception("I didn't find this variant: {}".format(genotype_info_container))
variant['genotype_info'] = genotype_info_container['genotype_info']
db.variants.save(variant)
progress.update(sites_vcf.fileobj.tell())
progress.finish()
# grab genes from GTF
gtf_file = gzip.open(app.config['GENCODE_GTF'])
size = os.path.getsize(app.config['GENCODE_GTF'])
progress = xbrowse.utils.get_progressbar(size, 'Loading Genes')
for gene in get_genes_from_gencode_gtf(gtf_file):
db.genes.insert(gene)
progress.update(gtf_file.fileobj.tell())
progress.finish()
gtf_file.close()
# and now transcripts
gtf_file = gzip.open(app.config['GENCODE_GTF'])
size = os.path.getsize(app.config['GENCODE_GTF'])
progress = xbrowse.utils.get_progressbar(size, 'Loading Transcripts')
for transcript in get_transcripts_from_gencode_gtf(gtf_file):
db.transcripts.insert(transcript)
progress.update(gtf_file.fileobj.tell())
gtf_file.close()
progress.finish()
# Building up gene definitions
gtf_file = gzip.open(app.config['GENCODE_GTF'])
size = os.path.getsize(app.config['GENCODE_GTF'])
progress = xbrowse.utils.get_progressbar(size, 'Loading Exons')
for exon in get_exons_from_gencode_gtf(gtf_file):
db.exons.insert(exon)
progress.update(gtf_file.fileobj.tell())
gtf_file.close()
progress.finish()
def get_db():
"""
Opens a new database connection if there is none yet for the
current application context.
"""
if not hasattr(g, 'db_conn'):
g.db_conn = connect_db()
return g.db_conn
# @app.teardown_appcontext
# def close_db(error):
# """Closes the database again at the end of the request."""
# if hasattr(g, 'db_conn'):
# g.db_conn.close()
@app.route('/')
def homepage():
db = get_db()
return render_template('homepage.html')
@app.route('/autocomplete/<query>')
def awesome_autocomplete(query):
db = get_db()
suggestions = lookups.get_awesomebar_suggestions(db, query)
return Response(json.dumps([{'value': s} for s in suggestions]), mimetype='application/json')
@app.route('/awesome')
def awesome():
db = get_db()
query = request.args.get('query')
datatype, identifier = lookups.get_awesomebar_result(db, query)
if datatype == 'gene':
return redirect('/gene/{}'.format(identifier))
elif datatype == 'transcript':
return redirect('/transcript/{}'.format(identifier))
elif datatype == 'variant':
return redirect('/variant/{}'.format(identifier))
elif datatype == 'region':
return redirect('/region/{}'.format(identifier))
elif datatype == 'dbsnp_variant_set':
return redirect('/dbsnp/{}'.format(identifier))
else:
raise Exception
@app.route('/variant/<variant_str>')
def variant_page(variant_str):
db = get_db()
try:
chrom, pos, ref, alt = variant_str.split('-')
pos = int(pos)
except ValueError:
abort(404)
# pos, ref, alt = get_minimal_representation(pos, ref, alt)
xpos = xbrowse.get_xpos(chrom, pos)
variant = lookups.get_variant(db, xpos, ref, alt)
if variant is None:
variant = {
'chrom': chrom,
'pos': pos,
'xpos': xpos,
'ref': ref,
'alt': alt
}
variant = order_variant_by_csq(variant)
consequences = None
if 'vep_annotations' in variant:
consequences = defaultdict(lambda: defaultdict(list))
for annotation in variant['vep_annotations']:
consequences[csq_max_vep(annotation['Consequence'])][annotation['Gene']].append(annotation)
base_coverage = lookups.get_coverage_for_bases(db, xpos, xpos + len(ref) - 1)
any_covered = any([x['has_coverage'] for x in base_coverage])
return render_template('variant.html', variant=variant, base_coverage=base_coverage, consequences=consequences, any_covered=any_covered)
@app.route('/gene/<gene_id>')
def gene_page(gene_id):
db = get_db()
gene = lookups.get_gene(db, gene_id)
variants_in_gene = lookups.get_variants_in_gene(db, gene_id)
transcripts_in_gene = lookups.get_transcripts_in_gene(db, gene_id)
# Get csome anonical transcript and corresponding info
#transcript_id = get_canonical_transcript()
transcript_id = 'ENST00000296029' # Guaranteed to be canonical transcript
transcript = lookups.get_transcript(db, transcript_id)
variants_in_transcript = lookups.get_variants_in_transcript(db, transcript_id)
exons = lookups.get_exons_in_transcript(db, transcript_id)
genomic_coord_to_exon = dict([(y, i) for i, x in enumerate(exons) for y in range(x['start'], x['stop'] + 1)])
coverage_stats = lookups.get_coverage_for_transcript(db, genomic_coord_to_exon, transcript['xstart'], transcript['xstop'])
add_transcript_coordinate_to_variants(db, variants_in_transcript, transcript_id)
add_consequence_to_variants(variants_in_transcript)
lof_variants = [
x for x in variants_in_gene
if any([y['LoF'] == 'HC' for y in x['vep_annotations'] if y['Gene'] == gene_id])
]
composite_lof_frequency = sum([x['allele_freq'] for x in lof_variants])
return render_template(
'gene.html',
gene=gene,
variants_in_gene=variants_in_transcript,
number_variants_in_gene=len(variants_in_gene),
lof_variants_in_gene=lof_variants,
composite_lof_frequency=composite_lof_frequency,
transcripts_in_gene=transcripts_in_gene,
coverage_stats=coverage_stats,
exons=exons
)
@app.route('/transcript/<transcript_id>')
def transcript_page(transcript_id):
db = get_db()
transcript = lookups.get_transcript(db, transcript_id)
gene = lookups.get_gene(db, transcript['gene_id'])
variants_in_transcript = lookups.get_variants_in_transcript(db, transcript_id)
exons = lookups.get_exons_in_transcript(db, transcript_id)
genomic_coord_to_exon = dict([(y, i) for i, x in enumerate(exons) for y in range(x['start'], x['stop'] + 1)])
coverage_stats = lookups.get_coverage_for_transcript(db, genomic_coord_to_exon, transcript['xstart'], transcript['xstop'])
lof_variants = [
x for x in variants_in_transcript
if any([y['LoF'] == 'HC' for y in x['vep_annotations'] if y['Feature'] == transcript_id])
]
composite_lof_frequency = sum([x['allele_freq'] for x in lof_variants])
add_transcript_coordinate_to_variants(db, variants_in_transcript, transcript_id)
add_consequence_to_variants(variants_in_transcript)
return render_template(
'transcript.html',
transcript=transcript,
variants_in_transcript=variants_in_transcript,
lof_variants=lof_variants,
composite_lof_frequency=composite_lof_frequency,
coverage_stats=coverage_stats,
exons=exons,
gene=gene
)
@app.route('/region/<region_id>')
def region_page(region_id):
db = get_db()
chrom, start, stop = region_id.split('-')
start = int(start)
stop = int(stop)
genes_in_region = lookups.get_genes_in_region(db, chrom, start, stop)
variants_in_region = lookups.get_variants_in_region(db, chrom, start, stop)
xstart = xbrowse.get_xpos(chrom, start)
xstop = xbrowse.get_xpos(chrom, stop)
coverage_array = lookups.get_coverage_for_bases(db, xstart, xstop)
return render_template(
'region.html',
genes_in_region=genes_in_region,
variants_in_region=variants_in_region,
chrom=chrom,
start=start,
stop=stop,
coverage=coverage_array
)
@app.route('/dbsnp/<rsid>')
def dbsnp_page(rsid):
db = get_db()
variant = lookups.get_variants_by_rsid(db, rsid)
print variant
return render_template(
'region.html',
rsid=rsid,
variants_in_region=variant,
coverage=None,
genes_in_region=None
)
@app.route('/howtouse')
def howtouse_page():
return render_template('howtouse.html')
@app.route('/downloads')
def downloads_page():
return render_template('downloads.html')
@app.route('/contact')
def contact_page():
return render_template('contact.html')
if __name__ == "__main__":
app.run()