|
| 1 | +""" High-level client sitting on top of bioblend for running CWL |
| 2 | +stuff in Galaxy. |
| 3 | +""" |
| 4 | +from __future__ import print_function |
| 5 | + |
| 6 | +import json |
| 7 | +import os |
| 8 | + |
| 9 | +from planemo.io import wait_on |
| 10 | + |
| 11 | + |
| 12 | +DEFAULT_HISTORY_NAME = "CWL Target History" |
| 13 | + |
| 14 | + |
| 15 | +def run_cwl_tool(tool_path, job_path, config, **kwds): |
| 16 | + user_gi = config.user_gi |
| 17 | + admin_gi = config.gi |
| 18 | + |
| 19 | + tool_id = _tool_id(tool_path) |
| 20 | + history_id = _history_id(user_gi, **kwds) |
| 21 | + job_dict = _galactic_job_json(job_path, user_gi, history_id) |
| 22 | + final_state = _wait_for_history(user_gi, history_id) |
| 23 | + if final_state != "ok": |
| 24 | + msg = "Failed to run CWL job final job state is [%s]." % final_state |
| 25 | + with open("errored_galaxy.log", "w") as f: |
| 26 | + f.write(config.log_contents) |
| 27 | + raise Exception(msg) |
| 28 | + run_tool_payload = dict( |
| 29 | + history_id=history_id, |
| 30 | + tool_id=tool_id, |
| 31 | + tool_inputs=job_dict, |
| 32 | + inputs_representation="cwl", |
| 33 | + ) |
| 34 | + run_response = user_gi.tools._tool_post(run_tool_payload) |
| 35 | + job = run_response["jobs"][0] |
| 36 | + job_id = job["id"] |
| 37 | + final_state = _wait_for_job(user_gi, job_id) |
| 38 | + if final_state != "ok": |
| 39 | + msg = "Failed to run CWL job final job state is [%s]." % final_state |
| 40 | + with open("errored_galaxy.log", "w") as f: |
| 41 | + f.write(config.log_contents) |
| 42 | + raise Exception(msg) |
| 43 | + |
| 44 | + job_info = admin_gi.jobs.show_job(job_id) |
| 45 | + cwl_command_state = job_info["cwl_command_state"] |
| 46 | + |
| 47 | + return CwlRunResponse(cwl_command_state, run_response) |
| 48 | + |
| 49 | + |
| 50 | +class CwlRunResponse(object): |
| 51 | + |
| 52 | + def __init__(self, cwl_command_state, api_run_response): |
| 53 | + self.cwl_command_state = cwl_command_state |
| 54 | + self.api_run_response = api_run_response |
| 55 | + |
| 56 | + |
| 57 | +def _history_id(gi, **kwds): |
| 58 | + history_id = kwds.get("history_id", None) |
| 59 | + if history_id is None: |
| 60 | + history_name = kwds.get("history_name", DEFAULT_HISTORY_NAME) |
| 61 | + history_id = gi.histories.create_history(history_name)["id"] |
| 62 | + return history_id |
| 63 | + |
| 64 | + |
| 65 | +def _tool_id(tool_path): |
| 66 | + tool_id, _ = os.path.splitext(os.path.basename(tool_path)) |
| 67 | + return tool_id |
| 68 | + |
| 69 | + |
| 70 | +def _galactic_job_json(job_path, gi, history_id): |
| 71 | + with open(job_path, "r") as f: |
| 72 | + job_as_dict = json.load( f ) |
| 73 | + |
| 74 | + replace_keys = {} |
| 75 | + for key, value in job_as_dict.iteritems(): |
| 76 | + if isinstance( value, dict ): |
| 77 | + type_class = value.get("class", None) |
| 78 | + if type_class != "File": |
| 79 | + continue |
| 80 | + |
| 81 | + file_path = value.get("path", None) |
| 82 | + if file_path is None: |
| 83 | + continue |
| 84 | + |
| 85 | + if not os.path.isabs(file_path): |
| 86 | + directory = os.path.dirname(job_path) |
| 87 | + file_path = os.path.join(directory, file_path) |
| 88 | + |
| 89 | + upload_response = gi.tools.upload_file(file_path, history_id) |
| 90 | + dataset_id = upload_response["outputs"][0]["id"] |
| 91 | + |
| 92 | + replace_keys[key] = {"src": "hda", "id": dataset_id} |
| 93 | + |
| 94 | + job_as_dict.update(replace_keys) |
| 95 | + return job_as_dict |
| 96 | + |
| 97 | + |
| 98 | +def _wait_for_history(gi, history_id): |
| 99 | + state_func = lambda: gi.histories.show_history(history_id) |
| 100 | + return _wait_on_state(state_func) |
| 101 | + |
| 102 | + |
| 103 | +def _wait_for_job(gi, job_id): |
| 104 | + state_func = lambda: gi.jobs.show_job(job_id) |
| 105 | + return _wait_on_state(state_func) |
| 106 | + |
| 107 | + |
| 108 | +def _wait_on_state(state_func): |
| 109 | + |
| 110 | + def get_state(): |
| 111 | + response = state_func() |
| 112 | + state = response[ "state" ] |
| 113 | + if str(state) not in [ "running", "queued", "new", "ready" ]: |
| 114 | + return state |
| 115 | + else: |
| 116 | + return None |
| 117 | + |
| 118 | + final_state = wait_on(get_state, "state", timeout=100) |
| 119 | + return final_state |
0 commit comments