Skip to content

Commit

Permalink
Adding prep template data handler to REST api (#1871)
Browse files Browse the repository at this point in the history
* Adding prep template data handler to REST api

* flake8ing

* Addressing @antgonza's comments

* flake8 - 2, Jose - 0
  • Loading branch information
josenavas authored and antgonza committed Jun 29, 2016
1 parent bf9c525 commit 15a03de
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
65 changes: 65 additions & 0 deletions qiita_db/handlers/prep_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -----------------------------------------------------------------------------
# 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

import qiita_db as qdb
from .oauth2 import OauthBaseHandler, authenticate_oauth


def _get_prep_template(pid):
"""Returns the prep template with the given `pid` if it exists
Parameters
----------
pid : str
The prep template id
Returns
-------
qiita_db.metadata_template.prep_template.PrepTemplate
The requested prep template
Raises
------
HTTPError
If the prep template does not exist, with error code 404
If there is a problem instantiating the template, with error code 500
"""
try:
pid = int(pid)
pt = qdb.metadata_template.prep_template.PrepTemplate(pid)
except qdb.exceptions.QiitaDBUnknownIDError:
raise HTTPError(404)
except Exception as e:
raise HTTPError(500, 'Error instantiating prep template %s: %s'
% (pid, str(e)))

return pt


class PrepTemplateDataHandler(OauthBaseHandler):
@authenticate_oauth
def get(self, prep_id):
"""Retrieves the prep contents
Parameters
----------
prep_id : str
The id of the prep template whose information is being retrieved
Returns
-------
dict
The contents of the prep information keyed by sample id
"""
with qdb.sql_connection.TRN:
pt = _get_prep_template(prep_id)
response = {'data': pt.to_dataframe().to_dict(orient='index')}

self.write(response)
100 changes: 100 additions & 0 deletions qiita_db/handlers/tests/test_prep_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -----------------------------------------------------------------------------
# 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, TestCase
from json import loads

from tornado.web import HTTPError

from qiita_db.handlers.tests.oauthbase import OauthTestingBase
import qiita_db as qdb
from qiita_db.handlers.prep_template import _get_prep_template


class UtilTests(TestCase):
def test_get_prep_template(self):
obs = _get_prep_template(1)
exp = qdb.metadata_template.prep_template.PrepTemplate(1)
self.assertEqual(obs, exp)

# It does not exist
with self.assertRaises(HTTPError):
_get_prep_template(100)


class PrepTemplateDataHandlerTests(OauthTestingBase):
def test_get_does_not_exist(self):
obs = self.get('/qiita_db/prep_template/100/data/',
headers=self.header)
self.assertEqual(obs.code, 404)

def test_get_no_header(self):
obs = self.get('/qiita_db/prep_template/100/data/')
self.assertEqual(obs.code, 400)

def test_get(self):
obs = self.get('/qiita_db/prep_template/1/data/', headers=self.header)
self.assertEqual(obs.code, 200)

obs = loads(obs.body)
self.assertEqual(obs.keys(), ['data'])

obs = obs['data']
exp = ['1.SKB2.640194', '1.SKM4.640180', '1.SKB3.640195',
'1.SKB6.640176', '1.SKD6.640190', '1.SKM6.640187',
'1.SKD9.640182', '1.SKM8.640201', '1.SKM2.640199',
'1.SKD2.640178', '1.SKB7.640196', '1.SKD4.640185',
'1.SKB8.640193', '1.SKM3.640197', '1.SKD5.640186',
'1.SKB1.640202', '1.SKM1.640183', '1.SKD1.640179',
'1.SKD3.640198', '1.SKB5.640181', '1.SKB4.640189',
'1.SKB9.640200', '1.SKM9.640192', '1.SKD8.640184',
'1.SKM5.640177', '1.SKM7.640188', '1.SKD7.640191']
self.assertItemsEqual(obs.keys(), exp)

obs = obs['1.SKB1.640202']
exp = {
'barcode': 'GTCCGCAAGTTA',
'center_name': 'ANL',
'center_project_name': None,
'emp_status': 'EMP',
'experiment_center': 'ANL',
'experiment_design_description':
'micro biome of soil and rhizosphere of cannabis plants '
'from CA',
'experiment_title': 'Cannabis Soil Microbiome',
'illumina_technology': 'MiSeq',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol':
'This analysis was done as in Caporaso et al 2011 Genome '
'research. The PCR primers (F515/R806) were developed against '
'the V4 region of the 16S rRNA (both bacteria and archaea), '
'which we determined would yield optimal community clustering '
'with reads of this length using a procedure similar to that '
'of ref. 15. [For reference, this primer pair amplifies the '
'region 533_786 in the Escherichia coli strain 83972 sequence '
'(greengenes accession no. prokMSA_id:470367).] The reverse '
'PCR primer is barcoded with a 12-base error-correcting Golay '
'code to facilitate multiplexing of up to 1,500 samples per '
'lane, and both PCR primers contain sequencer adapter '
'regions.',
'pcr_primers': 'FWD:GTGCCAGCMGCCGCGGTAA; REV:GGACTACHVGGGTWTCTAAT',
'platform': 'Illumina',
'primer': 'GTGCCAGCMGCCGCGGTAA',
'run_center': 'ANL',
'run_date': '8/1/12',
'run_prefix': 's_G1_L001_sequences',
'samp_size': '.25,g',
'sample_center': 'ANL',
'sequencing_meth': 'Sequencing by synthesis',
'study_center': 'CCME',
'target_gene': '16S rRNA',
'target_subfragment': 'V4'}
self.assertEqual(obs, exp)

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions qiita_pet/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
JobHandler, HeartbeatHandler, ActiveStepHandler, CompleteHandler,
ProcessingJobAPItestHandler)
from qiita_db.handlers.artifact import ArtifactHandler
from qiita_db.handlers.prep_template import PrepTemplateDataHandler
from qiita_db.handlers.oauth2 import TokenAuthHandler
from qiita_db.handlers.reference import ReferenceFilepathsHandler
from qiita_db.handlers.core import ResetAPItestHandler
Expand Down Expand Up @@ -148,6 +149,7 @@ def __init__(self):
(r"/qiita_db/jobs/(.*)/complete/", CompleteHandler),
(r"/qiita_db/jobs/(.*)", JobHandler),
(r"/qiita_db/artifacts/(.*)/", ArtifactHandler),
(r"/qiita_db/prep_template/(.*)/data/", PrepTemplateDataHandler),
(r"/qiita_db/references/(.*)/filepaths/",
ReferenceFilepathsHandler)
]
Expand Down

0 comments on commit 15a03de

Please sign in to comment.