Skip to content
1 change: 1 addition & 0 deletions qiita_db/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def get_files_from_uploads_folders(study_id):
list
List of the filepaths for upload for that study
"""
study_id = str(study_id)
fp = []
for pid, p in get_mountpoint("uploads", retrieve_all=True):
t = join(p, study_id)
Expand Down
5 changes: 3 additions & 2 deletions qiita_pet/handlers/api_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
prep_template_summary_get_req, prep_template_post_req,
prep_template_put_req, prep_template_delete_req, prep_template_get_req,
prep_template_graph_get_req, prep_template_filepaths_get_req,
ena_ontology_get_req, prep_template_samples_get_req)
ena_ontology_get_req, prep_template_samples_get_req,
new_prep_template_get_req)
from .studies import (
data_types_get_req, study_get_req, study_prep_get_req, study_delete_req)
from .artifact import (artifact_graph_get_req, artifact_types_get_req,
Expand All @@ -43,4 +44,4 @@
'artifact_post_req', 'ena_ontology_get_req',
'sample_template_meta_cats_get_req',
'sample_template_samples_get_req', 'prep_template_samples_get_req',
'sample_template_category_get_req']
'sample_template_category_get_req', 'new_prep_template_get_req']
42 changes: 37 additions & 5 deletions qiita_pet/handlers/api_proxy/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,45 @@
from qiita_core.util import execute_as_transaction
from qiita_pet.handlers.api_proxy.util import check_access, check_fp
from qiita_db.metadata_template.util import load_template_to_dataframe
from qiita_db.util import convert_to_id
from qiita_db.util import convert_to_id, get_files_from_uploads_folders
from qiita_db.study import Study
from qiita_db.ontology import Ontology
from qiita_db.metadata_template.prep_template import PrepTemplate


def new_prep_template_get_req(study_id):
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a description to this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Parameters
----------
study_id : int
The study id

Returns
-------
(list of str, list of str, dict of {str: list of str})
The list of txt,csv files in the upload dir for the given study
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

csv is not in the list below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, same comment, plus txt,csv -> txt, csv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is actually a typo in the documentation. We are supporting tsv (Tab Separated Values) rather than csv (Comma Separated Values).

The list of available data types
The investigation type ontology information
"""
prep_files = [f for _, f in get_files_from_uploads_folders(study_id)
if f.endswith(('txt', 'tsv'))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest "anchoring" the extension to the dot i.e. '.txt', '.tsv', though I imagine it is very unlikely to find an occurrence where the file ends in txt and txt is not an extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

data_types = sorted(Study.all_data_types())

# Get all the ENA terms for the investigation type
ontology = Ontology(convert_to_id('ENA', 'ontology'))
ena_terms = sorted(ontology.terms)
# make "Other" last on the list
ena_terms.remove('Other')
ena_terms.append('Other')

ontology_info = {'ENA': ena_terms,
'User': sorted(ontology.user_defined_terms)}
return {'status': 'success',
'prep_files': prep_files,
'data_types': data_types,
'ontology': ontology_info}


@execute_as_transaction
def _process_investigation_type(inv_type, user_def_type, new_type):
"""Return the investigation_type and add it to the ontology if needed
Expand Down Expand Up @@ -197,11 +230,10 @@ def prep_template_post_req(study_id, user_id, prep_template, data_type,
prep = None
try:
with warnings.catch_warnings(record=True) as warns:
data_type_id = convert_to_id(data_type, 'data_type')
# deleting previous uploads and inserting new one
prep = PrepTemplate.create(load_template_to_dataframe(fp_rpt),
Study(int(study_id)), int(data_type_id),
investigation_type=investigation_type)
prep = PrepTemplate.create(
load_template_to_dataframe(fp_rpt), Study(study_id), data_type,
investigation_type=investigation_type)
remove(fp_rpt)

# join all the warning messages into one. Note that this info
Expand Down
19 changes: 12 additions & 7 deletions qiita_pet/handlers/api_proxy/studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,23 @@ def study_prep_get_req(study_id, user_id):
prep_info[dtype] = []
for prep in study.prep_templates(dtype):
start_artifact = prep.artifact
youngest_artifact = prep.artifact.youngest_artifact
info = {
'name': 'PREP %d NAME' % prep.id,
'id': prep.id,
'status': prep.status,
'start_artifact': start_artifact.artifact_type,
'start_artifact_id': start_artifact.id,
'youngest_artifact': ' - '.join(
[youngest_artifact.name, youngest_artifact.artifact_type])
}
if start_artifact is not None:
youngest_artifact = prep.artifact.youngest_artifact
info['start_artifact'] = start_artifact.artifact_type
info['start_artifact_id'] = start_artifact.id
info['youngest_artifact'] = '%s - %s' % (
youngest_artifact.name, youngest_artifact.artifact_type)
else:
info['start_artifact'] = None
info['start_artifact_id'] = None
info['youngest_artifact'] = None

prep_info[dtype].append(info)
return {'status': 'success',
'message': '',
'info': prep_info
}
'info': prep_info}
24 changes: 21 additions & 3 deletions qiita_pet/handlers/api_proxy/tests/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,27 @@
prep_template_put_req, prep_template_delete_req, prep_template_get_req,
prep_template_graph_get_req, prep_template_filepaths_get_req,
ena_ontology_get_req, _process_investigation_type,
_check_prep_template_exists)
_check_prep_template_exists, new_prep_template_get_req)


class TestPrepAPIReadOnly(TestCase):
def test_new_prep_template_get_req(self):
obs = new_prep_template_get_req(1)
exp = {
'status': 'success',
'prep_files': ['uploaded_file.txt'],
'data_types': ['16S', '18S', 'ITS', 'Metabolomic', 'Metagenomic',
'Proteomic'],
'ontology': {
'ENA': ['Cancer Genomics', 'Epigenetics', 'Exome Sequencing',
'Forensic or Paleo-genomics', 'Gene Regulation Study',
'Metagenomics', 'Pooled Clone Sequencing',
'Population Genomics', 'RNASeq', 'Resequencing',
'Synthetic Genomics', 'Transcriptome Analysis',
'Whole Genome Sequencing', 'Other'],
'User': []}}
self.assertEqual(obs, exp)

def test_check_prep_template_exists(self):
obs = _check_prep_template_exists(1)
self.assertEqual(obs, {'status': 'success', 'message': ''})
Expand Down Expand Up @@ -228,8 +245,9 @@ def setUp(self):
with open(self.update_fp, 'w') as f:
f.write("""sample_name\tnew_col\n1.SKD6.640190\tnew_value\n""")

def tear_down(self):
remove(self.update_fp)
def tearDown(self):
if exists(self.update_fp):
remove(self.update_fp)

fp = join(get_mountpoint("uploads")[0][1], '1', 'uploaded_file.txt')
if not exists(fp):
Expand Down
30 changes: 30 additions & 0 deletions qiita_pet/handlers/prep_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -----------------------------------------------------------------------------
# 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 authenticated

from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_pet.handlers.api_proxy import prep_template_post_req


class PrepTemplateHandler(BaseHandler):
@authenticated
def post(self):
"""Creates a prep template"""
study_id = self.get_argument('study_id')
data_type = self.get_argument('data-type')
ena_ontology = self.get_argument('ena-ontology', None)
user_ontology = self.get_argument('user-ontology', None)
new_ontology = self.get_argument('new-ontology', None)
prep_fp = self.get_argument('prep-file')

response = prep_template_post_req(
study_id, self.get_current_user().id, prep_fp, data_type,
ena_ontology, user_ontology, new_ontology)

self.write(response)
9 changes: 6 additions & 3 deletions qiita_pet/handlers/study_handlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from .description_handlers import PreprocessingSummaryHandler
from .ebi_handlers import EBISubmitHandler
from .vamps_handlers import VAMPSHandler
from .base import StudyIndexHandler, StudyBaseInfoAJAX, StudyDeleteAjax
from .base import (StudyIndexHandler, StudyBaseInfoAJAX, StudyDeleteAjax,
DataTypesMenuAJAX)
from .prep_template import (
PrepTemplateGraphAJAX, PrepTemplateAJAX, PrepFilesHandler)
PrepTemplateGraphAJAX, PrepTemplateAJAX, PrepFilesHandler,
NewPrepTemplateAjax)
from .processing import (ProcessArtifactHandler, ListCommandsHandler,
ListOptionsHandler)
from .artifact import (ArtifactGraphAJAX, NewArtifactHandler,
Expand All @@ -29,4 +31,5 @@
'StudyBaseInfoAJAX', 'SampleTemplateAJAX', 'PrepTemplateAJAX',
'NewArtifactHandler', 'PrepFilesHandler', 'ProcessArtifactHandler',
'ListCommandsHandler', 'ListOptionsHandler', 'SampleAJAX',
'StudyDeleteAjax', 'ArtifactAJAX']
'StudyDeleteAjax', 'ArtifactAJAX', 'NewPrepTemplateAjax',
'DataTypesMenuAJAX']
4 changes: 2 additions & 2 deletions qiita_pet/handlers/study_handlers/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def post(self, study_id):
artifact = artifact_post_req(
self.current_user.id, files, artifact_type, name, prep['id'])
if artifact['status'] == 'success' and prep['status'] != 'warning':
self.write({'status': 'success',
'message': 'Artifact created successfully'})
self.write({'status': 'success',
'message': 'Artifact created successfully'})
else:
self.write(prep)

Expand Down
33 changes: 22 additions & 11 deletions qiita_pet/handlers/study_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,23 @@
from qiita_pet.handlers.util import to_int, doi_linkifier
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_pet.handlers.api_proxy import (
study_prep_get_req, data_types_get_req, study_get_req, study_delete_req)
study_prep_get_req, study_get_req, study_delete_req)


class StudyIndexHandler(BaseHandler):
@authenticated
def get(self, study_id):
study = to_int(study_id)

# Proxies for what will become API requests
prep_info = study_prep_get_req(study, self.current_user.id)
# Make sure study exists
if prep_info['status'] != 'success':
raise HTTPError(404, prep_info['message'])
study_info = study_get_req(study, self.current_user.id)
if study_info['status'] != 'success':
raise HTTPError(404, study_info['message'])

study_info = study_info['info']

prep_info = prep_info['info']
data_types = data_types_get_req()['data_types']
study_info = study_get_req(study, self.current_user.id)['info']
editable = study_info['status'] == 'sandbox'

self.render("study_base.html", prep_info=prep_info,
data_types=data_types, study_info=study_info,
self.render("study_base.html", study_info=study_info,
editable=editable)


Expand All @@ -57,3 +53,18 @@ class StudyDeleteAjax(BaseHandler):
def post(self):
study_id = self.get_argument('study_id')
self.write(study_delete_req(int(study_id), self.current_user.id))


class DataTypesMenuAJAX(BaseHandler):
@authenticated
def get(self):
study_id = to_int(self.get_argument('study_id'))
# Retrieve the prep template information for the menu
prep_info = study_prep_get_req(study_id, self.current_user.id)
# Make sure study exists
if prep_info['status'] != 'success':
raise HTTPError(404, prep_info['message'])

prep_info = prep_info['info']

self.render('study_ajax/data_type_menu.html', prep_info=prep_info)
15 changes: 14 additions & 1 deletion qiita_pet/handlers/study_handlers/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,20 @@
prep_template_summary_get_req, prep_template_post_req,
prep_template_put_req, prep_template_delete_req,
prep_template_filepaths_get_req, data_types_get_req,
prep_template_graph_get_req, ena_ontology_get_req)
prep_template_graph_get_req, ena_ontology_get_req,
new_prep_template_get_req)


class NewPrepTemplateAjax(BaseHandler):
@authenticated
def get(self):
study_id = to_int(self.get_argument('study_id'))
result = new_prep_template_get_req(study_id)
self.render('study_ajax/add_prep_template.html',
prep_files=result['prep_files'],
data_types=result['data_types'],
ontology=result['ontology'],
study_id=study_id)


class PrepTemplateGraphAJAX(BaseHandler):
Expand Down
13 changes: 13 additions & 0 deletions qiita_pet/handlers/study_handlers/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,18 @@ def test_delete_study(self):
'erased because it has a sample template'}
self.assertEqual(loads(response.body), exp)


class DataTypesMenuAJAXTests(TestHandlerBase):
def test_get(self):
response = self.get('/study/description/data_type_menu/',
{'study_id': '1'})
self.assertEqual(response.code, 200)
self.assertNotEqual(response.body, "")

def test_get_no_exists(self):
response = self.get('/study/description/data_type_menu/',
{'study_id': '245'})
self.assertEqual(response.code, 404)

if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions qiita_pet/handlers/study_handlers/tests/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from qiita_db.ontology import Ontology


class TestNewPrepTemplateAjax(TestHandlerBase):
def test_get(self):
response = self.get('/study/new_prep_template/', {'study_id': '1'})
self.assertEqual(response.code, 200)


class TestPrepTemplateGraphAJAX(TestHandlerBase):
def test_get(self):
response = self.get('/prep/graph/', {'prep_id': 1})
Expand Down
9 changes: 5 additions & 4 deletions qiita_pet/templates/study_ajax/add_prep_artifact.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
$(ft+'-div').removeClass('highlight');
$("#"+ft).val(files);
}
return true;
}

$(document).ready(function () {
Expand Down Expand Up @@ -148,15 +149,15 @@ <h4>Select Investigation Type</h4>
<option value="{{o}}">{{o}}</option>
{% end %}
</select></p>
<p id="user-ena-info"> User defined investigation type:
<p id="user-ena-info"> User defined investigation type:
<select id="user-ontology" name="user-ontology" value="user-ontology">
<option value=""></option>
{% for o in ontology['User'] %}
<option value="{{o}}">{{o}}</option>
{% end %}
<option value="New Type">New Type</option>
</select></p>
<p id="new-ena-info">New user defined term:
<p id="new-ena-info">New user defined term:
<input type="textbox" id="new-ontology" name="new-ontology"></p>
</div>
<div class="col-md-3">
Expand Down Expand Up @@ -184,7 +185,7 @@ <h4>Click and drag your files to the correct file subtype.</h4>
</div>
<div class="row" id="submit-div" hidden>
<div class="col-md-12">
<button class="btn btn-sm btn-danger" onclick="fill_new_prep(); return false;">Reset</button> <input type="submit" class="btn btn-sm btn-success" value="Create New Preperation">
<button class="btn btn-sm btn-danger" onclick="fill_new_prep(); return false;">Reset</button> <input type="submit" class="btn btn-sm btn-success" value="Create New Preparation">
</div>
</div>
</form>
</form>
Loading