adjacency
Core graph representation for molecular structures.
Provides a MolecularGraph class that wraps a scipy sparse adjacency matrix with vertex and edge attributes for atoms and bonds.
MolecularGraph
Graph representation of a molecular structure.
Stores connectivity as a sparse adjacency matrix with atomic numbers and positions as vertex attributes, and bond distances/orders as edge attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
n_atoms |
int
|
Number of atoms (vertices) in the graph. |
atomic_numbers |
Array of atomic numbers for each atom. |
|
positions |
(N, 3) array of Cartesian coordinates (Angstroms). |
|
formal_charges |
Array of formal charges for each atom. |
|
adjacency |
Sparse CSR adjacency matrix (symmetric, undirected). |
|
bond_orders |
Sparse matrix of bond orders (1=single, 2=double, etc). |
|
bond_distances |
Sparse matrix of bond distances (Angstroms). |
Source code in chmpy/graph/adjacency.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
degree
property
Degree (number of neighbors) for each atom.
n_atoms
property
Number of atoms in the graph.
n_bonds
property
Number of bonds in the graph.
__init__(atomic_numbers, positions, adjacency, bond_orders=None, bond_distances=None, formal_charges=None)
Initialize a MolecularGraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atomic_numbers
|
ndarray
|
(N,) array of atomic numbers. |
required |
positions
|
ndarray
|
(N, 3) array of Cartesian coordinates. |
required |
adjacency
|
csr_matrix
|
Sparse adjacency matrix (N, N). |
required |
bond_orders
|
csr_matrix | None
|
Optional sparse matrix of bond orders. |
None
|
bond_distances
|
csr_matrix | None
|
Optional sparse matrix of bond distances. |
None
|
formal_charges
|
ndarray | None
|
Optional (N,) array of formal charges. |
None
|
Source code in chmpy/graph/adjacency.py
all_neighbors()
Get neighbor lists for all atoms.
Returns:
| Type | Description |
|---|---|
list[ndarray]
|
List of arrays, where each array contains neighbor indices. |
Source code in chmpy/graph/adjacency.py
bond_distance(atom_i, atom_j)
Get the bond distance between two atoms.
Returns:
| Type | Description |
|---|---|
float | None
|
Bond distance in Angstroms, or None if no bond exists. |
Source code in chmpy/graph/adjacency.py
bond_order(atom_i, atom_j)
Get the bond order between two atoms.
Returns:
| Type | Description |
|---|---|
int
|
Bond order (1, 2, 3, etc), or 0 if no bond exists. |
Source code in chmpy/graph/adjacency.py
connected_components()
Find connected components (fragments) in the graph.
Returns:
| Type | Description |
|---|---|
int
|
Tuple of (n_components, labels) where labels assigns each |
ndarray
|
atom to a component (0 to n_components-1). |
Source code in chmpy/graph/adjacency.py
edges()
Get all edges (bonds) as a list of (i, j) tuples.
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
List of unique (i, j) pairs where i < j. |
Source code in chmpy/graph/adjacency.py
from_edge_list(atomic_numbers, positions, edges, bond_orders=None, formal_charges=None)
classmethod
Create a MolecularGraph from an edge list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atomic_numbers
|
ndarray
|
(N,) array of atomic numbers. |
required |
positions
|
ndarray
|
(N, 3) array of Cartesian coordinates. |
required |
edges
|
list[tuple[int, int]]
|
List of (i, j) pairs representing bonds. |
required |
bond_orders
|
list[int] | None
|
Optional list of bond orders for each edge. |
None
|
formal_charges
|
ndarray | None
|
Optional (N,) array of formal charges. |
None
|
Returns:
| Type | Description |
|---|---|
MolecularGraph
|
New MolecularGraph. |
Source code in chmpy/graph/adjacency.py
from_molecule(molecule)
classmethod
Create a MolecularGraph from a Molecule object.
Uses the molecule's existing bonds if available, otherwise calls guess_bonds() to determine connectivity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
molecule
|
Molecule
|
A Molecule object. |
required |
Returns:
| Type | Description |
|---|---|
MolecularGraph
|
MolecularGraph representation of the molecule. |
Source code in chmpy/graph/adjacency.py
has_bond(atom_i, atom_j)
neighbors(atom_idx)
Get indices of atoms bonded to the given atom.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom_idx
|
int
|
Index of the atom. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of neighbor atom indices. |
subgraph(atom_indices)
Extract a subgraph containing only the specified atoms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom_indices
|
ndarray
|
Indices of atoms to include in the subgraph. |
required |
Returns:
| Type | Description |
|---|---|
MolecularGraph
|
New MolecularGraph containing only the specified atoms. |