[973924]: / qiita_db / handlers / prep_template.py

Download this file

143 lines (117 with data), 4.7 kB

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