substructure
Substructure matching using the VF2++ algorithm.
Provides subgraph isomorphism detection for finding molecular substructures without requiring external dependencies like graph_tool.
VF2++ improves on VF2 with: - Optimal matching order based on node rarity and connectivity - Improved feasibility rules with better lookahead - More aggressive pruning
count_substructures(target, query, match_element=True)
Count occurrences of a query substructure in a target graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
MolecularGraph
|
The target molecular graph to search in. |
required |
query
|
MolecularGraph
|
The query pattern to search for. |
required |
match_element
|
bool
|
If True, only match atoms with same atomic number. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
Number of times the query appears in the target. |
Source code in chmpy/graph/substructure.py
find_functional_groups(target, group_name)
Find instances of a named functional group in a molecule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
MolecularGraph
|
The target molecular graph. |
required |
group_name
|
str
|
Name of the functional group (e.g., "hydroxyl", "carbonyl"). |
required |
Returns:
| Type | Description |
|---|---|
list[dict[int, int]]
|
List of mappings from pattern indices to target indices. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If group_name is not recognized. |
Source code in chmpy/graph/substructure.py
find_substructure(target, query, match_element=True)
Find all occurrences of a query substructure in a target graph.
Uses the VF2++ algorithm for subgraph isomorphism.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
MolecularGraph
|
The target molecular graph to search in. |
required |
query
|
MolecularGraph
|
The query pattern to search for. |
required |
match_element
|
bool
|
If True, only match atoms with same atomic number. |
True
|
Returns:
| Type | Description |
|---|---|
list[dict[int, int]]
|
List of mappings from query atom indices to target atom indices. |
list[dict[int, int]]
|
Each mapping is a dict {query_idx: target_idx}. |
Source code in chmpy/graph/substructure.py
functional_group_matches(target, pattern_atoms, pattern_bonds, match_element=True)
Find matches of a functional group pattern in a target molecule.
Convenience function for matching simple patterns specified as atom lists and bond lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
MolecularGraph
|
The target molecular graph. |
required |
pattern_atoms
|
list[int]
|
List of atomic numbers for pattern atoms. |
required |
pattern_bonds
|
list[tuple[int, int]]
|
List of (i, j) tuples for pattern bonds. |
required |
match_element
|
bool
|
If True, match atomic numbers. |
True
|
Returns:
| Type | Description |
|---|---|
list[dict[int, int]]
|
List of mappings from pattern indices to target indices. |
Source code in chmpy/graph/substructure.py
has_substructure(target, query, match_element=True)
Check if a target graph contains a query substructure.
More efficient than find_substructure when only existence is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
MolecularGraph
|
The target molecular graph to search in. |
required |
query
|
MolecularGraph
|
The query pattern to search for. |
required |
match_element
|
bool
|
If True, only match atoms with same atomic number. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the query is found in the target. |