Skip to content

Fix #3149 #3152

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 11 commits into from
Oct 15, 2021
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()
17 changes: 13 additions & 4 deletions qiita_pet/handlers/rest/study_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the load_template_to_dataframe accepts string so perhaps worth not converting to dataframe and then back and just do:
data = load_template_to_dataframe(json_decode(self.request.body)) but I'm not sure if json_decode returns a string or a byte.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks like it opens a filehandle, so I don't think a regular string would work. The spec for the REST endpoint as well is not a str representation of a TSV, but a JSON representation derived from DataFrame.to_json. I may be missing something, but I think this transform is necessary

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:
Expand Down
14 changes: 7 additions & 7 deletions qiita_pet/support_files/doc/source/processingdata/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand Down
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