Skip to content

Commit

Permalink
Merge pull request #1800 from josenavas/fix-rest-api-target-gene
Browse files Browse the repository at this point in the history
Fix target gene plugin
  • Loading branch information
ElDeveloper committed Apr 20, 2016
2 parents d9d7aa7 + d2acb88 commit 36c0fcd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 89 deletions.
22 changes: 3 additions & 19 deletions qiita_plugins/target_gene/tgp/pick_otus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from glob import glob
from tarfile import open as taropen

from qiita_client import format_payload

from tgp.util import system_call


Expand Down Expand Up @@ -156,24 +154,10 @@ def pick_closed_reference_otus(qclient, job_id, parameters, out_dir):
qclient.update_job_step(job_id, "Step 1 of 4: Collecting information")
artifact_id = parameters['input_data']
fps_info = qclient.get("/qiita_db/artifacts/%s/filepaths/" % artifact_id)
if not fps_info or not fps_info['success']:
error_msg = "Could not get artifact filepath information: %s"
if fps_info:
error_msg = error_msg % fps_info['error']
else:
error_msg = error_msg % "could not connect with the server"
raise ValueError(error_msg)
fps = fps_info['filepaths']

reference_id = parameters['reference']
ref_info = qclient.get("/qiita_db/references/%s/filepaths/" % reference_id)
if not ref_info or not ref_info['success']:
error_msg = "Could not get artifact filepath information: %s"
if ref_info:
error_msg = error_msg % ref_info['error']
else:
error_msg = error_msg % "could not connect with the server"
raise ValueError(error_msg)
reference_fps = ref_info['filepaths']

qclient.update_job_step(job_id, "Step 2 of 4: Generating command")
Expand All @@ -185,16 +169,16 @@ def pick_closed_reference_otus(qclient, job_id, parameters, out_dir):
if return_value != 0:
error_msg = ("Error running OTU picking:\nStd out: %s\nStd err: %s"
% (std_out, std_err))
return format_payload(False, error_msg=error_msg)
return False, None, error_msg

qclient.update_job_step(job_id,
"Step 4 of 4: Generating tgz sortmerna folder")
try:
generate_sortmerna_tgz(pick_out)
except Exception as e:
error_msg = ("Error while tgz failures:\nError: %s" % str(e))
return format_payload(False, error_msg=error_msg)
return False, None, error_msg

artifacts_info = generate_artifact_info(pick_out)

return format_payload(True, artifacts_info=artifacts_info)
return True, artifacts_info, ""
52 changes: 24 additions & 28 deletions qiita_plugins/target_gene/tgp/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from os import makedirs, environ
from future import standard_library

from qiita_client import QiitaClient, format_payload
from qiita_client import QiitaClient

