From c6bbb7fe7d4a02b47ffdc36d012aab7daa1e6961 Mon Sep 17 00:00:00 2001
From: unknown <david.hoksza@gmail.com>
Date: Tue, 22 Oct 2019 13:22:50 +0200
Subject: [PATCH] map transfer store split to map_transfer and store

---
 talent/utils/transfer.py | 70 ++++++++++++++++++++++++----------------
 1 file changed, 42 insertions(+), 28 deletions(-)

diff --git a/talent/utils/transfer.py b/talent/utils/transfer.py
index f40e98a..c6bb67d 100644
--- a/talent/utils/transfer.py
+++ b/talent/utils/transfer.py
@@ -8,6 +8,7 @@ import pickle
 from sged import sged_graph as ed
 
 from typing import List
+from typing import NamedTuple
 
 import sys
 sys.path.append(os.path.dirname(os.path.abspath(__file__))+"/../..")
@@ -23,6 +24,9 @@ from talent.beautification import BeautificationChain
 from talent.beautification import BeautificationOp
 from talent.beautification import BEAUTY_OPERATION_TYPE
 
+class MapTransferResult(NamedTuple):
+    bc: BeautificationChain
+    tmp: ReactionGraph
 
 def get_rg_restricted_g(rg, g):
     res = gu.deepcopy_graph(g)
@@ -72,7 +76,7 @@ def process_tgt(tgt_name, tgt_fname, tgt_fmt, split_by_cmprtmnt, ddup):
     return tgt_ccs
 
 
