|
a |
|
b/qiita_db/handlers/prep_template.py |
|
|
1 |
# ----------------------------------------------------------------------------- |
|
|
2 |
# Copyright (c) 2014--, The Qiita Development Team. |
|
|
3 |
# |
|
|
4 |
# Distributed under the terms of the BSD 3-clause License. |
|
|
5 |
# |
|
|
6 |
# The full license is in the file LICENSE, distributed with this software. |
|
|
7 |
# ----------------------------------------------------------------------------- |
|
|
8 |
|
|
|
9 |
from json import loads |
|
|
10 |
from os.path import basename |
|
|
11 |
|
|
|
12 |
from tornado.web import HTTPError |
|
|
13 |
import pandas as pd |
|
|
14 |
|
|
|
15 |
import qiita_db as qdb |
|
|
16 |
from .oauth2 import OauthBaseHandler, authenticate_oauth |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def _get_prep_template(pid): |
|
|
20 |
"""Returns the prep template with the given `pid` if it exists |
|
|
21 |
|
|
|
22 |
Parameters |
|
|
23 |
---------- |
|
|
24 |
pid : str |
|
|
25 |
The prep template id |
|
|
26 |
|
|
|
27 |
Returns |
|
|
28 |
------- |
|
|
29 |
qiita_db.metadata_template.prep_template.PrepTemplate |
|
|
30 |
The requested prep template |
|
|
31 |
|
|
|
32 |
Raises |
|
|
33 |
------ |
|
|
34 |
HTTPError |
|
|
35 |
If the prep template does not exist, with error code 404 |
|
|
36 |
If there is a problem instantiating the template, with error code 500 |
|
|
37 |
""" |
|
|
38 |
try: |
|
|
39 |
pid = int(pid) |
|
|
40 |
pt = qdb.metadata_template.prep_template.PrepTemplate(pid) |
|
|
41 |
except qdb.exceptions.QiitaDBUnknownIDError: |
|
|
42 |
raise HTTPError(404) |
|
|
43 |
except Exception as e: |
|
|
44 |
raise HTTPError(500, reason='Error instantiating prep template %s: %s' |
|
|
45 |
% (pid, str(e))) |
|
|
46 |
|
|
|
47 |
return pt |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
class PrepTemplateDBHandler(OauthBaseHandler): |
|
|
51 |
@authenticate_oauth |
|
|
52 |
def get(self, prep_id): |
|
|
53 |
"""Retrieves the prep template information |
|
|
54 |
|
|
|
55 |
Parameters |
|
|
56 |
---------- |
|
|
57 |
prep_id: str |
|
|
58 |
The id of the prep template whose information is being retrieved |
|
|
59 |
|
|
|
60 |
Returns |
|
|
61 |
------- |
|
|
62 |
dict |
|
|
63 |
The prep information: |
|
|
64 |
'data_type': prep info data type |
|
|
65 |
'artifact': artifact attached to the given prep |
|
|
66 |
'investigation_type': prep info investigation type |
|
|
67 |
'study': study that the prep info belongs to |
|
|
68 |
'status': prep info status |
|
|
69 |
'sample-file': the path to the sample information file |
|
|
70 |
'prep-file': the path to the prep info file |
|
|
71 |
""" |
|
|
72 |
with qdb.sql_connection.TRN: |
|
|
73 |
pt = _get_prep_template(prep_id) |
|
|
74 |
prep_files = [fp for _, fp in pt.get_filepaths() |
|
|
75 |
if 'qiime' not in basename(fp)] |
|
|
76 |
artifact = pt.artifact.id if pt.artifact is not None else None |
|
|
77 |
sid = pt.study_id |
|
|
78 |
response = { |
|
|
79 |
'data_type': pt.data_type(), |
|
|
80 |
'artifact': artifact, |
|
|
81 |
'investigation_type': pt.investigation_type, |
|
|
82 |
'study': sid, |
|
|
83 |
'status': pt.status, |
|
|
84 |
# get_filepaths returns an ordered list of [filepath_id, |
|
|
85 |
# filepath] and we want the last pair |
|
|
86 |
'sample-file': qdb.study.Study( |
|
|
87 |
sid).sample_template.get_filepaths()[0][1], |
|
|
88 |
# The first element in the prep_files is the newest |
|
|
89 |
# prep information file - hence the correct one |
|
|
90 |
'prep-file': prep_files[0] |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
self.write(response) |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
class PrepTemplateDataHandler(OauthBaseHandler): |
|
|
97 |
@authenticate_oauth |
|
|
98 |
def get(self, prep_id): |
|
|
99 |
"""Retrieves the prep contents |
|
|
100 |
|
|
|
101 |
Parameters |
|
|
102 |
---------- |
|
|
103 |
prep_id : str |
|
|
104 |
The id of the prep template whose information is being retrieved |
|
|
105 |
|
|
|
106 |
Returns |
|
|
107 |
------- |
|
|
108 |
dict |
|
|
109 |
The contents of the prep information keyed by sample id |
|
|
110 |
""" |
|
|
111 |
sample_info = self.get_argument('sample_information', False) |
|
|
112 |
|
|
|
113 |
with qdb.sql_connection.TRN: |
|
|
114 |
pt = _get_prep_template(prep_id) |
|
|
115 |
if not sample_info: |
|
|
116 |
response = {'data': pt.to_dataframe().to_dict(orient='index')} |
|
|
117 |
else: |
|
|
118 |
ST = qdb.metadata_template.sample_template.SampleTemplate |
|
|
119 |
response = {'data': ST(pt.study_id).to_dataframe( |
|
|
120 |
samples=list(pt)).to_dict(orient='index')} |
|
|
121 |
|
|
|
122 |
self.write(response) |
|
|
123 |
|
|
|
124 |
|
|
|
125 |
class PrepTemplateAPIHandler(OauthBaseHandler): |
|
|
126 |
@authenticate_oauth |
|
|
127 |
def post(self): |
|
|
128 |
prep_info_dict = loads(self.get_argument('prep_info')) |
|
|
129 |
study = self.get_argument('study') |
|
|
130 |
data_type = self.get_argument('data_type') |
|
|
131 |
name = self.get_argument('name', None) |
|
|
132 |
jid = self.get_argument('job-id', None) |
|
|
133 |
|
|
|
134 |
metadata = pd.DataFrame.from_dict(prep_info_dict, orient='index') |
|
|
135 |
pt = qdb.metadata_template.prep_template.PrepTemplate.create( |
|
|
136 |
metadata, qdb.study.Study(study), data_type, name=name, |
|
|
137 |
creation_job_id=jid) |
|
|
138 |
self.write({'prep': pt.id}) |
|
|
139 |
|
|
|
140 |
|
|
|
141 |
class PrepTemplateAPItestHandler(PrepTemplateAPIHandler): |
|
|
142 |
pass |