12
12
import subprocess
13
13
14
14
# Installed packages
15
+ import requests
15
16
import psycopg2
16
17
from flask import Flask , request , jsonify , redirect , render_template , url_for
17
18
from psycopg2 .extras import DictCursor
@@ -142,6 +143,20 @@ def home_page():
142
143
return render_template ("search_by_structure.html" )
143
144
144
145
146
+ @application .route ('/name' )
147
+ def name_search ():
148
+ """ Render the name search."""
149
+
150
+ term = request .args .get ('term' , "" )
151
+ if term :
152
+ results = requests .get ('http://alatis.nmrfam.wisc.edu/search/inchi' , params = {'term' : term }).json ()
153
+ else :
154
+ results = []
155
+ var_dict = {'title' : term , 'results' : results }
156
+
157
+ return render_template ("search_by_name.html" , ** var_dict )
158
+
159
+
145
160
@application .route ('/chemical_identifier_search' )
146
161
def identifier_search ():
147
162
""" Search for a compound by chemical identifier. """
@@ -156,81 +171,6 @@ def reroute():
156
171
return redirect ("query?term=%s" % term , code = 302 )
157
172
158
173
159
- @application .route ('/search/results' )
160
- def results ():
161
- """ Search results. """
162
- var_dict = {'title' : request .args .get ('term' , "" ),
163
- 'results' : search (local = True )}
164
-
165
- return render_template ("search.html" , ** var_dict )
166
-
167
-
168
- @application .route ('/search/query' )
169
- def search (local = False ):
170
- """ Search the DB. """
171
-
172
- term = request .args .get ('term' , None )
173
- if not term :
174
- return "Specify term."
175
-
176
- cur = get_postgres_connection (dictionary_cursor = True )[1 ]
177
-
178
- limit = " LIMIT 75;"
179
- if local :
180
- limit = ";"
181
-
182
- if request .args .get ('debug' , None ):
183
- return '''
184
- SELECT * FROM (
185
- SELECT id,db,term,termname,data_path,similarity(term, '%s') AS sml FROM search_terms
186
- WHERE lower(term) LIKE lower('%s')
187
- UNION
188
- SELECT id,db,term,termname,data_path,similarity(term, '%s') FROM search_terms
189
- WHERE identical_term @@ plainto_tsquery('%s')
190
- UNION
191
- (SELECT cm.id::text, 'PubChem', coalesce(cn.name, 'Unknown Name'), 'Compound',
192
- 'pubchem/'||cm.id, 1 FROM compound_metadata AS cm
193
- LEFT JOIN compound_name AS cn
194
- ON cn.id = cm.id WHERE cm.id=to_number('0'||'%s', '99999999999')::int ORDER BY cn.seq LIMIT 1)
195
- UNION
196
- (SELECT id::text, 'PubChem', name, 'Compound', 'pubchem/'||id, similarity(lower(name), lower(%s)) FROM compound_name
197
- WHERE lower(name) LIKE lower('%s') LIMIT 50)) AS f
198
- ORDER by sml DESC, 2!='PubChem', id ASC LIMIT 75;''' % (term , term + "%" , term , term , term , term , term + "%" )
199
-
200
- cur .execute ('''
201
- SELECT * FROM (
202
- SELECT id,db as database,term,termname,data_path,similarity(term, %s) AS sml FROM search_terms
203
- WHERE lower(term) LIKE lower(%s)
204
- UNION
205
- SELECT id,db,term,termname,data_path,similarity(term, %s) FROM search_terms
206
- WHERE identical_term @@ plainto_tsquery(%s)
207
- UNION
208
- (SELECT cm.id::text, 'PubChem', coalesce(cn.name, 'Unknown Name'), 'Compound',
209
- 'pubchem/'||cm.id, 1 FROM compound_metadata AS cm
210
- LEFT JOIN compound_name AS cn
211
- ON cn.id = cm.id WHERE cm.id=to_number('0'||%s, '99999999999')::int ORDER BY cn.seq LIMIT 1)
212
- UNION
213
- (SELECT id::text, 'PubChem', name, 'Compound', 'pubchem/'||id, similarity(lower(name), lower(%s)) FROM compound_name
214
- WHERE lower(name) LIKE lower(%s) LIMIT 50)) AS f
215
- ORDER by sml DESC, database!='PubChem', id ASC''' + limit , [term , term + "%" , term , term , term , term , term + "%" ])
216
-
217
- # First query
218
- result = []
219
- for item in cur .fetchall ():
220
- res = {"link" : item ['data_path' ],
221
- "db" : item ["database" ],
222
- "entry" : item ['id' ],
223
- "termname" : item ['termname' ],
224
- "term" : unicode (item ['term' ], 'utf-8' )}
225
-
226
- result .append (res )
227
-
228
- if not local :
229
- return jsonify (result )
230
-
231
- return result
232
-
233
-
234
174
@application .route ('/reload' )
235
175
def reload_db ():
236
176
""" Reload the DB."""
0 commit comments