Skip to content

Commit d46392e

Browse files
committed
changing callback placing
1 parent 0fc122b commit d46392e

File tree

1 file changed

+119
-105
lines changed

1 file changed

+119
-105
lines changed

qiita_pet/handlers/qiita_redbiom.py

Lines changed: 119 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -18,117 +18,131 @@ def get(self, search):
1818

1919
@execute_as_transaction
2020
def _redbiom_search(self, query, search_on, callback):
21+
error = False
22+
message = ''
23+
results = []
24+
2125
try:
2226
df = redbiom.summarize.contexts()
23-
contexts = df.ContextName.values
2427
except ConnectionError:
25-
callback(([], 'Redbiom is down - contact admin, thanks!'))
28+
error = True
29+
message = 'Redbiom is down - contact admin, thanks!'
2630

27-
query = query.lower()
28-
samples, categories = [], []
31+
if not error:
32+
contexts = df.ContextName.values
33+
query = query.lower()
34+
samples, categories = [], []
2935

30-
if search_on == 'metadata':
31-
try:
32-
samples = redbiom.search.metadata_full(query, categories=False)
33-
except TypeError:
34-
callback(([], 'Not a valid search: "%s", are you sure this is '
35-
'a valid metadata value?' % query))
36-
elif search_on == 'categories':
37-
try:
38-
categories = redbiom.search.metadata_full(query,
39-
categories=True)
40-
except ValueError:
41-
callback(([], 'Not a valid search: "%s", try a longer query'
42-
% query))
43-
except TypeError:
44-
callback(([], 'Not a valid search: "%s", are you sure this is '
45-
'a valid metadata category?' % query))
46-
elif search_on == 'observations':
47-
samples = [s.split('_', 1)[1] for context in contexts
48-
for s in redbiom.util.samples_from_observations(
49-
query.split(' '), True, context)]
50-
else:
51-
callback(([], 'Incorrect search by: you can use observations '
52-
'or metadata and you passed: %s' % search_on))
36+
if search_on == 'metadata':
37+
try:
38+
samples = redbiom.search.metadata_full(
39+
query, categories=False)
40+
except TypeError:
41+
error = True
42+
message = ('Not a valid search: "%s", are you sure this '
43+
'is a valid metadata value?' % query)
44+
elif search_on == 'categories':
45+
try:
46+
categories = redbiom.search.metadata_full(query,
47+
categories=True)
48+
except ValueError:
49+
error = True
50+
message = ('Not a valid search: "%s", try a longer query'
51+
% query)
52+
except TypeError:
53+
error = True
54+
message = ('Not a valid search: "%s", are you sure this '
55+
'is a valid metadata category?' % query)
56+
elif search_on == 'observations':
57+
samples = [s.split('_', 1)[1] for context in contexts
58+
for s in redbiom.util.samples_from_observations(
59+
query.split(' '), True, context)]
60+
else:
61+
error = True
62+
message = ('Incorrect search by: you can use observations '
63+
'or metadata and you passed: %s' % search_on)
5364

