forked from chembl/chembl_beaker
-
Notifications
You must be signed in to change notification settings - Fork 5
/
views.py
156 lines (129 loc) · 9.48 KB
/
views.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
__author__ = 'mnowotka'
from bottle import request, response
import base64
from chembl_beaker.beaker.utils.io import _parseFlag
from chembl_beaker.beaker import app
from chembl_beaker.beaker.core_apps.rasterImages.impl import _ctab2image, _smiles2image
#-----------------------------------------------------------------------------------------------------------------------
def ctab2imageView(data, params):
kwargs = dict()
kwargs['size'] = int(params.get('size', 200))
separator = params.get('separator', '|')
kwargs['legend'] = params.get('legend', '').split(separator)
kwargs['sanitize'] = _parseFlag(params.get('sanitize', True))
kwargs['removeHs'] = _parseFlag(params.get('removeHs', True))
kwargs['strictParsing'] = _parseFlag(params.get('strictParsing', True))
kwargs['atomMapNumber'] = _parseFlag(params.get('atomMapNumber', False))
kwargs['computeCoords'] = _parseFlag(params.get('computeCoords', True))
response.content_type = 'image/png'
ret = _ctab2image(data, **kwargs)
if request.is_ajax:
ret = base64.b64encode(ret)
return ret
#-----------------------------------------------------------------------------------------------------------------------
@app.route('/ctab2image/<ctab>', method=['OPTIONS', 'GET'], name="ctab2image")
def ctab2image(ctab):
"""
Converts CTAB to PNG image. CTAB is urlsafe_base64 encoded string containing
single molfile or concatenation of multiple molfiles. Size is the optional size of image in pixels (default value
is 200 px). Legend is optional label in the bottom of image.
cURL examples:
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat aspirin.mol | base64 -w 0 | tr "+/" "-_") > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat aspirin.mol | base64 -w 0 | tr "+/" "-_")?computeCoords=0 > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat aspirin.mol | base64 -w 0 | tr "+/" "-_")?atomMapNumber=1 > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat aspirin.mol | base64 -w 0 | tr "+/" "-_")?legend=aspirin > aspirin.png
curl -X GET "${BEAKER_ROOT_URL}ctab2image/"$(cat mcs.sdf | base64 -w 0 | tr "+/" "-_")"?legend=foo|bar|bla" > out.png
curl -X GET "${BEAKER_ROOT_URL}ctab2image/"$(cat mcs.sdf | base64 -w 0 | tr "+/" "-_")"?legend=foo|bar|bla&computeCoords=0" > out.png
curl -X GET "${BEAKER_ROOT_URL}ctab2image/"$(cat mcs.sdf | base64 -w 0 | tr "+/" "-_")"?legend=foo|bar|bla" > out.png
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat mcs_no_coords.sdf | base64 -w 0 | tr "+/" "-_")?legend=foo > out.png
curl -X GET ${BEAKER_ROOT_URL}ctab2image/$(cat aspirin.mol | base64 -w 0 | tr "+/" "-_")?size=400 > aspirin.png
"""
data = base64.urlsafe_b64decode(ctab)
return ctab2imageView(data, request.params)
#-----------------------------------------------------------------------------------------------------------------------
@app.route('/ctab2image', method=['OPTIONS', 'POST'], name="ctab2image")
def ctab2image():
"""
Converts CTAB to PNG image. CTAB is either single molfile or SDF file. Size is the optional size of image in pixels
(default value is 200 px). Legend is optional label in the bottom of image.
cURL examples:
curl -X POST --data-binary @aspirin.mol ${BEAKER_ROOT_URL}ctab2image > aspirin.png
curl -X POST -F "file=@aspirin.mol" -F "computeCoords=0" ${BEAKER_ROOT_URL}ctab2image > aspirin.png
curl -X POST -F "file=@aspirin.mol" -F "atomMapNumber=1" ${BEAKER_ROOT_URL}ctab2image > aspirin.png
curl -X POST -F "file=@aspirin.mol" -F "legend=aspirin" ${BEAKER_ROOT_URL}ctab2image > aspirin.png
curl -X POST -F "file=@mcs.sdf" -F "legend=foo|bar|bla" ${BEAKER_ROOT_URL}ctab2image > out.png
curl -X POST -F "file=@mcs.sdf" -F "legend=foo|bar|bla" -F "computeCoords=0" ${BEAKER_ROOT_URL}ctab2image > out.png
curl -X POST -F "file=@mcs_no_coords.sdf" -F "legend=foo|bar|bla" ${BEAKER_ROOT_URL}ctab2image > out.png
curl -X POST -F "file=@mcs.sdf" -F "legend=foo" ${BEAKER_ROOT_URL}ctab2image > out.png
curl -X POST -F "file=@mcs.sdf" -F "legend=foo|bar|bla" -F "size=400" ${BEAKER_ROOT_URL}ctab2image > out.png
curl -X POST -F "file=@aspirin.mol" -F "size=400" ${BEAKER_ROOT_URL}ctab2image > aspirin.png
"""
data = request.files.values()[0].file.read() if len(request.files) else request.body.read()
return ctab2imageView(data, request.params)
#-----------------------------------------------------------------------------------------------------------------------
def smiles2imageView(data, params):
kwargs = dict()
kwargs['size'] = int(params.get('size', 200))
separator = params.get('separator', '|')
kwargs['legend'] = params.get('legend', '').split(separator)
kwargs['computeCoords'] = _parseFlag(params.get('computeCoords', True))
kwargs['delimiter'] = params.get('delimiter', ' ')
kwargs['smilesColumn'] = int(params.get('smilesColumn', 0))
kwargs['nameColumn'] = int(params.get('nameColumn', 1))
kwargs['sanitize'] = _parseFlag(params.get('sanitize', True))
kwargs['atomMapNumber'] = _parseFlag(params.get('atomMapNumber', False))
if params.get('titleLine') is None and not data.startswith('SMILES Name'):
kwargs['titleLine'] = False
else:
kwargs['titleLine'] = _parseFlag(params.get('titleLine', True))
response.content_type = 'image/png'
ret = _smiles2image(data, **kwargs)
if request.is_ajax:
ret = base64.b64encode(ret)
return ret
#-----------------------------------------------------------------------------------------------------------------------
@app.route('/smiles2image/<smiles>', method=['OPTIONS', 'GET'], name="smiles2image")
def smiles2image(smiles):
"""
Converts SMILES to PNG image. This method accepts urlsafe_base64 encoded string containing single or multiple
SMILES optionally containing header line, specific to *.smi format. Size is the optional size of image in pixels
(default value is 200 px). Legend is optional label in the bottom of image.
cURL examples:
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_with_header.smi | base64 -w 0 | tr "+/" "-_") > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_no_header.smi | base64 -w 0 | tr "+/" "-_") > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_no_header.smi | base64 -w 0 | tr "+/" "-_")?atomMapNumber=1 > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_with_header.smi | base64 -w 0 | tr "+/" "-_")?legend=aspirin > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_no_header.smi | base64 -w 0 | tr "+/" "-_")?legend=aspirin > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_with_header.smi | base64 -w 0 | tr "+/" "-_")?size=400 > aspirin.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat aspirin_no_header.smi | base64 -w 0 | tr "+/" "-_")?size=400 > aspirin.png
curl -X GET "${BEAKER_ROOT_URL}smiles2image/"$(cat mcs.smi | base64 -w 0 | tr "+/" "-_")"?legend=foo|bar|bla" > out.png
curl -X GET "${BEAKER_ROOT_URL}smiles2image/"$(cat mcs_no_header.smi | base64 -w 0 | tr "+/" "-_")"?legend=foo|bar|bla" > out.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat mcs.smi | base64 -w 0 | tr "+/" "-_")?legend=foo > out.png
curl -X GET ${BEAKER_ROOT_URL}smiles2image/$(cat mcs_no_header.smi | base64 -w 0 | tr "+/" "-_")?legend=foo > out.png
"""
data = base64.urlsafe_b64decode(smiles)
return smiles2imageView(data, request.params)
#-----------------------------------------------------------------------------------------------------------------------
@app.route('/smiles2image', method=['OPTIONS', 'POST'], name="smiles2image")
def smiles2image():
"""
Converts SMILES to PNG image. This method accepts single or multiple SMILES or *.smi file. Size is the optional
size of image in pixels (default value is 200 px). Legend is optional label in the bottom of image.
cURL examples:
curl -X POST -F "file=@aspirin_no_header.smi" -F "atomMapNumber=1" ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST --data-binary @aspirin_with_header.smi ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST --data-binary @aspirin_no_header.smi ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST -F "file=@aspirin_with_header.smi" -F "legend=aspirin" ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST -F "file=@aspirin_no_header.smi" -F "legend=aspirin" ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST -F "file=@aspirin_with_header.smi" -F "size=400" ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST -F "file=@aspirin_no_header.smi" -F "size=400" ${BEAKER_ROOT_URL}smiles2image > aspirin.png
curl -X POST -F "file=@mcs.smi" -F "legend=foo|bar|bla" ${BEAKER_ROOT_URL}smiles2image > out.png
curl -X POST -F "file=@mcs_no_header.smi" -F "legend=foo|bar|bla" ${BEAKER_ROOT_URL}smiles2image > out.png
curl -X POST -F "file=@mcs.smi" -F "legend=foo" ${BEAKER_ROOT_URL}smiles2image > out.png
curl -X POST -F "file=@mcs_no_header.smi" -F "legend=foo" ${BEAKER_ROOT_URL}smiles2image > out.png
curl -X POST -F "file=@mcs.smi" -F "legend=foo|bar|bla" -F "size=400" ${BEAKER_ROOT_URL}smiles2image > out.png
curl -X POST -F "file=@mcs_no_header.smi" -F "legend=foo|bar|bla" -F "size=400" ${BEAKER_ROOT_URL}smiles2image > out.png
"""
data = request.files.values()[0].file.read() if len(request.files) else request.body.read()
return smiles2imageView(data, request.params)
#-----------------------------------------------------------------------------------------------------------------------