|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Copyright (c) 2024-2025 The WfCommons Team. |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import shutil |
| 13 | + |
| 14 | +from logging import Logger |
| 15 | +from typing import Optional, Union |
| 16 | + |
| 17 | +from .abstract_translator import Translator |
| 18 | +from ...common import Workflow |
| 19 | + |
| 20 | +this_dir = pathlib.Path(__file__).resolve().parent |
| 21 | + |
| 22 | +class SnakemakeTranslator(Translator): |
| 23 | + """ |
| 24 | + A WfFormat parser for creating Snakemake workflow applications. |
| 25 | +
|
| 26 | + :param workflow: Workflow benchmark object or path to the workflow benchmark JSON instance. |
| 27 | + :type workflow: Union[Workflow, pathlib.Path], |
| 28 | + :param logger: The logger where to log information/warning or errors (optional). |
| 29 | + :type logger: Logger |
| 30 | + """ |
| 31 | + def __init__(self, |
| 32 | + workflow: Union[Workflow, pathlib.Path], |
| 33 | + logger: Optional[Logger] = None) -> None: |
| 34 | + """Create an object of the translator.""" |
| 35 | + super().__init__(workflow, logger) |
| 36 | + self._script = "" |
| 37 | + |
| 38 | + def translate(self, output_folder: pathlib.Path) -> None: |
| 39 | + """ |
| 40 | + Translate a workflow benchmark description (WfFormat) into an actual workflow application. |
| 41 | +
|
| 42 | + :param output_folder: The path to the folder in which the workflow benchmark will be generated. |
| 43 | + :type output_folder: pathlib.Path |
| 44 | + """ |
| 45 | + |
| 46 | + # Generate code |
| 47 | + self._generate_code() |
| 48 | + |
| 49 | + # write benchmark files |
| 50 | + output_folder.mkdir(parents=True) |
| 51 | + with open(output_folder.joinpath("workflow.smk"), "w") as fp: |
| 52 | + fp.write(self._script) |
| 53 | + |
| 54 | + # additional files |
| 55 | + self._copy_binary_files(output_folder) |
| 56 | + self._generate_input_files(output_folder) |
| 57 | + |
| 58 | + # README file |
| 59 | + self._write_readme_file(output_folder) |
| 60 | + |
| 61 | + def _generate_code(self): |
| 62 | + """ |
| 63 | + Generate the Makeflow code |
| 64 | +
|
| 65 | + :return: the code |
| 66 | + :rtype: str |
| 67 | + """ |
| 68 | + all_rule = ("# Rule to force all task executions\n" |
| 69 | + "rule all_wfbench_tasks:\n" |
| 70 | + "\tinput:\n") |
| 71 | + |
| 72 | + self._script = "\n# WfBench task rules\n" |
| 73 | + for task_name, task in self.workflow.tasks.items(): |
| 74 | + rule = f"rule {task_name}:\n" |
| 75 | + # input files |
| 76 | + rule += "\tinput:\n" |
| 77 | + for input_file in task.input_files: |
| 78 | + rule += f"\t\t\"data/{input_file.file_id}\",\n" |
| 79 | + # output files |
| 80 | + rule += "\toutput:\n" |
| 81 | + for output_file in task.output_files: |
| 82 | + all_rule += f"\t\t\"data/{output_file.file_id}\",\n" |
| 83 | + rule += f"\t\t\"data/{output_file.file_id}\",\n" |
| 84 | + # shell |
| 85 | + rule += "\tshell:\n" |
| 86 | + rule += "\t\t'" + task.program + " '\n" |
| 87 | + |
| 88 | + input_spec = "\\'[" |
| 89 | + for file in task.input_files: |
| 90 | + input_spec += f"\"data/{file.file_id}\"," |
| 91 | + input_spec = input_spec[:-1] + "]\\'" |
| 92 | + |
| 93 | + output_spec = "\\'{{" |
| 94 | + for file in task.output_files: |
| 95 | + output_spec += f"\"data/{file.file_id}\":{str(file.size)}," |
| 96 | + output_spec = output_spec[:-1] + "}}\\'" |
| 97 | + |
| 98 | + args = [] |
| 99 | + for a in task.args: |
| 100 | + if "--output-files" in a: |
| 101 | + args.append(f"--output-files {output_spec}") |
| 102 | + elif "--input-files" in a: |
| 103 | + args.append(f"--input-files {input_spec}") |
| 104 | + else: |
| 105 | + args.append(a) |
| 106 | + |
| 107 | + for a in args: |
| 108 | + rule += "\t\t'" + a + " '\n" |
| 109 | + |
| 110 | + self._script += rule + "\n\n" |
| 111 | + |
| 112 | + self._script = all_rule + self._script |
| 113 | + return |
| 114 | + |
| 115 | + def _write_readme_file(self, output_folder: pathlib.Path) -> None: |
| 116 | + """ |
| 117 | + Write the README file. |
| 118 | +
|
| 119 | + :param output_folder: The path of the output folder. |
| 120 | + :type output_folder: pathlib.Path |
| 121 | + """ |
| 122 | + readme_file_path = output_folder.joinpath("README") |
| 123 | + with open(readme_file_path, "w") as out: |
| 124 | + out.write(f"In directory {str(output_folder)}:\n") |
| 125 | + out.write(f" - The Snakemake file: workflow.smk\n") |
| 126 | + out.write(f" - Run the workflow: snakemake -s workflow.smk --cores 1 [--logger snkmt --logger-snkmt-db ./snkmt.sqlite]\n") |
0 commit comments