Skip to content

Commit 75fd55b

Browse files
author
Jon Wedell
committed
Add search by name function.
1 parent 1caa195 commit 75fd55b

File tree

5 files changed

+30
-92
lines changed

5 files changed

+30
-92
lines changed

gateway.py

+15-75
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313

1414
# Installed packages
15+
import requests
1516
import psycopg2
1617
from flask import Flask, request, jsonify, redirect, render_template, url_for
1718
from psycopg2.extras import DictCursor
@@ -142,6 +143,20 @@ def home_page():
142143
return render_template("search_by_structure.html")
143144

144145

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+
145160
@application.route('/chemical_identifier_search')
146161
def identifier_search():
147162
""" Search for a compound by chemical identifier. """
@@ -156,81 +171,6 @@ def reroute():
156171
return redirect("query?term=%s" % term, code=302)
157172

158173

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-
234174
@application.route('/reload')
235175
def reload_db():
236176
""" Reload the DB."""

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
psycopg2-binary==2.7.6.1
22
flask==1.0.2
3+
requests==2.19.1

static/search.css

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
}
1111

1212
ul.dropdown-content.select-dropdown li span {
13-
color: #000; /* no need for !important :) */
13+
color: #000;
14+
}
15+
16+
.bold-blue {
17+
font-weight: bold;
18+
color: #000097;
1419
}
1520

1621
.checkbox-indigo[type="checkbox"] + label:before{
@@ -35,4 +40,4 @@ ul.dropdown-content.select-dropdown li span {
3540
border-left: 2px solid transparent;
3641
border-right: 2px solid #fff;
3742
border-bottom: 2px solid #fff;
38-
}
43+
}

templates/base.html

+1-15
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,12 @@
3737
</nav>
3838

3939
<div class="col s3">
40-
<!-- Instant search bar
41-
<div class="search">
42-
<form method="get" action="/search/results" target="_blank">
43-
<p><span style="font-weight: bold;">Search:</span>
44-
<input type="text" id="search" name="term" title="Search">
45-
</p>
46-
</form>
47-
</div>-->
4840
<div class="card blue lighten-2">
4941
<div class="card-content">
5042
<span class="card-title">News</span>
5143
<ul>
5244
<li><b>Aug 2018</b>: Initiation.</li>
45+
<li><b>March 2019</b>: Added <a href="{{url_for('name_search')}}" class="bold-blue">search by name</a> function.</li>
5346
</ul>
5447
</div>
5548
</div>
@@ -86,13 +79,6 @@ <h5 class="black-text">Other Servers</h5>
8679
</footer>
8780

8881
</div>
89-
<script type="text/javascript" src="/static/materialize.min.js"></script>
90-
<script>document.addEventListener('DOMContentLoaded', function() {
91-
var elems = document.querySelectorAll('select');
92-
var instances = M.FormSelect.init(elems, 'options');
93-
});
94-
95-
</script>
9682
</body>
9783
</html>
9884

templates/search_by_structure.html

+6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@
7171
</fieldset>
7272
</form>
7373

74+
<script type="text/javascript" src="/static/materialize.min.js"></script>
7475
<script>
76+
document.addEventListener('DOMContentLoaded', function() {
77+
var elems = document.querySelectorAll('select');
78+
var instances = M.FormSelect.init(elems, 'options');
79+
});
80+
7581
document.getElementById('infile').onchange = function() {
7682
// document.getElementById('FORMAT').selectedIndex =
7783
$('#FORMAT').val(this.value.split('.').pop());

0 commit comments

Comments
 (0)