Skip to content

mv some methods #2268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions qiita_db/artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,20 +532,29 @@ def delete(cls, artifact_id):

# Check if there is a job queued, running, waiting or
# in_construction that will use/is using the artifact
sql = """SELECT EXISTS(
SELECT *
FROM qiita.artifact_processing_job
JOIN qiita.processing_job USING (processing_job_id)
JOIN qiita.processing_job_status
USING (processing_job_status_id)
WHERE artifact_id = %s
AND processing_job_status IN (
'queued', 'running', 'waiting'))"""
sql = """SELECT processing_job_id
FROM qiita.artifact_processing_job
JOIN qiita.processing_job USING (processing_job_id)
JOIN qiita.processing_job_status
USING (processing_job_status_id)
WHERE artifact_id = %s
AND processing_job_status IN (
'queued', 'running', 'waiting')"""
qdb.sql_connection.TRN.add(sql, [artifact_id])
if qdb.sql_connection.TRN.execute_fetchlast():
raise qdb.exceptions.QiitaDBArtifactDeletionError(
artifact_id,
"there is a queued/running job that uses this artifact")
jobs = qdb.sql_connection.TRN.execute_fetchflatten()
if jobs:
# if the artifact has active jobs we need to raise an error
# but we also need to check that if it's only 1 job, that the
# job is not the delete_artifact actual job
raise_error = True
job_name = qdb.processing_job.ProcessingJob(
jobs[0]).command.name
if len(jobs) == 1 and job_name == 'delete_artifact':
raise_error = False
if raise_error:
raise qdb.exceptions.QiitaDBArtifactDeletionError(
artifact_id, "there is a queued/running job that "
"uses this artifact")

# We can now remove the artifact
filepaths = instance.filepaths
Expand Down
20 changes: 19 additions & 1 deletion qiita_db/support_files/patches/python_patches/58.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Retrieve the Qiita plugin
qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')

# Create the submit to VAMPS command
# Create the submit command for VAMPS command
parameters = {'artifact': ['artifact:["Demultiplexed"]', None]}
Command.create(qiita_plugin, "submit_to_VAMPS",
"submits an artifact to VAMPS", parameters)
Expand All @@ -23,3 +23,21 @@
'prep_template': ['prep_template', None]}
Command.create(qiita_plugin, "copy_artifact",
"Creates a copy of an artifact", parameters)

# Create the submit command for EBI command
parameters = {'artifact': ['artifact:["Demultiplexed"]', None],
'submission_type': ['choice:["ADD", "MODIFY"]', 'ADD']}
Command.create(qiita_plugin, "submit_to_EBI",
"submits an artifact to EBI", parameters)

# Create the submit command for delete_artifact
parameters = {'artifact': ['artifact:["Demultiplexed"]', None]}
Command.create(qiita_plugin, "delete_artifact",
"Delete an artifact", parameters)

