From 6b23c6ded45f7bb4f551698939ba42061cd63225 Mon Sep 17 00:00:00 2001 From: George Marchment <georgemarchment@yahoo.fr> Date: Fri, 20 Dec 2024 12:15:36 +0100 Subject: [PATCH] update to DSL1 to DSL2: add val around variables in tuple --- src/outils.py | 27 ++++++++++++++++++++++++++- src/process.py | 13 +++++++++---- src/workflow.py | 8 ++++---- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/outils.py b/src/outils.py index 120fb39..65a91ba 100644 --- a/src/outils.py +++ b/src/outils.py @@ -1053,7 +1053,32 @@ def process_2_DSL2(code): code = re.sub(r'file( | *\()', replace_file_by_path, code) code = re.sub(r'set( | *\()', replace_set_by_tuple, code) - return code + return add_vals_when_necessary(code) + +#Function that adds vals around variables in a tuple +def add_vals_when_necessary(line): + starts_with_tuple = False + code_to_replace = "" + for match in re.finditer(r"tuple\s*\((.+)\)", line): + starts_with_tuple = True + code_to_replace = match.group(1) + if(not starts_with_tuple): + for match in re.finditer(r"tuple\s*(.+)", line): + starts_with_tuple = True + code_to_replace = match.group(1) + + if(starts_with_tuple): + code_to_replace = re.split(r'\,\s*emit\s*\:', code_to_replace)[0] + #Adding val to cases where it's just the variable + line_split = code_to_replace.split(',') + for y in range(len(line_split)) : + param = line_split[y] + if(bool(re.fullmatch('\w+', param.strip()))): + line_split[y] = f"val({param.strip()})" + temp = ", ".join(line_split) + line = line.replace(code_to_replace, temp) + return line + def operation_2_DSL2(code, origin): diff --git a/src/process.py b/src/process.py index 4359ea6..f7888f6 100644 --- a/src/process.py +++ b/src/process.py @@ -558,15 +558,20 @@ class Process(Nextflow_Building_Blocks): code = process_2_DSL2(code) lines = [] for line in code.split("\n"): - lines.append(line.split(" from ")[0]) + temp = process_2_DSL2(line.split(" from ")[0]) + lines.append(temp) code = "\n".join(lines) return code def convert_output_code_to_DSL2(self): code = self.output_code - code = process_2_DSL2(code) - code = code.replace(" into ", ", emit: ") - code = code.replace(" mode flatten", "") + lines = [] + for line in code.split("\n"): + line = line.replace(" into ", ", emit: ") + line = line.replace(" mode flatten", "") + line = process_2_DSL2(line) + lines.append(line) + code = "\n".join(lines) return code #This method is to detect which are the channels which need to be flattened diff --git a/src/workflow.py b/src/workflow.py index 43adf83..1a58051 100644 --- a/src/workflow.py +++ b/src/workflow.py @@ -856,7 +856,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen processes_added = [] things_added_in_cluster = [] if(len(elements)>1): - name, body, take, emit = "", "main:\n", "", "" + name, body, take, emit = "", "", "", "" first_element = True for ele in elements: @@ -976,9 +976,9 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen #Replace names inside subworkflow - subworkflow_code = f"subworkflow {name} {{\n{take}\n{body}\n{emit}\n}}" + subworkflow_code = f"workflow {name} {{\n{take}\nmain:\n{body}\n{emit}\n}}" for i in range(len(new_param_names)): - pattern = fr"[\=\,\(] *({re.escape(takes_param[i].get_code())})[\s\,\)]" + pattern = fr"[\=\,\(] *({re.escape(takes_param[i].get_code())})[\s\,\)\.]" subworkflow_code = replace_group1(subworkflow_code, pattern, new_param_names[i]) #subworkflow_code = subworkflow_code.replace(takes_param[i].get_code(), new_param_names[i]) @@ -1011,7 +1011,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen print(format_with_tabs(subworkflow_code)) - print("-----------") + print("//-----------") #TODO -> rmoving the conditions which are problematic -- GitLab