diff --git a/src/outils.py b/src/outils.py index 9049cdacb2c0b81ce8a31c771ca3415655a60dcd..44c59802f4807efe7298a9b26a41c09f18243fe1 100644 --- a/src/outils.py +++ b/src/outils.py @@ -1343,9 +1343,9 @@ def get_channels_to_add_in_false_conditions(body, emitted_channels): return body -#This function removes the empty conditions -> while keeping the anker_clusters -> if it's orignally in a condtion +#This function removes the empty conditions def remove_empty_conditions(code): - pattern = r"(if *\(.+\)|else)\s*{(\s*|\s*\/\/Anker_clusters\s*)}" + pattern = r"(if *\(.+\)|else)\s*{(\s*)}" def replace(text, pattern): def replacer(match): return match.group(0).replace(match.group(0), match.group(2)) @@ -1356,5 +1356,22 @@ def remove_empty_conditions(code): temp = code code = replace(code, pattern) return code + +#This function removes the empty conditions -> while keeping the anker_clusters -> if it's orignally in a condtion +def remove_empty_conditions_place_anker(code): + conditions = extract_conditions(code) + OG_anker= "//Anker_clusters" + pos = code.find(OG_anker) + conditions_containing_anker = [] + for condition in conditions: + start, end = conditions[condition] + if(start<=pos and pos<=end): + conditions_containing_anker.append(condition.split('$$__$$')[0]) + new_anker = OG_anker + for condition in conditions_containing_anker: + new_anker = f"\n}}\n{new_anker}\nif({condition}) {{\n" + code = code.replace(OG_anker, new_anker) + code = remove_empty_conditions(code) + return code diff --git a/src/workflow.py b/src/workflow.py index 786911629dfae8912dbea7aabf5b35a919d91fb0..2c065fb2c032e2823fb2738f44b8f822ef82358a 100644 --- a/src/workflow.py +++ b/src/workflow.py @@ -3,7 +3,7 @@ from .nextflow_file import Nextflow_File from .ro_crate import RO_Crate from . import constant -from .outils import is_git_directory, format_with_tabs, replace_thing_by_call, replace_group1, group_together_ifs, extract_curly, remove_extra_jumps, get_channels_to_add_in_false_conditions, extract_conditions, remove_empty_conditions +from .outils import is_git_directory, format_with_tabs, replace_thing_by_call, replace_group1, group_together_ifs, extract_curly, remove_extra_jumps, get_channels_to_add_in_false_conditions, extract_conditions, remove_empty_conditions_place_anker from .outils_graph import get_flatten_dico, initia_link_dico_rec, get_number_cycles, generate_graph from .outils_annotate import get_tools_commands_from_user_for_process from .bioflowinsighterror import BioFlowInsightError @@ -838,10 +838,11 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen last_executor_in_cluster = get_last_executor_in_cluster(executors_in_order, clusters) if(last_executor_in_cluster.get_type()=="Process"): call = last_executor_in_cluster.get_call() - code = code.replace(call.get_code(get_OG = True), "//Anker_clusters") + code = code.replace(call.get_code(get_OG = True), "\n//Anker_clusters\n") elif(last_executor_in_cluster.get_type()=="Operation"): if(not last_executor_in_cluster.get_artificial_status()): - code = code.replace(last_executor_in_cluster.get_code(get_OG = True), "//Anker_clusters", 1) + print(last_executor_in_cluster.get_code(get_OG = True)) + code = code.replace(last_executor_in_cluster.get_code(get_OG = True), "\n//Anker_clusters\n", 1) else: raise Exception("This shoudn't happen") else: @@ -863,7 +864,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen raise Exception("This shoudn't happen") #Remove the empty conditions left in the code - code = remove_empty_conditions(code) + code = remove_empty_conditions_place_anker(code) #Add the subworkflow defintions @@ -884,6 +885,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen subworkflow_clusters_to_add, subworkflow_cluster_calls_to_add = [], [] index_cluster = len(clusters) + #We replace the last clusters first -> this is cause the outputs of the last clusters aren't used anywhere else in the workflow by definition for elements in list(reversed(clusters)): channels_to_replace_outside_of_cluster = [] @@ -1087,59 +1089,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen index_cluster-=1 - - """ - #TODO -> rmoving the conditions which are problematic - #This might not be the probleme -> when rerunnung the analysis isn't totally robust - still_simplifying_conditions = True - while(still_simplifying_conditions): - still_simplifying_conditions = False - to_replace, anker1, anker2 = "", "", "" - #Replace if/else - for match in re.finditer(r"if\s*\([^\{]+\{\s*(\/\/Anker_cluster\d|\s)\s*\}\s*else\s*\{\s*(\/\/Anker_cluster\d|\s)\s*\}", code): - to_replace = match.group(0) - anker1, anker2 = match.group(1), match.group(2) - still_simplifying_conditions = True - break - #Replace empty if on its own - if(not still_simplifying_conditions): - for match in re.finditer(r"(if\s*\([^\{]+\{\s*(\/\/Anker_cluster\d|\s)\s*\})\s*[^e]", code): - to_replace = match.group(1) - anker1 = match.group(2) - still_simplifying_conditions = True - break - if(still_simplifying_conditions): - code = code.replace(to_replace, f"{anker1}\n{anker2}") - - - #Replace the ankers by the calls of the subworkflows - for i in range(len(subworkflow_clusters_to_add)): - #Extracting the conditions in the code - conditions_in_code = extract_conditions(code) - anker = f"//Anker_cluster{i}" - pos_anker = code.find(anker) - #For each anker get the condition in which it is called - #Cause we do not want the call of the subworkflows to be done in a condition - #So around the call we place ""}} call if(){if(){"" -> so it's executed ouside of a condition - conditions_for_anker = [] - for c in conditions_in_code: - if(conditions_in_code[c][0]<pos_anker and pos_anker <conditions_in_code[c][1]): - conditions_for_anker.append(c.split("$$__$$")[0]) - #Placing the extract curlies around it - subworkflow_call = subworkflow_cluster_calls_to_add[i] - for c in conditions_for_anker: - subworkflow_call = f"\n}}\n{subworkflow_call}\nif({c}){{\n" - code = code.replace(anker, subworkflow_call) - - - for old, new in channels_to_replace_outside_of_cluster: - pattern= fr"[ \(,]({re.escape(old)})[^\w]" - code = replace_group1(code, pattern, new) - #code = code.replace(old, new) - - - - """ + #Putting || back code = code.replace("$OR$", "||")