-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Let's get redbiom-ing #2118
Merged
Merged
Let's get redbiom-ing #2118
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
06bc8b2
installing redbiom
antgonza 41f0fcd
classifiers=classifiers ??
antgonza 574c538
one of those days!
antgonza 4210a61
init commit
antgonza 2c194fe
Merge branch 'master' of https://github.com/biocore/qiita into redbio…
antgonza 36c41bf
apt redis
antgonza 8a7e9b7
redis-server &
antgonza a23c360
redis-server
antgonza a870b3c
Merge branch 'master' of https://github.com/biocore/qiita into redbio…
antgonza acda0d0
adding redbiom tests
antgonza 0e72f2a
redbiom setup.py
antgonza a03f1cd
redbiom #egg
antgonza 07fb481
ready for test env
antgonza 0e56fa0
Merge branch 'redbiom' of https://github.com/biocore/qiita into redbi…
antgonza 2da0aaf
should fix tests
antgonza c2a844b
redis 7777 &
antgonza f1840f7
before script redis-server
antgonza 0ac26f1
sudo: required
antgonza 27d9968
adding more tests and REDBIOM_HOST
antgonza e2bcaf9
rm redbiom from conf
antgonza 369628a
retriving artifact info via SQL
antgonza cc5afbb
retriving info from preps
antgonza 9bd1166
creating redbiom context
antgonza 28a9902
adding total of samples
antgonza 17e0780
rm button when not logged
antgonza d378518
addressing @wasade initial comments
antgonza 16fc478
new gui + search on categories
antgonza e41cdb4
adding a connection error message
antgonza babbede
request -> requests
antgonza 0fc122b
rm query from connection error
antgonza d46392e
changing callback placing
antgonza c64a019
addressing @josenavas comments
antgonza f886d32
flake8
antgonza 6e7cb7e
rm duplicated js imports
antgonza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
# ----------------------------------------------------------------------------- | ||
# Copyright (c) 2014--, The Qiita Development Team. | ||
# | ||
# Distributed under the terms of the BSD 3-clause License. | ||
# | ||
# The full license is in the file LICENSE, distributed with this software. | ||
# ----------------------------------------------------------------------------- | ||
|
||
from requests import ConnectionError | ||
import redbiom.summarize | ||
import redbiom.search | ||
import redbiom._requests | ||
import redbiom.util | ||
import redbiom.fetch | ||
from tornado.gen import coroutine, Task | ||
|
||
from qiita_core.util import execute_as_transaction | ||
|
||
from .base_handlers import BaseHandler | ||
|
||
|
||
class RedbiomPublicSearch(BaseHandler): | ||
@execute_as_transaction | ||
def get(self, search): | ||
self.render('redbiom.html') | ||
|
||
@execute_as_transaction | ||
def _redbiom_search(self, query, search_on, callback): | ||
error = False | ||
message = '' | ||
results = [] | ||
|
||
try: | ||
df = redbiom.summarize.contexts() | ||
except ConnectionError: | ||
error = True | ||
message = 'Redbiom is down - contact admin, thanks!' | ||
|
||
if not error: | ||
contexts = df.ContextName.values | ||
query = query.lower() | ||
features = [] | ||
|
||
if search_on in ('metadata', 'categories'): | ||
try: | ||
features = redbiom.search.metadata_full( | ||
query, categories=(search_on == 'categories')) | ||
except TypeError: | ||
error = True | ||
message = ( | ||
'Not a valid search: "%s", are you sure this is a ' | ||
'valid metadata %s?' % ( | ||
query, 'value' if search_on == 'metadata' else | ||
'category')) | ||
except ValueError: | ||
error = True | ||
message = ( | ||
'Not a valid search: "%s", your query is too small ' | ||
'(too few letters), try a longer query' % query) | ||
elif search_on == 'observations': | ||
features = [s.split('_', 1)[1] for context in contexts | ||
for s in redbiom.util.samples_from_observations( | ||
query.split(' '), True, context)] | ||
else: | ||
error = True | ||
message = ('Incorrect search by: you can use observations ' | ||
'or metadata and you passed: %s' % search_on) | ||
|
||
if not error: | ||
import qiita_db as qdb | ||
import qiita_db.sql_connection as qdbsc | ||
if features: | ||
if search_on in ('metadata', 'observations'): | ||
sql = """ | ||
WITH main_query AS ( | ||
SELECT study_title, study_id, artifact_id, | ||
array_agg(DISTINCT sample_id) AS samples, | ||
qiita.artifact_descendants(artifact_id) AS | ||
children | ||
FROM qiita.study_prep_template | ||
JOIN qiita.prep_template USING (prep_template_id) | ||
JOIN qiita.prep_template_sample USING | ||
(prep_template_id) | ||
JOIN qiita.study USING (study_id) | ||
WHERE sample_id IN %s | ||
GROUP BY study_title, study_id, artifact_id) | ||
SELECT study_title, study_id, samples, | ||
name, command_id, | ||
(main_query.children).artifact_id AS artifact_id | ||
FROM main_query | ||
JOIN qiita.artifact a ON | ||
(main_query.children).artifact_id = a.artifact_id | ||
JOIN qiita.artifact_type at ON ( | ||
at.artifact_type_id = a.artifact_type_id | ||
AND artifact_type = 'BIOM') | ||
ORDER BY artifact_id | ||
""" | ||
with qdbsc.TRN: | ||
qdbsc.TRN.add(sql, [tuple(features)]) | ||
results = [] | ||
commands = {} | ||
for row in qdbsc.TRN.execute_fetchindex(): | ||
title, sid, samples, name, cid, aid = row | ||
nr = {'study_title': title, 'study_id': sid, | ||
'artifact_id': aid, 'aname': name, | ||
'samples': samples} | ||
if cid is not None: | ||
if cid not in commands: | ||
c = qdb.software.Command(cid) | ||
commands[cid] = { | ||
'sfwn': c.software.name, | ||
'sfv': c.software.version, | ||
'cmdn': c.name | ||
} | ||
nr['command'] = commands[cid]['cmdn'] | ||
nr['software'] = commands[cid]['sfwn'] | ||
nr['version'] = commands[cid]['sfv'] | ||
else: | ||
nr['command'] = None | ||
nr['software'] = None | ||
nr['version'] = None | ||
results.append(nr) | ||
else: | ||
sql = """ | ||
WITH get_studies AS ( | ||
SELECT | ||
trim(table_name, 'sample_')::int AS | ||
study_id, | ||
array_agg(column_name::text) AS columns | ||
FROM information_schema.columns | ||
WHERE column_name IN %s | ||
AND table_name LIKE 'sample_%%' | ||
AND table_name NOT IN ( | ||
'prep_template', | ||
'prep_template_sample') | ||
GROUP BY table_name) | ||
SELECT study_title, get_studies.study_id, columns | ||
FROM get_studies | ||
JOIN qiita.study ON get_studies.study_id = | ||
qiita.study.study_id""" | ||
with qdbsc.TRN: | ||
results = [] | ||
qdbsc.TRN.add(sql, [tuple(features)]) | ||
for row in qdbsc.TRN.execute_fetchindex(): | ||
title, sid, cols = row | ||
nr = {'study_title': title, 'study_id': sid, | ||
'artifact_id': None, 'aname': None, | ||
'samples': cols, | ||
'command': ', '.join(cols), | ||
'software': None, 'version': None} | ||
results.append(nr) | ||
else: | ||
error = True | ||
message = 'No samples where found! Try again ...' | ||
callback((results, message)) | ||
|
||
@coroutine | ||
@execute_as_transaction | ||
def post(self, search): | ||
search = self.get_argument('search', None) | ||
search_on = self.get_argument('search_on', None) | ||
|
||
data = [] | ||
if search is not None and search and search != ' ': | ||
if search_on in ('observations', 'metadata', 'categories'): | ||
data, msg = yield Task( | ||
self._redbiom_search, search, search_on) | ||
else: | ||
msg = 'Not a valid option for search_on' | ||
else: | ||
msg = 'Nothing to search for ...' | ||
|
||
self.write({'status': 'success', 'message': msg, 'data': data}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if there is some other error raised? Are we allowing the 500 to be raised on the user screen or should we catch it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, I can see pros/cons on both (catch everything or just specific errors). I decided to go with specific so we can narrow down why it's failing. Note that we can see the errors in the logs if not caught. -- BTW I think we have caught all of them at this point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, just wanted to make sure that this was the desired behavior.