diff --git a/src/call.py b/src/call.py index 592912314f451cd1c8052a650372611669637846..6a03975d8308967d67c55c16f812d76da08fcb49 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 0ab7136c51299033e25e92373d46c1d3208429e4..0a6a0cb4720bc2ed8acb6b3759ed1fee0e51131f 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 946ac4a1070172799a688d1723f2adcc88671f84..42e547598f50bb9263d87ce4192ce4a6dc9fd569 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 5f024e506b962ffa2c6bb17672bff6cd11a1ab81..0dc2e697fd353ed3a5e14c18eaa0aa5ac7c4db76 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 829999c2f7f07ff4f0f04d4ac90dd31b9c097888..824b613b651fcc47f9852433791871f474308030 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 4de0c436ebaf93922ed1b9eb8f3faccc831d8e61..e682a168485b30f7171dc7517993cb7c648c07e9 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 3248fe725d2a53d3c8356e13628bb8c40a988c70..eee7ab3bb15ccc1af77a2dfeebcd8585413823f7 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 bc1f5ceccbfb40ad9a8ce21dd6baa6263c08b6f8..40f8f768cfdecb89bd01987ac8c503c2b204b9f2 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 96a5aca8275beea7f63ce677afe5456a465b44c4..4c5c7019e0936e90f34d72968cfe279475531cc1 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)