From 773221c7ef5811dd4ec30d65c0ef018237b88ba3 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Fri, 21 Mar 2025 15:44:07 +0100
Subject: [PATCH] I've renamed the takes of a subworkflow when simplifying them
 -> so that the same name isn't deplicated (it created some problems in
 workflows with the same input channels) + i tried to add the id to channels
 -> it doesn't work (so it's in a comment)

---
 src/channel.py     |   4 ++
 src/operation.py   | 101 +++++++++++++++++++++++++++++++++------------
 src/subworkflow.py |  12 +++++-
 3 files changed, 89 insertions(+), 28 deletions(-)

diff --git a/src/channel.py b/src/channel.py
index ca7a9e5..f0ffc4b 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 e682a16..d539056 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 82883dc..ad6b204 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):
-- 
GitLab