-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway.py
277 lines (230 loc) · 9.38 KB
/
gateway.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
#!/usr/bin/env python3
""" Power the gateway server. """
import os
import re
import shutil
import tempfile
from contextlib import contextmanager
from time import sleep
import subprocess
# Installed packages
import requests
import psycopg2
from flask import Flask, request, jsonify, redirect, render_template, url_for
from psycopg2.extras import DictCursor
# Set up the flask application
application = Flask(__name__)
dir_path = os.path.dirname(os.path.realpath(__file__))
def get_postgres_connection(user='web', database='webservers', host="localhost",
dictionary_cursor=False):
""" Returns a connection to postgres and a cursor."""
if dictionary_cursor:
conn = psycopg2.connect(user=user, database=database, host=host, cursor_factory=DictCursor)
else:
conn = psycopg2.connect(user=user, database=database, host=host)
cur = conn.cursor()
cur.execute("SET search_path TO dci, public")
return conn, cur
@contextmanager
def TemporaryDirectory():
name = tempfile.mkdtemp()
try:
yield name
finally:
shutil.rmtree(name)
@application.route('/upload', methods=['POST'])
def upload_file():
# get posted parameters
file_ = request.files.get('infile', None)
input_text = request.form.get('inputtext', '')
input_format = request.form.get("FORMAT", 'mol')
input_project = request.form.get("proj2to3", '')
input_addhyd = request.form.get('addHydr', '')
if input_project == 'on':
projection_3d = '1'
else:
projection_3d = '0'
if input_addhyd == 'on':
add_hyd = '1'
else:
add_hyd = '0'
# if there is no uploaded structure
if not file_ and not input_text:
return render_template("error.html", error='No uploaded file or pasted file.')
with TemporaryDirectory() as folder_path:
if not file_:
open(os.path.join(folder_path, 'submitted.data'), 'w').write(input_text)
else:
file_.save(os.path.join(folder_path, 'submitted.data'))
variables = {'binary_path': os.path.join(dir_path, 'binary', 'get_inchi.py'),
'input_format': input_format,
'projection_3d': projection_3d,
'add_hyd': add_hyd}
with open(os.path.join(folder_path, 'inchi.sub'), 'w') as fout:
fout.write("""universe = vanilla
executable = get_inchi.py
arguments = submitted.data {input_format} {projection_3d} {add_hyd}
error = temp.err
output = temp.out
log = temp.log
should_transfer_files = yes
transfer_input_files = inchi-1, submitted.data
when_to_transfer_output = on_exit
transfer_output_files = inchi.txt
periodic_remove = (time() - QDate) > 7200
queue
""".format(**variables))
shutil.copy(os.path.join(dir_path, 'binary', 'inchi-1'), os.path.join(folder_path, 'inchi-1'))
shutil.copy(variables['binary_path'], folder_path)
os.chdir(folder_path)
subprocess.call(['condor_submit', 'inchi.sub'])
ipath = os.path.join(folder_path, 'inchi.txt')
timeout = 0
while True:
# Easier to ask for forgiveness than permission
try:
err = open(os.path.join(folder_path, 'temp.out'), 'r').read()
if len(err) > 0:
return render_template("error.html", error=err)
inchi = open(ipath, 'r').read()
if len(inchi) == 0:
raise ValueError
return redirect(url_for('inchi_search', inchi=inchi))
# Results are not yet ready
except (IOError, ValueError):
sleep(.1)
timeout += .1
if timeout > 120:
return render_template("error.html", error='Timeout when calculating the InChI string.')
@application.route('/inchi')
@application.route('/inchi/<path:inchi>')
def inchi_search(inchi=None):
""" Show the results for a given InChI. """
# The aren't using the URL, they are sending as parameter
if inchi is None:
inchi = request.args.get('inchi', '')
if not inchi:
return []
# Strip off a trailing path if needed
if inchi.endswith('/'):
inchi = inchi[:-1]
if not inchi.startswith('InChI='):
inchi = 'InChI=' + inchi
# InChI=1S/C6H12O6/c7-1-3(9)5(11)6(12)4(10)2-8/h3,5-9,11-12H,1-2H2/t3?,5?,6u/m1/s1
def enumerate_chirality(chiral_inchi_segment):
for chiral_center in re.finditer('[u?]', chiral_inchi_segment):
position = chiral_center.span()[0]
left_hand = enumerate_chirality(
chiral_inchi_segment[0:position] + '+' + chiral_inchi_segment[position + 1:])
right_hand = enumerate_chirality(
chiral_inchi_segment[0:position] + '-' + chiral_inchi_segment[position + 1:])
return right_hand + left_hand
return [chiral_inchi_segment]
try:
chiral_start = inchi.index(r'/t') + 1
chiral_end = inchi.index('/', chiral_start)
replace_from = inchi[chiral_start:chiral_end]
chiral_options = enumerate_chirality(replace_from)
except ValueError:
chiral_options = ['']
replace_from = 'DO NOT REPLACE'
request_format = request.args.get('format', 'html')
# Have to query multiple times or postgres doesn't realize it can use the index - so weird
cur = get_postgres_connection(dictionary_cursor=True)[1]
sql = 'SELECT inchi,alatis_ids as pubmed_ids,gissmo_ids,camp_ids,bmod_ids, names FROM dci.db_links where inchi=%s'
results = []
for chiral_chunk in chiral_options:
cur.execute(sql, [inchi.replace(replace_from, chiral_chunk)])
result = cur.fetchone()
if result:
if request_format == 'json':
results.append(dict(result))
else:
results.append(result)
if request.args.get('format', 'html') == 'json':
return jsonify(results)
if results:
# No chirality - go straight to the results
if len(chiral_options) == 1:
return render_template("multi_search.html", inchi=inchi, matches=results[0], chiral_matches=results,
active='result')
else:
# Chirality - display the results page
return render_template("multi_search.html", inchi=inchi, chiral_matches=results, active='inchi')
else:
# No matches
return render_template("multi_search.html", inchi=inchi, active='inchi',
error="No compound matching that InChI was found in the database.")
@application.route('/')
def home_page():
""" Render the home page."""
return render_template("multi_search.html", active='structure')
@application.route('/name')
def name_search():
""" Render the name search."""
term = request.args.get('term', "")
if term:
results = requests.get('https://alatis.bmrb.io/search/inchi', params={'term': term}).json()
else:
results = None
var_dict = {'title': term, 'results': results, 'active': 'name'}
if request.args.get('format', 'html') == 'json':
return jsonify(results)
return render_template("multi_search.html", **var_dict)
@application.route('/reload')
def reload_db():
""" Reload the DB."""
# Open the DB and clear the existing index
conn, cur = get_postgres_connection(user='postgres')
cur.execute('''
CREATE MATERIALIZED VIEW IF NOT EXISTS dci.inchi_index_tmp AS SELECT DISTINCT(inchi) FROM (
SELECT inchi FROM gissmo.entries
UNION
SELECT inchi FROM camp.camp
UNION
SELECT inchi FROM bmod.bmod_index
UNION
SELECT inchi FROM alatis.compound_alatis) s
WHERE inchi IS NOT NULL and inchi != '' and inchi != 'FAILED';
CREATE UNIQUE INDEX ON dci.inchi_index_tmp (inchi);
ALTER MATERIALIZED VIEW IF EXISTS dci.inchi_index RENAME TO inchi_index_old;
ALTER MATERIALIZED VIEW dci.inchi_index_tmp RENAME TO inchi_index;
DROP MATERIALIZED VIEW IF EXISTS dci.inchi_index_old CASCADE;
DROP VIEW IF EXISTS dci.names CASCADE;
CREATE VIEW dci.names AS
select inchi, array_remove(array_agg(name ORDER BY sequence), NULL) as name FROM
(SELECT ii.inchi, cn.name, cn.seq as sequence
FROM dci.inchi_index as ii
LEFT JOIN alatis.compound_alatis as ca on ii.inchi=ca.inchi
LEFT JOIN alatis.compound_name as cn on ca.id=cn.id
UNION ALL
SELECT ii.inchi, g.name, 0
FROM dci.inchi_index as ii
LEFT JOIN gissmo.entries as g on ii.inchi=g.inchi
UNION ALL
SELECT ii.inchi, unnest(c.name) as name, 100
FROM dci.inchi_index as ii
LEFT JOIN camp.camp as c ON ii.inchi=c.inchi) as why
GROUP BY inchi;
DROP VIEW IF EXISTS dci.db_links;
CREATE VIEW dci.db_links AS SELECT
d.inchi,
array_remove(array_agg(DISTINCT a.id), NULL) as alatis_ids,
array_remove(array_agg(DISTINCT g.id), NULL) as gissmo_ids,
array_remove(array_agg(DISTINCT c.id), NULL) as camp_ids,
array_remove(array_agg(DISTINCT b.id), NULL) as bmod_ids,
n.name as names
FROM dci.inchi_index AS d
LEFT JOIN alatis.compound_alatis AS a ON a.inchi=d.inchi
LEFT JOIN gissmo.entries AS g ON g.inchi=d.inchi
LEFT JOIN camp.camp AS c ON c.inchi=d.inchi
LEFT JOIN bmod.bmod_index AS b ON b.inchi=d.inchi
LEFT JOIN names AS n ON n.inchi=d.inchi
GROUP BY d.inchi, n.name;
GRANT USAGE ON SCHEMA dci TO web;
GRANT SELECT ON ALL TABLES IN SCHEMA dci TO web;
''')
conn.commit()
return redirect(url_for('home_page'), 302)
if __name__ == "__main__":
reload_db()