stko.Network

class stko.Network(graph)[source]

Bases: object

Definition of a networkx graph of an stk.Molecule.

Parameters:

graph (Graph) – The NetworkX graph to initialise from.

Examples

An stk molecule can be converted into a NetworkX object. This allows for the disconnection and manipulation of the molecular graph.

import stk
import stko
import numpy as np

molecule = stk.BuildingBlock('NCCNCCN').with_centroid(
    position=np.array((10, 10, 10))
)
graph = stko.Network.init_from_molecule(molecule)

# Pick some bonds to break based on atom ids in those bonds.
atom_ids_to_disconnect = ((2, 3),)
graph = graph.with_deleted_bonds(atom_ids_to_disconnect)

# A series of graphs still connected.
# This gives a list of atoms in each graph.
connected_graphs = graph.get_connected_components()

# Get centroids of connected graphs.
centroids = [
    molecule.get_centroid(
        atom_ids=tuple(i.get_id() for i in list(cg))
    )
    for cg in connected_graphs
]

# Get individual stk.Molecule classes of each connected graph.
# This is what stko.molecule_analysis.DecomposeMOC does
# after deleting metal atoms!
for atom_ids in connected_graphs:
    # Get atoms from nodes.
    atoms = list(atom_ids)
    atom_ids = tuple(i.get_id() for i in atoms)

    # Sort both by atom id.
    atom_ids, atoms = zip(
        *sorted(zip(atom_ids, atoms, strict=True)), strict=True
    )

    # Map old ids to a new molecule ids.
    atom_ids_map = {atom_ids[i]: i for i in range(len(atom_ids))}

    # Make a new stk.Building Block.
    new_mol = stk.BuildingBlock.init(
        atoms=(
            stk.Atom(
                id=atom_ids_map[i.get_id()],
                atomic_number=i.get_atomic_number(),
                charge=i.get_charge(),
            )
            for i in atoms
        ),
        bonds=(
            i.with_ids(id_map=atom_ids_map)
            for i in molecule.get_bonds()
            if i.get_atom1().get_id() in atom_ids
            and i.get_atom2().get_id() in atom_ids
        ),
        position_matrix=np.array(
            tuple(
                i
                for i in molecule.get_atomic_positions(
                    atom_ids=atom_ids
                )
            )
        ),
    )

    # Now do something with this molecule!

Methods

clone

Return a clone.

get_connected_components

Get connected components within full graph.

get_graph

Return a networkx.Graph.

get_nodes

Yield nodes of networkx.Graph (PositionAtom).

init_from_molecule

Initialize from an stk molecule.

with_deleted_bonds

Return a clone with edges between atom_ids deleted.

with_deleted_elements

Return a clone with nodes with atomic numbers deleted.

classmethod init_from_molecule(molecule)[source]

Initialize from an stk molecule.

Parameters:

molecule (Molecule) – The molecule to initialise from.

Return type:

Self

clone()[source]

Return a clone.

Return type:

Self

get_connected_components()[source]

Get connected components within full graph.

Returns:

List of connected components of graph.

Return type:

list[Graph]

get_graph()[source]

Return a networkx.Graph.

Return type:

Graph

get_nodes()[source]

Yield nodes of networkx.Graph (PositionAtom).

Return type:

Iterator[PositionedAtom]

with_deleted_bonds(atom_ids)[source]

Return a clone with edges between atom_ids deleted.

Parameters:

atom_ids (Iterable[tuple[int, int]])

Return type:

Self

with_deleted_elements(atomic_numbers)[source]

Return a clone with nodes with atomic numbers deleted.

Warning

This code is only present in the latest versions of stko that require Python 3.11!

Parameters:

atomic_numbers (tuple[int])

Return type:

Self