Skip to content

Instantly share code, notes, and snippets.

@andrewtarzia
Last active November 15, 2023 17:42
Show Gist options
  • Save andrewtarzia/242f246a48cd20acd20590cbda43abdd to your computer and use it in GitHub Desktop.
Save andrewtarzia/242f246a48cd20acd20590cbda43abdd to your computer and use it in GitHub Desktop.
Extract topologies from stk molecules
"""
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 stko
from rdkit.Chem import AllChem as rdkit
def extract_topo():
struct = stk.BuildingBlock.init_from_file('init.mol')
struct = struct.with_centroid([0, 0, 0])
broken_bonds_by_id = []
disconnectors = []
# This depends on your molecule.
smarts_to_search_for = '[#6X3]~[#7X2]'
rdkit_mol = struct.to_rdkit_mol()
rdkit.SanitizeMol(rdkit_mol)
for atom_ids in rdkit_mol.GetSubstructMatches(
query=rdkit.MolFromSmarts(smarts_to_search_for),
):
bond_c = atom_ids[0]
bond_n = atom_ids[1]
broken_bonds_by_id.append(sorted((bond_c, bond_n)))
disconnectors.extend((bond_c, bond_n))
new_topology_graph = stko.TopologyExtractor()
tg_info = new_topology_graph.extract_topology(
molecule=struct,
broken_bonds_by_id=broken_bonds_by_id,
disconnectors=set(disconnectors),
)
struct.write('tg_cage.mol')
tg_info.write('tg_info.pdb')
return tg_info
def main():
# Generate a fake molecule.
bb1 = stk.BuildingBlock(
smiles='NCCN',
functional_groups=[stk.PrimaryAminoFactory()],
)
bb2 = stk.BuildingBlock(
smiles='O=CC(C=O)C=O',
functional_groups=[stk.AldehydeFactory()],
)
cage = stk.ConstructedMolecule(
topology_graph=stk.cage.FourPlusSix(
building_blocks=(bb1, bb2),
optimizer=stk.MCHammer(),
),
)
cage.write('init.mol')
topo_info = extract_topo()
# Prints the information you need to make an
# stk topology graph.
v_pos = topo_info.get_vertex_positions()
v_con = topo_info.get_connectivities()
for i in v_pos:
pos = v_pos[i]
print(i, v_con[i], [round(i, 1) for i in pos])
edges = topo_info.get_edge_pairs()
for i, pair in enumerate(edges):
print(
'stk.Edge(\n'
f' id={i},\n'
f' vertex1=_vertex_prototypes[{pair[0]}],\n'
f' vertex2=_vertex_prototypes[{pair[1]}],\n'
'),\n'
)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment