| 1 |
"""Tests for build_conntable (shelxfile.misc.misc) and Atoms.conntable property.""" |
| 2 |
from unittest import TestCase |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
|
| 6 |
from shelxfile.atoms.pairs import Bond, SymBond |
| 7 |
from shelxfile.misc.misc import build_conntable |
| 8 |
from shelxfile.shelx.shelx import Shelxfile |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
_CO_DIST = 1.4 ) |
| 14 |
|
| 15 |
|
| 16 |
def _two_coords(dist: float = _CO_DIST) -> np.ndarray: |
| 17 |
return np.array([[0.0, 0.0, 0.0], [dist, 0.0, 0.0]]) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class TestBuildConntableBasic(TestCase): |
| 25 |
|
| 26 |
def test_two_bonded_atoms(self): |
| 27 |
"""C and O at 1.4 Å should be bonded.""" |
| 28 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [0, 0]) |
| 29 |
self.assertIn((0, 1), bonds) |
| 30 |
|
| 31 |
def test_two_far_atoms_not_bonded(self): |
| 32 |
"""C and O at 5 Å should NOT be bonded (exceeds 4 Å pre-filter).""" |
| 33 |
bonds = build_conntable(np.array([[0, 0, 0], [5, 0, 0]], dtype=float), |
| 34 |
['C', 'O'], [0, 0]) |
| 35 |
self.assertNotIn((0, 1), bonds) |
| 36 |
|
| 37 |
def test_same_atom_not_self_bonded(self): |
| 38 |
"""A single atom should produce no bonds.""" |
| 39 |
bonds = build_conntable(np.array([[0, 0, 0]], dtype=float), ['C'], [0]) |
| 40 |
self.assertEqual(bonds, ()) |
| 41 |
|
| 42 |
def test_empty_input(self): |
| 43 |
"""Empty atom list returns empty tuple.""" |
| 44 |
bonds = build_conntable(np.zeros((0, 3)), [], []) |
| 45 |
self.assertEqual(bonds, ()) |
| 46 |
|
| 47 |
def test_result_i_less_than_j(self): |
| 48 |
"""All returned pairs should have i < j.""" |
| 49 |
shx = Shelxfile() |
| 50 |
shx.read_file('tests/resources/p21c.res') |
| 51 |
for i, j in shx.atoms.conntable: |
| 52 |
self.assertLess(i, j) |
| 53 |
|
| 54 |
def test_hh_not_bonded(self): |
| 55 |
"""H–H pair at bonding distance should NOT produce a bond.""" |
| 56 |
coords = np.array([[0, 0, 0], [0.74, 0, 0]], dtype=float) |
| 57 |
bonds = build_conntable(coords, ['H', 'H'], [0, 0]) |
| 58 |
self.assertNotIn((0, 1), bonds) |
| 59 |
|
| 60 |
def test_ch_bonded(self): |
| 61 |
"""C–H pair at 1.09 Å should be bonded.""" |
| 62 |
coords = np.array([[0, 0, 0], [1.09, 0, 0]], dtype=float) |
| 63 |
bonds = build_conntable(coords, ['C', 'H'], [0, 0]) |
| 64 |
self.assertIn((0, 1), bonds) |
| 65 |
|
| 66 |
|
| 67 |
class TestBuildConntableDisorder(TestCase): |
| 68 |
|
| 69 |
def test_different_nonzero_parts_not_bonded(self): |
| 70 |
"""Atoms in different non-zero disorder parts should NOT be bonded.""" |
| 71 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [1, 2]) |
| 72 |
self.assertNotIn((0, 1), bonds) |
| 73 |
|
| 74 |
def test_same_nonzero_parts_bonded(self): |
| 75 |
"""Atoms in the same non-zero disorder part should be bonded.""" |
| 76 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [1, 1]) |
| 77 |
self.assertIn((0, 1), bonds) |
| 78 |
|
| 79 |
def test_nonzero_to_zero_part_bonded(self): |
| 80 |
"""Atom in a non-zero part is allowed to bond to an atom in part 0.""" |
| 81 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [1, 0]) |
| 82 |
self.assertIn((0, 1), bonds) |
| 83 |
|
| 84 |
|
| 85 |
class TestBuildConntableNegativePart(TestCase): |
| 86 |
|
| 87 |
def test_neg_part_both_base_bonded(self): |
| 88 |
"""Two base atoms with the same negative part should bond.""" |
| 89 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [-1, -1], |
| 90 |
symmgen=[False, False]) |
| 91 |
self.assertIn((0, 1), bonds) |
| 92 |
|
| 93 |
def test_neg_part_both_symmgen_bonded(self): |
| 94 |
"""Two symmgen atoms with the same negative part should bond.""" |
| 95 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [-1, -1], |
| 96 |
symmgen=[True, True]) |
| 97 |
self.assertIn((0, 1), bonds) |
| 98 |
|
| 99 |
def test_neg_part_cross_boundary_excluded(self): |
| 100 |
"""Base + symmgen with negative part should NOT be bonded.""" |
| 101 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [-1, -1], |
| 102 |
symmgen=[False, True]) |
| 103 |
self.assertNotIn((0, 1), bonds) |
| 104 |
|
| 105 |
def test_positive_parts_cross_boundary_allowed(self): |
| 106 |
"""Normal atoms that happen to cross base/symmgen boundary are still bonded.""" |
| 107 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [0, 0], |
| 108 |
symmgen=[False, True]) |
| 109 |
self.assertIn((0, 1), bonds) |
| 110 |
|
| 111 |
def test_no_symmgen_no_filter(self): |
| 112 |
"""When symmgen is None, the negative-part filter is not applied.""" |
| 113 |
bonds = build_conntable(_two_coords(), ['C', 'O'], [-1, -1], |
| 114 |
symmgen=None) |
| 115 |
self.assertIn((0, 1), bonds) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
class TestAtomConntableProperty(TestCase): |
| 123 |
|
| 124 |
def setUp(self): |
| 125 |
self.shx = Shelxfile() |
| 126 |
self.shx.read_file('tests/resources/p21c.res') |
| 127 |
|
| 128 |
def test_returns_tuple(self): |
| 129 |
"""conntable must return a tuple.""" |
| 130 |
self.assertIsInstance(self.shx.atoms.conntable, tuple) |
| 131 |
|
| 132 |
def test_bonds_are_pairs(self): |
| 133 |
"""Every entry in conntable must be a (int, int) pair.""" |
| 134 |
for bond in self.shx.atoms.conntable: |
| 135 |
self.assertEqual(len(bond), 2) |
| 136 |
self.assertIsInstance(bond[0], int) |
| 137 |
self.assertIsInstance(bond[1], int) |
| 138 |
|
| 139 |
def test_nonzero_bond_count(self): |
| 140 |
"""A real structure should have at least one bond.""" |
| 141 |
self.assertGreater(len(self.shx.atoms.conntable), 0) |
| 142 |
|
| 143 |
def test_bond_distances_sensible(self): |
| 144 |
"""All detected bonds between non-q-peak atoms should be between 0.5 and 4.0 Å.""" |
| 145 |
atoms = self.shx.atoms.all_atoms |
| 146 |
for i, j in self.shx.atoms.conntable: |
| 147 |
ai, aj = atoms[i], atoms[j] |
| 148 |
if ai.qpeak or aj.qpeak: |
| 149 |
continue |
| 150 |
d = np.linalg.norm([ai.xc - aj.xc, ai.yc - aj.yc, ai.zc - aj.zc]) |
| 151 |
self.assertGreater(d, 0.5, f"Bond {ai.name}-{aj.name} too short: {d:.3f} Å") |
| 152 |
self.assertLess(d, 4.0, f"Bond {ai.name}-{aj.name} too long: {d:.3f} Å") |
| 153 |
|
| 154 |
def test_o1_c1_bonded(self): |
| 155 |
"""O1 and C1 (indices 0, 1) in p21c.res should be bonded.""" |
| 156 |
atoms = self.shx.atoms.all_atoms |
| 157 |
idx_o1 = next(i for i, a in enumerate(atoms) if a.name == 'O1') |
| 158 |
idx_c1 = next(i for i, a in enumerate(atoms) if a.name == 'C1') |
| 159 |
pair = tuple(sorted((idx_o1, idx_c1))) |
| 160 |
self.assertIn(pair, self.shx.atoms.conntable) |
| 161 |
|
| 162 |
def test_no_hh_bonds(self): |
| 163 |
"""The connectivity table must not contain any H–H bonds.""" |
| 164 |
atoms = self.shx.atoms.all_atoms |
| 165 |
for i, j in self.shx.atoms.conntable: |
| 166 |
both_h = atoms[i].is_hydrogen and atoms[j].is_hydrogen |
| 167 |
self.assertFalse(both_h, |
| 168 |
f"Unexpected H–H bond: {atoms[i].name}–{atoms[j].name}") |
| 169 |
|
| 170 |
def test_symmetry_consistency_with_build_conntable(self): |
| 171 |
"""conntable matches a manual call to build_conntable with same inputs.""" |
| 172 |
atoms = self.shx.atoms.all_atoms |
| 173 |
coords = np.array([[at.xc, at.yc, at.zc] for at in atoms]) |
| 174 |
types = [at.element for at in atoms] |
| 175 |
parts = [at.part.n for at in atoms] |
| 176 |
radii = np.array([at.radius for at in atoms]) |
| 177 |
symmgen = [at.symmgen for at in atoms] |
| 178 |
expected = build_conntable(coords, types, parts, radii=radii, symmgen=symmgen) |
| 179 |
self.assertEqual(self.shx.atoms.conntable, expected) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
class TestAtomsBonds(TestCase): |
| 187 |
|
| 188 |
def setUp(self) -> None: |
| 189 |
self.shx = Shelxfile() |
| 190 |
self.shx.read_file('tests/resources/p21c.res') |
| 191 |
|
| 192 |
def test_bonds_returns_list(self): |
| 193 |
self.assertIsInstance(self.shx.atoms.bonds, list) |
| 194 |
|
| 195 |
def test_bonds_items_are_bond_objects(self): |
| 196 |
for b in self.shx.atoms.bonds: |
| 197 |
self.assertIsInstance(b, Bond) |
| 198 |
|
| 199 |
def test_bond_count_matches_conntable(self): |
| 200 |
self.assertEqual(len(self.shx.atoms.bonds), |
| 201 |
len(self.shx.atoms.conntable)) |
| 202 |
|
| 203 |
def test_bond_distance_is_positive(self): |
| 204 |
for b in self.shx.atoms.bonds: |
| 205 |
self.assertGreater(b.distance, 0.0) |
| 206 |
|
| 207 |
def test_bond_distance_is_realistic(self): |
| 208 |
"""All bonds should be between 0.01 Å (build_conntable threshold) and 4.0 Å.""" |
| 209 |
for b in self.shx.atoms.bonds: |
| 210 |
self.assertGreater(b.distance, 0.01, |
| 211 |
f"Bond {b!r} is unrealistically short") |
| 212 |
self.assertLess(b.distance, 4.0, |
| 213 |
f"Bond {b!r} is unrealistically long") |
| 214 |
|
| 215 |
def test_bond_atoms_are_atom_objects(self): |
| 216 |
from shelxfile.atoms.atom import Atom |
| 217 |
for b in self.shx.atoms.bonds: |
| 218 |
self.assertIsInstance(b.atom1, Atom) |
| 219 |
self.assertIsInstance(b.atom2, Atom) |
| 220 |
|
| 221 |
def test_bond_str_contains_atom_names(self): |
| 222 |
b = self.shx.atoms.bonds[0] |
| 223 |
s = str(b) |
| 224 |
self.assertIn(b.atom1.fullname_short, s) |
| 225 |
self.assertIn(b.atom2.fullname_short, s) |
| 226 |
|
| 227 |
def test_bond_str_contains_angstrom(self): |
| 228 |
b = self.shx.atoms.bonds[0] |
| 229 |
self.assertIn('Å', str(b)) |
| 230 |
|
| 231 |
def test_bond_repr(self): |
| 232 |
b = self.shx.atoms.bonds[0] |
| 233 |
r = repr(b) |
| 234 |
self.assertTrue(r.startswith('Bond(')) |
| 235 |
self.assertIn('Å', r) |
| 236 |
|
| 237 |
def test_bond_unpack(self): |
| 238 |
"""Bond supports tuple-style unpacking into (atom1, atom2, distance).""" |
| 239 |
b = self.shx.atoms.bonds[0] |
| 240 |
a1, a2, dist = b |
| 241 |
self.assertIs(a1, b.atom1) |
| 242 |
self.assertIs(a2, b.atom2) |
| 243 |
self.assertEqual(dist, b.distance) |
| 244 |
|
| 245 |
def test_bonds_sorted_by_name(self): |
| 246 |
"""Bond list is sorted by atom1 name then atom2 name.""" |
| 247 |
bonds = self.shx.atoms.bonds |
| 248 |
names = [(b.atom1.fullname_short, b.atom2.fullname_short) for b in bonds] |
| 249 |
self.assertEqual(names, sorted(names)) |
| 250 |
|
| 251 |
def test_no_hh_bonds(self): |
| 252 |
for b in self.shx.atoms.bonds: |
| 253 |
self.assertFalse(b.atom1.is_hydrogen and b.atom2.is_hydrogen, |
| 254 |
f"Unexpected H–H bond: {b!r}") |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
rs |
| 259 |
|
| 260 |
|
| 261 |
class TestFullBondList(TestCase): |
| 262 |
"""Uses I-43d.res which has genuine symmetry bonds.""" |
| 263 |
|
| 264 |
def setUp(self) -> None: |
| 265 |
ds |
| 266 |
self.shx_symm = Shelxfile() |
| 267 |
self.shx_symm.read_file('tests/resources/I-43d.res') |
| 268 |
|
| 269 |
self.shx_plain = Shelxfile() |
| 270 |
self.shx_plain.read_file('tests/resources/p21c.res') |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
def test_returns_list(self): |
| 275 |
self.assertIsInstance(self.shx_symm.atoms.full_bond_list(), list) |
| 276 |
|
| 277 |
def test_items_are_symbond(self): |
| 278 |
for b in self.shx_symm.atoms.full_bond_list(): |
| 279 |
self.assertIsInstance(b, SymBond) |
| 280 |
|
| 281 |
def test_symbond_is_subclass_of_bond(self): |
| 282 |
for b in self.shx_symm.atoms.full_bond_list(): |
| 283 |
self.assertIsInstance(b, Bond) |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
def test_plain_bonds_match_conntable(self): |
| 288 |
"""Plain (no-symm) bonds must be a subset of the conntable.""" |
| 289 |
plain = [b for b in self.shx_plain.atoms.full_bond_list() |
| 290 |
if not b.is_symmetry_bond] |
| 291 |
|
| 292 |
|
| 293 |
self.assertGreater(len(plain), 0) |
| 294 |
self.assertLessEqual(len(plain), len(self.shx_plain.atoms.conntable)) |
| 295 |
|
| 296 |
def test_has_symmetry_bonds(self): |
| 297 |
symm = [b for b in self.shx_symm.atoms.full_bond_list() |
| 298 |
if b.is_symmetry_bond] |
| 299 |
self.assertGreater(len(symm), 0) |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
def test_symm_label_is_string(self): |
| 304 |
for b in self.shx_symm.atoms.full_bond_list(): |
| 305 |
self.assertIsInstance(b.symm_label, str) |
| 306 |
|
| 307 |
def test_plain_bond_has_empty_label(self): |
| 308 |
for b in self.shx_plain.atoms.full_bond_list(): |
| 309 |
self.assertEqual('', b.symm_label) |
| 310 |
|
| 311 |
def test_symm_bond_label_contains_axis(self): |
| 312 |
"""Symmetry labels must contain at least one axis letter.""" |
| 313 |
symm = [b for b in self.shx_symm.atoms.full_bond_list() |
| 314 |
if b.is_symmetry_bond] |
| 315 |
for b in symm: |
| 316 |
self.assertTrue( |
| 317 |
any(ax in b.symm_label for ax in ('x', 'y', 'z')), |
| 318 |
f"Label {b.symm_label!r} contains no axis" |
| 319 |
) |
| 320 |
|
| 321 |
def test_symm_str_shows_label_in_brackets(self): |
| 322 |
symm = [b for b in self.shx_symm.atoms.full_bond_list() |
| 323 |
if b.is_symmetry_bond] |
| 324 |
for b in symm[:5]: |
| 325 |
s = str(b) |
| 326 |
self.assertIn('[', s) |
| 327 |
self.assertIn(b.symm_label, s) |
| 328 |
|
| 329 |
def test_plain_str_has_no_brackets(self): |
| 330 |
for b in self.shx_plain.atoms.full_bond_list(): |
| 331 |
self.assertNotIn('[', str(b)) |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
def test_distances_positive(self): |
| 336 |
for b in self.shx_symm.atoms.full_bond_list(): |
| 337 |
self.assertGreater(b.distance, 0.0) |
| 338 |
|
| 339 |
def test_angstrom_in_str(self): |
| 340 |
b = self.shx_symm.atoms.full_bond_list()[0] |
| 341 |
self.assertIn('Å', str(b)) |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
def test_unpack_four_fields(self): |
| 346 |
b = self.shx_symm.atoms.full_bond_list()[0] |
| 347 |
a1, a2, dist, label = b |
| 348 |
self.assertIs(a1, b.atom1) |
| 349 |
self.assertIs(a2, b.atom2) |
| 350 |
self.assertEqual(dist, b.distance) |
| 351 |
self.assertEqual(label, b.symm_label) |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
def test_sorted_by_name(self): |
| 356 |
bl = self.shx_symm.atoms.full_bond_list() |
| 357 |
names = [(b.atom1.fullname_short, b.atom2.fullname_short) for b in bl] |
| 358 |
self.assertEqual(names, sorted(names)) |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
def test_repr_plain(self): |
| 363 |
b = [b for b in self.shx_plain.atoms.full_bond_list()][0] |
| 364 |
self.assertTrue(repr(b).startswith('SymBond(')) |
| 365 |
|
| 366 |
def test_repr_symm_contains_bracket(self): |
| 367 |
b = [b for b in self.shx_symm.atoms.full_bond_list() |
| 368 |
if b.is_symmetry_bond][0] |
| 369 |
self.assertIn('[', repr(b)) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
def test_no_qpeaks_by_default(self): |
| 374 |
for b in self.shx_symm.atoms.full_bond_list(): |
| 375 |
self.assertFalse(b.atom1.qpeak or b.atom2.qpeak, |
| 376 |
f"Q-peak appeared in default bond list: {b!r}") |
| 377 |
|
| 378 |
|