Skip to content

Instantly share code, notes, and snippets.

@andrewtarzia
Last active November 15, 2023 17:42
Show Gist options
  • Save andrewtarzia/6cd18699bf68a7698d1ab01247f31619 to your computer and use it in GitHub Desktop.
Save andrewtarzia/6cd18699bf68a7698d1ab01247f31619 to your computer and use it in GitHub Desktop.
Replace guests in host-guest complexes
"""
MIT License
Copyright (c) 2023 Andrew Tarzia
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import stk
import networkx as nx
import sys
import os
def get_disconnected_components(molecule):
# Produce a graph from the molecule that does not include edges
# where the bonds to be optimized are.
mol_graph = nx.Graph()
for atom in molecule.get_atoms():
mol_graph.add_node(atom.get_id())
# Add edges.
for bond in molecule.get_bonds():
pair_ids = (
bond.get_atom1().get_id(), bond.get_atom2().get_id()
)
mol_graph.add_edge(*pair_ids)
# Get atom ids in disconnected subgraphs.
components = {}
for c in nx.connected_components(mol_graph):
c_ids = sorted(c)
molecule.write('temp_mol.mol', atom_ids=c_ids)
num_atoms = len(c_ids)
newbb = stk.BuildingBlock.init_from_file('temp_mol.mol')
os.system('rm temp_mol.mol')
components[num_atoms] = newbb
return components
def extract_host(molecule):
components = get_disconnected_components(molecule)
return components[max(components.keys())]
def extract_guest(molecule):
components = get_disconnected_components(molecule)
return components[min(components.keys())]
def main():
if (not len(sys.argv) == 3):
print(
f'Usage: {__file__}\n'
' Expected 2 arguments: host_with_g_file new_guest_file'
)
sys.exit()
else:
host_with_g_file = sys.argv[1]
new_guest_file = sys.argv[2]
# Load in host.
host_with_guest = stk.BuildingBlock.init_from_file(host_with_g_file)
# Load in new guest.
new_guest = stk.BuildingBlock.init_from_file(new_guest_file)
# Split host and guest, assuming host has more atoms than guest.
host = extract_host(host_with_guest)
old_guest = extract_guest(host_with_guest)
# Build new host-guest structure, with Spindry optimiser to
# do some conformer searching.
new_host = stk.ConstructedMolecule(
stk.host_guest.Complex(
host=stk.BuildingBlock.init_from_molecule(host),
guests=(stk.host_guest.Guest(new_guest), ),
# There are options for the Spinner class,
# if the optimised conformer is crap.
optimizer=stk.Spinner(),
),
)
# Write out new host guest.
new_host.write('new_host_guest.mol')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment