forked from jeromeku/api-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
366 lines (301 loc) · 12.2 KB
/
main.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file serves two main purposes
- it serves up the main html page
- and it provides a simple set of apis to the javascript
"""
import httplib2
import time
import jinja2
import json
import logging
import os
import re
import socket
import webapp2
from oauth2client import appengine
from google.appengine.api import users
socket.setdefaulttimeout(60)
http = httplib2.Http(timeout=60)
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True,
extensions=['jinja2.ext.autoescape'])
client_secrets = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
decorator = appengine.oauth2decorator_from_clientsecrets(
client_secrets,
scope=[
'https://www.googleapis.com/auth/genomics',
])
# TODO: Dataset information should come from the list datasets api call
# But that call is not in the GA4GH API yet
SUPPORTED_BACKENDS = {
'GOOGLE' : {
'name': 'Google',
'url': 'https://www.googleapis.com/genomics/v1beta2/%s?%s',
'supportsPartialResponse': True,
'datasets': {'1000 Genomes': '10473108253681171589',
'Platinum Genomes': '3049512673186936334',
'DREAM SMC Challenge': '337315832689',
'PGP': '383928317087',
'Simons Foundation' : '461916304629'}
}
}
class ApiException(Exception):
pass
# Request handlers
class BaseRequestHandler(webapp2.RequestHandler):
def handle_exception(self, exception, debug_mode):
if isinstance(exception, ApiException):
# ApiExceptions are expected, and will return nice error
# messages to the client
self.response.write(exception.message)
self.response.set_status(400)
else:
# All other exceptions are unexpected and should be logged
logging.exception('Unexpected exception')
self.response.write('Unexpected internal exception')
self.response.set_status(500)
def get_backend(self):
backend = self.request.get('backend')
if not backend:
raise ApiException('Backend parameter must be set')
return backend
def supports_name_filter(self):
return SUPPORTED_BACKENDS[self.get_backend()].has_key('supportsNameFilter')
def supports_partial_response(self):
return SUPPORTED_BACKENDS[self.get_backend()]\
.has_key('supportsPartialResponse')
def get_base_api_url(self):
return SUPPORTED_BACKENDS[self.get_backend()]['url']
def get_content(self, path, method='POST', body=None, params=''):
http = decorator.http()
uri= self.get_base_api_url() % (path, params)
startTime = time.clock()
response, content = http.request(
uri,
method=method, body=json.dumps(body) if body else None,
headers={'Content-Type': 'application/json; charset=UTF-8'})
contentLen = len(content)
try:
content = json.loads(content)
except ValueError:
logging.error('while requesting {}'.format(uri))
logging.error('non-json api content %s' % content[:1000])
raise ApiException('The API returned invalid JSON')
if response.status >= 300:
logging.error('error api response %s' % response)
logging.error('error api content %s' % content)
if 'error' in content:
raise ApiException(content['error']['message'])
else:
raise ApiException('Something went wrong with the API call!')
logging.info('get_content {}: {}kb {}s'
.format(uri, contentLen/1024, time.clock() - startTime))
return content
def write_response(self, content):
self.response.headers['Content-Type'] = "application/json"
self.response.write(json.dumps(content))
def write_content(self, path, method='POST', body=None, params=''):
self.write_response(self.get_content(path, method, body, params))
class SetSearchHandler(BaseRequestHandler):
def write_read_group_sets(self, dataset_id, name):
self.write_content('readgroupsets/search',
body={'datasetIds': [dataset_id], 'name': name},
params='fields=readGroupSets(id,name)')
def write_call_sets(self, dataset_id, name):
variant_sets = self.get_content('variantsets/search',
body={ 'datasetIds' : [dataset_id]})
variant_set_id = variant_sets['variantSets'][0]['id']
self.write_content('callsets/search',
body={'variantSetIds': [variant_set_id], 'name': name},
params='fields=callSets(id,name)')
def write_read_group_set(self, set_id):
set = self.get_content('readgroupsets/%s' % set_id, method='GET')
# For read group sets, we also load up the reference set data
reference_set_id = set.get('referenceSetId') or \
set['readGroups'][0].get('referenceSetId')
if not reference_set_id:
# TODO: Get coverage API added to GA4GH
buckets = self.get_content('readgroupsets/%s/coveragebuckets' % set_id,
method='GET')
set['references'] = [{'name': b['range']['referenceName'],
'length': b['range']['end']}
for b in buckets['coverageBuckets']]
else:
# TODO: Get search by refSetId added to GA4GH
references = self.get_content('references/search',
body={'referenceSetId': [reference_set_id]},
params='fields=references(name,length)')
set['references'] = references['references']
self.response.write(json.dumps(set))
def write_call_set(self, set_id):
set = self.get_content('callsets/%s' % set_id, method='GET')
# For call sets, we also load up the variant set data to get
# the available reference names and lengths
variant_set_id = set['variantSetIds'][0]
variant_set = self.get_content('variantsets/%s' % variant_set_id,
method="GET")
# TODO: Get variantset.refSetId added to GA4GH
set['references'] = [{'name': b['referenceName'],
'length': b['upperBound']}
for b in variant_set['referenceBounds']]
self.response.write(json.dumps(set))
@decorator.oauth_aware
def get(self):
use_callsets = self.request.get('setType') == 'CALLSET'
set_id = self.request.get('setId')
if set_id:
if use_callsets:
self.write_call_set(set_id)
else:
self.write_read_group_set(set_id)
else:
dataset_id = self.request.get('datasetId')
name = self.request.get('name')
try:
if use_callsets:
self.write_call_sets(dataset_id, name)
else:
self.write_read_group_sets(dataset_id, name)
except:
self.response.write('{}')
class ReadSearchHandler(BaseRequestHandler):
@decorator.oauth_aware
def get(self):
body = {
'readGroupSetIds': self.request.get('setIds').split(','),
'referenceName': self.request.get('sequenceName'),
'start': max(0, int(self.request.get('sequenceStart'))),
'end': int(self.request.get('sequenceEnd')),
}
readFields = self.request.get('readFields')
params = ''
if readFields and self.supports_partial_response():
params = 'fields=nextPageToken,alignments(%s)' % readFields
body['pageSize'] = 1024
pageToken = self.request.get('pageToken')
if pageToken:
body['pageToken'] = pageToken
content = self.get_content('reads/search', body=body, params=params)
# Emulate support for partial responses by supplying only the
# requested fields to the client.
if readFields and not self.supports_partial_response():
fields = readFields.split(',')
def filterKeys(dict, keys):
return { key: dict[key] for key in keys }
newReads = [ filterKeys(read, fields) for read in content['alignments']]
content['alignments'] = newReads
self.write_response(content)
class VariantSearchHandler(BaseRequestHandler):
@decorator.oauth_aware
def get(self):
body = {
'callSetIds': self.request.get('setIds').split(','),
'referenceName': self.request.get('sequenceName'),
'start': max(0, int(self.request.get('sequenceStart'))),
'end': int(self.request.get('sequenceEnd')),
'pageSize': 100
}
pageToken = self.request.get('pageToken')
if pageToken:
body['pageToken'] = pageToken
self.write_content('variants/search', body=body)
class BaseSnpediaHandler(webapp2.RequestHandler):
def getSnppediaPageContent(self, snp):
uri = "http://bots.snpedia.com/api.php?action=query&prop=revisions&" \
"format=json&rvprop=content&titles=%s" % snp
response, content = http.request(uri=uri)
page_id, page = json.loads(content)['query']['pages'].popitem()
return page['revisions'][0]['*']
def getContentValue(self, content, key):
try:
matcher = '%s=(.*)\n' % key
return re.search(matcher, content, re.I).group(1)
except (KeyError, AttributeError):
return ''
def complement(self, base):
return {'A': 'T', 'T': 'A', 'G': 'C', 'C': 'G'}[base]
class SnpSearchHandler(BaseSnpediaHandler):
def getSnpResponse(self, name, content):
return {
'name': name,
'link': 'http://www.snpedia.com/index.php/%s' % name,
'position': self.getContentValue(content, 'position'),
'chr': self.getContentValue(content, 'chromosome')
}
def get(self):
snp = self.request.get('snp')
try:
content = self.getSnppediaPageContent(snp)
if snp[:2].lower() == 'rs':
snps = [self.getSnpResponse(snp, content)]
else:
# Try a gene format
snps = re.findall('\[\[(rs\d+?)\]\]', content, re.I)
snps = [self.getSnpResponse(s, self.getSnppediaPageContent(s))
for s in set(snps)]
except (ValueError, KeyError, AttributeError):
snps = []
self.response.write(json.dumps({'snps' : snps}))
class AlleleSearchHandler(BaseSnpediaHandler):
def getAlleleResponse(self, name, content):
return {
'name': name,
'link': 'http://www.snpedia.com/index.php/%s' % name,
'repute': self.getContentValue(content, 'repute'),
'summary': self.getContentValue(content, 'summary') or 'Unknown',
'magnitude': self.getContentValue(content, 'magnitude')
}
def get(self):
snp = self.request.get('snp')
a1 = self.request.get('a1')
a2 = self.request.get('a2')
a1c = self.complement(a1)
a2c = self.complement(a2)
possible_names = [(snp, a1, a2), (snp, a2, a1),
(snp, a1c, a2c), (snp, a2c, a1c)]
for name in possible_names:
try:
page = "%s(%s;%s)" % name
content = self.getSnppediaPageContent(page)
self.response.write(json.dumps(self.getAlleleResponse(page, content)))
return
except (ValueError, KeyError, AttributeError):
pass # Continue trying the next allele name
self.response.write(json.dumps({}))
class MainHandler(webapp2.RequestHandler):
@decorator.oauth_aware
def get(self):
if decorator.has_credentials():
template = JINJA_ENVIRONMENT.get_template('main.html')
self.response.write(template.render({
'backends': SUPPORTED_BACKENDS,
'username': users.User().nickname(),
'logout_url': users.create_logout_url('/'),
}))
else:
template = JINJA_ENVIRONMENT.get_template('grantaccess.html')
self.response.write(template.render({
'url': decorator.authorize_url()
}))
web_app = webapp2.WSGIApplication(
[
('/', MainHandler),
('/api/reads', ReadSearchHandler),
('/api/variants', VariantSearchHandler),
('/api/sets', SetSearchHandler),
('/api/snps', SnpSearchHandler),
('/api/alleles', AlleleSearchHandler),
(decorator.callback_path, decorator.callback_handler()),
],
debug=True)