From 3ced45b6dd8cbcd8d92b761927c942457b2c26bd Mon Sep 17 00:00:00 2001
From: unknown <david.hoksza@gmail.com>
Date: Mon, 21 Oct 2019 16:02:49 +0200
Subject: [PATCH] using template reaction positions when laying out not mapped
 connecting species

---
 talent/layout.py | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/talent/layout.py b/talent/layout.py
index 7db754e..a834e21 100644
--- a/talent/layout.py
+++ b/talent/layout.py
@@ -1662,7 +1662,7 @@ def get_graph_stats(g, layout_key=None) -> Stats:
     return stats
 
 
-def l_not_mapped_connecting_species(g_res, tgt_orig, to_layout, nodes_mapping, tmp_stats):
+def l_not_mapped_connecting_species(g_res, tgt_orig, tmp_orig, to_layout, nodes_mapping, md, tmp_stats):
 
     for r1_tgt, r2_tgt, s_ids_to_add, new_ids, cn_existing in to_layout:
 
@@ -1680,10 +1680,12 @@ def l_not_mapped_connecting_species(g_res, tgt_orig, to_layout, nodes_mapping, t
                 return sum(poss_filtered) / len(ids) if len(poss_filtered) > 0 else None
 
             # 1. Find out whether to use horizontal or vertical vector
-            r1_center = get_average_pos(list(g_res[r1_tgt]))
-            r2_center = get_average_pos(list(g_res[r2_tgt]))
+            # We use two strategies, one
 
-            if r1_center[0] == r2_center[0] and r1_center[1] == r2_center[1] and len(s_ids_to_add):
+            r1_center_tgt = get_average_pos(list(g_res[r1_tgt]))
+            r2_center_tgt = get_average_pos(list(g_res[r2_tgt]))
+
+            if r1_center_tgt[0] == r2_center_tgt[0] and r1_center_tgt[1] == r2_center_tgt[1] and len(s_ids_to_add):
                 # This is a situation when two species are connected with two reactions which is a special situations and needs to be handled separately
                 # We need to find the orientation of the reactions which is based on existing connecting species and their already laid out neighbors
                 aux_poss = []
@@ -1706,7 +1708,13 @@ def l_not_mapped_connecting_species(g_res, tgt_orig, to_layout, nodes_mapping, t
                 poss[s_id] = np.array(cn_pos + dir * settings.render.optimal_species_reaction_dist*2)
             else:
 
-                r_vect = r1_center - r2_center
+                # If possible we will use position of the reaction from template to find out how to lay out the connecting species
+                # The reason for using template and not the nascent result target is that it can be missing some of the nodes (such as ODS) which
+                # might be important for deciding the orientation of the line on which the connecting species should be laid out
+                r1_center_tmp_tgt = gu.get_original_layout(tmp_orig, md[0][r1_tgt]).get_center() if r1_tgt in md[0] and md[0][r1_tgt] is not None else r1_center_tgt
+                r2_center_tmp_tgt = gu.get_original_layout(tmp_orig, md[0][r2_tgt]).get_center() if r2_tgt in md[0] and md[0][r2_tgt] is not None else r2_center_tgt
+
+                r_vect = r1_center_tmp_tgt - r2_center_tmp_tgt
                 # TODO: consider the role of the added species (we can place reactants and and products in such a way that the lines will cross unnecessarilly)
                 if gr.utils.is_more_horizontal(r_vect):
                     dir = np.array([0, 1])
@@ -1859,7 +1867,7 @@ def tl_not_mapped_connecting_species(g_res: nx.MultiGraph, tmp, tgt, md, laid_ou
             to_layout.append((r1_tgt, r2_tgt, s_ids_to_add, new_ids, cn_existing))
 
 
-    l_not_mapped_connecting_species(g_res, tgt_orig, to_layout, nodes_mapping, tmp_stats)
+    l_not_mapped_connecting_species(g_res, tgt_orig, tmp.get_reaction_graph(), to_layout, nodes_mapping, md, tmp_stats)
 
 
 
-- 
GitLab