diff --git a/qiita_db/handlers/studies.py b/qiita_db/handlers/studies.py new file mode 100644 index 000000000..e8b3780c3 --- /dev/null +++ b/qiita_db/handlers/studies.py @@ -0,0 +1,67 @@ +# ----------------------------------------------------------------------------- +# 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 tornado.web import HTTPError + +from qiita_db.sql_connection import TRN +from .oauth2 import OauthBaseHandler, authenticate_oauth + + +def _generate_study_list_for_api(visibility, only_biom=True): + """Get general study information + + Parameters + ---------- + visibility : string + The visibility to get studies + + Returns + ------- + list of dict + The list of studies and their information + """ + artifact_type = '' + if only_biom: + artifact_type = "AND artifact_type = 'BIOM'" + + sql = f""" + SELECT study_id, array_agg(DISTINCT artifact_id) FROM qiita.study + INNER JOIN qiita.study_artifact USING (study_id) + INNER JOIN qiita.artifact USING (artifact_id) + INNER JOIN qiita.artifact_type USING (artifact_type_id) + INNER JOIN qiita.visibility USING (visibility_id) + WHERE visibility = %s + {artifact_type} + GROUP BY study_id + """ + with TRN: + TRN.add(sql, [visibility]) + return dict(TRN.execute_fetchindex()) + + +class APIStudiesListing(OauthBaseHandler): + @authenticate_oauth + def get(self, visibility): + """Retrieves the studies and their BIOM artifacts in visibility + + Parameters + ---------- + visibility : str {'public', 'sandbox'} + The visibility of the studies and artifacts requested + + Returns + ------- + see qiita_db.util.generate_study_list + """ + if visibility not in {'public', 'private'}: + raise HTTPError( + 403, reason='You can only request public or private studies') + + response = { + 'data': _generate_study_list_for_api(visibility=visibility)} + self.write(response) diff --git a/qiita_db/handlers/tests/test_studies.py b/qiita_db/handlers/tests/test_studies.py new file mode 100644 index 000000000..c54589a97 --- /dev/null +++ b/qiita_db/handlers/tests/test_studies.py @@ -0,0 +1,37 @@ +# ----------------------------------------------------------------------------- +# 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 unittest import main +from json import loads + +from qiita_db.handlers.tests.oauthbase import OauthTestingBase + + +class TestAPIStudiesListing(OauthTestingBase): + def setUp(self): + super(TestAPIStudiesListing, self).setUp() + + def test_get_studies_failure(self): + obs = self.get('/qiita_db/studies/not-valid', headers=self.header) + self.assertEqual(obs.code, 403) + self.assertEqual(str(obs.error), 'HTTP 403: You can only request ' + 'public or private studies') + + def test_get_studies_private(self): + obs = self.get('/qiita_db/studies/private', headers=self.header) + exp = {'data': {'1': [4, 5, 6, 7]}} + self.assertEqual(loads(obs.body), exp) + + def test_get_studies_public(self): + obs = self.get('/qiita_db/studies/public', headers=self.header) + exp = {'data': {}} + self.assertEqual(loads(obs.body), exp) + + +if __name__ == '__main__': + main() diff --git a/qiita_pet/handlers/rest/study_samples.py b/qiita_pet/handlers/rest/study_samples.py index 0679b3b57..5a4804088 100644 --- a/qiita_pet/handlers/rest/study_samples.py +++ b/qiita_pet/handlers/rest/study_samples.py @@ -6,6 +6,8 @@ # The full license is in the file LICENSE, distributed with this software. # ----------------------------------------------------------------------------- from collections import defaultdict +import io +from qiita_db.metadata_template.util import load_template_to_dataframe from tornado.escape import json_encode, json_decode import pandas as pd @@ -150,13 +152,20 @@ def patch(self, study_id): else: sample_info = study.sample_template.to_dataframe() - data = pd.DataFrame.from_dict(json_decode(self.request.body), - orient='index') - - if len(data.index) == 0: + # convert from json into a format that qiita can validate + rawdata = pd.DataFrame.from_dict(json_decode(self.request.body), + orient='index') + rawdata.index.name = 'sample_name' + if len(rawdata.index) == 0: self.fail('No samples provided', 400) return + buffer = io.StringIO() + rawdata.to_csv(buffer, sep='\t', index=True, header=True) + buffer.seek(0) + # validate on load + data = load_template_to_dataframe(buffer) + categories = set(study.sample_template.categories) if set(data.columns) != categories: diff --git a/qiita_pet/support_files/doc/source/processingdata/index.rst b/qiita_pet/support_files/doc/source/processingdata/index.rst index c2d96a446..79a04511e 100755 --- a/qiita_pet/support_files/doc/source/processingdata/index.rst +++ b/qiita_pet/support_files/doc/source/processingdata/index.rst @@ -115,13 +115,13 @@ Deblurring * **Input Data** (required): Data being trimmed * **Parameter Set** (required): How many bases to trim off - * **90 base pairs**- Removes first 90 base pairs from the sequences - * **100 base pairs**- Removes first 100 base pairs from the sequences - * **125 base pairs**- Removes first 125 base pairs from the sequences - * **150 base pairs**- Removes first 150 base pairs from the sequences - * **200 base pairs**- Removes first 200 base pairs from the sequences - * **250 base pairs**- Removes first 250 base pairs from the sequences - * **300 base pairs**- Removes first 300 base pairs from the sequences + * **90 base pairs**- Keeps first 90 base pairs from the sequences + * **100 base pairs**- Keeps first 100 base pairs from the sequences + * **125 base pairs**- Keeps first 125 base pairs from the sequences + * **150 base pairs**- Keeps first 150 base pairs from the sequences + * **200 base pairs**- Keeps first 200 base pairs from the sequences + * **250 base pairs**- Keeps first 250 base pairs from the sequences + * **300 base pairs**- Keeps first 300 base pairs from the sequences **Command from Trimmed Artifact**: diff --git a/qiita_pet/webserver.py b/qiita_pet/webserver.py index 4109f5747..45d694d50 100644 --- a/qiita_pet/webserver.py +++ b/qiita_pet/webserver.py @@ -78,6 +78,7 @@ ReloadPluginAPItestHandler) from qiita_db.handlers.analysis import APIAnalysisMetadataHandler from qiita_db.handlers.archive import APIArchiveObservations +from qiita_db.handlers.studies import APIStudiesListing from qiita_db.util import get_mountpoint from qiita_pet.handlers.rest import ENDPOINTS as REST_ENDPOINTS from qiita_pet.handlers.qiita_redbiom import RedbiomPublicSearch @@ -229,7 +230,8 @@ def __init__(self): (r"/qiita_db/plugins/(.*)/(.*)/commands/", CommandListHandler), (r"/qiita_db/plugins/(.*)/(.*)/", PluginHandler), (r"/qiita_db/analysis/(.*)/metadata/", APIAnalysisMetadataHandler), - (r"/qiita_db/archive/observations/", APIArchiveObservations) + (r"/qiita_db/archive/observations/", APIArchiveObservations), + (r"/qiita_db/studies/(.*)", APIStudiesListing) ] # rest endpoints