Skip to content

Commit c62daae

Browse files
committed
merge upstream/artifact-study-pages
2 parents 028615e + 85662c5 commit c62daae

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1130
-93
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,31 @@ def update_category(self, category, samples_and_values):
13201320

13211321
raise e
13221322

1323+
def get_category(self, category):
1324+
"""Returns the values of all samples for the given category
1325+
1326+
Parameters
1327+
----------
1328+
category : str
1329+
Metadata category to get information for
1330+
1331+
Returns
1332+
-------
1333+
dict
1334+
Sample metadata for the category in the form {sample_id: value}
1335+
1336+
Raises
1337+
------
1338+
QiitaDBColumnError
1339+
If category is not part of the template
1340+
"""
1341+
with qdb.sql_connection.TRN:
1342+
qdb.util.check_table_cols([category], self._table_name(self._id))
1343+
sql = 'SELECT sample_id, {0} FROM qiita.{1}'.format(
1344+
category, self._table_name(self._id))
1345+
qdb.sql_connection.TRN.add(sql)
1346+
return dict(qdb.sql_connection.TRN.execute_fetchindex())
1347+
13231348
def check_restrictions(self, restrictions):
13241349
"""Checks if the template fulfills the restrictions
13251350

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,44 @@ def test_clean_validate_template(self):
765765
exp.sort_index(axis=1, inplace=True)
766766
assert_frame_equal(obs, exp)
767767

768+
def test_get_category(self):
769+
pt = qdb.metadata_template.prep_template.PrepTemplate(1)
770+
obs = pt.get_category('primer')
771+
exp = {
772+
'1.SKB2.640194': 'GTGCCAGCMGCCGCGGTAA',
773+
'1.SKM4.640180': 'GTGCCAGCMGCCGCGGTAA',
774+
'1.SKB3.640195': 'GTGCCAGCMGCCGCGGTAA',
775+
'1.SKB6.640176': 'GTGCCAGCMGCCGCGGTAA',
776+
'1.SKD6.640190': 'GTGCCAGCMGCCGCGGTAA',
777+
'1.SKM6.640187': 'GTGCCAGCMGCCGCGGTAA',
778+
'1.SKD9.640182': 'GTGCCAGCMGCCGCGGTAA',
779+
'1.SKM8.640201': 'GTGCCAGCMGCCGCGGTAA',
780+
'1.SKM2.640199': 'GTGCCAGCMGCCGCGGTAA',
781+
'1.SKD2.640178': 'GTGCCAGCMGCCGCGGTAA',
782+
'1.SKB7.640196': 'GTGCCAGCMGCCGCGGTAA',
783+
'1.SKD4.640185': 'GTGCCAGCMGCCGCGGTAA',
784+
'1.SKB8.640193': 'GTGCCAGCMGCCGCGGTAA',
785+
'1.SKM3.640197': 'GTGCCAGCMGCCGCGGTAA',
786+
'1.SKD5.640186': 'GTGCCAGCMGCCGCGGTAA',
787+
'1.SKB1.640202': 'GTGCCAGCMGCCGCGGTAA',
788+
'1.SKM1.640183': 'GTGCCAGCMGCCGCGGTAA',
789+
'1.SKD1.640179': 'GTGCCAGCMGCCGCGGTAA',
790+
'1.SKD3.640198': 'GTGCCAGCMGCCGCGGTAA',
791+
'1.SKB5.640181': 'GTGCCAGCMGCCGCGGTAA',
792+
'1.SKB4.640189': 'GTGCCAGCMGCCGCGGTAA',
793+
'1.SKB9.640200': 'GTGCCAGCMGCCGCGGTAA',
794+
'1.SKM9.640192': 'GTGCCAGCMGCCGCGGTAA',
795+
'1.SKD8.640184': 'GTGCCAGCMGCCGCGGTAA',
796+
'1.SKM5.640177': 'GTGCCAGCMGCCGCGGTAA',
797+
'1.SKM7.640188': 'GTGCCAGCMGCCGCGGTAA',
798+
'1.SKD7.640191': 'GTGCCAGCMGCCGCGGTAA'}
799+
self.assertEqual(obs, exp)
800+
801+
def test_get_category_no_exists(self):
802+
pt = qdb.metadata_template.prep_template.PrepTemplate(1)
803+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
804+
pt.get_category('DOESNOTEXIST')
805+
768806

769807
@qiita_test_checker()
770808
class TestPrepTemplateReadWrite(BaseTestPrepTemplate):

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,30 @@ def test_clean_validate_template(self):
879879
exp.sort_index(axis=1, inplace=True)
880880
assert_frame_equal(obs, exp)
881881

882+
def test_get_category(self):
883+
pt = qdb.metadata_template.sample_template.SampleTemplate(1)
884+
obs = pt.get_category('latitude')
885+
exp = {'1.SKB2.640194': 35.2374368957, '1.SKM4.640180': None,
886+
'1.SKB3.640195': 95.2060749748, '1.SKB6.640176': 78.3634273709,
887+
'1.SKD6.640190': 29.1499460692, '1.SKM6.640187': 0.291867635913,
888+
'1.SKD9.640182': 23.1218032799, '1.SKM8.640201': 3.21190859967,
889+
'1.SKM2.640199': 82.8302905615, '1.SKD2.640178': 53.5050692395,
890+
'1.SKB7.640196': 13.089194595, '1.SKD4.640185': 40.8623799474,
891+
'1.SKB8.640193': 74.0894932572, '1.SKM3.640197': None,
892+
'1.SKD5.640186': 85.4121476399, '1.SKB1.640202': 4.59216095574,
893+
'1.SKM1.640183': 38.2627021402, '1.SKD1.640179': 68.0991287718,
894+
'1.SKD3.640198': 84.0030227585, '1.SKB5.640181': 10.6655599093,
895+
'1.SKB4.640189': 43.9614715197, '1.SKB9.640200': 12.6245524972,
896+
'1.SKM9.640192': 12.7065957714, '1.SKD8.640184': 57.571893782,
897+
'1.SKM5.640177': 44.9725384282, '1.SKM7.640188': 60.1102854322,
898+
'1.SKD7.640191': 68.51099627}
899+
self.assertEqual(obs, exp)
900+
901+
def test_get_category_no_exists(self):
902+
pt = qdb.metadata_template.sample_template.SampleTemplate(1)
903+
with self.assertRaises(qdb.exceptions.QiitaDBColumnError):
904+
pt.get_category('DOESNOTEXIST')
905+
882906

883907
@qiita_test_checker()
884908
class TestSampleTemplateReadWrite(BaseTestSampleTemplate):

qiita_pet/handlers/api_proxy/__init__.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
from .sample_template import (
1515
sample_template_post_req, sample_template_put_req,
1616
sample_template_summary_get_req, sample_template_delete_req,
17-
sample_template_filepaths_get_req)
17+
sample_template_filepaths_get_req, sample_template_get_req,
18+
sample_template_meta_cats_get_req, sample_template_samples_get_req,
19+
sample_template_category_get_req)
1820
from .prep_template import (
1921
prep_template_summary_get_req, prep_template_post_req,
2022
prep_template_put_req, prep_template_delete_req, prep_template_get_req,
2123
prep_template_graph_get_req, prep_template_filepaths_get_req,
22-
ena_ontology_get_req)
23-
from .studies import data_types_get_req, study_get_req, study_prep_get_req
24+
ena_ontology_get_req, prep_template_samples_get_req)
25+
from .studies import (
26+
data_types_get_req, study_get_req, study_prep_get_req, study_delete_req)
2427
from .artifact import artifact_graph_get_req
2528

2629
__all__ = ['prep_template_summary_get_req', 'sample_template_post_req',
@@ -30,5 +33,9 @@
3033
'prep_template_summary_get_req', 'prep_template_post_req',
3134
'prep_template_put_req', 'prep_template_delete_req',
3235
'prep_template_graph_get_req', 'prep_template_filepaths_get_req',
33-
'artifact_graph_get_req', 'prep_template_get_req',
34-
'study_prep_get_req', 'ena_ontology_get_req']
36+
'artifact_get_graph', 'prep_template_get_req', 'study_delete_req',
37+
'study_prep_get_req', 'sample_template_get_req',
38+
'artifact_graph_get_req', 'ena_ontology_get_req',
39+
'sample_template_meta_cats_get_req',
40+
'sample_template_samples_get_req', 'prep_template_samples_get_req',
41+
'sample_template_category_get_req']

qiita_pet/handlers/api_proxy/prep_template.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ def _process_investigation_type(inv_type, user_def_type, new_type):
5050
return inv_type
5151

5252

53+
def _check_prep_template_exists(prep_id):
54+
"""Make sure a prep template exists in the system
55+
56+
Parameters
57+
----------
58+
prep_id : int or str castable to int
59+
PrepTemplate id to check
60+
61+
Returns
62+
-------
63+
dict
64+
{'status': status,
65+
'message': msg}
66+
"""
67+
if not PrepTemplate.exists(int(prep_id)):
68+
return {'status': 'error',
69+
'message': 'Prep template %d does not exist' % int(prep_id)
70+
}
71+
return {'status': 'success',
72+
'message': ''}
73+
74+
5375
def prep_template_get_req(prep_id, user_id):
5476
"""Gets the json of the full prep template
5577
@@ -67,6 +89,10 @@ def prep_template_get_req(prep_id, user_id):
6789
'message': message,
6890
'template': {sample: {column: value, ...}, ...}
6991
"""
92+
exists = _check_prep_template_exists(int(prep_id))
93+
if exists['status'] != 'success':
94+
return exists
95+
7096
prep = PrepTemplate(int(prep_id))
7197
access_error = check_access(prep.study_id, user_id)
7298
if access_error:
@@ -98,6 +124,10 @@ def prep_template_summary_get_req(prep_id, user_id):
98124
'num_samples': value,
99125
'category': [(val1, count1), (val2, count2), ...], ...}
100126
"""
127+
exists = _check_prep_template_exists(int(prep_id))
128+
if exists['status'] != 'success':
129+
return exists
130+
101131
prep = PrepTemplate(int(prep_id))
102132
access_error = check_access(prep.study_id, user_id)
103133
if access_error:
@@ -187,6 +217,38 @@ def prep_template_post_req(study_id, user_id, prep_template, data_type,
187217
'file': prep_template}
188218

