20
20
import argparse
21
21
import tempfile
22
22
import subprocess
23
+ import json
23
24
24
25
import matplotlib
25
26
@@ -313,7 +314,7 @@ def make_mc_report(identifier, results, directory, diagram_file, chart_file):
313
314
launch_word_processor (output_file )
314
315
315
316
316
- def main (structure , work_directory , library , csdrefcode ):
317
+ def main (structure , work_directory , failure_directory , library , csdrefcode ):
317
318
# This loads up the CSD if a refcode is requested, otherwise loads the structural file supplied
318
319
if csdrefcode :
319
320
try :
@@ -334,6 +335,7 @@ def main(structure, work_directory, library, csdrefcode):
334
335
coformer_files = glob .glob (os .path .join (library , '*.mol2' ))
335
336
tempdir = tempfile .mkdtemp ()
336
337
mc_dictionary = {}
338
+ failures = []
337
339
338
340
# for each coformer in the library, make a pair file for the api/coformer and run a HBP calculation
339
341
for i , f in enumerate (coformer_files ):
@@ -343,22 +345,33 @@ def main(structure, work_directory, library, csdrefcode):
343
345
crystal = crystal_reader [0 ]
344
346
345
347
directory = os .path .join (os .path .abspath (work_directory ), crystal .identifier )
346
-
347
- try :
348
- propensities , donors , acceptors = propensity_calc (crystal , directory )
349
- coordination_scores = coordination_scores_calc (crystal , directory )
350
- pair_output (crystal .identifier , propensities , donors , acceptors , coordination_scores , directory )
351
- mc_dictionary [coformer_name ] = get_mc_scores (propensities , crystal .identifier )
352
-
353
- except RuntimeError :
354
- print ("Propensity calculation failure for %s!" % coformer_name )
355
- mc_dictionary [coformer_name ] = ["N/A" , "N/A" , "N/A" , "N/A" , "N/A" , crystal .identifier ]
348
+ if os .path .exists (os .path .join (directory , "success.json" )):
349
+ with open (os .path .join (directory , "success.json" ), "r" ) as file :
350
+ tloaded = json .load (file )
351
+ mc_dictionary [coformer_name ] = tloaded
352
+ else :
353
+ try :
354
+ propensities , donors , acceptors = propensity_calc (crystal , directory )
355
+ coordination_scores = coordination_scores_calc (crystal , directory )
356
+ pair_output (crystal .identifier , propensities , donors , acceptors , coordination_scores , directory )
357
+ with open (os .path .join (directory , "success.json" ), "w" ) as file :
358
+ tdata = get_mc_scores (propensities , crystal .identifier )
359
+ json .dump (tdata , file )
360
+ mc_dictionary [coformer_name ] = get_mc_scores (propensities , crystal .identifier )
361
+ print (get_mc_scores (propensities , crystal .identifier ))
362
+ except RuntimeError :
363
+ print ("Propensity calculation failure for %s!" % coformer_name )
364
+ mc_dictionary [coformer_name ] = ["N/A" , "N/A" , "N/A" , "N/A" , "N/A" , crystal .identifier ]
365
+ failures .append (coformer_name )
356
366
357
367
# Make sense of the outputs of all the calculations
358
368
mc_hbp_screen = sorted (mc_dictionary .items (), key = lambda e : 0 if e [1 ][0 ] == 'N/A' else e [1 ][0 ], reverse = True )
359
369
diagram_file = make_diagram (api_molecule , work_directory )
360
370
chart_file = make_mc_chart (mc_hbp_screen , directory , api_molecule )
361
371
make_mc_report (structure , mc_hbp_screen , work_directory , diagram_file , chart_file )
372
+ if failure_directory is not None :
373
+ with open (os .path .join (failure_directory , 'failures.txt' ), 'w' , encoding = 'utf-8' , newline = '' ) as file :
374
+ file .write ('\n ' .join (map (str , failures )))
362
375
363
376
364
377
if __name__ == '__main__' :
@@ -396,9 +409,10 @@ def main(structure, work_directory, library, csdrefcode):
396
409
parser .add_argument ('-c' , '--coformer_library' , type = str ,
397
410
help = 'the directory of the desired coformer library' ,
398
411
default = ccdc_coformers_dir )
412
+ parser .add_argument ('-f' , '--failure_directory' , type = str ,
413
+ help = 'The location where the failures file should be generated' )
399
414
400
415
args = parser .parse_args ()
401
-
402
416
refcode = False
403
417
404
418
if not os .path .isfile (args .input_structure ):
@@ -413,4 +427,4 @@ def main(structure, work_directory, library, csdrefcode):
413
427
if not os .path .isdir (args .coformer_library ):
414
428
parser .error ('%s - library not found.' % args .coformer_library )
415
429
416
- main (args .input_structure , args .directory , args .coformer_library , refcode )
430
+ main (args .input_structure , args .directory , args .failure_directory , args . coformer_library , refcode )
0 commit comments