-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
353 lines (302 loc) · 12.4 KB
/
app.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
from flask import Flask, request
from flask_restplus import Resource, Api
import requests
import time
import json
import pandas as pd
from collections import defaultdict
app = Flask(__name__)
description = """ An NCATS Translator SMARTAPI compliant wrapper for the
BigGIM (Gene Interaction Miner) API http://biggim.ncats.io/api
"""
api = Api(app, description=description)
# instantiate namespaces
interactions_ns = api.namespace('interactions', "Mine the interaction profiles of various entities")
metadata_ns = api.namespace('metadata', 'Access the metadata for available datasets')
base_url = 'http://biggim.ncats.io/api'
# map of bto -> uberon -> bg terms
uberon_bto_map = json.loads(open('bto_uberon_bg.json').read())
# map of columns to metadata objects
meta_columns = json.loads(open('bg_column_map.json').read())
def id2term(var, key, return_key, json_blob):
"""
check json map for value
:param var: identifier
:param key: source of identifier
:param return_key: key to return in matched object
:param json_blob: json to search
:return: bg term name if mapping to that id exists, input var if no mapping exists
"""
result = var
for obj in json_blob:
if obj[key] == var:
result = obj[return_key]
return result
# http request methods
def postBG(endpoint, data={}, base_url=base_url):
req = requests.post('%s/%s' % (base_url, endpoint), json=data)
req.raise_for_status()
return req.json()
def getBG(endpoint, data={}, base_url=base_url):
req = requests.get('%s/%s' % (base_url, endpoint), data=data)
req.raise_for_status()
return req.json()
##########
# /metadata
##########
@metadata_ns.route('/openapiv3')
class MetaDataStudy(Resource):
"""
Return the OpenAPI v3 spec for this API
"""
def get(Request):
try:
studies = getBG(endpoint='metadata/openapiv3', data={}, base_url=base_url)
return studies
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/study')
class MetaDataStudy(Resource):
"""
Return all available studies
"""
def get(Request):
try:
studies = getBG(endpoint='metadata/study', data={}, base_url=base_url)
return studies
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/study/<string:study_name>')
class SingleStudy(Resource):
"""
Return a single study and associated substudies
"""
def get(self, study_name):
try:
endpoint = 'metadata/study/%s' % (study_name)
study_meta = getBG(endpoint=endpoint, data={}, base_url=base_url)
return study_meta
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/swagger')
class MetaDataSwagger(Resource):
"""
Return the swagger v2 spec for this API
"""
def get(Request):
try:
swagger = getBG(endpoint='metadata/swagger', data={}, base_url=base_url)
return swagger
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/table')
class MetaDataTable(Resource):
"""
Retreive list of avaiable tables
"""
def get(Request):
try:
table_result = getBG(endpoint='metadata/table', data={}, base_url=base_url)
return table_result
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/table/<string:table_name>')
class SingleTable(Resource):
"""
Retrieve metadata about a table
"""
def get(self, table_name):
try:
endpoint = 'metadata/table/%s' % (table_name)
table_meta = getBG(endpoint=endpoint, data={}, base_url=base_url)
return table_meta
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/table/<string:table_name>/column/<string:column_name>')
class SingleColumn(Resource):
"""
Retrieve metadata about a column in a table
"""
def get(self, table_name, column_name):
try:
endpoint = 'metadata/table/%s/column/%s' % (table_name, column_name)
table_meta = getBG(endpoint=endpoint, data={}, base_url=base_url)
return table_meta
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/tissue')
class Tissues(Resource):
"""
Return a list of available tissues (bto terms with underscores)
"""
def get(Request):
try:
studies = getBG(endpoint='metadata/tissue', data={}, base_url=base_url)
return studies
except requests.HTTPError as e:
return {
'error': str(e)
}
@metadata_ns.route('/tissue/<string:tissue_name>')
class SingleTissue(Resource):
"""
Return a list of substudies and columns associated with a tissue
"""
def get(self, tissue_name):
# retrieve bg term if bto or uberon as input
if 'UBERON:' in tissue_name:
tissue_name = id2term(var=tissue_name, key='uberon_id', return_key='bg_label', json_blob=uberon_bto_map)
if 'BTO:' in tissue_name:
tissue_name = id2term(var=tissue_name, key='bto_id', return_key='bg_label', json_blob=uberon_bto_map)
try:
endpoint = 'metadata/tissue/%s' % (tissue_name)
single_tissue = getBG(endpoint=endpoint, data={}, base_url=base_url)
return single_tissue
except requests.HTTPError as e:
return {
'message': "'{0}' is not a valid tissue name or identifier".format(tissue_name),
'error': str(e)
}
##########
# /interactions
##########
@interactions_ns.route('/query')
@interactions_ns.param('table', 'The table to select from.', default='BigGIM_70_v1', required=True)
@interactions_ns.param('columns', 'A comma delimited list of column names to return',
default='all columns', required=True)
@interactions_ns.param('ids1', 'A comma delimited list of Entrez gene ids to select',
default='all genes', required=True)
@interactions_ns.param('ids2', 'Entrez gene ids to select: If not given, the query selects any '
'gene related to a gene in ids 1. If given, the query only selects '
'relations that contain a gene in ids1 and a gene in ids2.',
default='all genes', required=True)
@interactions_ns.param('restriction_bool', 'A list of pairs of values column name,value with which to '
'restrict the results of the query to rows where the value of'
' the column is True or False',
default='No restrictions', required=True)
@interactions_ns.param('restriction_lt', 'A list of pairs of values column name,value with which to '
'restrict the results of the query to rows where the value '
'of the column is less than the given value.',
default='No restrictions', required=True)
@interactions_ns.param('restriction_gt', 'A list of pairs of values column name,value with which to '
'restrict the results of the query to rows where the value '
'of the column is greater than the given value.',
default='No restrictions', required=True)
@interactions_ns.param('restriction_join', 'The type of join made on restrictions. Either intersect or union',
default='intersect', required=True)
@interactions_ns.param('limit', 'The maximum number of rows to return',
default='10000', required=True)
@interactions_ns.param('format', 'The format of the output e.g. json/csv',
default='json', required=False)
# add format parameter
class GetInteractionsQuery(Resource):
def post(self):
try:
query_submit = postBG(endpoint='interactions/query', base_url=base_url, data=request.args)
except Exception as e:
return {
'error': str(e)
}
query_status = self.get_query_status(query_key=query_submit['request_id'])
return self.pandas2json(query_status['request_uri'])
def get(self):
try:
query_submit = getBG('interactions/query', base_url=base_url, data=request.args)
except Exception as e:
return {
'error': str(e)
}
query_status = self.get_query_status(query_key=query_submit['request_id'])
return self.pandas2json(query_status['request_uri'])
def get_query_status(self, query_key):
"""
use the query key from initial interactions/query request to return uri for interactions csv
:param query_key:
:return:
"""
try:
while True:
query_status = getBG(endpoint='interactions/query/status/%s' % (query_key),
base_url=base_url, data={})
if query_status['status'] != 'running':
# query has finished
break
else:
time.sleep(1)
except requests.HTTPError as e:
return {
'error': str(e)
}
return query_status
def remove_kv_pair(self, obj, key):
obj1 = obj.copy()
obj1.pop(key)
return obj1
def pandas2json(self, request_uri):
# use pandas to get csv with request uri and serialize into json for return
pd_df = pd.read_csv(request_uri[0])
if request.args['format'] == 'json':
out_json = json.loads(pd_df.to_json(orient='records'))
final_json = list()
for record in out_json:
new_record = {
'Gene1': record['Gene1'],
'Gene2': record['Gene2'],
'GPID': record['GPID'],
'interactions': []
}
# use defaultdict to sort by unique tissues
sources = {
'BioGRID': defaultdict(list),
'TCGA': defaultdict(list),
'GTEx': defaultdict(list),
'GIANT': defaultdict(list)
}
for k, v in record.items():
# map columns to metadata json blobs
if k in meta_columns.keys() and v is not None:
col = meta_columns[k]
int_source = col['source']
# remove redundent biogrid values
if int_source == 'BioGRID' and isinstance(v, str):
v = v.split(',')
v = ",".join(set(v))
col[col['type']] = v
new_col = self.remove_kv_pair(col, 'type')
sources[int_source][col['type']].append(new_col)
# group by tissue
if col['tissue'] is not None:
col[col['type']] = v
new_col = self.remove_kv_pair(col, 'type')
sources[int_source][col['tissue']['bg_label']].append(new_col)
# group by cancer type
if col['cancer_type'] is not None:
col[col['type']] = v
new_col = self.remove_kv_pair(col, 'type')
sources[int_source][col['cancer_type']].append(new_col)
# join interaction params with same tissue e.g. correlation and pvalue
for skey in sources.keys():
for k, v in sources[skey].items():
for vobj in v[1:]:
v[0].update(vobj)
new_record['interactions'].append(v[0])
final_json.append(new_record)
return final_json
elif request.args['format'] == 'csv':
return request_uri[0]
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)