From 0b0fd48c4ef36971e94a1139643d406234f84ea4 Mon Sep 17 00:00:00 2001
From: TJ Lane <thomas.joseph.lane@gmail.com>
Date: Sat, 12 Jun 2021 17:22:10 +0200
Subject: [PATCH] robustness improvements

---
 xia2pipe/dbdaemon.py   |  2 +-
 xia2pipe/dmpldaemon.py |  4 ++--
 xia2pipe/projbase.py   | 54 ++++++++++++++++++++++++++++++------------
 3 files changed, 42 insertions(+), 18 deletions(-)

diff --git a/xia2pipe/dbdaemon.py b/xia2pipe/dbdaemon.py
index f289337..8c04ae1 100644
--- a/xia2pipe/dbdaemon.py
+++ b/xia2pipe/dbdaemon.py
@@ -12,7 +12,7 @@ class DBDaemon(ProjectBase):
 
     def in_db(self, metadata, run, table):
 
-        cid = self.metadata_to_id(metadata, run)
+        cid = self.metadata_to_id(metadata, run) # crystal ID
 
         if table == 'Data_Reduction':
             try:
diff --git a/xia2pipe/dmpldaemon.py b/xia2pipe/dmpldaemon.py
index 0994d21..f24b1e8 100644
--- a/xia2pipe/dmpldaemon.py
+++ b/xia2pipe/dmpldaemon.py
@@ -166,7 +166,7 @@ class DimplingDaemon(ProjectBase):
 #SBATCH --ntasks=1
 #SBATCH --cpus-per-task={nproc}
 #SBATCH --mem=6GB
-#SBATCH --time=5:00:00
+#SBATCH --time=10:00:00
 #SBATCH --job-name  {name}-dmpl_{metadata}-{run}
 #SBATCH --output    {outdir}/{name}-dmpl_{metadata}-{run}.out
 #SBATCH --error     {outdir}/{name}-dmpl_{metadata}-{run}.err
@@ -252,7 +252,7 @@ if __name__ == '__main__':
              #('l11p17_11',   1),
            ] 
 
-    dd = DimplingDaemon.load_config('../configs/d1p7_v2.yaml')
+    dd = DimplingDaemon.load_config('../configs/mpro_v2/DIALS_v2.yaml')
 
     for md, run in jobs:
         print(md, run)
diff --git a/xia2pipe/projbase.py b/xia2pipe/projbase.py
index dd09d51..c51437f 100644
--- a/xia2pipe/projbase.py
+++ b/xia2pipe/projbase.py
@@ -78,22 +78,41 @@ def _phenix_model_b(pdb_path):
 
 
 def _get_pdb_chosen_by_dimple(dimple_log_path):
+    """
+    dimple is used to choose the closest matching PDB
 
-    pdb_path = None
-
-    with open(dimple_log_path, 'r') as f:
-
-        watching = False
-        for line in f.readlines():
+    this function returns just the PDB file name, not a full path
+    """
 
-            if line.startswith('# PDBs in order of similarity (using the first one):'):
-                watching = True
 
-            if line.startswith('#') and watching:
-                g = re.search('(\S+\.pdb)', line)
-                if g:
-                    pdb_path = g.groups()[0]
-                    break
+    # TJL 06 Jan 2021
+    # I think this works and is much better
+    # the old version (commented below) breaks if you have only one PDB input
+
+    config = configparser.ConfigParser()
+    config.read(dimple_log_path)
+
+    pdbs = ast.literal_eval( config['workflow']['pdb_files'] )
+    pdb_path = pdbs[0] # they are in order
+
+    #pdb_path = None
+    #
+    #with open(dimple_log_path, 'r') as f:
+    #    watching = False
+    #    for line in f.readlines():
+    #
+    #        if line.startswith('# PDBs in order of similarity (using the first one):'):
+    #            watching = True
+    #
+    #        if line.startswith('#') and watching:
+    #            g = re.search('(\S+\.pdb)', line)
+    #            if g:
+    #                pdb_path = g.groups()[0]
+    #                break
+    #
+    #if pdb_path is None:
+    #    raise IOError('could not find dimple-chosen reference PDB'
+    #                  ' in: {}'.format(dimple_log_path))
                    
     return pdb_path
 
@@ -139,6 +158,11 @@ class ProjectBase:
         self.xia2_config       = xia2_config
         self.refinement_config = refinement_config
 
+        # this is for backward compatability and is not desirable
+        # provides a default method name
+        if 'method_name' not in self.refinement_config.keys():
+            self.refinement_config['method_name'] = 'dmpl2'
+
         return
 
 
@@ -221,7 +245,7 @@ class ProjectBase:
         qid = self.db.select('refinement_id',
                              '{}.Refinement'.format(self._analysis_db),
                              {'data_reduction_id' : data_reduction_id, 
-                              'method' : 'dmpl2'})
+                              'method' : self.refinement_config['method_name']})
         return get_single(qid, crystal_id, run, 'refinement_id') 
 
 
@@ -603,7 +627,7 @@ class ProjectBase:
                      'initial_pdb_path':     initial_pdb_path,
                      'final_pdb_path':       pdb_path,
                      'refinement_mtz_path':  mtz_path,
-                     'method':               'dmpl2',
+                     'method':               self.refinement_config['method_name'],
                      'resolution_cut':       self.get_refinement_res(metadata, run),
                      'rfree':                log_results[1],
                      'rwork':                log_results[0],
-- 
GitLab