From f9d947fdf6cfac09360f1e4b992609432a7f77c4 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Tue, 15 Apr 2025 10:57:17 +0200
Subject: [PATCH] Added functionnality that the dot positions for the nodes are
 added in the json

---
 src/outils_graph.py | 38 ++++++++++++++++++++++++++++++++++++++
 src/workflow.py     | 14 +++++++++++++-
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/src/outils_graph.py b/src/outils_graph.py
index 878a10d..4091730 100644
--- a/src/outils_graph.py
+++ b/src/outils_graph.py
@@ -1254,3 +1254,41 @@ def remove_artificial_nodes(param_dico):
                 edge = {"A" : source['source'], "B" : sink['sink'], "label" : source['label']}
                 dico["edges"].append(edge)
     return dico
+
+def enrich_json_with_positions(file):
+    import json
+    with open(file, 'r') as JSON:
+        dico = json.load(JSON)
+
+    pos_file = file[:-len(".json")]+"_pos.dot"
+    try:
+        f=open(pos_file)
+        contents_pos =f.read()
+        f.close()
+    except:
+        return 
+    
+    def search_for_pos(id, text):
+        import re
+        pattern = re.escape(id)+r'.+\n.+\s+pos="([^,]+),([^"]+)",'
+        for match in re.finditer(pattern, text):
+            return match.group(1), match.group(2)
+        return -1, -1
+    
+    def enrich_nodes(wf_dico):
+        for node in wf_dico["nodes"]:
+            id = node['id']
+            x_pos, y_pos = search_for_pos(id, contents_pos)
+            if(x_pos==-1 and y_pos==-1):
+                return 
+            else:
+                node['position'] = {}
+                node['position']['x'] = x_pos
+                node['position']['y'] = y_pos
+        for sub in wf_dico['subworkflows']:
+            enrich_nodes(wf_dico['subworkflows'][sub])
+    enrich_nodes(dico)
+    with open(file, 'w') as output_file :
+            json.dump(dico, output_file, indent=4)
+    
+
diff --git a/src/workflow.py b/src/workflow.py
index 02d0650..16231fe 100644
--- a/src/workflow.py
+++ b/src/workflow.py
@@ -4,7 +4,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_place_anker
-from .outils_graph import get_flatten_dico, initia_link_dico_rec, get_number_cycles, generate_graph
+from .outils_graph import get_flatten_dico, initia_link_dico_rec, get_number_cycles, generate_graph, enrich_json_with_positions
 from .outils_annotate import get_tools_commands_from_user_for_process
 from .bioflowinsighterror import BioFlowInsightError
 from .graph import Graph
@@ -197,17 +197,29 @@ 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)
         self.graph.get_specification_graph(render_graphs = render_graphs)
+        self.enrich_json_files_with_positions()
 
     def generate_process_dependency_graph(self, render_graphs = True):
         self.iniatilise_tab_processes_2_remove()
         self.graph.initialise(processes_2_remove = self.processes_2_remove)
         self.graph.render_process_dependency_graph(render_graphs = render_graphs)
+        self.enrich_json_files_with_positions()
+
 
     #TODO -> update this
     def generate_all_graphs(self, render_graphs = True):
         self.generate_specification_graph(render_graphs = render_graphs)
         self.generate_process_dependency_graph(render_graphs = render_graphs)
+        self.enrich_json_files_with_positions()
+
 
+    #This function takes the json files and searches for the corrresponding dot pos files
+    #And adds the information regarding the position of the nodes
+    def enrich_json_files_with_positions(self):
+        jsons = glob.glob(f'{self.get_output_dir()/"graphs"}/*.json', recursive=False)
+        for file in jsons:
+            enrich_json_with_positions(file)
+    
     #Method that checks if a given graph sepcification is an isomorphism with the workflows
     def check_if_json_equal_to_full_structure(self, file):
         self.iniatilise_tab_processes_2_remove()
-- 
GitLab