Skip to content

add qiita_db/studies/ endpoint #3154

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

Merged
merged 2 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions qiita_db/handlers/studies.py
Original file line number Diff line number Diff line change
@@ -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)
37 changes: 37 additions & 0 deletions qiita_db/handlers/tests/test_studies.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 3 additions & 1 deletion qiita_pet/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down