|
| 1 | +import json |
| 2 | +import pickle |
| 3 | +from yaml import CDumper as Dumper, dump |
| 4 | + |
| 5 | + |
| 6 | +from python_workflow_definition.purepython import ( |
| 7 | + group_edges, |
| 8 | + resort_total_lst, |
| 9 | +) |
| 10 | +from python_workflow_definition.shared import ( |
| 11 | + convert_nodes_list_to_dict, |
| 12 | + remove_result, |
| 13 | + EDGES_LABEL, |
| 14 | + NODES_LABEL, |
| 15 | + TARGET_LABEL, |
| 16 | + TARGET_PORT_LABEL, |
| 17 | + SOURCE_LABEL, |
| 18 | + SOURCE_PORT_LABEL, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def get_function_argument(argument: str, position: int = 3) -> dict: |
| 23 | + return { |
| 24 | + argument + '_file': { |
| 25 | + 'type': 'File', |
| 26 | + 'inputBinding': {'prefix': '--arg_' + argument + '=', 'separate': False, 'position': position}, |
| 27 | + }, |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | +def get_function_template(function_name: str) -> dict: |
| 32 | + return { |
| 33 | + 'function': { |
| 34 | + 'default': function_name, |
| 35 | + 'inputBinding': {'position': 2, 'prefix': '--function=', 'separate': False}, |
| 36 | + 'type': 'string', |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def get_output_name(output_name: str) -> dict: |
| 42 | + return { |
| 43 | + output_name + '_file': { |
| 44 | + 'type': 'File', |
| 45 | + 'outputBinding': { |
| 46 | + 'glob': output_name + '.pickle' |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +def get_function(workflow): |
| 53 | + function_nodes_dict = { |
| 54 | + n['id']: n['value'] |
| 55 | + for n in workflow[NODES_LABEL] if n["type"] == "function" |
| 56 | + } |
| 57 | + funct_dict = {} |
| 58 | + for funct_id in function_nodes_dict.keys(): |
| 59 | + target_ports = list(set([e[TARGET_PORT_LABEL] for e in workflow[EDGES_LABEL] if e["target"] == funct_id])) |
| 60 | + source_ports = list(set([e[SOURCE_PORT_LABEL] for e in workflow[EDGES_LABEL] if e["source"] == funct_id])) |
| 61 | + funct_dict[funct_id] = {"targetPorts": target_ports, "sourcePorts": source_ports} |
| 62 | + return function_nodes_dict, funct_dict |
| 63 | + |
| 64 | + |
| 65 | +def write_function_cwl(workflow): |
| 66 | + function_nodes_dict, funct_dict = get_function(workflow) |
| 67 | + file_lst = [] |
| 68 | + |
| 69 | + for i in range(len(function_nodes_dict)): |
| 70 | + template = { |
| 71 | + 'cwlVersion': 'v1.2', |
| 72 | + 'class': 'CommandLineTool', |
| 73 | + 'baseCommand': 'python', |
| 74 | + 'inputs': { |
| 75 | + 'wrapper': { |
| 76 | + 'type': 'File', |
| 77 | + 'inputBinding': {'position': 1}, |
| 78 | + 'default': {'class': 'File', 'location': 'wrapper.py'} |
| 79 | + }, |
| 80 | + }, |
| 81 | + 'outputs': { |
| 82 | + } |
| 83 | + } |
| 84 | + file_name = function_nodes_dict[i].split(".")[-1] + ".cwl" |
| 85 | + if file_name not in file_lst: |
| 86 | + file_lst.append(file_name) |
| 87 | + template["inputs"].update(get_function_template(function_name=function_nodes_dict[i])) |
| 88 | + for j, arg in enumerate(funct_dict[i]['targetPorts']): |
| 89 | + template["inputs"].update(get_function_argument(argument=arg, position=3+j)) |
| 90 | + for out in funct_dict[i]['sourcePorts']: |
| 91 | + if out is None: |
| 92 | + template["outputs"].update(get_output_name(output_name="result")) |
| 93 | + else: |
| 94 | + template["outputs"].update(get_output_name(output_name=out)) |
| 95 | + with open(file_name, "w") as f: |
| 96 | + dump(template, f, Dumper=Dumper) |
| 97 | + |
| 98 | + |
| 99 | +def write_workflow_config(workflow): |
| 100 | + input_dict = { |
| 101 | + n["name"]: n["value"] |
| 102 | + for n in workflow[NODES_LABEL] if n["type"] == "input" |
| 103 | + } |
| 104 | + with open("workflow.yml", "w") as f: |
| 105 | + dump( |
| 106 | + { |
| 107 | + k + "_file": {"class": "File", "path": k + ".pickle"} |
| 108 | + for k in input_dict.keys() |
| 109 | + }, |
| 110 | + f, |
| 111 | + Dumper=Dumper, |
| 112 | + ) |
| 113 | + for k, v in input_dict.items(): |
| 114 | + with open(k + ".pickle", "wb") as f: |
| 115 | + pickle.dump(v, f) |
| 116 | + |
| 117 | + |
| 118 | +def write_workflow(workflow): |
| 119 | + workflow_template = { |
| 120 | + 'cwlVersion': 'v1.2', |
| 121 | + 'class': 'Workflow', |
| 122 | + 'inputs': {}, |
| 123 | + 'steps': {}, |
| 124 | + 'outputs': {}, |
| 125 | + } |
| 126 | + input_dict = { |
| 127 | + n["name"]: n["value"] |
| 128 | + for n in workflow[NODES_LABEL] if n["type"] == "input" |
| 129 | + } |
| 130 | + function_nodes_dict, funct_dict = get_function(workflow) |
| 131 | + result_id = [n["id"] for n in workflow[NODES_LABEL] if n["type"] == "output"][0] |
| 132 | + last_compute_id = [e[SOURCE_LABEL] for e in workflow[EDGES_LABEL] if e[TARGET_LABEL] == result_id][0] |
| 133 | + workflow_template["inputs"].update({k + "_file": "File" for k in input_dict.keys()}) |
| 134 | + if funct_dict[last_compute_id]["sourcePorts"] == [None]: |
| 135 | + workflow_template["outputs"] = { |
| 136 | + "result_file": { |
| 137 | + "type": "File", |
| 138 | + "outputSource": function_nodes_dict[last_compute_id].split(".")[-1] + "/result_file" |
| 139 | + }, |
| 140 | + } |
| 141 | + else: |
| 142 | + raise ValueError() |
| 143 | + |
| 144 | + content = remove_result(workflow_dict=workflow) |
| 145 | + edges_new_lst = content[EDGES_LABEL] |
| 146 | + total_lst = group_edges(edges_new_lst) |
| 147 | + nodes_new_dict = { |
| 148 | + int(k): v for k, v in convert_nodes_list_to_dict(nodes_list=content[NODES_LABEL]).items() |
| 149 | + } |
| 150 | + total_new_lst = resort_total_lst(total_lst=total_lst, nodes_dict=nodes_new_dict) |
| 151 | + step_name_lst = {t[0]: function_nodes_dict[t[0]].split(".")[-1] for t in total_new_lst} |
| 152 | + input_id_dict = {n["id"]: n["name"] for n in workflow[NODES_LABEL] if n["type"] == "input"} |
| 153 | + for t in total_new_lst: |
| 154 | + ind = t[0] |
| 155 | + node_script = step_name_lst[ind] + ".cwl" |
| 156 | + output = [o + "_file" if o is not None else "result_file" for o in funct_dict[ind]['sourcePorts']] |
| 157 | + in_dict = {} |
| 158 | + for k, v in t[1].items(): |
| 159 | + if v[SOURCE_LABEL] in input_id_dict: |
| 160 | + in_dict[k + "_file"] = input_id_dict[v[SOURCE_LABEL]] + "_file" |
| 161 | + else: |
| 162 | + in_dict[k + "_file"] = step_name_lst[v[SOURCE_LABEL]] + "/" + v[SOURCE_PORT_LABEL] + "_file" |
| 163 | + workflow_template["steps"].update({step_name_lst[ind]: {"run": node_script, "in": in_dict, "out": output}}) |
| 164 | + with open("workflow.cwl", "w") as f: |
| 165 | + dump(workflow_template, f, Dumper=Dumper) |
| 166 | + |
| 167 | + |
| 168 | +def load_workflow_json(file_name: str): |
| 169 | + with open(file_name, "r") as f: |
| 170 | + workflow = json.load(f) |
| 171 | + |
| 172 | + write_function_cwl(workflow=workflow) |
| 173 | + write_workflow_config(workflow=workflow) |
| 174 | + write_workflow(workflow=workflow) |
0 commit comments