Skip to content

Commit

Permalink
fix #2569
Browse files Browse the repository at this point in the history
  • Loading branch information
antgonza committed Jan 15, 2019
1 parent eea8ca2 commit 09fa4ee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
27 changes: 14 additions & 13 deletions qiita_pet/handlers/qiita_redbiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# -----------------------------------------------------------------------------

from requests import ConnectionError
from future.utils import viewitems
from collections import defaultdict
import redbiom.summarize
import redbiom.search
Expand All @@ -18,7 +17,6 @@

from qiita_core.util import execute_as_transaction
from qiita_db.util import generate_study_list_without_artifacts
from qiita_db.study import Study

from .base_handlers import BaseHandler

Expand All @@ -29,10 +27,10 @@ def get(self, search):
self.render('redbiom.html')

def _redbiom_metadata_search(self, query, contexts):
study_artifacts = defaultdict(list)
study_artifacts = defaultdict(lambda: defaultdict(list))
message = ''
try:
samples = redbiom.search.metadata_full(query, False)
redbiom_samples = redbiom.search.metadata_full(query, False)
except ValueError:
message = (
'Not a valid search: "%s", your query is too small '
Expand All @@ -43,13 +41,17 @@ def _redbiom_metadata_search(self, query, contexts):
'check the search help for more information on the queries.'
% query)
if not message:
study_samples = defaultdict(list)
for s in samples:
study_samples[s.split('.', 1)[0]].append(s)
for sid, samps in viewitems(study_samples):
study_artifacts[sid] = {
a.id: samps for a in Study(sid).artifacts(
artifact_type='BIOM')}
study_artifacts = defaultdict(lambda: defaultdict(list))
for ctx in contexts:
# redbiom.fetch.data_from_samples returns a biom, which we
# will ignore, and a dict
_, data = redbiom.fetch.data_from_samples(ctx, redbiom_samples)
for vals in data.values():
for idx in vals:
print idx
aid, sample_id = idx.split('_', 1)
sid = sample_id.split('.', 1)[0]
study_artifacts[sid][aid].append(sample_id)

return message, study_artifacts

Expand All @@ -70,8 +72,7 @@ def _redbiom_taxon_search(self, query, contexts):
# find the features with those taxonomies and then search
# those features in the samples
features = redbiom.fetch.taxon_descendents(ctx, query)
for idx in redbiom.util.ids_from(features, True, 'feature',
ctx):
for idx in redbiom.util.ids_from(features, True, 'feature', ctx):
aid, sample_id = idx.split('_', 1)
sid = sample_id.split('.', 1)[0]
study_artifacts[sid][aid].append(sample_id)
Expand Down
8 changes: 3 additions & 5 deletions qiita_pet/test/test_qiita_redbiom.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ def test_post_metadata(self):
response = self.post('/redbiom/', post_args)
self.assertEqual(response.code, 200)

samples = ['1.SKD6.640190', '1.SKD9.640182', '1.SKD8.640184',
'1.SKD5.640186', '1.SKD2.640178', '1.SKD4.640185',
'1.SKD1.640179', '1.SKD3.640198', '1.SKD7.640191']
exp_artifact_biom_ids = {
'5': samples, '4': samples, '7': samples, '6': samples}
'5': ['1.SKD2.640178'],
'4': ['1.SKD2.640178', '1.SKD8.640184']}
response_body = loads(response.body)
obs_artifact_biom_ids = response_body['data'][0].pop(
'artifact_biom_ids')
self.assertItemsEqual(obs_artifact_biom_ids, exp_artifact_biom_ids)
self.assertDictEqual(obs_artifact_biom_ids, exp_artifact_biom_ids)
exp = {'status': 'success', 'message': '', 'data': DATA}
self.assertEqual(response_body, exp)

Expand Down

0 comments on commit 09fa4ee

Please sign in to comment.