forked from ptrxyz/ketcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ketcher.py
282 lines (250 loc) · 10.4 KB
/
ketcher.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from os import path, getcwd
from mimetypes import guess_type
from cgi import FieldStorage
from wsgiref.util import FileWrapper
from wsgiref.headers import Headers
from base64 import b64encode
class application(object):
# don't serve static by default
static_serve = False
static_alias = { '' : 'ketcher.html' }
static_root = None
indigo = None
indigo_inchi = None
def __init__(self, environ, start_response):
self.path = environ['PATH_INFO'].strip('/')
self.method = environ['REQUEST_METHOD']
self.content_type = environ.get('CONTENT_TYPE', '')
self.fields = FieldStorage(fp=environ['wsgi.input'],
environ=environ, keep_blank_values=True)
self.FileWrapper = environ.get('wsgi.file_wrapper', FileWrapper)
self.headers = Headers([])
route = getattr(self, 'on_' + self.path, None)
if route is None:
route = self.serve_static if self.method == 'GET' and \
self.static_serve else self.notsupported
status = "200 OK"
try:
self.response = route()
except self.HttpException as e:
status = e.args[0]
self.response = [e.args[1]]
self.headers.setdefault('Content-Type', 'text/plain')
start_response(status, self.headers.items())
def __iter__(self):
for chunk in self.response:
yield chunk if sys.version_info[0] < 3 or \
not hasattr(chunk, 'encode') else chunk.encode()
def notsupported(self):
raise self.HttpException("405 Method Not Allowed",
"Request not supported")
def indigo_required(method):
def wrapper(self, **args):
if not self.indigo:
raise self.HttpException("501 Not Implemented",
"Indigo libraries are not found")
try:
return method(self, **args)
except indigo.IndigoException as e:
message = str(sys.exc_info()[1])
if 'indigoLoad' in message: # error on load
message = "Cannot load the specified " + \
"structure: %s " % str(e)
raise self.HttpException("400 Bad Request",
message)
return wrapper
@indigo_required
def on_knocknock(self):
return ["You are welcome!"]
@indigo_required
def on_layout(self):
moldata = None
if self.method == 'GET' and 'smiles' in self.fields:
moldata = self.fields.getfirst('smiles')
elif self.is_form_request() and 'moldata' in self.fields:
moldata = self.fields.getfirst('moldata')
selective = 'selective' in self.fields
if moldata:
if '>>' in moldata or moldata.startswith('$RXN'):
rxn = self.indigo.loadQueryReaction(moldata)
if selective:
for mol in rxn.iterateMolecules():
self.selective_layout(mol)
else:
rxn.layout()
return ["Ok.\n",
rxn.rxnfile()]
elif moldata.startswith('InChI'):
mol = self.indigo_inchi.loadMolecule(moldata)
mol.layout()
return ["Ok.\n",
mol.molfile()]
else:
mol = self.indigo.loadQueryMolecule(moldata)
if selective:
for rg in mol.iterateRGroups():
for frag in rg.iterateRGroupFragments():
self.selective_layout(frag)
self.selective_layout(mol)
else:
mol.layout()
return ["Ok.\n",
mol.molfile()]
self.notsupported()
@indigo_required
def on_automap(self):
moldata = None
if self.method == 'GET' and 'smiles' in self.fields:
moldata = self.fields.getfirst('smiles')
elif self.is_form_request() and 'moldata' in self.fields:
moldata = self.fields.getfirst('moldata')
if moldata:
mode = self.fields.getfirst('mode', 'discard')
rxn = self.indigo.loadQueryReaction(moldata)
if not moldata.startswith('$RXN'):
rxn.layout()
rxn.automap(mode)
return ["Ok.\n",
rxn.rxnfile()]
self.notsupported()
@indigo_required
def on_aromatize(self):
try:
md, is_rxn = self.load_moldata()
except:
message = str(sys.exc_info()[1])
if message.startswith("\"molfile loader:") and \
message.endswith("queries\""): # hack to avoid user confusion
md, is_rxn = self.load_moldata(True)
else:
raise
md.aromatize()
return ["Ok.\n",
md.rxnfile() if is_rxn else md.molfile()]
@indigo_required
def on_getinchi(self):
md, is_rxn = self.load_moldata()
inchi = self.indigo_inchi.getInchi(md)
return ["Ok.\n", inchi]
@indigo_required
def on_dearomatize(self):
try:
md, is_rxn = self.load_moldata()
except: # TODO: test for query features presence
raise self.HttpException("400 Bad Request",
"Molecules and reactions " + \
"containing query features " + \
"cannot be dearomatized yet.")
md.dearomatize()
return ["Ok.\n",
md.rxnfile() if is_rxn else md.molfile()]
def on_open(self):
if self.is_form_request():
self.headers.add_header('Content-Type', 'text/html')
return ['<html><body onload="parent.ui.loadMoleculeFromFile()" title="',
b64encode("Ok.\n"),
b64encode(self.fields.getfirst('filedata')),
'"></body></html>']
self.notsupported()
def on_save(self):
if self.is_form_request():
type, data = self.fields.getfirst('filedata').split('\n', 1)
type = type.strip()
if type == 'smi':
self.headers.add_header('Content-Type',
'chemical/x-daylight-smiles')
elif type == 'mol':
if data.startswith('$RXN'):
type = 'rxn'
self.headers.add_header('Content-Type',
'chemical/x-mdl-%sfile' % type)
self.headers.add_header('Content-Length', str(len(data)))
self.headers.add_header('Content-Disposition', 'attachment',
filename='ketcher.%s' % type)
return [data]
self.notsupported()
class HttpException(Exception): pass
def load_moldata(self, is_query=False):
moldata = self.fields.getfirst('moldata')
if moldata.startswith('$RXN'):
if is_query:
md = self.indigo.loadQueryReaction(moldata)
else:
md = self.indigo.loadReaction(moldata)
is_rxn = True
else:
if is_query:
md = self.indigo.loadQueryMolecule(moldata)
else:
md = self.indigo.loadMolecule(moldata)
is_rxn = False
return md, is_rxn
def selective_layout(self, mol):
dsgs = [dsg for dsg in mol.iterateDataSGroups() \
if dsg.description() == '_ketcher_selective_layout' and \
dsg.data() == '1']
atoms = sorted([atom.index() for dsg in dsgs \
for atom in dsg.iterateAtoms()])
for dsg in dsgs:
dsg.remove()
mol.getSubmolecule(atoms).layout()
return mol
def serve_static(self):
root = self.static_root or getcwd()
fpath = self.static_alias.get(self.path, self.path)
fpath = path.abspath(path.join(root, fpath))
if not fpath.startswith(root + path.sep) or not path.isfile(fpath) \
or fpath == path.abspath(__file__):
raise self.HttpException("404 Not Found",
"Requested file isn't accessible")
self.headers['Content-Type'] = guess_type(fpath)[0] or 'text/plain'
try:
fd = open(fpath, 'rb')
return self.FileWrapper(fd) if self.method == 'GET' else ['']
except (IOError, OSError):
raise self.HttpException("402 Payment Required", # or 403, hmm..
"Must get more money for overtime")
def is_form_request(self):
return self.method == 'POST' and \
(self.content_type.startswith('application/x-www-form-urlencoded')
or self.content_type.startswith('multipart/form-data'))
try:
import indigo
import indigo_inchi
application.indigo = indigo.Indigo()
application.indigo_inchi = indigo_inchi.IndigoInchi(application.indigo)
application.indigo.setOption('ignore-stereochemistry-errors', 'true')
except:
pass
if __name__ == '__main__':
import socket
from wsgiref.simple_server import make_server
def parse_args():
arg = sys.argv[1] if len(sys.argv) > 1 else ''
res = arg.rsplit(':', 1)
return res[0] if len(res) > 1 else '0.0.0.0', \
int(res[-1] or 8080)
def usage():
progname = sys.argv[0]
if not progname.startswith('./'):
progname = 'python ' + progname
print( "USAGE:\n %s [port|address:port]\n" % progname)
if not application.indigo:
print("WARNING: Indigo is not found. Server-side functionality " + \
"will not be available")
application.static_serve = True # allow to serve static
try: # in standalone python-server mode
address, port = parse_args()
httpd = make_server(address, port, application)
print("Serving on %s:%d..." % (address, port))
httpd.serve_forever()
except ValueError:
usage()
except (socket.error, socket.gaierror, socket.herror, OSError) as e:
print("Server error: %s" % e.args[1])
usage()
except KeyboardInterrupt:
pass