-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneric_methods.py
460 lines (329 loc) · 12.9 KB
/
generic_methods.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import requests
import yaml
import os
"""
This file will contain methods and variables common to the use of the GEL CIPAPI interface, as well as other resources
This file can be kept in any project where it may be useful, and encourages DRY practices
Alternatively, add this file as an addtional environment variable/PYTHON_PATH variable to use the methods from anywhere
This script assumes that the environment variable file has been created (based on the example credentials in this folder)
and that the populated file is added as an environment variable 'ENV_CREDENTIALS'
"""
def blind_recent_ir_and_version_from_member(member):
"""
a method which takes a member ID, and returns the most recent interp request and version number
:param member:
:return:
"""
cipapi_details = blind_get_gel_credentials_by_app('cip_api')
member_check_url = get_generic_members_url().format(cipapi=cipapi_details['host'], member=member)
member_json = get_url_json_response_with_header(member_check_url,
get_cipapi_header_from_credentials(cipapi_details))
feedback = {}
if member_json:
for result in member_json['results']:
ir_and_version = result['interpretation_request_id']
ir, version = ir_and_version.split('-')
if ir not in feedback:
feedback[ir] = {'versions': [int(version)]}
else:
feedback[ir]['versions'].append(int(version))
feedback[ir].update({'cip': result['cip'],
'sample_type': result['sample_type'],
'assembly': result['assembly']})
for ir in feedback:
feedback[ir]['recent'] = max(feedback[ir]['versions'])
return feedback
def recent_ir_and_version_from_member_with_details(member, details):
"""
a method which takes a member ID, and returns the most recent interp request and version number
:param member:
:return:
"""
member_check_url = get_generic_members_url().format(cipapi=details['host'], member=member)
member_json = get_url_json_response_with_header(member_check_url, get_cipapi_header_from_credentials(details))
feedback = {}
if member_json:
for result in member_json['results']:
ir_and_version = result['interpretation_request_id']
ir, version = ir_and_version.split('-')
if ir not in feedback:
feedback[ir] = {'versions': [int(version)]}
else:
feedback[ir]['versions'].append(int(version))
feedback[ir].update({'cip': result['cip'],
'sample_type': result['sample_type'],
'assembly': result['assembly']})
for ir in feedback:
feedback[ir]['recent'] = max(feedback[ir]['versions'])
return feedback
def recent_ir_and_version_from_member_with_details_and_header(member, details, header):
"""
a method which takes a member ID, and returns the most recent interp request and version number
:param member:
:return:
"""
member_check_url = get_generic_members_url().format(cipapi=details['host'], member=member)
member_json = get_url_json_response_with_header(member_check_url, header)
feedback = {}
if member_json:
for result in member_json['results']:
ir_and_version = result['interpretation_request_id']
ir, version = ir_and_version.split('-')
if ir not in feedback:
feedback[ir] = {'versions': [int(version)]}
else:
feedback[ir]['versions'].append(int(version))
feedback[ir].update({'cip': result['cip'],
'sample_type': result['sample_type'],
'assembly': result['assembly']})
for ir in feedback:
feedback[ir]['recent'] = max(feedback[ir]['versions'])
return feedback
def get_assumed_env_file():
"""
checks if the standard env file for credentials exists, returns if true
:return:
"""
try:
if os.path.exists(os.getenv('ENV_CREDENTIALS')):
return os.getenv('ENV_CREDENTIALS')
else:
print 'the standard credentials file cannot be located'
print 'the environment variable tried was "ENV_CREDENTIALS"'
raise KeyError('Standard Env file not seen')
except:
print('error encountered when trying to find/read the ENV_CREDENTIALS env file')
return False
def get_url_json_response(url):
"""
take a URL; return the result as JSON
:param url:
:param header:
:return:
"""
response = requests.get(url=url)
if response.status_code != 200:
raise ValueError(
"Received status: {status} for url: {url} with response: {response}".format(
status=response.status_code, url=url, response=response.content)
)
return response.json()
def get_url_json_response_with_header(url, header):
"""
take a URL and an authenticated header; return the result as JSON
:param url:
:param header:
:return:
"""
response = requests.get(url=url, headers=header)
if response.status_code != 200:
raise ValueError(
"Received status: {status} for url: {url} with response: {response}".format(
status=response.status_code, url=url, response=response.content)
)
return response.json()
def get_panel_list_from_single_ir_json(json):
"""
for the results of an interpretation-request/ir/version query, get the panels
example of an in-built highly specific query to prevent re-writing in future
:param json:
:return:
"""
return json['interpretation_request_data']['json_request']['pedigree']['analysisPanels']
def get_pedigree_from_single_ir_json(json):
"""
retrieve only the pedigree section from an IR JSON
:param json:
:return:
"""
return json['interpretation_request_data']['json_request']['pedigree']
def get_gel_credentials(env_path):
"""
takes the path to the YAML environment file, and reads out all credential sets it contains
return in dictionary form
:param env_path:
:return:
"""
# check the file exists
if not os.path.exists(env_path):
raise ValueError('The stated file for environment login credentials doesn\'t exist: {}'.format(env_path))
# open the file and read into python variables
cred_dict = yaml.load(open(env_path))
# repackage the list of dictinoaries as a dictionary of dictionaries - reference credentials by name
credentials = {entry['name']: entry for entry in cred_dict}
# return the contents in dictionary form
return credentials
def get_gel_credentials_by_app(env_path, app):
"""
takes the path to the YAML environment file, and reads out all credentials for <app>
return in dictionary form
:param env_path:
:param app: name of an app with credentials in this file; return only those
:return:
"""
# check the file exists
if not os.path.exists(env_path):
raise ValueError('The stated file for environment login credentials doesn\'t exist: {}'.format(env_path))
# open the file and read into python variables
cred_dict = yaml.load(open(env_path))
# identify the credential sets which were found, list under the resource name
credentials = {entry['name']: entry for entry in cred_dict}
# return the contents in dictionary form
return credentials[app]
def blind_get_gel_credentials():
"""
takes the path to the environment file, in YAML format, and reads out all credential sets it contains
return in dictinoary form
:param env_path:
:return:
"""
# open the file and read into python variables
cred_dict = yaml.load(open(get_assumed_env_file()))
# identify the credential sets which were found, list under the resource name
credentials = {entry['name']: entry for entry in cred_dict}
# return the contents in dictionary form
return credentials
def blind_get_gel_credentials_by_app(app):
"""
takes the path to the environment file, in YAML format, and reads out all credentials for <app>
return in dictionary form
'blind' as this works on assumptions about where the environment variable lives and how it is referenced
:param app: name of an app with credentials in this file; return only those
:return:
"""
# open the file and read into python variables
cred_dict = yaml.load(open(get_assumed_env_file()))
# identify the credential sets which were found, list under the resource name
credentials = {entry['name']: entry for entry in cred_dict}
# return the contents in dictionary form
return credentials[app]
def get_cipapi_header(url, username, password):
"""
manually send login details to get a header for the cipapi
could feasibly provide tokens for any other JWT service using get-token interface
:param url:
:param username:
:param password:
:return:
"""
auth_endpoint = "/get-token/"
irl_response = requests.post(
url=url + auth_endpoint,
json=dict(
username=username,
password=password,
),
)
irl_response_json = irl_response.json()
token = irl_response_json.get('token')
auth_header = {
'Accept': 'application/json',
"Authorization": "JWT {token}".format(token=token),
}
return auth_header
def get_cipapi_header_from_credentials(credentials):
"""
takes the environment variables for the cip api in dictionary form and goes to get the authenticated header
:param credentials: the env variables dict
:return:
"""
url = credentials['host']
username = credentials['username']
password = credentials['password']
auth_endpoint = "/get-token/"
irl_response = requests.post(
url=url + auth_endpoint,
json=dict(
username=username,
password=password,
),
)
irl_response_json = irl_response.json()
token = irl_response_json.get('token')
auth_header = {
'Accept': 'application/json',
"Authorization": "JWT {token}".format(token=token),
}
return auth_header
def get_opencga_sid(credentials):
"""
a generic method for getting a valid session ID in opencga using the REST API
:param credentials:
:return:
"""
endpoint_ext = '/users/{username}/login/'.format(
username=credentials['username']
)
full_url = credentials['host'] + endpoint_ext
sid_response = requests.post(
url=full_url,
json=dict(
password=credentials['password']
)
)
if sid_response.status_code != 200:
raise ValueError(
"Received status: {status} for url: {url} with response: {response}".format(
status=sid_response.status_code, url=full_url, response=sid_response.content)
)
json_response = sid_response.json()
for response in json_response['response']:
if response['numResults'] == 1:
for result in response['result']:
return result['sessionId']
def complete_sid_process():
"""
everything required to generate a SID for OpenCGA, chained together for simplicity
:return:
"""
return get_opencga_sid(blind_get_gel_credentials_by_app('opencga_rest'))
def return_study_from_assembly_and_type(assembly, disease, somatic=False):
"""
returns a study ID, or returns False
:param assembly:
:param disease:
:return:
"""
if assembly == 'GRCh37':
if disease == 'raredisease':
return '1000000024'
elif disease == 'cancer':
if somatic:
return '1000000030'
else:
return '1000000026'
else:
return False
elif assembly == 'GRCh38':
if disease == 'raredisease':
return '1000000032'
elif disease == 'cancer':
if somatic:
return '1000000038'
else:
return '1000000034'
else:
return False
else:
return False
def get_generic_single_ir_url():
"""
commonly used URL
:return: a URL template for getting the JSON results for a single IR and Version
"""
return '{cipapi}/interpretation-request/{ir}/{ver}/'
def get_generic_single_file_url():
"""
:return: a URL template for getting a single file for this sample and study
"""
return '{opencga}/files/{file}/info?sid={sid}&study={study}&lazy=true'
def get_generic_sample_study_url():
"""
:return: a URL template for getting the study this sample is in
"""
return '{opencga}/studies/{study}/samples?sid={sid}&name={name}'
def get_generic_members_url():
"""
:return: a URL template for getting the details of an individual based on their member/Proband ID
"""
return '{cipapi}/interpretation-request?format=json&members={member}'