stko.TopologyExtractor

class stko.TopologyExtractor[source]

Bases: object

Extractor of topology definitions from a molecule.

Examples

Using a SMARTS string and the stk.SmartsFunctionalGroupFactory, you can split a molecule at any point to define a topology.

import stk
import stko
import pathlib

smarts = '[#6]~[#7]'

mol = stk.BuildingBlock(
    smiles='C1=CC=C(C=C1)CN',
    # Find the functional groups uses to split the molecule.
    functional_groups=(stk.SmartsFunctionalGroupFactory(
        smarts=smarts,
        bonders=(),
        deleters=(),
    ), )
)

# For each functional group, we add disconnecting atom ids to two
# objects for passing to extractor.
broken_bonds_by_id = []
disconnectors = []
for fg in mol.get_functional_groups():
    atom_ids = list(fg.get_atom_ids())
    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=mol,
    broken_bonds_by_id=broken_bonds_by_id,
    disconnectors=set(disconnectors),
)

# Use these variables to write new topology graph like seen in
# stk source code.
vertex_positions = tg_info.get_vertex_positions()
connectivities = tg_info.get_connectivities()
edge_pairs = tg_info.get_edge_pairs()

# Write molecules to file to visualise new topology graph.
output_directory = pathlib.Path("output_directory")
output_directory.mkdir(exist_ok=True)

# Original molecule.
mol.write(output_directory / "tg_cage.mol")
# Underlying topology graph.
tg_info.write(output_directory / "tg_info.pdb")

Methods

extract_topology

Extract a toplogy defining a molecule with disconnections.

get_connected_graphs

extract_topology(molecule, broken_bonds_by_id, disconnectors)[source]

Extract a toplogy defining a molecule with disconnections.

Parameters:
  • molecule (Molecule) – Molecule to get underlying topology of.

  • broken_bonds_by_id (Iterable[tuple[int, int]]) – Tuples of bonds to break by atom id.

  • disconnectors (set[int]) – Atom ids of disconnection points.

Returns:

Information of the underlying topology.

Return type:

TopologyInfo

get_connected_graphs(molecule, atom_ids_to_disconnect)[source]
Parameters:
Return type:

list