mooonpy.molspace.graph_theory.interface module

This module provides basic support for generating and working with graphs. The main purpose is to provide an interface between a Molspace instance and the graph data structure used in all the graph theory supported workflows.

mooonpy.molspace.graph_theory.interface.find_cumulative_neighs(graph: dict[int, list[int]], node: int, max_depth: int)[source]

Finds the cummulative neighbors of a graph starting at a given node and traversing to a maximum depth of max_depth. Depth is defined as follows:

  1. first neighbors

  2. second neighbors

  3. thrid neighbors

Note

For rings/cycles, where each neighbor has two classifications of depth due to ring symmetry, the lowest neighbor depth classification will be assigned and it will not be duplicated in the higher neighbor depth set.

Parameters:
  • graph (dict[int, list[int]]) – An undirected graph

  • node (int) – A node to find all cummulative neighbors from

  • max_depth (int) – The maximum traversal depth to search (typically 4)

Returns:

neighbors

Return type:

dict[int, set[int]]

mooonpy.molspace.graph_theory.interface.find_reachable_neighbors(graph: dict[int, list[int]], node: int, max_depth: int) set[int][source]

Flat-set version of find_cumulative_neighs(): every node within max_depth bonds of node, including node itself.

Parameters:
  • graph (dict[int, list[int]]) – An undirected graph.

  • node (int) – Starting node.

  • max_depth (int) – Maximum traversal depth (>= 0).

Returns:

All reachable node ids.

Return type:

set[int]

mooonpy.molspace.graph_theory.interface.find_reachable_neighbors_all(graph: dict[int, list[int]], max_depth: int) dict[int, set[int]][source]

Apply find_reachable_neighbors() once per node in graph.

Returns:

{node: set_of_reachable_ids}.

mooonpy.molspace.graph_theory.interface.generate_graph(mol)[source]

Generates an undirected graph from a Molspace instance.

Note

The generated graph will contain the most current nodes/edges each time this function is called. Thus if adding atoms/bonds to a Molspace instance, it is important to first update the Molspace instance before calling this function.

Parameters:

mol (Molspace) – Molspace instance to generate undirected graph from

Returns:

graph

Return type:

dict[int, list[int]]

mooonpy.molspace.graph_theory.interface.steps_from_initiator(graph: dict[int, list[int]], initiator: int, n_steps: int)[source]

BFS exactly n_steps outward from initiator. Returns the interior set (nodes within n_steps bonds, inclusive of initiator) and the boundary set (nodes at exactly depth n_steps, suitable as fix bond/react template edge atoms).

Parameters:
  • graph (dict[int, list[int]]) – An undirected graph.

  • initiator (int) – Starting node.

  • n_steps (int) – Number of bonds to walk (>= 0). With n_steps=0 the interior is just {initiator} and the boundary is empty.

Returns:

(interior, boundary).

Return type:

tuple[set[int], set[int]]