54-
import qiita_db as qdb
55-
import qiita_db.sql_connection as qdbsc
56-
if samples:
57-
sql = """
58-
WITH main_query AS (
59-
SELECT study_title, study_id, artifact_id,
60-
array_agg(DISTINCT sample_id) AS samples,
61-
qiita.artifact_descendants(artifact_id) AS children
62-
FROM qiita.study_prep_template
63-
JOIN qiita.prep_template USING (prep_template_id)
64-
JOIN qiita.prep_template_sample USING (prep_template_id)
65-
JOIN qiita.study USING (study_id)
66-
WHERE sample_id IN %s
67-
GROUP BY study_title, study_id, artifact_id)
68-
SELECT study_title, study_id, samples, name, command_id,
69-
(main_query.children).artifact_id AS artifact_id
70-
FROM main_query
71-
JOIN qiita.artifact a ON (main_query.children).artifact_id =
72-
a.artifact_id
73-
JOIN qiita.artifact_type at ON (
74-
at.artifact_type_id = a.artifact_type_id
75-
AND artifact_type = 'BIOM')
76-
ORDER BY artifact_id
77-
"""
78-
with qdbsc.TRN:
79-
qdbsc.TRN.add(sql, [tuple(samples)])
80-
results = []
81-
commands = {}
82-
for row in qdbsc.TRN.execute_fetchindex():
83-
title, sid, samples, name, cid, aid = row
84-
nr = {'study_title': title, 'study_id': sid,
85-
'artifact_id': aid, 'aname': name,
86-
'samples': samples}
87-
if cid is not None:
88-
if cid not in commands:
89-
c = qdb.software.Command(cid)
90-
commands[cid] = {
91-
'sfwn': c.software.name,
92-
'sfv': c.software.version,
93-
'cmdn': c.name
94-
}
95-
nr['command'] = commands[cid]['cmdn']
96-
nr['software'] = commands[cid]['sfwn']
97-
nr['version'] = commands[cid]['sfv']
98-
else:
99-
nr['command'] = None
100-
nr['software'] = None
101-
nr['version'] = None
102-
results.append(nr)
103-
callback((results, ''))
104-
elif categories:
105-
sql = """
106-
WITH get_studies AS (
107-
SELECT trim(table_name, 'sample_')::int AS study_id,
108-
array_agg(column_name::text) AS columns
109-
FROM information_schema.columns
110-
WHERE column_name IN %s
111-
AND table_name LIKE 'sample_%%'
112-
AND table_name NOT IN (
113-
'prep_template', 'prep_template_sample')
114-
GROUP BY table_name)
115-
SELECT study_title, get_studies.study_id, columns
116-
-- artifact_id, samples
117-
FROM get_studies
118-
JOIN qiita.study ON get_studies.study_id =
119-
qiita.study.study_id"""
120-
with qdbsc.TRN:
121-
results = []
122-
qdbsc.TRN.add(sql, [tuple(categories)])
123-
for title, sid, cols in qdbsc.TRN.execute_fetchindex():
124-
nr = {'study_title': title, 'study_id': sid,
125-
'artifact_id': None, 'aname': None,
126-
'samples': cols, 'command': ', '.join(cols),
127-
'software': None, 'version': None}
128-
results.append(nr)
129-
callback((results, ''))
130-
else:
131-
callback(([], 'No samples where found! Try again ...'))
65+
if not error:
66+
import qiita_db as qdb
67+
import qiita_db.sql_connection as qdbsc
68+
if samples:
69+
sql = """
70+
WITH main_query AS (
71+
SELECT study_title, study_id, artifact_id,
72+
array_agg(DISTINCT sample_id) AS samples,
73+
qiita.artifact_descendants(artifact_id) AS children
74+
FROM qiita.study_prep_template
75+
JOIN qiita.prep_template USING (prep_template_id)
76+
JOIN qiita.prep_template_sample USING
77+
(prep_template_id)
78+
JOIN qiita.study USING (study_id)
79+
WHERE sample_id IN %s
80+
GROUP BY study_title, study_id, artifact_id)
81+
SELECT study_title, study_id, samples, name, command_id,
82+
(main_query.children).artifact_id AS artifact_id
83+
FROM main_query
84+
JOIN qiita.artifact a ON
85+
(main_query.children).artifact_id = a.artifact_id
86+
JOIN qiita.artifact_type at ON (
87+
at.artifact_type_id = a.artifact_type_id
88+
AND artifact_type = 'BIOM')
89+
ORDER BY artifact_id
90+
"""
91+
with qdbsc.TRN:
92+
qdbsc.TRN.add(sql, [tuple(samples)])
93+
results = []
94+
commands = {}
95+
for row in qdbsc.TRN.execute_fetchindex():
96+
title, sid, samples, name, cid, aid = row
97+
nr = {'study_title': title, 'study_id': sid,
98+
'artifact_id': aid, 'aname': name,
99+
'samples': samples}
100+
if cid is not None:
101+
if cid not in commands:
102+
c = qdb.software.Command(cid)
103+
commands[cid] = {
104+
'sfwn': c.software.name,
105+
'sfv': c.software.version,
106+
'cmdn': c.name
107+
}
108+
nr['command'] = commands[cid]['cmdn']
109+
nr['software'] = commands[cid]['sfwn']
110+
nr['version'] = commands[cid]['sfv']
111+
else:
112+
nr['command'] = None
113+
nr['software'] = None
114+
nr['version'] = None
115+
results.append(nr)
116+
elif categories:
117+
sql = """
118+
WITH get_studies AS (
119+
SELECT
120+
trim(table_name, 'sample_')::int AS study_id,
121+
array_agg(column_name::text) AS columns
122+
FROM information_schema.columns
123+
WHERE column_name IN %s
124+
AND table_name LIKE 'sample_%%'
125+
AND table_name NOT IN (
126+
'prep_template', 'prep_template_sample')
127+
GROUP BY table_name)
128+
SELECT study_title, get_studies.study_id, columns
129+
-- artifact_id, samples
130+
FROM get_studies
131+
JOIN qiita.study ON get_studies.study_id =
132+
qiita.study.study_id"""
133+
with qdbsc.TRN:
134+
results = []
135+
qdbsc.TRN.add(sql, [tuple(categories)])
136+
for title, sid, cols in qdbsc.TRN.execute_fetchindex():
137+
nr = {'study_title': title, 'study_id': sid,
138+
'artifact_id': None, 'aname': None,
139+
'samples': cols, 'command': ', '.join(cols),
140+
'software': None, 'version': None}
141+
results.append(nr)
142+
else:
143+
error = True
144+
message = 'No samples where found! Try again ...'
145+
callback((results, message))
132146

133147
@coroutine
134148
@execute_as_transaction

0 commit comments

Comments
 (0)