canonicalization
Graph canonicalization using the Morgan algorithm.
Provides unique atom ordering for canonical SMILES generation and Morgan/ECFP-style fingerprints.
atom_invariant(graph, atom_idx)
Compute the invariant for a single atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
atom_idx
|
int
|
Index of the atom. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Integer invariant encoding atom properties. |
Source code in chmpy/graph/canonicalization.py
canonical_atom_map(graph)
Get mapping from original atom indices to canonical indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, int]
|
Dictionary mapping original index to canonical index. |
Source code in chmpy/graph/canonicalization.py
canonical_ordering(graph)
Compute a canonical ordering of atoms in the molecular graph.
Uses an extended Morgan algorithm with symmetry breaking to produce a unique, deterministic ordering regardless of input atom order.
The algorithm: 1. Compute initial atom invariants (atomic number, degree, etc.) 2. Iteratively refine using neighbor information 3. Break ties using lexicographic ordering
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_atoms,) containing the canonical index for each atom. |
ndarray
|
To reorder atoms: new_order[canonical_ordering[i]] = i |
Source code in chmpy/graph/canonicalization.py
equivalent_atoms(graph)
Find sets of equivalent (symmetric) atoms.
Two atoms are equivalent if they have the same Morgan invariant after convergence, meaning they are in symmetric positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
Returns:
| Type | Description |
|---|---|
list[set[int]]
|
List of sets, each containing indices of equivalent atoms. |
Source code in chmpy/graph/canonicalization.py
morgan_fingerprint(graph, radius=2, n_bits=2048)
Compute an ECFP-style Morgan fingerprint.
Generates circular fingerprints by hashing atom environments at different radii.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
radius
|
int
|
Maximum radius for environment expansion. |
2
|
n_bits
|
int
|
Size of the fingerprint bit vector. |
2048
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean array of shape (n_bits,) representing the fingerprint. |
Source code in chmpy/graph/canonicalization.py
morgan_fingerprint_bits(graph, radius=2)
Get the set of Morgan fingerprint bit indices (on bits).
Useful for Tanimoto similarity calculations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
radius
|
int
|
Maximum radius for environment expansion. |
2
|
Returns:
| Type | Description |
|---|---|
set[int]
|
Set of bit indices that are on. |
Source code in chmpy/graph/canonicalization.py
reorder_graph(graph)
Create a new graph with atoms in canonical order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
MolecularGraph
|
A MolecularGraph instance. |
required |
Returns:
| Type | Description |
|---|---|
MolecularGraph
|
New MolecularGraph with atoms reordered canonically. |
Source code in chmpy/graph/canonicalization.py
tanimoto_similarity(fp1, fp2)
Compute Tanimoto similarity between two fingerprints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fp1
|
ndarray
|
First fingerprint (boolean array). |
required |
fp2
|
ndarray
|
Second fingerprint (boolean array). |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tanimoto similarity in [0, 1]. |