From f0484852d9bb1e074503a3c658b2ba7d65af1eaf Mon Sep 17 00:00:00 2001 From: George Marchment <georgemarchment@yahoo.fr> Date: Fri, 21 Mar 2025 14:20:25 +0100 Subject: [PATCH] Update simplified calls + updated the remove of elents inside of maps -> it actually removes them and not just puts ''' inside the maps (so it's cleaner now) --- src/call.py | 4 ++-- src/code_.py | 21 ++++++++++++++++----- src/emitted.py | 2 +- src/nextflow_building_blocks.py | 3 +++ src/nextflow_file.py | 3 +++ src/operation.py | 6 +++--- src/outils_graph.py | 8 +++++++- src/process.py | 3 +++ src/workflow.py | 19 ++++++++++++------- 9 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/call.py b/src/call.py index 5929123..6a03975 100644 --- a/src/call.py +++ b/src/call.py @@ -98,7 +98,7 @@ class Call(Executor): if(temp==code): raise Exception("This souldn't happen") #code = code.replace(param.get_code(get_OG=True), param_new_name) - new_bit = param.simplify_code(new_name = param_new_name) + new_bit = param.simplify_code(new_name = param_new_name, return_tab = False) temp = code code = code.replace(tag_to_add, f"{tag_to_add}\n{new_bit}") if(temp==code): @@ -148,7 +148,7 @@ class Call(Executor): if(temp==code): raise Exception("This souldn't happen") #code = code.replace(param.get_code(get_OG=True), param_new_name) - new_bit = f"{param_new_name} = {param.simplify_code()}" + new_bit = f"{param_new_name} = {param.simplify_code(return_tab = False)}" temp = code code = code.replace(tag_to_add, f"{tag_to_add}\n{new_bit}") if(temp==code): diff --git a/src/code_.py b/src/code_.py index 0ab7136..0a6a0cb 100644 --- a/src/code_.py +++ b/src/code_.py @@ -22,7 +22,7 @@ class Code: self.code_wo_comments = self.code_wo_comments.replace("||", "$OR$") self.code_wo_comments = self.turn_single_condition_into_multiline(self.code_wo_comments) self.code_wo_comments = self.rewrite_ternary_operation_to_normal_condition(self.code_wo_comments) - self.code_wo_comments = self.add_triple_quote_inside_map(self.code_wo_comments ) + self.code_wo_comments = self.remove_things_inside_map(self.code_wo_comments ) self.code_wo_comments = self.rewrite_jump_dot(self.code_wo_comments) @@ -123,7 +123,8 @@ class Code: return code #This function takes the code and adds '''...''' inside the map operator - def add_triple_quote_inside_map(self, code): + def remove_things_inside_map(self, code): + index = 0 searching = True while(searching): searching = False @@ -134,10 +135,17 @@ class Code: for match in re.finditer(pattern, code): start_map, end = match.span(0) end_map = extract_curly(code, end) - old = code[start_map:end_map] - new_code = f".{word}_modified {{ ¤'''\n{code[end:end_map-1]}\n'''¤ }}" - code = code.replace(old, new_code) + + old = code[end:end_map-1] + new = f"¤{id(self)}_{index}¤" + self.add_map_element(old, new) + + old_code = code[start_map:end_map] + new_code = f".{word}_modified {{ {new} }}" + code = code.replace(old_code, new_code) + searching = True + index+=1 break return code @@ -145,6 +153,9 @@ class Code: def add_to_ternary_operation_dico(self, old, new): self.origin.add_to_ternary_operation_dico(old, new) + def add_map_element(self, old, new): + self.origin.add_map_element(old, new) + #This methods rewrite ternary operation into "normal" conditions #variable = (condition) ? Expression2 : Expression3; def rewrite_ternary_operation_to_normal_condition(self, code): diff --git a/src/emitted.py b/src/emitted.py index 946ac4a..42e5475 100644 --- a/src/emitted.py +++ b/src/emitted.py @@ -19,7 +19,7 @@ class Emitted(Channel): def get_all_conditions(self): return self.origin.get_all_conditions() - def simplify_code(self): + def simplify_code(self, return_tab): code = self.get_code(get_OG=True) thing_which_emits = self.emitted_by.get_first_element_called() old_name = thing_which_emits.get_alias() diff --git a/src/nextflow_building_blocks.py b/src/nextflow_building_blocks.py index 5f024e5..0dc2e69 100644 --- a/src/nextflow_building_blocks.py +++ b/src/nextflow_building_blocks.py @@ -20,6 +20,9 @@ class Nextflow_Building_Blocks: def add_to_ternary_operation_dico(self, old, new): self.origin.add_to_ternary_operation_dico(old, new) + + def add_map_element(self, old, new): + self.origin.add_map_element(old, new) def get_code(self, get_OG = False): return self.code.get_code(get_OG = get_OG) diff --git a/src/nextflow_file.py b/src/nextflow_file.py index 829999c..824b613 100644 --- a/src/nextflow_file.py +++ b/src/nextflow_file.py @@ -42,6 +42,9 @@ class Nextflow_File(Nextflow_Building_Blocks): def add_to_ternary_operation_dico(self, old, new): self.workflow.add_to_ternary_operation_dico(old, new) + + def add_map_element(self, old, new): + self.workflow.add_map_element(old, new) def get_root_directory(self): return self.workflow.get_root_directory() diff --git a/src/operation.py b/src/operation.py index 4de0c43..e682a16 100644 --- a/src/operation.py +++ b/src/operation.py @@ -945,7 +945,7 @@ class Operation(Executor): operation_id = str(self)[-7:-2] def add_origin_equals(call, index): - simplified_code = call.simplify_code() + simplified_code = call.simplify_code(return_tab = False) lines = simplified_code.split('\n') return f"{simplified_code}\noperation_{operation_id}_{index} = {call.get_first_element_called().get_alias_and_id()}.out[0]" #if(len(lines)==1): @@ -980,7 +980,7 @@ class Operation(Executor): if(type(val)==str): code = code.replace(origin, val) else: - code = code.replace(origin, val.simplify_code()) + code = code.replace(origin, val.simplify_code(return_tab = False)) if(temp==code): raise Exception("This souldn't happen") #Case there are mutiple origins then: @@ -1000,7 +1000,7 @@ class Operation(Executor): temp = calls[e.emitted_by] except: calls[e.emitted_by] = [] - calls[e.emitted_by].append(e.simplify_code()) + calls[e.emitted_by].append(e.simplify_code(return_tab = False)) for c in calls: #This is just one value if(len(c.get_all_conditions())>1): diff --git a/src/outils_graph.py b/src/outils_graph.py index 3248fe7..eee7ab3 100644 --- a/src/outils_graph.py +++ b/src/outils_graph.py @@ -1170,7 +1170,13 @@ def check_if_equal(dicoA, dicoB): return translated #TO do that we rewrite the structure using a commun language (without using the ids) -> then just check if the translated structures are the same - return translate_dico(dicoA) ==translate_dico(dicoB) + equal = translate_dico(dicoA) ==translate_dico(dicoB) + if(not equal): + #TODO -> make a better affichage des differences + print(translate_dico(dicoA)) + print(translate_dico(dicoB)) + print() + return equal #This function removes the artificial nodes from the dico #MAtching the nodes together between the artificial nodes diff --git a/src/process.py b/src/process.py index bc1f5ce..40f8f76 100644 --- a/src/process.py +++ b/src/process.py @@ -63,6 +63,9 @@ class Process(Nextflow_Building_Blocks): def add_to_ternary_operation_dico(self, old, new): self.nextflow_file.add_to_ternary_operation_dico(old, new) + + def add_map_element(self, old, new): + self.nextflow_file.add_map_element(old, new) def add_to_emits(self, emit): self.later_emits.append(emit) diff --git a/src/workflow.py b/src/workflow.py index 96a5aca..4c5c701 100644 --- a/src/workflow.py +++ b/src/workflow.py @@ -77,6 +77,7 @@ class Workflow: self.graph = None self.conditions_2_ignore = [] self.ternary_operation_dico = {} + self.map_element_dico = {} OG_file = Nextflow_File(file, workflow = self, first_file = True) @@ -440,20 +441,23 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen def add_to_ternary_operation_dico(self, old, new): self.ternary_operation_dico[new] = old + def add_map_element(self, old, new): + self.map_element_dico[new] = old + def put_back_old_ternary_operations(self, code, ternary_operation_dico): for new in ternary_operation_dico: old = ternary_operation_dico[new] code = code.replace(new.strip(), old) return code - def put_modified_operations_back(self, code): + def put_modified_operations_back(self, code, dico_operations): searching = True while(searching): searching = False - for match in re.finditer(r"\.(\w+)_modified\s*\{\s*¤\'\'\'([^¤]+)¤\s*\}", code): + for match in re.finditer(r"\.(\w+)_modified\s*\{\s*(¤[^¤]+¤)\s*\}", code): operator = match.group(1) - inside = match.group(2)[:-3]#Cause we want to remove the extras ...''' - code = code.replace(match.group(0), f".{operator} {{ {inside} }}") + inside = match.group(2)#Cause we want to remove the extras ...''' + code = code.replace(match.group(0), f".{operator} {{ {dico_operations[inside]} }}") searching = True break return code @@ -577,9 +581,9 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen self.graph.initialise(processes_2_remove = self.processes_2_remove) if(def_check_the_same and not self.graph.check_if_process_dependendy_is_equivalent_to_other_without_subworkflows(temp_process_dependency_graph)): if(render_graphs==True): - generate_graph(self.get_output_dir()/ "debug" /"spec_graph_OG", temp_spec_graph, render_graphs = True) + #generate_graph(self.get_output_dir()/ "debug" /"spec_graph_OG", temp_spec_graph, render_graphs = True) generate_graph(self.get_output_dir()/ "debug" /"spec_graph", self.graph.full_dico, render_graphs = True) - generate_graph(self.get_output_dir()/ "debug" /"process_dependency_graph_OG", temp_process_dependency_graph, render_graphs = True) + #generate_graph(self.get_output_dir()/ "debug" /"process_dependency_graph_OG", temp_process_dependency_graph, render_graphs = True) generate_graph(self.get_output_dir()/ "debug" /"process_dependency_graph", self.graph.get_process_dependency_graph() , render_graphs = True) raise Exception("Something went wrong: The flat dependency graph is not the same!") @@ -746,6 +750,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen self.iniatilise_tab_processes_2_remove() self.graph.initialise(processes_2_remove = self.processes_2_remove) ternary_operation_dico = self.ternary_operation_dico + map_element_dico = self.map_element_dico if(self.get_DSL()=="DSL1"): code = self.convert_to_DSL2() @@ -1165,7 +1170,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen #Putting || back code = self.put_back_old_ternary_operations(code, ternary_operation_dico) code = code.replace("$OR$", "||") - code = self.put_modified_operations_back(code) + code = self.put_modified_operations_back(code, map_element_dico) code = remove_extra_jumps(format_with_tabs(code)) f = open(self.get_output_dir()/ "debug" / "rewritten.nf", "w") f.write(code) -- GitLab