from tgp.split_libraries import split_libraries, split_libraries_fastq
from tgp.pick_otus import pick_closed_reference_otus
Expand Down Expand Up @@ -43,39 +43,35 @@ def execute_job(server_url, job_id, output_dir):
If there is a problem gathering the job information
"""
# Set up the Qiita Client
try:
conf_fp = environ['QP_TARGET_GENE_CONFIG_FP']
except KeyError:
conf_fp = join(dirname(abspath(__file__)), 'support_files',
'config_file.cfg')

dflt_conf_fp = join(dirname(abspath(__file__)), 'support_files',
'config_file.cfg')
conf_fp = environ.get('QP_TARGET_GENE_CONFIG_FP', dflt_conf_fp)
config = ConfigParser()
with open(conf_fp, 'U') as conf_file:
config.readfp(conf_file)

qclient = QiitaClient(server_url, config.get('main', 'CLIENT_ID'),
config.get('main', 'CLIENT_SECRET'),
server_cert=config.get('main', 'SERVER_CERT'))

# Request job information
# Request job information. If there is a problem retrieving the job
# information, the QiitaClient already raises an error
job_info = qclient.get_job_info(job_id)
# Check if we have received the job information so we can start it
if job_info and job_info['success']:
# Starting the heartbeat
qclient.start_heartbeat(job_id)
# Execute the given task
task_name = job_info['command']
task = TASK_DICT[task_name]
# Starting the heartbeat
qclient.start_heartbeat(job_id)
# Execute the given task
task_name = job_info['command']
task = TASK_DICT[task_name]

if not exists(output_dir):
makedirs(output_dir)
try:
payload = task(qclient, job_id, job_info['parameters'],
output_dir)
except Exception:
exc_str = repr(traceback.format_exception(*sys.exc_info()))
error_msg = ("Error executing %s:\n%s" % (task_name, exc_str))
payload = format_payload(False, error_msg=error_msg)
# The job completed
qclient.complete_job(job_id, payload)
else:
raise RuntimeError("Can't get job (%s) information" % job_id)
if not exists(output_dir):
makedirs(output_dir)
try:
success, artifacts_info, error_msg = task(
qclient, job_id, job_info['parameters'], output_dir)
except Exception:
exc_str = repr(traceback.format_exception(*sys.exc_info()))
error_msg = ("Error executing %s:\n%s" % (task_name, exc_str))
success = False
# The job completed
qclient.complete_job(job_id, success, error_msg=error_msg,
artifacts_info=artifacts_info)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

from os.path import join, basename, splitext

from qiita_client import format_payload

from tgp.util import system_call
from .util import (get_artifact_information, split_mapping_file,
generate_demux_file, generate_artifact_info)
Expand Down Expand Up @@ -290,4 +288,4 @@ def split_libraries(qclient, job_id, parameters, out_dir):

artifacts_info = generate_artifact_info(output_dir)

return format_payload(True, artifacts_info=artifacts_info)
return False, artifacts_info, ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import re

import pandas as pd
from qiita_client import format_payload

from tgp.util import system_call
from .util import (get_artifact_information, split_mapping_file,
Expand Down Expand Up @@ -267,4 +266,4 @@ def split_libraries_fastq(qclient, job_id, parameters, out_dir):

artifacts_info = generate_artifact_info(sl_out)

return format_payload(True, artifacts_info=artifacts_info)
return True, artifacts_info, ""
19 changes: 3 additions & 16 deletions qiita_plugins/target_gene/tgp/split_libraries/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ def test_get_artifact_information(self):
httpretty.GET,
"https://test_server.com/qiita_db/artifacts/1/filepaths/",
body='{"filepaths": [["forward_seqs.fastq.gz", "raw_forward_seqs"]'
', ["barcodes.fastq.gz", "raw_barcodes"]], "success": true, '
'"error": ""}')
', ["barcodes.fastq.gz", "raw_barcodes"]]}')
httpretty.register_uri(
httpretty.GET,
"https://test_server.com/qiita_db/artifacts/1/mapping/",
body='{"mapping": "mapping_file.txt", "success": true, '
'"error": ""}')
body='{"mapping": "mapping_file.txt"}')
httpretty.register_uri(
httpretty.GET,
"https://test_server.com/qiita_db/artifacts/1/type/",
body='{"type": "FASTQ", "success": true, "error": ""}')
body='{"type": "FASTQ"}')

obs_fps, obs_mf, obs_at = get_artifact_information(self.qclient, 1)

Expand All @@ -70,17 +68,6 @@ def test_get_artifact_information(self):
self.assertEqual(obs_mf, "mapping_file.txt")
self.assertEqual(obs_at, "FASTQ")

@httpretty.activate
def test_get_artifact_error(self):
# Mock the URIs
httpretty.register_uri(
httpretty.GET,
"https://test_server.com/qiita_db/artifacts/1/filepaths/",
body='{"filepaths": '', "success": false, "error": "some error"}')

with self.assertRaises(ValueError):
get_artifact_information(self.qclient, 1)

def test_split_mapping_file_single(self):
out_dir = mkdtemp()
self._clean_up_files.append(out_dir)
Expand Down
21 changes: 0 additions & 21 deletions qiita_plugins/target_gene/tgp/split_libraries/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,15 @@ def get_artifact_information(qclient, artifact_id):
"""
# Get the artifact filepath information
fps_info = qclient.get("/qiita_db/artifacts/%s/filepaths/" % artifact_id)
if not fps_info or not fps_info['success']:
error_msg = "Could not get artifact filepath information: %s"
if fps_info:
error_msg = error_msg % fps_info['error']
else:
error_msg = error_msg % "could not connect with the server"
raise ValueError(error_msg)
fps = fps_info['filepaths']

# Get the artifact metadata
metadata_info = qclient.get(
"/qiita_db/artifacts/%s/mapping/" % artifact_id)
if not metadata_info or not metadata_info['success']:
error_msg = "Could not get artifact metadata information %s"
if metadata_info:
error_msg = error_msg % metadata_info['error']
else:
error_msg = error_msg % "could not connect with the server"
raise ValueError(error_msg)
mapping_file = metadata_info['mapping']

# Get the artifact type
type_info = qclient.get("/qiita_db/artifacts/%s/type/" % artifact_id)
if not type_info or not type_info['success']:
error_msg = "Could not get artifact metadata information %s"
if type_info:
error_msg = error_msg % type_info['error']
else:
error_msg = error_msg % "could not connect with the server"
raise ValueError(error_msg)
artifact_type = type_info['type']

return fps, mapping_file, artifact_type
Expand Down

0 comments on commit 36c0fcd

Please sign in to comment.