-
Notifications
You must be signed in to change notification settings - Fork 0
/
cts_rest.py
executable file
·697 lines (619 loc) · 19.1 KB
/
cts_rest.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
"""
CTS workflow/module-oriented REST endpoints
For Chemical Editor, p-chem table, chemical speciation,
and reaction pathways.
"""
import logging
import json
import datetime
import pytz
from django.http import HttpResponse, HttpRequest
from django.template.loader import render_to_string
from django.shortcuts import render_to_response
from ..cts_calcs.calculator_chemaxon import JchemCalc
from ..cts_calcs.calculator_epi import EpiCalc
from ..cts_calcs.calculator_measured import MeasuredCalc
from ..cts_calcs.calculator_test import TestCalc
from ..cts_calcs.calculator_test import TestWSCalc
from ..cts_calcs.calculator_sparc import SparcCalc
from ..cts_calcs.calculator_metabolizer import MetabolizerCalc
from ..models.chemspec import chemspec_output # todo: have cts_calcs handle specation, sans chemspec output route
from ..cts_calcs.calculator import Calculator
from ..cts_calcs.chemical_information import SMILESFilter
from ..cts_calcs.chemical_information import ChemInfo
class CTS_REST(object):
"""
CTS level endpoints for REST API.
Will have subclasses for calculators and
other CTS features, like metabolizer.
"""
def __init__(self):
self.calcs = ['chemaxon', 'epi', 'test', 'sparc', 'measured']
self.endpoints = ['cts', 'metabolizer'] + self.calcs
self.meta_info = {
'metaInfo': {
'model': "cts",
'collection': "qed",
'modelVersion': "1.3.22",
'description': "The Chemical Transformation System (CTS) was generated by researchers at the U.S. Enivornmental Protection Agency to provide access to a collection of physicochemical properties and reaction transformation pathways.",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest"
}
},
}
self.links = [
{
'rel': "episuite",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/episuite"
},
{
'rel': "chemaxon",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/chemaxon"
},
{
'rel': "sparc",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/sparc"
},
{
'rel': "test",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/test"
},
{
'rel': "metabolizer",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/metabolizer"
}
]
self.calc_links = [
{
'rel': "inputs",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/{}/inputs",
'description': "ChemAxon input schema",
'method': "POST",
},
{
'rel': "outputs",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/{}/outputs",
'description': "ChemAxon output schema",
'method': "POST"
},
{
'rel': "run",
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/{}/run",
'description': "ChemAxon estimated values",
'method': "POST"
}
]
self.pchem_inputs = ['chemical', 'calc', 'prop', 'run_type']
self.metabolizer_inputs = ['structure', 'generationLimit', 'transformationLibraries']
@classmethod
def getCalcObject(self, calc):
if calc == 'cts':
return CTS_REST()
elif calc == 'chemaxon':
return Chemaxon_CTS_REST()
elif calc == 'epi':
return EPI_CTS_REST()
elif calc == 'test':
return TEST_CTS_REST()
elif calc == 'testws':
return TEST_CTS_REST()
elif calc == 'sparc':
return SPARC_CTS_REST()
elif calc == 'measured':
return Measured_CTS_REST()
elif calc == 'metabolizer':
return Metabolizer_CTS_REST()
else:
return None
def getCalcLinks(self, calc):
if calc in self.calcs:
_links = self.calc_links
for item in _links:
if 'href' in item:
item['href'] = item['href'].format(calc) # insert calc name into href
return _links
else:
return None
def getCTSREST(self):
_response = self.meta_info
_response['links'] = self.links
return HttpResponse(json.dumps(_response), content_type='application/json')
def getCalcEndpoints(self, calc):
_response = {}
calc_obj = self.getCalcObject(calc)
_response.update({
'metaInfo': calc_obj.meta_info,
'links': self.getCalcLinks(calc)
})
return HttpResponse(json.dumps(_response), content_type="application/json")
def getCalcInputs(self, chemical, calc, prop=None):
_response = {}
calc_obj = self.getCalcObject(calc)
_response.update({'metaInfo': calc_obj.meta_info})
if calc in self.calcs:
_response.update({
'inputs': {
'chemical': chemical,
'prop': prop,
'calc': calc,
'run_type': "rest",
}
})
elif calc == 'metabolizer':
_response.update({
'inputs': calc_obj.inputs
})
return HttpResponse(json.dumps(_response), content_type="application/json")
def runCalc(self, calc, request_dict):
_response = {}
_response = self.meta_info
if calc == 'metabolizer':
structure = request_dict.get('structure')
gen_limit = request_dict.get('generationLimit')
trans_libs = request_dict.get('transformationLibraries')
# TODO: Add transformationLibraries key:val logic
metabolizer_request = {
'structure': structure,
'generationLimit': gen_limit,
'populationLimit': 0,
'likelyLimit': 0.001,
# 'transformationLibraries': trans_libs,
'excludeCondition': "" # 'generateImages': False
}
# metabolizerList = ["hydrolysis", "abiotic_reduction", "human_biotransformation"]
# NOTE: Only adding 'transformationLibraries' key:val if hydrolysis and/or reduction selected, but not mammalian metabolism
if len(trans_libs) > 0 and not 'human_biotransformation' in trans_libs:
metabolizer_request.update({'transformationLibraries': trans_libs})
try:
response = MetabolizerCalc().getTransProducts(metabolizer_request)
except Exception as e:
logging.warning("error making data request: {}".format(e))
raise
_progeny_tree = MetabolizerCalc().recursive(response, int(gen_limit))
_response.update({'data': json.loads(_progeny_tree)})
elif calc == 'speciation':
logging.info("CTS REST - speciation")
return getChemicalSpeciationData(request_dict)
else:
try:
logging.warning("REQUEST DICT TYPE: {}".format(type(request_dict)))
_orig_smiles = request_dict.get('chemical')
logging.info("ORIG SMILES: {}".format(_orig_smiles))
_filtered_smiles = SMILESFilter().filterSMILES(_orig_smiles)
request_dict.update({
'orig_smiles': _orig_smiles,
'chemical': _filtered_smiles,
})
except AttributeError as ae:
# POST type is django QueryDict (most likely)
request_dict = dict(request_dict) # convert QueryDict to dict
for key, val in request_dict.items():
request_dict.update({key: val[0]}) # vals of QueryDict are lists of 1 item
request_dict.update({
'orig_smiles': _orig_smiles,
'chemical': _filtered_smiles,
})
except Exception as e:
logging.warning("exception in cts_rest.py runCalc: {}".format(e))
logging.warning("skipping SMILES filter..")
pchem_data = {}
if calc == 'chemaxon':
pchem_data = JchemCalc().data_request_handler(request_dict)
# logging.warning("PCHEM DATA: {}".format(pchem_data))
elif calc == 'epi':
_epi_calc = EpiCalc()
pchem_data = _epi_calc.data_request_handler(request_dict)
if not pchem_data.get('valid'):
logging.warning("{} request error: {}".format(calc, pchem_data))
_response_obj = {'error': pchem_data.get('data')}
_response_obj.update(request_dict)
return HttpResponse(json.dumps(_response_obj))
# with updated epi, have to pick out desired prop:
# _epi_water_sol = [] # water_sol will return two data objects for api
_methods_list = []
for data_obj in pchem_data.get('data'):
epi_prop_name = _epi_calc.propMap[request_dict['prop']]['result_key']
if data_obj['prop'] == epi_prop_name:
# if request_dict['prop'] == 'water_sol':
# _methods_list.append(data_obj)
if data_obj.get('method'):
_epi_methods = _epi_calc.propMap.get(request_dict['prop']).get('methods')
data_obj['method'] = _epi_methods.get(data_obj['method']) # use pchem table name for method
_methods_list.append(data_obj)
else:
pchem_data['data'] = data_obj['data'] # only want request prop
pchem_data['prop'] = request_dict['prop'] # use cts prop name
if len(_methods_list) > 0:
# epi water solubility has two data objects..
pchem_data['data'] = _methods_list
elif calc == 'test':
pchem_data = TestCalc().data_request_handler(request_dict)
elif calc == 'testws':
pchem_data = TestWSCalc().data_request_handler(request_dict)
elif calc == 'sparc':
pchem_data = SparcCalc().data_request_handler(request_dict)
elif calc == 'measured':
pchem_data = MeasuredCalc().data_request_handler(request_dict)
if not pchem_data.get('valid'):
logging.warning("{} request error: {}".format(calc, pchem_data))
_response_obj = {'error': pchem_data.get('data')}
_response_obj.update(request_dict)
return HttpResponse(json.dumps(_response_obj))
# with updated measured, have to pick out desired prop:
for data_obj in pchem_data.get('data'):
measured_prop_name = MeasuredCalc().propMap[request_dict['prop']]['result_key']
if data_obj['prop'] == measured_prop_name:
pchem_data['data'] = data_obj['data'] # only want request prop
pchem_data['prop'] = request_dict['prop'] # use cts prop name
_response.update({'data': pchem_data})
return HttpResponse(json.dumps(_response), content_type="application/json")
class Chemaxon_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for ChemAxon
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "chemaxon",
'collection': "qed",
'modelVersion': "Jchem Web Services 15.3.23.0",
'description': "Cheminformatics software platforms, applications, and services to optimize the value of chemistry information in life science and other R&D.",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/chemaxon"
},
'props': ['water_sol', 'ion_con', 'kow_no_ph', 'kow_wph'],
'availableProps': [
{
'prop': 'water_sol',
'units': 'mg/L',
'description': "water solubility"
},
{
'prop': 'ion_con',
'description': "pKa and pKa values"
},
{
'prop': 'kow_no_ph',
'units': "log",
'description': "Octanol/water partition coefficient",
'methods': ['KLOP', 'PHYS', 'VG']
},
{
'prop': 'kow_wph',
'units': "log",
'description': "pH-dependent octanol/water partition coefficient",
'methods': ['KLOP', 'PHYS', 'VG']
}
]
}
}
class EPI_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for EPI Suite
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "epi",
'collection': "qed",
'modelVersion': "4.11",
'description': "EPI Suite is a Windows-based suite of physical/chemical property and environmental fate estimation programs developed by EPA and Syracuse Research Corp. (SRC).",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/epi"
},
'availableProps': [
{
'prop': 'melting_point',
'units': 'degC',
'description': "melting point"
},
{
'prop': 'boiling_point',
'units': 'degC',
'description': "boiling point"
},
{
'prop': 'water_sol',
'units': 'mg/L',
'description': "water solubility"
},
{
'prop': 'vapor_press',
'units': 'mmHg',
'description': "vapor pressure"
},
{
'prop': 'henrys_law_con',
'units': '(atm*m^3)/mol',
'description': "henry's law constant"
},
{
'prop': 'kow_no_ph',
'units': "log",
'description': "Octanol/water partition coefficient"
},
{
'prop': 'koc',
# 'units': "L/kg",
'units': "log",
'description': "organic carbon partition coefficient"
}
]
}
}
class TEST_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for EPI Suite
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "test",
'collection': "qed",
'modelVersion': "4.2.1",
'description': "The Toxicity Estimation Software Tool (TEST) allows users to easily estimate the toxicity of chemicals using QSARs methodologies.",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/test"
},
'availableProps': [
{
'prop': 'melting_point',
'units': 'degC',
'description': "melting point",
'method': "FDAMethod"
},
{
'prop': 'boiling_point',
'units': 'degC',
'description': "boiling point",
'method': "FDAMethod"
},
{
'prop': 'water_sol',
'units': 'mg/L',
'description': "water solubility",
'method': "FDAMethod"
},
{
'prop': 'vapor_press',
'units': 'mmHg',
'description': "vapor pressure",
'method': "FDAMethod"
}
]
}
}
class SPARC_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for EPI Suite
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "sparc",
'collection': "qed",
'modelVersion': "",
'description': "SPARC Performs Automated Reasoning in Chemistry (SPARC) is a chemical property estimator developed by UGA and the US EPA",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/sparc"
},
'availableProps': [
{
'prop': 'boiling_point',
'units': 'degC',
'description': "boiling point"
},
{
'prop': 'water_sol',
'units': 'mg/L',
'description': "water solubility"
},
{
'prop': 'vapor_press',
'units': 'mmHg',
'description': "vapor pressure"
},
{
'prop': 'mol_diss',
'units': 'cm^2/s',
'description': "molecular diffusivity"
},
{
'prop': 'ion_con',
'description': "pKa and pKa values"
},
{
'prop': 'henrys_law_con',
'units': '(atm*m^3)/mol',
'description': "henry's law constant"
},
{
'prop': 'kow_no_ph',
'units': "log",
'description': "octanol/water partition coefficient"
},
{
'prop': 'kow_wph',
'units': "log",
'description': "pH-dependent octanol/water partition coefficient"
}
]
}
}
class Measured_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for EPI Suite
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "measured",
'collection': "qed",
'modelVersion': "EPI Suite 4.11",
'description': "Measured data from EPI Suite 4.11.",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/measured"
},
'availableProps': [
{
'prop': 'melting_point',
'units': 'degC',
'description': "melting point",
'method': "FDAMethod"
},
{
'prop': 'boiling_point',
'units': 'degC',
'description': "boiling point"
},
{
'prop': 'water_sol',
'units': 'mg/L',
'description': "water solubility"
},
{
'prop': 'vapor_press',
'units': 'mmHg',
'description': "vapor pressure"
},
{
'prop': 'henrys_law_con',
'units': '(atm*m^3)/mol',
'description': "henry's law constant"
},
{
'prop': 'kow_no_ph',
'units': "log",
'description': "octanol/water partition coefficient"
},
{
'prop': 'koc',
'units': "L/kg",
'description': "organic carbon partition coefficient"
}
]
}
}
class Metabolizer_CTS_REST(CTS_REST):
"""
CTS REST endpoints, etc. for EPI Suite
"""
def __init__(self):
self.meta_info = {
'metaInfo': {
'model': "metabolizer",
'collection': "qed",
'modelVersion': "",
'description': "",
'status': '',
'timestamp': gen_jid(),
'url': {
'type': "application/json",
'href': "http://qedinternal.epa.gov/cts/rest/metabolizer"
},
}
}
self.inputs = {
'structure': '',
'generationLimit': 1,
'transformationLibraries': ["hydrolysis", "abiotic_reduction", "human_biotransformation"]
}
def showSwaggerPage(request):
"""
display swagger.json with swagger UI
for CTS API docs/endpoints
"""
return render_to_response('cts_api/swagger_index.html')
def getChemicalEditorData(request):
"""
Makes call to Calculator for chemaxon
data. Converts incoming structure to smiles,
then filters smiles, and then retrieves data
:param request:
:return: chemical details response json
Note: Due to marvin sketch image data (<cml> image) being
so large, a bool, "structureData", is used to determine
whether or not to grab it. It's only needed in chem edit tab.
"""
try:
if 'message' in request.POST:
# Receiving request from cts_stress node server..
# todo: should generalize and not have conditional
request_post = json.loads(request.POST.get('message'))
else:
request_post = request.POST
_cheminfo_results = ChemInfo().get_cheminfo(request_post)
json_data = json.dumps(_cheminfo_results)
logging.warning("Returning Chemical Info: {}".format(json_data))
return HttpResponse(json_data, content_type='application/json')
except KeyError as error:
logging.warning(error)
wrapped_post = {
'status': False,
'error': 'Error validating chemical',
'chemical': chemical
}
return HttpResponse(json.dumps(wrapped_post), content_type='application/json')
except Exception as error:
logging.warning(error)
# wrapped_post = {'status': False, 'error': str(error)}
wrapped_post = {'status': False, 'error': "Cannot validate chemical.."}
return HttpResponse(json.dumps(wrapped_post), content_type='application/json')
def getChemicalSpeciationData(request_dict):
"""
CTS web service endpoint for getting
chemical speciation data through the
chemspec model/class
:param request - chemspec_model
:return: chemical speciation data response json
"""
try:
logging.info("Incoming request for speciation data: {}".format(request_dict))
filtered_smiles = SMILESFilter().filterSMILES(request_dict.get('chemical'))
logging.info("Speciation filtered SMILES: {}".format(filtered_smiles))
request_dict['chemical'] = filtered_smiles
django_request = HttpResponse()
django_request.POST = request_dict
django_request.method = 'POST'
chemspec_obj = chemspec_output.chemspecOutputPage(django_request)
wrapped_post = {
'status': True, # 'metadata': '',
'data': chemspec_obj.run_data
}
json_data = json.dumps(wrapped_post)
logging.info("chemspec model data: {}".format(chemspec_obj))
return HttpResponse(json_data, content_type='application/json')
except Exception as error:
logging.warning("Error in cts_rest, getChemicalSpecation(): {}".format(error))
return HttpResponse("Error getting speciation data")
def gen_jid():
ts = datetime.datetime.now(pytz.UTC)
localDatetime = ts.astimezone(pytz.timezone('US/Eastern'))
jid = localDatetime.strftime('%Y%m%d%H%M%S%f')
return jid