-def process_db(tmps_path, fmt, split_by_cmprtmnt, ddup, cc_before_ddup):
+def process_db(tmps_path, fmt, split_by_cmprtmnt, ddup, cc_before_ddup) -> List[ReactionGraph]:
     """
     DB is a directory with json files of the compartments to be used as templates
     :param dir_tmps:
@@ -244,14 +248,11 @@ def approximate_distances(tgt, tmps, node_edge_costs, cache_path):
     return approx_res
 
 
-# def map_tgt_tmps(tgt_orig, tmps, use_cache, output_dir = ""):
-def map_transfer_store(tgt, tmps, cache_path, output_path, ix_connected_component) -> List[str]:
+def map_transfer(tgt, tmps, cache_path) -> List[MapTransferResult]:
 
-    # tgt = tgt_orig.deepcopy()
+    assert (len(tmps) > 0)
 
-    assert(len(tmps) > 0)
-
-    output_fnames = []
+    rv: List[MapTransferResult] = []
 
     ed_settings = settings.ed
 
@@ -260,23 +261,25 @@ def map_transfer_store(tgt, tmps, cache_path, output_path, ix_connected_componen
 
     if len(tmps) > 1:
         approx_res = approximate_distances(tgt, tmps, costs, cache_path)
-        approx_res_ix_sort = sorted(range(len(approx_res)), key=lambda k: approx_res[k][0]['distance'] if len(approx_res[k]) > 0 else sys.float_info.max)
+        approx_res_ix_sort = sorted(range(len(approx_res)), key=lambda k: approx_res[k][0]['distance'] if len(
+            approx_res[k]) > 0 else sys.float_info.max)
     else:
         logging.info("Skipping approximation")
 
+
     for i in range(min(settings.cnt_approx_to_try, len(tmps))):
 
         if len(tmps) > 1:
-            i_closest = approx_res_ix_sort[i]
+            ix_tmp = approx_res_ix_sort[i]
         else:
-            i_closest = 0
+            ix_tmp = 0
 
-        tmp = tmps[i_closest]
+        tmp = tmps[ix_tmp]
         logging.info("Picking {} as the template ({}. closest template)".format(tmp.get_name(), i))
 
-        res = obtain_ed(tgt, tmp, costs[i_closest], cache_path, ed_settings)
+        res = obtain_ed(tgt, tmp, costs[ix_tmp], cache_path, ed_settings)
         if len(res) == 0:
-            res = approx_res[i_closest]
+            res = approx_res[ix_tmp]
             logging.info("Used approximate mapping")
             if len(res) == 0:
                 logging.info("No mapping available. Skipping...")
@@ -284,23 +287,33 @@ def map_transfer_store(tgt, tmps, cache_path, output_path, ix_connected_componen
         logging.info("Used mapping: distance {} - {}".format(res[0]['distance'], res[0]['mapping']))
 
         logging.info("Transferring layout from {} to {}".format(tmp.get_name(), tgt.get_name()))
-        res = lt.transfer_layout(tgt, tmp, res[0]["mapping"]) #TODO result can contain multiple mappings with the same minimum distance
+        res = lt.transfer_layout(tgt, tmp, res[0][
+            "mapping"])  # TODO result can contain multiple mappings with the same minimum distance
         bc = BeautificationChain(res)
-        # bc.add([
-        #     # BeautificationStep(BEAUTY_OPERATION_TYPE.PUSH_AWAY_SIDE_SPECIES),
-        #     BeautificationOp(BEAUTY_OPERATION_TYPE.ALIGN_TO_GRID),
-        #     BeautificationOp(BEAUTY_OPERATION_TYPE.REMOVE_EMPTY_BANDS),
-        #     BeautificationOp(BEAUTY_OPERATION_TYPE.HANDLE_REACTION_SPECIES_OVERLAPS)
-        # ])
-        # bc.run()
+        rv.append(MapTransferResult(bc=bc, tmp=tmp))
+
+    return rv
+
+def map_transfer_store(tgt:ReactionGraph, tmps: List[ReactionGraph], cache_path: str, output_path: str) -> List[str]:
+
+    assert (len(tmps) > 0)
+
+    rv_fnames: List[str] = []
+
+    mtrs: List[MapTransferResult] = map_transfer(tgt, tmps, cache_path)
+
+    i = 0
+    for mtr in mtrs:
+        tmp = mtr.tmp
+        bc = mtr.bc
         fname = "{}/{}-tmp_order_{}-tgt-ddup_{}_tmp-ddup_{}.pkl".format(output_path, get_tgt_tmp_name(tgt, tmp), i,
-                                                                            tgt.is_ddup(), tmp.is_ddup())
-        output_fnames.append(fname)
-        # fname = "{}_cc-{}_k-{}.pkl".format(output_path, ix_connected_component, i)
+                                                                        tgt.is_ddup(), tmp.is_ddup())
+        i += 1
+        rv_fnames.append(fname)
         logging.info("Storing transferred layout into {}".format(fname))
         bc.save(fname)
 
-    return output_fnames
+    return rv_fnames
 
 
 def transfer(tgt_name, tgt_fname, tgt_fmt, split_by_cmprtmnt, tmps_path, tmp_fmt,
@@ -331,14 +344,15 @@ def transfer(tgt_name, tgt_fname, tgt_fmt, split_by_cmprtmnt, tmps_path, tmp_fmt
     }
 
     tgt_ccs = process_tgt(tgt_name, tgt_fname, tgt_fmt, split_by_cmprtmnt, ddup_tgt)
-    tmps = process_db(tmps_path, tmp_fmt, split_by_cmprtmnt, ddup_tmp, tmp_cc_before_ddup)
+    tmps: List[ReactionGraph] = process_db(tmps_path, tmp_fmt, split_by_cmprtmnt, ddup_tmp, tmp_cc_before_ddup)
 
     i = 0
     for tgt in tgt_ccs:
         logging.info("Mapping connected component {} (size {})".format(i, len(tgt.get_reaction_graph())))
         i += 1
-        fnames = map_transfer_store(tgt, tmps, cache_path, output_path, i)
-        # TODO: remove in dist
+        # fnames = map_transfer_store(tgt, tmps, cache_path, output_path, i)
+        fnames = map_transfer_store(tgt, tmps, cache_path, output_path)
+        # TODO: remove in distribution
         for fname in fnames:
             export.export(input_path=fname, format="svg", minerva_instance=settings.minerva_instance, output_path=fname + ".svg")
             export.export(input_path=fname, format="celldesigner", minerva_instance=settings.minerva_instance, output_path=fname + ".xml")
-- 
GitLab