Geometry utilities¶
Fragment alignment, translation, and bond-perception helpers:
cage_isomer_builder.utils.geometry.
Geometry helpers: rotations, bonded pairs, fragment alignment and the permutation an operation induces on a set of sites.
fibonacci_sphere ¶
n points spread evenly over the unit sphere (Fibonacci spiral): a
deterministic cover of all directions without the gaps of random draws.
Examples:
>>> import numpy as np
>>> from cage_isomer_builder.utils.geometry import fibonacci_sphere
>>> points = fibonacci_sphere(500)
>>> points.shape, bool(np.allclose(np.linalg.norm(points, axis=1), 1))
((500, 3), True)
>>> bool(np.abs(points.mean(axis=0)).max() < 0.01) # no direction favoured
True
Source code in cage_isomer_builder/utils/geometry.py
random_rotation_matrix ¶
A uniformly random 3x3 rotation matrix.
Two independent random vectors are orthonormalised (Gram-Schmidt) and completed to a right-handed basis, which samples SO(3) uniformly.
Returns:
| Type | Description |
|---|---|
(ndarray, shape(3, 3))
|
A proper rotation matrix (determinant +1). |
Examples:
>>> import numpy as np
>>> from cage_isomer_builder.utils.geometry import random_rotation_matrix
>>> r = random_rotation_matrix()
>>> bool(np.allclose(r @ r.T, np.eye(3))), round(float(np.linalg.det(r)), 6)
(True, 1.0)
Source code in cage_isomer_builder/utils/geometry.py
find_third_point ¶
Extrapolate a third point P3 beyond P1, away from P2:
R-P2-P1----P3. Used to construct a second reference direction
for aligning a fragment onto a single bonding site.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
(ndarray, shape(3))
|
|
required |
p2
|
(ndarray, shape(3))
|
|
required |
bond_length
|
float
|
Distance of P3 from P1. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(3))
|
Coordinates of P3. |
Examples:
>>> import numpy as np
>>> from cage_isomer_builder.utils.geometry import find_third_point
>>> find_third_point(np.array([1.0, 0, 0]), np.array([0.0, 0, 0]), 1.5).tolist()
[2.5, 0.0, 0.0]
Source code in cage_isomer_builder/utils/geometry.py
kabsch ¶
Kabsch algorithm: the optimal rotation matrix aligning one point set onto another.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates1
|
ndarray
|
Coordinates of the first point set. |
required |
coordinates2
|
ndarray
|
Coordinates of the second point set. |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, shape(3, 3))
|
Rotation matrix that best aligns coordinates1 onto coordinates2. |
Examples:
>>> import numpy as np
>>> from cage_isomer_builder.utils.geometry import kabsch
>>> a = np.array([[1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]])
>>> rz = np.array([[0.0, -1, 0], [1, 0, 0], [0, 0, 1]]) # 90 degrees about z
>>> bool(np.allclose(a @ kabsch(a, a @ rz), a @ rz))
True
Source code in cage_isomer_builder/utils/geometry.py
find_connected_atoms ¶
Indices of every atom bonded to atom_index (e.g. the real atom
a dummy "X" marker is attached to), via a per-atom covalent-radius
cutoff.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
|
required |
atom_index
|
int
|
Index of the atom whose bonded neighbours are wanted. |
required |
Returns:
| Type | Description |
|---|---|
np.ndarray of int
|
Indices of atoms bonded to |
Examples:
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.geometry import find_connected_atoms
>>> sorted(find_connected_atoms(molecule("H2O"), 0).tolist())
[1, 2]
Source code in cage_isomer_builder/utils/geometry.py
bonded_pairs ¶
All bonded atom pairs (i, j), i < j, from interatomic distances.
Uses ase.neighborlist.natural_cutoffs (mult times each covalent
radius) with skin=0. ASE's default skin of 0.3 is added to the bonding
cutoff, which would bond the meta and para carbons of an aromatic ring
(about 2.4 and 2.8 Angstrom apart) and corrupt the molecular graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atoms
|
Atoms
|
|
required |
mult
|
float
|
Covalent-radius multiplier passed to |
1.2
|
exclude
|
int
|
Atom index to skip (e.g. a marker atom whose bonds the caller tracks). |
None
|
Returns:
| Type | Description |
|---|---|
list of (int, int)
|
Each bonded pair once, smaller index first. |
Examples:
>>> from ase.build import molecule
>>> from cage_isomer_builder.utils.geometry import bonded_pairs
>>> bonded_pairs(molecule("H2O"))
[(0, 1), (0, 2)]
>>> len(bonded_pairs(molecule("C6H6"))) # 6 C-C + 6 C-H, no meta/para pairs
12
Source code in cage_isomer_builder/utils/geometry.py
align_fragment_orientation ¶
align_fragment_orientation(host_system, sub_fragment, bond_length, host_marker='X', fragment_marker='X', host_index=None, host_neigh_index=None)
Rotate sub_fragment in place so that its marker bond lies along the
bonding direction of a site in host_system.
A second reference point (find_third_point) is placed on the host's bond
axis so that two point pairs can be aligned. Apply
resolve_fragment_twist afterwards to fix the rotation about that axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_system
|
Atoms
|
Host structure containing the bonding site. |
required |
sub_fragment
|
Atoms
|
Fragment with one marker atom. Rotated in place. |
required |
bond_length
|
float
|
Distance of the reference point from the site atom. |
required |
host_marker
|
str
|
Symbol of the site atom in the host ( |
'X'
|
fragment_marker
|
str
|
Symbol of the marker atom in |
'X'
|
host_index
|
int
|
Index of the host site to use, instead of the first |
None
|
host_neigh_index
|
int
|
Index of the site's bonded neighbour, instead of searching for it with
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
sub_fragment |
Atoms
|
The aligned fragment (the same object). |
sub_x |
int
|
Index of the marker atom in |
host_x |
int
|
Index of the site atom in |
sub_neigh |
int
|
Index of the fragment atom bonded to its marker. |
Examples:
>>> from ase.build import molecule
>>> from ase.io import read
>>> from cage_isomer_builder import example_data
>>> host = molecule("C6H6")
>>> host[6].symbol = "X" # a site on C 0
>>> nh2 = example_data.read("NH2") # N, H, H and its X attachment marker
>>> from cage_isomer_builder.utils.geometry import align_fragment_orientation
>>> fragment, sub_x, host_x, sub_neigh = align_fragment_orientation(
... host, nh2.copy(), 1.5, host_index=6, host_neigh_index=0)
>>> host_x, nh2[sub_neigh].symbol
(6, 'N')
Source code in cage_isomer_builder/utils/geometry.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
resolve_fragment_twist ¶
resolve_fragment_twist(host_system, sub_fragment, host_x, sub_neigh, tolerance=0.75, n_angles=24, host_neigh_index=None)
Rotate a translated fragment about its bond axis to avoid clashing with the host.
align_fragment_orientation fixes only the direction of the bond, so the
rotation about it is arbitrary; for a fragment of more than one atom (e.g.
NH2) that can point an atom into the host. n_angles rotations about the
bond axis are tried and the one with the largest closest fragment-host
approach (scaled by covalent radii) is kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_system
|
Atoms
|
Host to avoid. Its site atom |
required |
sub_fragment
|
Atoms
|
Fragment already translated so that |
required |
host_x
|
int
|
Index of the site atom in |
required |
sub_neigh
|
int
|
Index of the fragment atom bonded to its marker; the pivot of the rotation. |
required |
tolerance
|
float
|
Fraction of the summed covalent radii used as the clash threshold. |
0.75
|
n_angles
|
int
|
Number of evenly spaced trial angles in [0, 360). |
24
|
host_neigh_index
|
int
|
Index of |
None
|
Returns:
| Type | Description |
|---|---|
Atoms
|
|
Examples:
>>> from ase.build import molecule
>>> from ase.io import read
>>> from cage_isomer_builder import example_data
>>> host = molecule("C6H6")
>>> host[6].symbol = "X" # a site on C 0
>>> nh2 = example_data.read("NH2") # N, H, H and its X attachment marker
>>> from cage_isomer_builder.utils.geometry import (
... align_fragment_orientation, resolve_fragment_twist, translate_fragment)
>>> fragment, sub_x, host_x, sub_neigh = align_fragment_orientation(
... host, nh2.copy(), 1.5, host_index=6, host_neigh_index=0)
>>> fragment = translate_fragment(host, fragment, sub_neigh, host_x)
>>> fragment = resolve_fragment_twist(host, fragment, host_x, sub_neigh, host_neigh_index=0)
>>> import numpy as np
>>> hydrogens = [i for i in range(len(fragment)) if fragment[i].symbol == "H"]
>>> others = [i for i in range(len(host)) if i != host_x]
>>> gaps = np.linalg.norm(fragment.positions[hydrogens][:, None]
... - host.positions[others][None], axis=2)
>>> bool(gaps.min() > 1.5) # the NH2 hydrogens clear the ring
True
Source code in cage_isomer_builder/utils/geometry.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
translate_fragment ¶
Translate an aligned fragment so its bonded atom coincides with the host's site atom, forming the new bond.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host_system
|
Atoms
|
Host structure containing the bonding site. |
required |
sub_fragment
|
Atoms
|
Aligned fragment to translate. |
required |
sub_neigh
|
int
|
Index of the fragment atom bonded to its own dummy atom. |
required |
host_x
|
int
|
Index of the site atom in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
sub_fragment |
Atoms
|
The translated fragment (same object, moved in place). |
Examples:
>>> from ase.build import molecule
>>> from ase.io import read
>>> from cage_isomer_builder import example_data
>>> host = molecule("C6H6")
>>> host[6].symbol = "X" # a site on C 0
>>> nh2 = example_data.read("NH2") # N, H, H and its X attachment marker
>>> import numpy as np
>>> from cage_isomer_builder.utils.geometry import align_fragment_orientation, translate_fragment
>>> fragment, sub_x, host_x, sub_neigh = align_fragment_orientation(
... host, nh2.copy(), 1.5, host_index=6, host_neigh_index=0)
>>> fragment = translate_fragment(host, fragment, sub_neigh, host_x)
>>> bool(np.allclose(fragment.positions[sub_neigh], host.positions[host_x])) # N on the site
True
Source code in cage_isomer_builder/utils/geometry.py
reflect_positions ¶
Reflect all positions of ase_atom through the plane through the origin
with normal reflection_normal (Householder reflection
H = I - 2 n n^T, i.e. x -> x - 2 (x . n) n for the unit normal n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ase_atom
|
Atoms
|
Structure to reflect. Its positions are changed in place. |
required |
reflection_normal
|
(ndarray, shape(3))
|
Normal of the plane; it is normalised internally. |
required |
Returns:
| Type | Description |
|---|---|
Atoms
|
The reflected structure (the same object). |
Examples:
>>> from ase import Atoms
>>> from cage_isomer_builder.utils.geometry import reflect_positions
>>> reflect_positions(Atoms("H", [[1.0, 2.0, 3.0]]), [0, 0, 1]).positions.tolist()
[[1.0, 2.0, -3.0]]
Source code in cage_isomer_builder/utils/geometry.py
project_permutation ¶
The permutation that maps an anchor set onto its transformed copy.
The match is a one-to-one assignment minimising the total distance
(Hungarian algorithm, scipy.optimize.linear_sum_assignment). Matching
each point to its nearest neighbour independently can give two points the
same partner when anchors are close together (e.g. the eight positions of a
biphenyl linker); the assignment is always a bijection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_fg_anchors
|
Atoms
|
The anchors before the symmetry operation. |
required |
transformed_fg_anchors
|
Atoms
|
The anchors after it; same number of atoms. |
required |
max_distance
|
float
|
Largest allowed distance (Angstrom) between a transformed anchor and its match. |
1.0
|
Returns:
| Type | Description |
|---|---|
list of int
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the best assignment leaves a pair further apart than
|
Examples:
>>> from ase import Atoms
>>> from cage_isomer_builder.utils.geometry import project_permutation
>>> original = Atoms("X3", [[0, 0, 0], [3, 0, 0], [6, 0, 0]])
>>> moved = Atoms("X3", [[6, 0, 0], [0, 0, 0], [3, 0, 0]])
>>> project_permutation(original, moved)
[2, 0, 1]