From d259d50a6adacc710addb1abfe8653f7ac8d549e Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Wed, 19 Mar 2025 15:21:44 +0100
Subject: [PATCH] Fixed bug with pipe operators when rewritting (within
 operations and calls) -> using the modified code instead of the OG

---
 src/main.py      |  8 +++++++-
 src/operation.py | 49 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/src/main.py b/src/main.py
index 98127ae..0239858 100644
--- a/src/main.py
+++ b/src/main.py
@@ -50,7 +50,13 @@ class Main(Nextflow_Building_Blocks):
         
         for exe in sorted_executor_2_length:
             if(exe.get_type()=="Call" or exe.get_type()=="Operation"):
-                code = code.replace(exe.get_code(get_OG = True), exe.simplify_code(), 1)
+                old = exe.get_code(get_OG = True)
+                new = exe.simplify_code()
+                if(new!=old):
+                    temp = code
+                    code = code.replace(exe.get_code(get_OG = True), exe.simplify_code(), 1)
+                    if(temp==code):
+                        raise Exception("This shouldn't happen the Exeutor was not replaced")
             else:
                 raise Exception("This shouldn't happen")
         return code
diff --git a/src/operation.py b/src/operation.py
index fbcb5a9..62a1a97 100644
--- a/src/operation.py
+++ b/src/operation.py
@@ -80,6 +80,39 @@ class Operation(Executor):
                 tab+=o.get_elements_called()
         return tab
 
+    #def add_origin_channel(self, name):
+    #    from .channel import Channel
+    #    #Check that the name is not the list of illegal words
+    #    #and Check that the thing extarcted is not WorkflowNameFile like 'WorkflowHgtseq' in nf-core/hgtseq
+    #    if(name not in constant.ERROR_WORDS_ORIGINS):# and name.lower()!=f"workflow{self.get_name_file().lower()}"):
+    #        #channel = Channel(name=name, origin=self.origin)
+    #        if(self.origin.get_type()!="Subworkflow"):
+    #            #First check that the channel is not defined at the same level
+    #            channels = self.origin.get_channels_from_name_same_level(name)
+    #            #Then check that the channel is defined in the below level
+    #            if(channels==[]):
+    #                channels = self.origin.get_channels_from_name_inside_level(name)
+    #            #Finally check if the channels is defined above
+    #            if(channels==[]):
+    #                channels = self.origin.get_channels_from_name_above_level(name)
+    #            if(channels==[]):
+    #                channels = self.origin.get_channels_from_name_other_blocks_on_same_level(name)
+    #            #If it still doesn't exist -> we create it 
+    #            if(channels==[]):
+    #                channel = Channel(name=name, origin=self.origin)
+    #                self.origin.add_channel(channel)
+    #                channels = [channel]
+    #        else:
+    #            channel = Channel(name=name, origin=self.origin)
+    #            self.origin.takes_channels.append(channel)
+    #            channels = [channel]
+    #        
+    #        for channel in channels:
+    #            self.origins.append(channel)
+    #            #channel.initialise()
+    #            channel.add_sink(self)
+
+
 
     def add_origin_channel(self, name):
         from .channel import Channel
@@ -890,7 +923,12 @@ class Operation(Executor):
     
     #Method that rewrites operations to simplify it -> decompose it into multiple line -> to be able to manipulate the calls in a easier way
     def simplify_code(self):
-        code = self.get_code(get_OG=True)
+        code = self.get_code(replace_calls =False, clean_pipe=True)
+        #Remove "emit:" and "takes:" for the subworkklfow inputs and outputs
+        if(code[:6] in ["emit: ", "take: "]):
+            code = code[6:]
+            code = code.strip()
+        #code = self.get_code(get_OG=True)
         index = 1
         operation_id = str(self)[-7:-2]
 
@@ -907,17 +945,18 @@ class Operation(Executor):
         to_add = []
         dico_origin_2_replace = {}
         for o in self.origins:
+            OG_code = o.get_code(get_OG=True)
             try:
-                tmp = dico_origin_2_replace[o.get_code(get_OG=True)]
+                tmp = dico_origin_2_replace[OG_code]
             except:
-                dico_origin_2_replace[o.get_code(get_OG=True)] = []
+                dico_origin_2_replace[OG_code] = []
             if(o.get_type()=="Call"):
                 #If it's not a function -> then we rewrite it
                 if(o.get_first_element_called().get_type()!="Function"):
                     to_add.append(add_origin_equals(o, index))
-                    dico_origin_2_replace[o.get_code(get_OG=True)].append(f"operation_{operation_id}_{index}")
+                    dico_origin_2_replace[OG_code].append(f"operation_{operation_id}_{index}")
             elif(o.get_type()=="Emitted"):
-                dico_origin_2_replace[o.get_code(get_OG=True)].append(o)
+                dico_origin_2_replace[OG_code].append(o)
             index += 1
         
         temporary_index = 1
-- 
GitLab