diff --git a/src/channel.py b/src/channel.py index ca7a9e59c757d5e26e72fc29f9d64167a70be94d..f0ffc4b3ea08185484bb279a18b45b80d348dd0a 100644 --- a/src/channel.py +++ b/src/channel.py @@ -101,5 +101,9 @@ class Channel(Nextflow_Building_Blocks): for source in self.get_source(): dico["edges"].append({'A':str(source), 'B':str(B), "label":self.get_name()}) + def simplify_code(self, return_tab): + code = self.get_code(get_OG=True) + code = code.replace(code, f"{code}_{id(self)}") + return code diff --git a/src/operation.py b/src/operation.py index e682a168485b30f7171dc7517993cb7c648c07e9..d5390566c60309d9f16824fa729d144e8607fe76 100644 --- a/src/operation.py +++ b/src/operation.py @@ -969,6 +969,11 @@ class Operation(Executor): dico_origin_2_replace[OG_code].append(f"operation_{operation_id}_{index}") elif(o.get_type()=="Emitted"): dico_origin_2_replace[OG_code].append(o) + elif(o.get_type()=="Channel"): + #dico_origin_2_replace[OG_code].append(o) + None + else: + raise Exception("This shoudn't happen") index += 1 temporary_index = 1 @@ -985,36 +990,80 @@ class Operation(Executor): raise Exception("This souldn't happen") #Case there are mutiple origins then: else: + + + temporary_channel = f"temporary_val_{id(self)}_{temporary_index}" + types = [] + for e in dico_origin_2_replace[origin]: + types.append(e.get_type()) + + + if(set(types)=={"Emitted"}): + #Case the multiple origins are emits + #For example + #if(){ + # p1(ch1) + #} else { + # p1(ch2) + #} + #val = p1.out -> when wanting to rewrite this operation + calls = {} + for e in dico_origin_2_replace[origin]: + try: + temp = calls[e.emitted_by] + except: + calls[e.emitted_by] = [] + 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): + raise Exception("This shoudn't happen") + conditions = c.get_all_conditions() + if(conditions!=[]): + for condition in conditions: + new_body+=f"if({condition.get_value().strip()}) {{\n{temporary_channel} = {calls[c][0]}\n}}\n" + else: + values = set(calls[c]) + if(len(values)>1): + raise Exception("This souldn't happen") + for val in values: + new_body+=f"\n{temporary_channel} = {val}" + + #Case the multiple origins are channels #For example #if(){ - # p1(ch1) + # a = ... #} else { - # p1(ch2) + # a = ... #} - #val = p1.out -> when wanting to rewrite this operation - calls = {} - temporary_channel = f"temporary_val_{id(self)}_{temporary_index}" - for e in dico_origin_2_replace[origin]: - - try: - temp = calls[e.emitted_by] - except: - calls[e.emitted_by] = [] - 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): - raise Exception("This shoudn't happen") - conditions = c.get_all_conditions() - if(conditions!=[]): - for condition in conditions: - new_body+=f"if({condition.get_value().strip()}) {{\n{temporary_channel} = {calls[c][0]}\n}}\n" - else: - values = set(calls[c]) - if(len(values)>1): - raise Exception("This souldn't happen") - for val in values: - new_body+=f"\n{temporary_channel} = {val}" + #b = a -> when wanting to rewrite this operation + if(set(types)=={"Channel"}): + None + #operations = {} + #for c in dico_origin_2_replace[origin]: + # for source in c.get_source(): + # try: + # temp = operations[source] + # except: + # operations[source] = [] + # operations[source].append(c.simplify_code(return_tab = False)) + #for o in operations: + # #Turning into one condition + # conditions = [] + # for condition in o.get_all_conditions(): + # conditions.append(condition.get_value().strip()) + # if(conditions!=[]): + # new_body+=f"if({' && '.join(conditions)}) {{\n{temporary_channel} = {operations[o][0]}\n}}\n" + # else: + # values = set(operations[o]) + # if(len(values)>1): + # raise Exception("This souldn't happen") + # for val in values: + # new_body+=f"\n{temporary_channel} = {val}" + else: + raise Exception("This shoudn't happen") + + #new_body+=temporary_channel temp = code code = code.replace(origin, temporary_channel) diff --git a/src/subworkflow.py b/src/subworkflow.py index 82883dc1fc7913836afbc64f86069a077d60bd6f..ad6b204114977065b25cdd38949e86a07a7af699 100644 --- a/src/subworkflow.py +++ b/src/subworkflow.py @@ -5,7 +5,7 @@ import copy from .root import Root from .main import Main from .bioflowinsighterror import BioFlowInsightError -from .outils import remove_jumps_inbetween_parentheses, get_dico_from_tab_from_id, check_if_element_in_tab_rocrate +from .outils import remove_jumps_inbetween_parentheses, replace_group1, get_dico_from_tab_from_id, check_if_element_in_tab_rocrate @@ -99,10 +99,18 @@ class Subworkflow(Main): def simplify_code(self): code = super().simplify_code() + for o in self.emit: code = code.replace(o.get_code(get_OG = True), o.simplify_code(return_tab = False), 1) - return code + #Renaming the takes in the subworkflow + name = self.get_alias() + for t in self.take: + if(len(t.get_gives())!=1): + raise Exception("This shoudn't happen") + ch = t.get_gives()[0] + code = replace_group1(code, fr"[^\w]({re.escape(ch.get_code())})[^\w]", f"{ch.get_code()}_{name}") + return code def set_alias(self, alias):