From 678c01560da443eca2191a8b626450d1e074d525 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Thu, 27 Oct 2022 09:50:45 +0200
Subject: [PATCH] ensure proper processing of Entity when calling Molck

---
 modules/mol/alg/doc/molalg.rst        | 19 +++++++++++++++----
 modules/mol/alg/pymod/export_molck.cc |  5 +++--
 modules/mol/alg/src/molck.cc          | 27 ++++++++++++++++++++-------
 modules/mol/alg/src/molck.hh          |  5 +++--
 4 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/modules/mol/alg/doc/molalg.rst b/modules/mol/alg/doc/molalg.rst
index 89b170b95..3f44eae5d 100644
--- a/modules/mol/alg/doc/molalg.rst
+++ b/modules/mol/alg/doc/molalg.rst
@@ -1356,7 +1356,8 @@ API
 
 .. function:: Molck(ent, lib, settings, [prune=True])
 
-  Runs Molck on provided entity.
+  Runs Molck on provided entity. Reprocesses *ent* with
+  :class:`ost.conop.HeuristicProcessor` and given *lib* once done.
 
   :param ent: Structure to check
   :type ent: :class:`~ost.mol.EntityHandle`
@@ -1369,8 +1370,7 @@ API
   :type prune: :class:`bool` 
 
 
-
-.. function:: MapNonStandardResidues(ent, lib)
+.. function:: MapNonStandardResidues(ent, lib, reprocess=True)
 
   Maps modified residues back to the parent amino acid, for example MSE -> MET.
 
@@ -1378,10 +1378,16 @@ API
   :type ent: :class:`~ost.mol.EntityHandle`
   :param lib: Compound library
   :type lib: :class:`~ost.conop.CompoundLib`
+  :param reprocess: The function generates a deep copy of *ent*. Highly
+                    recommended to enable *reprocess* that runs
+                    :class:`ost.conop.HeuristicProcessor` with given *lib*.
+                    If set to False, you'll have no connectivity etc. after
+                    calling this function.
 
 .. function:: RemoveAtoms(ent, lib, rm_unk_atoms=False, rm_non_std=False, \
                           rm_hyd_atoms=True, rm_oxt_atoms=False, \
-                          rm_zero_occ_atoms=False, colored=False)
+                          rm_zero_occ_atoms=False, colored=False,
+                          reprocess=True)
 
   Removes atoms and residues according to some criteria.
 
@@ -1395,6 +1401,11 @@ API
   :param rm_oxt_atoms: See :attr:`MolckSettings.rm_oxt_atoms`
   :param rm_zero_occ_atoms: See :attr:`MolckSettings.rm_zero_occ_atoms`
   :param colored: See :attr:`MolckSettings.colored`
+  :param reprocess: Removing atoms may impact certain annotations on the
+                    structure (chem class etc.) which are set by 
+                    :class:`ost.conop.Processor`. If set to True,
+                    a :class:`ost.conop.HeuristicProcessor` with given
+                    *lib* reprocesses *ent*.
 
 .. function:: CleanUpElementColumn(ent, lib)
 
diff --git a/modules/mol/alg/pymod/export_molck.cc b/modules/mol/alg/pymod/export_molck.cc
index 9671434ba..aa50a674f 100644
--- a/modules/mol/alg/pymod/export_molck.cc
+++ b/modules/mol/alg/pymod/export_molck.cc
@@ -121,7 +121,7 @@ void export_Molck()
 
   def("MapNonStandardResidues", &MapNonStandardResidues, (arg("ent"),
                                                           arg("lib"),
-                                                          arg("log_diags")=false));
+                                                          arg("reprocess")=true));
 
   def("RemoveAtoms", &RemoveAtoms, (arg("ent"),
                                     arg("lib"),
@@ -130,7 +130,8 @@ void export_Molck()
                                     arg("rm_hyd_atoms")=true,
                                     arg("rm_oxt_atoms")=false,
                                     arg("rm_zero_occ_atoms")=false,
-                                    arg("colored")=false));
+                                    arg("colored")=false,
+                                    arg("reprocess")=true));
 
   def("CleanUpElementColumn", &CleanUpElementColumn, (arg("ent"), arg("lib")));
 
