Skip to content

Commit 9db35a5

Browse files
committed
Modifying interface so we can upload a QIIME mapping file
1 parent 94560b1 commit 9db35a5

File tree

7 files changed

+64
-23
lines changed

7 files changed

+64
-23
lines changed

qiita_db/metadata_template/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from .sample_template import SampleTemplate
1010
from .prep_template import PrepTemplate
11-
from .util import load_template_to_dataframe
11+
from .util import load_template_to_dataframe, looks_like_qiime_mapping_file
1212
from .constants import (TARGET_GENE_DATA_TYPES, SAMPLE_TEMPLATE_COLUMNS,
1313
PREP_TEMPLATE_COLUMNS,
1414
PREP_TEMPLATE_COLUMNS_TARGET_GENE, CONTROLLED_COLS)
@@ -17,4 +17,4 @@
1717
__all__ = ['SampleTemplate', 'PrepTemplate', 'load_template_to_dataframe',
1818
'TARGET_GENE_DATA_TYPES', 'SAMPLE_TEMPLATE_COLUMNS',
1919
'PREP_TEMPLATE_COLUMNS', 'PREP_TEMPLATE_COLUMNS_TARGET_GENE',
20-
'CONTROLLED_COLS']
20+
'CONTROLLED_COLS', 'looks_like_qiime_mapping_file']

qiita_db/metadata_template/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def looks_like_qiime_mapping_file(fp):
347347
'#SampleID', since a sample/prep template will start with 'sample_name' or
348348
some other different column.
349349
"""
350-
line = None
350+
first_line = None
351351
with open_file(fp, mode='U') as f:
352352
first_line = f.readline()
353353
if not first_line:

qiita_pet/handlers/study_handlers/description_handlers.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
from qiita_db.ontology import Ontology
2424
from qiita_db.metadata_template import (PrepTemplate, SampleTemplate,
2525
load_template_to_dataframe,
26-
SAMPLE_TEMPLATE_COLUMNS)
26+
SAMPLE_TEMPLATE_COLUMNS,
27+
looks_like_qiime_mapping_file)
2728
from qiita_db.util import convert_to_id, get_mountpoint
2829
from qiita_db.exceptions import (QiitaDBUnknownIDError, QiitaDBColumnError,
2930
QiitaDBExecutionError, QiitaDBDuplicateError,
3031
QiitaDBDuplicateHeaderError, QiitaDBError)
32+
from qiita_ware.metadata_pipeline import (
33+
create_templates_from_qiime_mapping_file)
3134
from qiita_pet.handlers.base_handlers import BaseHandler
3235
from qiita_pet.handlers.util import check_access
3336
from qiita_pet.handlers.study_handlers.listing_handlers import (
@@ -161,13 +164,11 @@ def process_sample_template(self, study, user, callback):
161164
HTTPError
162165
If the sample template file does not exists
163166
"""
164-
# If we are on this function, the argument "sample_template" must
165-
# defined. If not, let tornado raise its error
167+
# If we are on this function, the arguments "sample_template" and
168+
# "data_type" must be defined. If not, let tornado raise its error
166169
sample_template = self.get_argument('sample_template')
170+
data_type = self.get_argument('data_type')
167171

168-
# Define here the message and message level in case of success
169-
msg = "The sample template '%s' has been added" % sample_template
170-
msg_level = "success"
171172
# Get the uploads folder
172173
_, base_fp = get_mountpoint("uploads")[0]
173174
# Get the path of the sample template in the uploads folder
@@ -177,25 +178,33 @@ def process_sample_template(self, study, user, callback):
177178
# The file does not exist, fail nicely
178179
raise HTTPError(404, "This file doesn't exist: %s" % fp_rsp)
179180

181+
# Define here the message and message level in case of success
182+
is_mapping_file = looks_like_qiime_mapping_file(fp_rsp)
183+
180184
try:
181185
with warnings.catch_warnings(record=True) as warns:
182186
# deleting previous uploads and inserting new one
183187
self.remove_add_study_template(study.raw_data, study.id,
184-
fp_rsp)
188+
fp_rsp, data_type,
189+
is_mapping_file)
185190

186-
# join all the warning messages into one. Note that this info
187-
# will be ignored if an exception is raised
191+
# join all the warning messages into one. Note that this
192+
# info will be ignored if an exception is raised
188193
if warns:
189194
msg = '; '.join([str(w.message) for w in warns])
190195
msg_level = 'warning'
191196

192197
except (TypeError, QiitaDBColumnError, QiitaDBExecutionError,
193198
QiitaDBDuplicateError, IOError, ValueError, KeyError,
194-
CParserError, QiitaDBDuplicateHeaderError, QiitaDBError) as e:
199+
CParserError, QiitaDBDuplicateHeaderError,
200+
QiitaDBError) as e:
195201
# Some error occurred while processing the sample template
196202
# Show the error to the user so they can fix the template
197-
msg = html_error_message % ('parsing the sample template:',
198-
basename(fp_rsp), str(e))
203+
error_msg = ('parsing the QIIME mapping file'
204+
if is_mapping_file
205+
else 'parsing the sample template')
206+
msg = html_error_message % (error_msg, basename(fp_rsp),
207+
str(e))
199208
msg_level = "danger"
200209

