Skip to content

Commit

Permalink
Adding endpoint to create new artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
josenavas committed Jul 6, 2016
1 parent 665160c commit c93bcda
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
41 changes: 41 additions & 0 deletions qiita_db/handlers/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from tornado.web import HTTPError
from collections import defaultdict
from json import loads

import qiita_db as qdb
from .oauth2 import OauthBaseHandler, authenticate_oauth
Expand Down Expand Up @@ -136,3 +137,43 @@ def patch(self, artifact_id):
'supported operations: add' % req_op)

self.finish()


class ArtifactAPItestHandler(OauthBaseHandler):
@authenticate_oauth
def post(self):
"""Creates a new artifact
Parameters
----------
filepahts : str
Json string with a list of filepaths and its types
type : str
The artifact type
prep_template: int
The id of the template that the new artifact belongs to
name : str, optional
The artifact name
Returns
-------
dict
'artifact': the id of the new artifact
See Also
--------
qiita_db.artifact.Artifact.create
"""
filepaths = loads(self.get_argument('filepaths'))
artifact_type = self.get_argument('type')
prep_template = self.get_argument('prep')
name = self.get_argument('name', None)

if prep_template:
prep_template = qdb.metadata_template.prep_template.PrepTemplate(
prep_template)

a = qdb.artifact.Artifact.create(
filepaths, artifact_type, name=name, prep_template=prep_template)

self.write({'artifact': a.id})
65 changes: 65 additions & 0 deletions qiita_db/handlers/tests/test_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
from os.path import join, exists
from os import close, remove
from tempfile import mkstemp
from json import dumps

from tornado.web import HTTPError
import pandas as pd

from qiita_db.handlers.tests.oauthbase import OauthTestingBase
import qiita_db as qdb
Expand Down Expand Up @@ -122,5 +124,68 @@ def test_patch(self):
self.assertIn('No such file or directory', obs.body)


class ArtifactAPItestHandlerTests(OauthTestingBase):
database = True

def setUp(self):
super(ArtifactAPItestHandlerTests, self).setUp()

metadata_dict = {
'SKB8.640193': {'center_name': 'ANL',
'primer': 'GTGCCAGCMGCCGCGGTAA',
'barcode': 'GTCCGCAAGTTA',
'run_prefix': "s_G1_L001_sequences",
'platform': 'ILLUMINA',
'instrument_model': 'Illumina MiSeq',
'library_construction_protocol': 'AAAA',
'experiment_design_description': 'BBBB'}}
metadata = pd.DataFrame.from_dict(metadata_dict, orient='index')
self.prep_template = \
qdb.metadata_template.prep_template.PrepTemplate.create(
metadata, qdb.study.Study(1), "16S")

self._clean_up_files = []

def tearDown(self):
super(ArtifactAPItestHandlerTests, self).tearDown()

for f in self._clean_up_files:
if exists(f):
remove(f)

def test_post_root(self):
fd, fp1 = mkstemp(suffix='_seqs.fastq')
close(fd)
self._clean_up_files.append(fp1)
with open(fp1, 'w') as f:
f.write("@HWI-ST753:189:D1385ACXX:1:1101:1214:1906 1:N:0:\n"
"NACGTAGGGTGCAAGCGTTGTCCGGAATNA\n"
"+\n"
"#1=DDFFFHHHHHJJJJJJJJJJJJGII#0\n")

fd, fp2 = mkstemp(suffix='_barcodes.fastq')
close(fd)
self._clean_up_files.append(fp2)
with open(fp2, 'w') as f:
f.write("@HWI-ST753:189:D1385ACXX:1:1101:1214:1906 2:N:0:\n"
"NNNCNNNNNNNNN\n"
"+\n"
"#############\n")

data = {'filepaths': dumps([(fp1, 'raw_forward_seqs'),
(fp2, 'raw_barcodes')]),
'type': "FASTQ",
'name': "New test artifact",
'prep': self.prep_template.id}
obs = self.post('/apitest/artifact/', headers=self.header, data=data)
self.assertEqual(obs.code, 200)
obs = loads(obs.body)
self.assertEqual(obs.keys(), ['artifact'])

a = qdb.artifact.Artifact(obs['artifact'])
self._clean_up_files.extend([fp for _, fp, _ in a.filepaths])
self.assertEqual(a.name, "New test artifact")


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions qiita_pet/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
from qiita_db.handlers.processing_job import (
JobHandler, HeartbeatHandler, ActiveStepHandler, CompleteHandler,
ProcessingJobAPItestHandler)
from qiita_db.handlers.artifact import ArtifactHandler
from qiita_db.handlers.artifact import ArtifactHandler, ArtifactAPItestHandler
from qiita_db.handlers.prep_template import (
PrepTemplateDataHandler, PrepTemplateAPItestHandler,
PrepTemplateDBHandler)
Expand Down Expand Up @@ -168,7 +168,8 @@ def __init__(self):
test_handlers = [
(r"/apitest/processing_job/", ProcessingJobAPItestHandler),
(r"/apitest/reset/", ResetAPItestHandler),
(r"/apitest/prep_template/", PrepTemplateAPItestHandler)
(r"/apitest/prep_template/", PrepTemplateAPItestHandler),
(r"/apitest/artifact/", ArtifactAPItestHandler)
]
handlers.extend(test_handlers)

Expand Down

0 comments on commit c93bcda

Please sign in to comment.