Skip to content

Commit f4126f3

Browse files
committed
gxformat2 abstraction layer...
1 parent ccd401f commit f4126f3

File tree

3 files changed

+81
-30
lines changed

3 files changed

+81
-30
lines changed

lib/galaxy/managers/workflows.py

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
)
1414

1515
import yaml
16-
from gxformat2 import (
17-
from_galaxy_native,
18-
ImporterGalaxyInterface,
19-
ImportOptions,
20-
python_to_workflow,
21-
)
2216
from gxformat2.abstract import from_dict
2317
from gxformat2.cytoscape import to_cytoscape
2418
from gxformat2.yaml import ordered_dump
@@ -109,6 +103,10 @@
109103
RawTextTerm,
110104
)
111105
from galaxy.work.context import WorkRequestContext
106+
from galaxy.workflow.format2 import (
107+
convert_from_format2,
108+
convert_to_format2,
109+
)
112110
from galaxy.workflow.modules import (
113111
module_factory,
114112
ToolModule,
@@ -610,10 +608,7 @@ def read_workflow_from_path(self, app, user, path, allow_in_directory=None) -> m
610608
workflow_class, as_dict, object_id = artifact_class(trans, as_dict, allow_in_directory=allow_in_directory)
611609
assert workflow_class == "GalaxyWorkflow"
612610
# Format 2 Galaxy workflow.
613-
galaxy_interface = Format2ConverterGalaxyInterface()
614-
import_options = ImportOptions()
615-
import_options.deduplicate_subworkflows = True
616-
as_dict = python_to_workflow(as_dict, galaxy_interface, workflow_directory=None, import_options=import_options)
611+
as_dict = convert_from_format2(as_dict, None)
617612
raw_description = RawWorkflowDescription(as_dict)
618613
created_workflow = self.build_workflow_from_raw_description(trans, raw_description, WorkflowCreateOptions())
619614
return created_workflow.workflow
@@ -640,15 +635,7 @@ def normalize_workflow_format(self, trans, as_dict):
640635
workflow_class, as_dict, object_id = artifact_class(trans, as_dict)
641636
if workflow_class == "GalaxyWorkflow" or "yaml_content" in as_dict:
642637
# Format 2 Galaxy workflow.
643-
galaxy_interface = Format2ConverterGalaxyInterface()
644-
import_options = ImportOptions()
645-
import_options.deduplicate_subworkflows = True
646-
try:
647-
as_dict = python_to_workflow(
648-
as_dict, galaxy_interface, workflow_directory=workflow_directory, import_options=import_options
649-
)
650-
except yaml.scanner.ScannerError as e:
651-
raise exceptions.MalformedContents(str(e))
638+
as_dict = convert_from_format2(as_dict, workflow_directory)
652639

653640
return RawWorkflowDescription(as_dict, workflow_path)
654641

@@ -927,8 +914,8 @@ def workflow_to_dict(self, trans, stored, style="export", version=None, history=
927914
fields like 'url' and 'url' and actual unencoded step ids instead of 'order_index'.
928915
"""
929916

930-
def to_format_2(wf_dict, **kwds):
931-
return from_galaxy_native(wf_dict, None, **kwds)
917+
def to_format_2(wf_dict, json_wrapper: bool):
918+
return convert_to_format2(wf_dict, json_wrapper=json_wrapper)
932919

933920
if version == "":
934921
version = None
@@ -955,7 +942,7 @@ def to_format_2(wf_dict, **kwds):
955942
wf_dict = self._workflow_to_dict_preview(trans, workflow=workflow)
956943
elif style == "format2":
957944
wf_dict = self._workflow_to_dict_export(trans, stored, workflow=workflow)
958-
wf_dict = to_format_2(wf_dict)
945+
wf_dict = to_format_2(wf_dict, json_wrapper=False)
959946
elif style == "format2_wrapped_yaml":
960947
wf_dict = self._workflow_to_dict_export(trans, stored, workflow=workflow)
961948
wf_dict = to_format_2(wf_dict, json_wrapper=True)
@@ -1006,7 +993,7 @@ def store_workflow_to_path(self, workflow_path, stored_workflow, workflow, **kwd
1006993
ordered_dump(abstract_dict, f)
1007994
else:
1008995
wf_dict = self._workflow_to_dict_export(trans, stored_workflow, workflow=workflow)
1009-
wf_dict = from_galaxy_native(wf_dict, None, json_wrapper=True)
996+
wf_dict = convert_to_format2(wf_dict, json_wrapper=True)
1010997
f.write(wf_dict["yaml_content"])
1011998

1012999
def _workflow_to_dict_run(self, trans: ProvidesUserContext, stored, workflow, history=None):
@@ -2237,13 +2224,6 @@ def __init__(self, as_dict, workflow_path=None):
22372224
self.workflow_path = workflow_path
22382225

22392226

2240-
class Format2ConverterGalaxyInterface(ImporterGalaxyInterface):
2241-
def import_workflow(self, workflow, **kwds):
2242-
raise NotImplementedError(
2243-
"Direct format 2 import of nested workflows is not yet implemented, use bioblend client."
2244-
)
2245-
2246-
22472227
def _get_stored_workflow(session, workflow_uuid, workflow_id, by_stored_id):
22482228
stmt = select(StoredWorkflow)
22492229
if workflow_uuid is not None:

lib/galaxy/workflow/format2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import Optional
2+
3+
import yaml
4+
from gxformat2 import (
5+
from_galaxy_native,
6+
ImporterGalaxyInterface,
7+
ImportOptions,
8+
python_to_workflow,
9+
)
10+
11+
from galaxy.exceptions import MalformedContents
12+
13+
14+
def convert_to_format2(as_dict, json_wrapper: bool):
15+
return from_galaxy_native(as_dict, None, json_wrapper=json_wrapper)
16+
17+
18+
def convert_from_format2(as_dict, workflow_directory: Optional[str]):
19+
# Format 2 Galaxy workflow.
20+
galaxy_interface = Format2ConverterGalaxyInterface()
21+
import_options = ImportOptions()
22+
import_options.deduplicate_subworkflows = True
23+
try:
24+
as_dict = python_to_workflow(
25+
as_dict, galaxy_interface, workflow_directory=workflow_directory, import_options=import_options
26+
)
27+
except yaml.scanner.ScannerError as e:
28+
raise MalformedContents(str(e))
29+
return as_dict
30+
31+
32+
class Format2ConverterGalaxyInterface(ImporterGalaxyInterface):
33+
def import_workflow(self, workflow, **kwds):
34+
raise NotImplementedError(
35+
"Direct format 2 import of nested workflows is not yet implemented, use bioblend client."
36+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from galaxy.workflow.format2 import (
2+
convert_from_format2,
3+
convert_to_format2,
4+
)
5+
6+
WORKFLOW_WITH_OUTPUTS = """
7+
class: GalaxyWorkflow
8+
inputs:
9+
input1: data
10+
outputs:
11+
wf_output_1:
12+
outputSource: first_cat/out_file1
13+
steps:
14+
first_cat:
15+
tool_id: cat1
16+
in:
17+
input1: input1
18+
queries_0|input2: input1
19+
"""
20+
21+
22+
def test_convert_from_simple():
23+
as_dict = {"yaml_content": WORKFLOW_WITH_OUTPUTS}
24+
ga_format = convert_from_format2(as_dict, None)
25+
assert ga_format["a_galaxy_workflow"] == "true"
26+
assert ga_format["format-version"] == "0.1"
27+
28+
29+
def test_convert_to_simple():
30+
as_dict = {"yaml_content": WORKFLOW_WITH_OUTPUTS}
31+
ga_format = convert_from_format2(as_dict, None)
32+
33+
as_dict = convert_to_format2(ga_format, True)
34+
assert "yaml_content" in as_dict
35+
assert "yaml_content" not in as_dict

0 commit comments

Comments
 (0)