# Create the submit command for create a sample template
parameters = {
'fp': ['string', None], 'study_id': ['integer', None],
'is_mapping_file': ['boolean', True], 'data_type': ['string', None]}
Command.create(qiita_plugin, "create_sample_template",
"Create a sample template", parameters)
29 changes: 16 additions & 13 deletions qiita_pet/handlers/api_proxy/sample_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
from qiita_db.metadata_template.util import looks_like_qiime_mapping_file
from qiita_db.exceptions import QiitaDBColumnError
from qiita_db.user import User
from qiita_db.software import Software, Parameters
from qiita_db.processing_job import ProcessingJob
from qiita_ware.dispatchable import (
create_sample_template, update_sample_template, delete_sample_template,
update_sample_template, delete_sample_template,
delete_sample_or_column)
from qiita_ware.context import safe_submit
from qiita_pet.handlers.api_proxy.util import check_access, check_fp
Expand Down Expand Up @@ -333,7 +335,8 @@ def sample_template_post_req(study_id, user_id, data_type,
message has the warnings or errors
file has the file name
"""
access_error = check_access(int(study_id), user_id)
study_id = int(study_id)
access_error = check_access(study_id, user_id)
if access_error:
return access_error
fp_rsp = check_fp(study_id, sample_template)
Expand All @@ -343,27 +346,27 @@ def sample_template_post_req(study_id, user_id, data_type,
fp_rsp = fp_rsp['file']

# Define here the message and message level in case of success
msg = ''
status = 'success'
is_mapping_file = looks_like_qiime_mapping_file(fp_rsp)
if is_mapping_file and not data_type:
return {'status': 'error',
'message': 'Please, choose a data type if uploading a '
'QIIME mapping file',
'file': sample_template}

study = Study(int(study_id))
qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')
cmd = qiita_plugin.get_command('create_sample_template')
params = Parameters.load(cmd, values_dict={
'fp': fp_rsp, 'study_id': study_id, 'is_mapping_file': is_mapping_file,
'data_type': data_type})
job = ProcessingJob.create(User(user_id), params)

r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study_id,
dumps({'job_id': job.id, 'is_qiita_job': True}))

# Offload the creation of the sample template to the cluster
job_id = safe_submit(user_id, create_sample_template, fp_rsp, study,
is_mapping_file, data_type)
# Store the job id attaching it to the sample template id
r_client.set(SAMPLE_TEMPLATE_KEY_FORMAT % study.id,
dumps({'job_id': job_id}))
job.submit()

return {'status': status,
'message': msg,
'file': sample_template}
return {'status': 'success', 'message': '', 'file': sample_template}


def sample_template_put_req(study_id, user_id, sample_template):
Expand Down
14 changes: 9 additions & 5 deletions qiita_pet/handlers/artifact_handlers/base_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_pet.handlers.util import safe_execution
from qiita_pet.exceptions import QiitaHTTPError
from qiita_ware.context import safe_submit
from qiita_ware.dispatchable import delete_artifact
from qiita_db.artifact import Artifact
from qiita_db.software import Command, Parameters
from qiita_db.software import Command, Software, Parameters
from qiita_db.processing_job import ProcessingJob
from qiita_db.util import get_visibilities

Expand Down Expand Up @@ -370,8 +368,14 @@ def artifact_post_req(user, artifact_id):
pt_id = artifact.prep_templates[0].id
redis_key = PREP_TEMPLATE_KEY_FORMAT % pt_id

job_id = safe_submit(user.id, delete_artifact, artifact_id)
r_client.set(redis_key, dumps({'job_id': job_id, 'is_qiita_job': False}))
qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')
cmd = qiita_plugin.get_command('delete_artifact')
params = Parameters.load(cmd, values_dict={'artifact': artifact_id})
job = ProcessingJob.create(user, params)

r_client.set(redis_key, dumps({'job_id': job.id, 'is_qiita_job': True}))

job.submit()


class ArtifactAJAX(BaseHandler):
Expand Down
6 changes: 6 additions & 0 deletions qiita_pet/handlers/study_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ class StudyIndexHandler(BaseHandler):
@authenticated
def get(self, study_id):
study = to_int(study_id)
level = self.get_argument('level', '')
message = self.get_argument('message', '')

study_info = study_get_req(study, self.current_user.id)
if study_info['status'] != 'success':
raise HTTPError(404, study_info['message'])

if message != '':
study_info['level'] = level
study_info['message'] = message

self.render("study_base.html", **study_info)


Expand Down
45 changes: 24 additions & 21 deletions qiita_pet/handlers/study_handlers/ebi_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
from __future__ import division

from tornado.web import authenticated, HTTPError
from tornado.escape import url_escape
from json import dumps

from qiita_files.demux import stats as demux_stats

from qiita_ware.context import submit
from qiita_ware.dispatchable import submit_to_ebi
from qiita_core.qiita_settings import r_client, qiita_config
from qiita_core.util import execute_as_transaction
from qiita_db.metadata_template.constants import (SAMPLE_TEMPLATE_COLUMNS,
PREP_TEMPLATE_COLUMNS)
from qiita_db.exceptions import QiitaDBUnknownIDError
from qiita_db.artifact import Artifact
from qiita_db.processing_job import ProcessingJob
from qiita_db.software import Software, Parameters
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_core.util import execute_as_transaction


class EBISubmitHandler(BaseHandler):
Expand Down Expand Up @@ -118,25 +121,25 @@ def post(self, preprocessed_data_id):
raise HTTPError(403, "User: %s, %s is not a recognized submission "
"type" % (user.id, submission_type))

msg = ''
msg_level = 'success'
study = Artifact(preprocessed_data_id).study
study_id = study.id
state = study.ebi_submission_status
if state == 'submitting':
msg = "Cannot resubmit! Current state is: %s" % state
msg_level = 'danger'
level = 'danger'
message = "Cannot resubmit! Current state is: %s" % state
else:
channel = user.id
job_id = submit(channel, submit_to_ebi, int(preprocessed_data_id),
submission_type)

self.render('compute_wait.html',
job_id=job_id, title='EBI Submission',
completion_redirect=('/study/description/%s?top_tab='
'preprocessed_data_tab&sub_tab=%s'
% (study_id,
preprocessed_data_id)))
return

self.display_template(preprocessed_data_id, msg, msg_level)
qiita_plugin = Software.from_name_and_version('Qiita', 'alpha')
cmd = qiita_plugin.get_command('submit_to_EBI')
params = Parameters.load(
cmd, values_dict={'artifact': preprocessed_data_id,
'submission_type': submission_type})
job = ProcessingJob.create(user, params)

r_client.set('ebi_submission_%s' % preprocessed_data_id,
dumps({'job_id': job.id, 'is_qiita_job': True}))
job.submit()

level = 'success'
message = 'EBI submission started. Job id: %s' % job.id

self.redirect("%s/study/description/%d?level=%s&message=%s" % (
qiita_config.portal_dir, study.id, level, url_escape(message)))
87 changes: 0 additions & 87 deletions qiita_ware/dispatchable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,6 @@
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from qiita_ware.commands import submit_EBI


def submit_to_ebi(preprocessed_data_id, submission_type):
"""Submit a study to EBI"""
submit_EBI(preprocessed_data_id, submission_type, True)


def delete_artifact(artifact_id):
"""Deletes an artifact from the system

Parameters
----------
artifact_id : int
The artifact to delete

Returns
-------
dict of {str: str}
A dict of the form {'status': str, 'message': str}
"""
from qiita_db.artifact import Artifact

status = 'success'
msg = ''
try:
Artifact.delete(artifact_id)
except Exception as e:
status = 'danger'
msg = str(e)

return {'status': status, 'message': msg}


def create_sample_template(fp, study, is_mapping_file, data_type=None):
"""Creates a sample template

Parameters
----------
fp : str
The file path to the template file
study : qiita_db.study.Study
The study to add the sample template to
is_mapping_file : bool
Whether `fp` contains a mapping file or a sample template
data_type : str, optional
If `is_mapping_file` is True, the data type of the prep template to be
created

Returns
-------
dict of {str: str}
A dict of the form {'status': str, 'message': str}
"""
# The imports need to be in here because this code is executed in
# the ipython workers
import warnings
from os import remove
from qiita_db.metadata_template.sample_template import SampleTemplate
from qiita_db.metadata_template.util import load_template_to_dataframe
from qiita_ware.metadata_pipeline import (
create_templates_from_qiime_mapping_file)

status = 'success'
msg = ''
try:
with warnings.catch_warnings(record=True) as warns:
if is_mapping_file:
create_templates_from_qiime_mapping_file(fp, study,
data_type)
else:
SampleTemplate.create(load_template_to_dataframe(fp),
study)
remove(fp)

# join all the warning messages into one. Note that this
# info will be ignored if an exception is raised
if warns:
msg = '\n'.join(set(str(w.message) for w in warns))
status = 'warning'
except Exception as e:
# Some error occurred while processing the sample template
# Show the error to the user so they can fix the template
status = 'danger'
msg = str(e)

return {'status': status, 'message': msg.decode('utf-8', 'replace')}


def update_sample_template(study_id, fp):
Expand Down
Loading