99from datetime import datetime , timedelta
1010from typing import Dict , Optional
1111
12+ import gemmi
1213import ispyb
1314import pandas as pd
1415import prometheus_client
2627 BLSample ,
2728 BLSession ,
2829 Container ,
30+ Crystal ,
2931 DataCollection ,
3032 ProcessingJob ,
3133 ProcessingJobParameter ,
@@ -250,10 +252,30 @@ def trigger_pandda_xchem(
250252 )
251253 return {"success" : True }
252254
253- # Find corresponding XChem visit directory and database
254- xchem_dir = pathlib .Path (
255- f"/dls/labxchem/data/{ proposal .proposalCode } { proposal .proposalNumber } "
255+ # Get sample details
256+ query = (
257+ session .query (DataCollection , BLSample )
258+ .join (BLSample , BLSample .blSampleId == DataCollection .BLSAMPLEID )
259+ .join (Container , Container .containerId == BLSample .containerId )
260+ .filter (DataCollection .dataCollectionId == dcid )
256261 )
262+
263+ query = query .with_entities (BLSample .name , BLSample .location , Container .code )
264+ dtag = query .one ()[0 ]
265+ location = int (query .one ()[1 ])
266+ container_code = query .one ()[2 ]
267+
268+ # Get user defined spacegroup
269+ query = (
270+ session .query (Crystal .spaceGroup )
271+ .join (BLSample , BLSample .crystalId == Crystal .crystalId )
272+ .filter (BLSample .name == dtag )
273+ )
274+
275+ user_sg = gemmi .find_spacegroup_by_name (query .one ()[0 ]).hm
276+
277+ # Find corresponding XChem visit directory and database
278+ xchem_dir = pathlib .Path (f"/dls/labxchem/data/{ proposal_string } " )
257279 yaml_files = []
258280
259281 for subdir in xchem_dir .iterdir ():
@@ -486,6 +508,7 @@ def trigger_pandda_xchem(
486508 AutoProcProgram ,
487509 AutoProcScalingStatistics ,
488510 AutoProcScaling .autoProcScalingId ,
511+ AutoProc .spaceGroup ,
489512 )
490513 .join (
491514 ProcessingJob ,
@@ -512,16 +535,23 @@ def trigger_pandda_xchem(
512535 )
513536
514537 df = pd .read_sql (query .statement , query .session .bind )
538+ df_filteredbysg = df [df ["spaceGroup" ] == user_sg ]
539+
540+ # use datasets processed in user spacegroup if possible
541+ if not df_filteredbysg .empty :
542+ df = df_filteredbysg
543+
544+ # rank datasets by I/sigI*completeness*# unique reflections
515545 df ["heuristic" ] = (
516546 df ["meanIOverSigI" ].astype (float )
517547 * df ["completeness" ].astype (float )
518548 * df ["nTotalUniqueObservations" ].astype (float )
519549 )
520- # I/sigI*completeness*# unique reflections
550+
521551 df = df [["autoProcScalingId" , "heuristic" ]].copy ()
522552 scaling_ids = df ["autoProcScalingId" ].tolist ()
523553
524- # find associated dimple jobs
554+ # find associated dimple jobs from scaling_ids
525555 query = (
526556 (
527557 session .query (
@@ -602,25 +632,12 @@ def trigger_pandda_xchem(
602632
603633 self .log .debug ("PanDDA2/Pipedream trigger: Starting" )
604634
605- # 2. Get ligand information, location & container code
606-
607- query = (
608- session .query (DataCollection , BLSample )
609- .join (BLSample , BLSample .blSampleId == DataCollection .BLSAMPLEID )
610- .join (Container , Container .containerId == BLSample .containerId )
611- .filter (DataCollection .dataCollectionId == dcid )
612- )
613-
614- query = query .with_entities (BLSample .location , BLSample .name , Container .code )
615- location = int (query .one ()[0 ]) # never multiple?
616- dtag = query .one ()[1 ]
617- code = query .one ()[2 ]
635+ # 2. Read XChem SQLite database for ligand info
618636
619- # Read XChem SQLite database for ligand info
620637 try :
621638 conn = sqlite3 .connect (f"file:{ db } ?mode=ro" , uri = True , timeout = 10 )
622639 df = pd .read_sql_query (
623- f"SELECT * from mainTable WHERE Puck = '{ code } ' AND PuckPosition = { location } AND CrystalName = '{ dtag } '" ,
640+ f"SELECT * from mainTable WHERE Puck = '{ container_code } ' AND PuckPosition = { location } AND CrystalName = '{ dtag } '" ,
624641 conn ,
625642 )
626643
@@ -636,7 +653,7 @@ def trigger_pandda_xchem(
636653
637654 if len (df ) != 1 :
638655 self .log .info (
639- f"Exiting PanDDA2/Pipedream trigger: Unique row in .sqlite for dtag { dtag } , puck { code } , puck position { location } cannot be found in { db } , skipping dcid { dcid } "
656+ f"Exiting PanDDA2/Pipedream trigger: Unique row in .sqlite for dtag { dtag } , puck { container_code } , puck position { location } cannot be found in { db } , skipping dcid { dcid } "
640657 )
641658 return {"success" : True }
642659
0 commit comments