|
| 1 | +# |
| 2 | +# This script can be used for any purpose without limitation subject to the |
| 3 | +# conditions at http://www.ccdc.cam.ac.uk/Community/Pages/Licences/v2.aspx |
| 4 | +# |
| 5 | +# This permission notice and the following statement of attribution must be |
| 6 | +# included in all copies or substantial portions of this script. |
| 7 | +# |
| 8 | +# 2016-12-15: created by S. B. Wiggin, the Cambridge Crystallographic Data Centre |
| 9 | +# 2024-07-02: minor update to include using ccdc utilities to find the solvent file |
| 10 | + |
| 11 | +""" |
| 12 | +Script to identify and remove bound solvent molecules from a MOF structure. |
| 13 | +
|
| 14 | +Solvents are identified using a defined list. |
| 15 | +Output in CIF format includes only framework component with all monodentate solvent removed. |
| 16 | +""" |
| 17 | +####################################################################### |
| 18 | + |
| 19 | +import os |
| 20 | +import glob |
| 21 | +import argparse |
| 22 | + |
| 23 | +from ccdc import io |
| 24 | +from ccdc import utilities |
| 25 | + |
| 26 | +####################################################################### |
| 27 | + |
| 28 | +arg_handler = argparse.ArgumentParser(description=__doc__) |
| 29 | +arg_handler.add_argument( |
| 30 | + 'input_file', |
| 31 | + help='CSD .gcd file from which to read MOF structures' |
| 32 | +) |
| 33 | +arg_handler.add_argument( |
| 34 | + '-o', '--output-directory', |
| 35 | + help='Directory into which to write stripped structures' |
| 36 | +) |
| 37 | +arg_handler.add_argument( |
| 38 | + '-m', '--monodentate', default=False, action='store_true', |
| 39 | + help='Whether or not to strip all unidenate (or monodentate) ligands from the structure' |
| 40 | +) |
| 41 | +arg_handler.add_argument( |
| 42 | + '-s', '--solvent-file', |
| 43 | + help='Location of solvent file' |
| 44 | +) |
| 45 | + |
| 46 | +args = arg_handler.parse_args() |
| 47 | +if not args.output_directory: |
| 48 | + args.output_directory = os.path.dirname(args.input_file) |
| 49 | + |
| 50 | +# Define the solvent smiles patterns |
| 51 | +if not args.solvent_file: |
| 52 | + args.solvent_file = utilities.Resources().get_ccdc_solvents_dir() |
| 53 | + |
| 54 | +if os.path.isdir(args.solvent_file): |
| 55 | + solvent_smiles = [ |
| 56 | + io.MoleculeReader(f)[0].smiles |
| 57 | + for f in glob.glob(os.path.join(args.solvent_file, '*.mol2')) |
| 58 | + ] |
| 59 | +else: |
| 60 | + solvent_smiles = [m.smiles for m in io.MoleculeReader(args.solvent_file)] |
| 61 | + |
| 62 | + |
| 63 | +####################################################################### |
| 64 | + |
| 65 | + |
| 66 | +def is_multidentate(c, mol): |
| 67 | + """ |
| 68 | + Check for components bonded to metals more than once. |
| 69 | + If monodentate is not specified in the arguments, skip this test. |
| 70 | + """ |
| 71 | + if not args.monodentate: |
| 72 | + return True |
| 73 | + got_one = False |
| 74 | + for a in c.atoms: |
| 75 | + orig_a = mol.atom(a.label) |
| 76 | + if any(x.is_metal for b in orig_a.bonds for x in b.atoms): |
| 77 | + if got_one: |
| 78 | + return True |
| 79 | + got_one = True |
| 80 | + return False |
| 81 | + |
| 82 | + |
| 83 | +def is_solvent(c): |
| 84 | + """Check if this component is a solvent.""" |
| 85 | + return c.smiles == 'O' or c.smiles in solvent_smiles |
| 86 | + |
| 87 | + |
| 88 | +def has_metal(c): |
| 89 | + """Check if this component has any metals.""" |
| 90 | + return any(a.is_metal for a in c.atoms) |
| 91 | + |
| 92 | + |
| 93 | +# Iterate over entries |
| 94 | +try: |
| 95 | + for entry in io.EntryReader(args.input_file): |
| 96 | + if entry.has_3d_structure: |
| 97 | + # Ensure labels are unique |
| 98 | + mol = entry.molecule |
| 99 | + mol.normalise_labels() |
| 100 | + # Use a copy |
| 101 | + clone = mol.copy() |
| 102 | + # Remove all bonds containing a metal atom |
| 103 | + clone.remove_bonds(b for b in clone.bonds if any(a.is_metal for a in b.atoms)) |
| 104 | + # Work out which components to remove |
| 105 | + to_remove = [ |
| 106 | + c |
| 107 | + for c in clone.components |
| 108 | + if not has_metal(c) and (not is_multidentate(c, mol) or is_solvent(c)) |
| 109 | + ] |
| 110 | + # Remove the atoms of selected components |
| 111 | + mol.remove_atoms( |
| 112 | + mol.atom(a.label) for c in to_remove for a in c.atoms |
| 113 | + ) |
| 114 | + # Write the CIF |
| 115 | + entry.crystal.molecule = mol |
| 116 | + with io.CrystalWriter('%s/%s_stripped.cif' % (args.output_directory, entry.identifier)) as writer: |
| 117 | + writer.write(entry.crystal) |
| 118 | +except RuntimeError: |
| 119 | + print('File format not recognised') |
0 commit comments