189219

220+
def prep_template_samples_get_req(prep_id, user_id):
221+
"""Returns list of samples in the prep template
222+
223+
Parameters
224+
----------
225+
prep_id : int or str typecastable to int
226+
PrepTemplate id to get info for
227+
user_id : str
228+
User requesting the prep template info
229+
230+
Returns
231+
-------
232+
dict
233+
Returns summary information in the form
234+
{'status': str,
235+
'message': str,
236+
'samples': list of str}
237+
samples is list of samples in the template
238+
"""
239+
exists = _check_prep_template_exists(int(prep_id))
240+
if exists['status'] != 'success':
241+
return exists
242+
prep = PrepTemplate(int(prep_id))
243+
access_error = check_access(prep.study_id, user_id)
244+
if access_error:
245+
return access_error
246+
return {'status': 'success',
247+
'message': '',
248+
'samples': sorted(x for x in PrepTemplate(int(prep_id)))
249+
}
250+
251+
190252
@execute_as_transaction
191253
def prep_template_put_req(prep_id, user_id, prep_template=None,
192254
investigation_type=None,
@@ -216,6 +278,10 @@ def prep_template_put_req(prep_id, user_id, prep_template=None,
216278
'message': message,
217279
'file': prep_template}
218280
"""
281+
exists = _check_prep_template_exists(int(prep_id))
282+
if exists['status'] != 'success':
283+
return exists
284+
219285
prep = PrepTemplate(int(prep_id))
220286
study_id = prep.study_id
221287
access_error = check_access(study_id, user_id)
@@ -276,11 +342,14 @@ def prep_template_delete_req(prep_id, user_id):
276342
{'status': status,
277343
'message': message}
278344
"""
345+
exists = _check_prep_template_exists(int(prep_id))
346+
if exists['status'] != 'success':
347+
return exists
348+
279349
prep = PrepTemplate(int(prep_id))
280350
access_error = check_access(prep.study_id, user_id)
281351
if access_error:
282352
return access_error
283-
284353
msg = ''
285354
status = 'success'
286355
try:
@@ -311,6 +380,10 @@ def prep_template_filepaths_get_req(prep_id, user_id):
311380
'message': message,
312381
'filepaths': [(filepath_id, filepath), ...]}
313382
"""
383+
exists = _check_prep_template_exists(int(prep_id))
384+
if exists['status'] != 'success':
385+
return exists
386+
314387
prep = PrepTemplate(int(prep_id))
315388
access_error = check_access(prep.study_id, user_id)
316389
if access_error:
@@ -370,6 +443,10 @@ def prep_template_graph_get_req(prep_id, user_id):
370443
-----
371444
Nodes are identified by the corresponding Artifact ID.
372445
"""
446+
exists = _check_prep_template_exists(int(prep_id))
447+
if exists['status'] != 'success':
448+
return exists
449+
373450
prep = PrepTemplate(int(prep_id))
374451
access_error = check_access(prep.study_id, user_id)
375452
if access_error:

0 commit comments

Comments
 (0)