diff --git a/modules/mol/alg/src/molck.cc b/modules/mol/alg/src/molck.cc
index 1a140b559..d9dd3ce63 100644
--- a/modules/mol/alg/src/molck.cc
+++ b/modules/mol/alg/src/molck.cc
@@ -12,7 +12,7 @@ using namespace ost::mol;
 
 namespace ost{ namespace mol{ namespace alg{
 
-void MapNonStandardResidues(EntityHandle& ent, CompoundLibPtr lib, bool log_diags) {
+void MapNonStandardResidues(EntityHandle& ent, CompoundLibPtr lib, bool reprocess) {
   // TODO: Maybe it is possible to make it in-place operation
 
   if(!lib) {
@@ -52,9 +52,11 @@ void MapNonStandardResidues(EntityHandle& ent, CompoundLibPtr lib, bool log_diag
     }
   }
   ent = new_ent;
-  // Since we didn't do it in-place: reprocess the new entity
-  RuleBasedProcessor pr(lib);
-  pr.Process(ent, log_diags);
+
+  if(reprocess) {
+    RuleBasedProcessor pr(lib);
+    pr.Process(ent, false);
+  }    
 }
 
 void RemoveAtoms(EntityHandle& ent,
@@ -64,7 +66,8 @@ void RemoveAtoms(EntityHandle& ent,
                  bool rm_hyd_atoms,
                  bool rm_oxt_atoms,
                  bool rm_zero_occ_atoms,
-                 bool colored /*=true*/){
+                 bool colored, /*=true*/
+                 bool reprocess){
 
   if(!lib) {
     throw ost::Error("Require valid compound library!");
@@ -140,6 +143,11 @@ void RemoveAtoms(EntityHandle& ent,
     }
     LOG_INFO(ss.str());
   }
+
+  if(reprocess) {
+    RuleBasedProcessor pr(lib);
+    pr.Process(ent, false);
+  }    
 }
 
 void CleanUpElementColumn(EntityHandle& ent, CompoundLibPtr lib){
@@ -191,7 +199,8 @@ void Molck(ost::mol::EntityHandle& ent,
               settings.rm_hyd_atoms,
               settings.rm_oxt_atoms,
               settings.rm_zero_occ_atoms,
-              settings.colored);
+              settings.colored,
+              false);
   if (settings.assign_elem)  {
     CleanUpElementColumn(ent, lib);
   } 
@@ -199,7 +208,11 @@ void Molck(ost::mol::EntityHandle& ent,
   if(prune) {
     ost::mol::XCSEditor edi = ent.EditXCS();
     edi.Prune();
-  }         
+  }
+
+  // reprocess
+  RuleBasedProcessor pr(lib);
+  pr.Process(ent, false);    
 }
 
 }}} // ns
\ No newline at end of file
diff --git a/modules/mol/alg/src/molck.hh b/modules/mol/alg/src/molck.hh
index 0dae68b93..145171b8e 100644
--- a/modules/mol/alg/src/molck.hh
+++ b/modules/mol/alg/src/molck.hh
@@ -81,7 +81,7 @@ struct MolckSettings{
 
 void MapNonStandardResidues(ost::mol::EntityHandle& ent,
                             ost::conop::CompoundLibPtr lib,
-                            bool log_diags = true);
+                            bool reprocess=true);
 
 void RemoveAtoms(ost::mol::EntityHandle& ent,
                  ost::conop::CompoundLibPtr lib,
@@ -90,7 +90,8 @@ void RemoveAtoms(ost::mol::EntityHandle& ent,
                  bool rm_hyd_atoms,
                  bool rm_oxt_atoms,
                  bool rm_zero_occ_atoms,
-                 bool colored=true);
+                 bool colored=true,
+                 bool reprocess=true);
 
 void CleanUpElementColumn(ost::mol::EntityHandle& ent,
                           ost::conop::CompoundLibPtr lib);
-- 
GitLab