201210
callback((msg, msg_level, None, None, None))
@@ -564,9 +573,14 @@ def unspecified_action(self, study, user, callback):
564573
msg_level = 'danger'
565574
callback((msg, msg_level, 'study_information_tab', None, None))
566575

567-
def remove_add_study_template(self, raw_data, study_id, fp_rsp):
576+
def remove_add_study_template(self, raw_data, study_id, fp_rsp, data_type,
577+
is_mapping_file):
568578
"""Replace prep templates, raw data, and sample template with a new one
569579
"""
580+
if is_mapping_file and data_type == "":
581+
raise ValueError("Please, choose a data type if uploading a QIIME "
582+
"mapping file")
583+
570584
for rd in raw_data():
571585
rd = RawData(rd)
572586
for pt in rd.prep_templates:
@@ -575,8 +589,13 @@ def remove_add_study_template(self, raw_data, study_id, fp_rsp):
575589
if SampleTemplate.exists(study_id):
576590
SampleTemplate.delete(study_id)
577591

578-
SampleTemplate.create(load_template_to_dataframe(fp_rsp),
579-
Study(study_id))
592+
if is_mapping_file:
593+
create_templates_from_qiime_mapping_file(fp_rsp, Study(study_id),
594+
int(data_type))
595+
else:
596+
SampleTemplate.create(load_template_to_dataframe(fp_rsp),
597+
Study(study_id))
598+
580599
remove(fp_rsp)
581600

582601
def remove_add_prep_template(self, fp_rpt, study, data_type_id,

qiita_pet/templates/study_description.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
.attr("type", "hidden")
6868
.attr("name", "sample_template")
6969
.attr("value", $("#sample_template").val()))
70+
.append($("<input>")
71+
.attr("type", "hidden")
72+
.attr("name", "data_type")
73+
.attr("value", $("#qiime_data_type").val()))
7074
.append($("<input>")
7175
.attr("type", "hidden")
7276
.attr("name", "action")

qiita_pet/templates/study_description_templates/study_information_tab.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@
1212
<hr>
1313

1414
{% if show_select_sample %}
15-
Select your sample template <br/>
15+
Select your sample template or, alternatively, a QIIME mapping file <br/>
1616
(only files with the "txt" and "tsv" file extensions will be displayed here):
1717
<select id="sample_template">
1818
{% for f in files %}
1919
<option value="{{f}}">{{f}}</option>
2020
{% end %}
2121
</select>
22+
23+
<br/>
24+
25+
{% if not sample_templates %}
26+
If you are uploading a QIIME mapping file, please choose a data type:
27+
<select id="qiime_data_type">
28+
<option value="">Select an option...</option>
29+
{% for name, value in data_types %}
30+
<option value="{{value}}">{{name}}</option>
31+
{% end %}
32+
</select>
33+
{% end %}
2234
{% end %}
2335

2436
<br/>

qiita_pet/uimodules/study_information_tab.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
# -----------------------------------------------------------------------------
88

99
from functools import partial
10+
from operator import itemgetter
1011

11-
from qiita_db.util import get_files_from_uploads_folders
12+
from future.utils import viewitems
13+
14+
from qiita_db.util import get_files_from_uploads_folders, get_data_types
1215
from qiita_db.study import StudyPerson
1316
from qiita_db.metadata_template import SampleTemplate
1417
from qiita_pet.util import linkify
@@ -34,9 +37,11 @@ def render(self, study):
3437
number_samples_promised = study_info['number_samples_promised']
3538
number_samples_collected = study_info['number_samples_collected']
3639
metadata_complete = study_info['metadata_complete']
40+
data_types = sorted(viewitems(get_data_types()), key=itemgetter(1))
3741

3842
# Retrieve the files from the uploads folder, so the user can choose
39-
# the sample template of the study
43+
# the sample template of the study. Filter them to only include the
44+
# ones that ends with 'txt' or 'tsv'.
4045
files = [f for _, f in get_files_from_uploads_folders(str(study.id))
4146
if f.endswith(('txt', 'tsv'))]
4247

@@ -69,4 +74,5 @@ def render(self, study):
6974
files=files,
7075
study_id=study.id,
7176
sample_templates=sample_templates,
72-
is_local_request=is_local_request)
77+
is_local_request=is_local_request,
78+
data_types=data_types)

qiita_ware/metadata_pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
PREP_TEMPLATE_COLUMNS_TARGET_GENE,
1515
CONTROLLED_COLS,
1616
TARGET_GENE_DATA_TYPES)
17-
from qiita_db.util import convert_to_id
17+
from qiita_db.util import convert_from_id
1818

1919

2020
def create_templates_from_qiime_mapping_file(fp, study, data_type):

0 commit comments

Comments
 (0)