comparison env/lib/python3.7/site-packages/planemo/engine/galaxy.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 """Module contianing the :class:`GalaxyEngine` implementation of :class:`Engine`."""
2 from __future__ import absolute_import
3
4 import abc
5 import contextlib
6
7 from galaxy.tool_util.verify import interactor
8
9 from planemo.galaxy.activity import execute
10 from planemo.galaxy.config import external_galaxy_config
11 from planemo.galaxy.serve import serve_daemon
12 from planemo.runnable import RunnableType
13 from .interface import BaseEngine
14
15
16 class GalaxyEngine(BaseEngine):
17 """An :class:`Engine` implementation backed by a managed Galaxy.
18
19 More information on Galaxy can be found at http://galaxyproject.org/.
20 """
21
22 __metaclass__ = abc.ABCMeta
23
24 handled_runnable_types = [
25 RunnableType.cwl_tool,
26 RunnableType.cwl_workflow,
27 RunnableType.galaxy_workflow,
28 RunnableType.galaxy_tool,
29 RunnableType.galaxy_datamanager,
30 ]
31
32 def _run(self, runnable, job_path):
33 """Run CWL job in Galaxy."""
34 self._ctx.vlog("Serving artifact [%s] with Galaxy." % (runnable,))
35 with self.ensure_runnables_served([runnable]) as config:
36 self._ctx.vlog("Running job path [%s]" % job_path)
37 run_response = execute(self._ctx, config, runnable, job_path, **self._kwds)
38
39 return run_response
40
41 @abc.abstractmethod
42 def ensure_runnables_served(self, runnables):
43 """Use a context manager and describe Galaxy instance with runnables being served."""
44
45 def _run_test_case(self, test_case):
46 if hasattr(test_case, "job_path"):
47 # Simple file-based job path.
48 return super(GalaxyEngine, self)._run_test_case(test_case)
49 else:
50 with self.ensure_runnables_served([test_case.runnable]) as config:
51 galaxy_interactor_kwds = {
52 "galaxy_url": config.galaxy_url,
53 "master_api_key": config.master_api_key,
54 "api_key": config.user_api_key,
55 "keep_outputs_dir": "", # TODO: this...
56 }
57 tool_id = test_case.tool_id
58 test_index = test_case.test_index
59 tool_version = test_case.tool_version
60 galaxy_interactor = interactor.GalaxyInteractorApi(**galaxy_interactor_kwds)
61
62 test_results = []
63
64 def _register_job_data(job_data):
65 test_results.append({
66 'id': tool_id + "-" + str(test_index),
67 'has_data': True,
68 'data': job_data,
69 })
70
71 verbose = self._ctx.verbose
72 try:
73 if verbose:
74 # TODO: this is pretty hacky, it'd be better to send a stream
75 # and capture the output information somehow.
76 interactor.VERBOSE_GALAXY_ERRORS = True
77
78 interactor.verify_tool(
79 tool_id,
80 galaxy_interactor,
81 test_index=test_index,
82 tool_version=tool_version,
83 register_job_data=_register_job_data,
84 quiet=not verbose,
85 )
86 except Exception:
87 pass
88
89 return test_results[0]
90
91
92 class LocalManagedGalaxyEngine(GalaxyEngine):
93 """An :class:`Engine` implementation backed by a managed Galaxy.
94
95 More information on Galaxy can be found at http://galaxyproject.org/.
96 """
97
98 @contextlib.contextmanager
99 def ensure_runnables_served(self, runnables):
100 # TODO: define an interface for this - not everything in config would make sense for a
101 # pre-existing Galaxy interface.
102 with serve_daemon(self._ctx, runnables, **self._serve_kwds()) as config:
103 yield config
104
105 def _serve_kwds(self):
106 return self._kwds.copy()
107
108
109 class DockerizedManagedGalaxyEngine(LocalManagedGalaxyEngine):
110 """An :class:`Engine` implementation backed by Galaxy running in Docker.
111
112 More information on Galaxy can be found at http://galaxyproject.org/.
113 """
114
115 def _serve_kwds(self):
116 serve_kwds = self._kwds.copy()
117 serve_kwds["dockerize"] = True
118 return serve_kwds
119
120
121 class ExternalGalaxyEngine(GalaxyEngine):
122 """An :class:`Engine` implementation backed by an external Galaxy instance.
123 """
124
125 @contextlib.contextmanager
126 def ensure_runnables_served(self, runnables):
127 # TODO: ensure tools are available
128 with external_galaxy_config(self._ctx, runnables, **self._kwds) as config:
129 config.install_workflows()
130 yield config
131
132
133 __all__ = (
134 "DockerizedManagedGalaxyEngine",
135 "ExternalGalaxyEngine",
136 "LocalManagedGalaxyEngine",
137 )