From df7df1592a3e1784178400ef7f06a8dcde9ed698 Mon Sep 17 00:00:00 2001
From: Gerardo Tauriello <gerardo.tauriello@unibas.ch>
Date: Mon, 15 Jul 2019 16:36:40 +0200
Subject: [PATCH] SCHWED-4334: fix gap handling in MM to allow reporting later.

---
 modelling/pymod/_pipeline.py     |  7 +++++--
 modelling/tests/test_pipeline.py | 15 +++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/modelling/pymod/_pipeline.py b/modelling/pymod/_pipeline.py
index b8aef45f..63117e0f 100644
--- a/modelling/pymod/_pipeline.py
+++ b/modelling/pymod/_pipeline.py
@@ -371,8 +371,11 @@ def MinimizeModelEnergy(mhandle, max_iterations=12, max_iter_sd=20,
 
     # update model
     simulation_ent = _GetSimEntity(sim)
-    mhandle.model = mol.CreateEntityFromView(simulation_ent.Select("ele!=H"),
-                                             True)
+    new_model = mol.CreateEntityFromView(simulation_ent.Select("ele!=H"), True)
+    # transfer gaps before overwriting model
+    for gap in mhandle.gaps:
+        gap.Transfer(new_model.FindChain(gap.GetChainName()))
+    mhandle.model = new_model
     # return model with hydrogens
     return simulation_ent
 
diff --git a/modelling/tests/test_pipeline.py b/modelling/tests/test_pipeline.py
index 61c8f44d..90471ecb 100644
--- a/modelling/tests/test_pipeline.py
+++ b/modelling/tests/test_pipeline.py
@@ -390,12 +390,27 @@ class PipelineTests(unittest.TestCase):
         # single chain
         mhandle = self.getRawModel()
         self.checkFinalModel(mhandle, exp_gaps=1, num_chains=1)
+        # repeat after sidechain reconstruction and MM
+        mhandle = self.getRawModel()
+        modelling.BuildSidechains(mhandle)
+        modelling.MinimizeModelEnergy(mhandle)
+        self.checkFinalModel(mhandle, exp_gaps=1, num_chains=1)
         # homo-dimer
         mhandle = self.getRawModelOligo("data/2aoh-1_cut")
         self.checkFinalModel(mhandle, exp_gaps=2, num_chains=2)
+        # repeat after sidechain reconstruction and MM
+        mhandle = self.getRawModelOligo("data/2aoh-1_cut")
+        modelling.BuildSidechains(mhandle)
+        modelling.MinimizeModelEnergy(mhandle)
+        self.checkFinalModel(mhandle, exp_gaps=2, num_chains=2)
         # hetero-dimer
         mhandle = self.getRawModelOligo("data/5d52-1_cut")
         self.checkFinalModel(mhandle, exp_gaps=2, num_chains=2)
+        # repeat after sidechain reconstruction and MM
+        mhandle = self.getRawModelOligo("data/5d52-1_cut")
+        modelling.BuildSidechains(mhandle)
+        modelling.MinimizeModelEnergy(mhandle)
+        self.checkFinalModel(mhandle, exp_gaps=2, num_chains=2)
 
     def testCheckFinalModelRingPunches(self):
         '''Check that we report models with ring punches.'''
-- 
GitLab