| 1 |
import contextlib |
| 2 |
import io |
| 3 |
from pathlib import Path |
| 4 |
from unittest import TestCase |
| 5 |
|
| 6 |
from shelxfile.refine.refine import ShelxlRefine |
| 7 |
from shelxfile.shelx.cards import RESI, ANIS, ABIN, SADI, DFIX, AFIX, PART |
| 8 |
from shelxfile.shelx.shelx import Shelxfile |
| 9 |
|
| 10 |
|
| 11 |
class TestCELL(TestCase): |
| 12 |
def setUp(self) -> None: |
| 13 |
self.shx = Shelxfile() |
| 14 |
self.shx.read_file('tests/resources/p21c.res') |
| 15 |
|
| 16 |
def test_volume(self): |
| 17 |
self.assertEqual(4493.0474, round(self.shx.cell.volume, 4)) |
| 18 |
|
| 19 |
|
| 20 |
class TestRESI(TestCase): |
| 21 |
def setUp(self) -> None: |
| 22 |
self.shx = Shelxfile() |
| 23 |
self.shx.read_file('tests/resources/p21c.res') |
| 24 |
|
| 25 |
def test_get_resi_1_tol(self): |
| 26 |
r = RESI(None, 'RESI 1 TOL'.split()) |
| 27 |
self.assertEqual(('TOL', 1, None, None), (r.residue_class, r.residue_number, r.chain_id, r.alias)) |
| 28 |
|
| 29 |
def test_tol_1(self): |
| 30 |
r = RESI(None, 'RESI TOL 1'.split()) |
| 31 |
self.assertEqual(('TOL', 1, None, None), (r.residue_class, r.residue_number, r.chain_id, r.alias)) |
| 32 |
|
| 33 |
def test_resi_chainid_tol(self): |
| 34 |
r = RESI(None, 'RESI A:100 TOL'.split()) |
| 35 |
self.assertEqual(('TOL', 100, 'A', None), (r.residue_class, r.residue_number, r.chain_id, r.alias)) |
| 36 |
|
| 37 |
def test_resi__negative_chain_tol(self): |
| 38 |
r = RESI(None, 'RESI -10 TOL'.split()) |
| 39 |
self.assertEqual(('TOL', -10, None, None), (r.residue_class, r.residue_number, r.chain_id, r.alias)) |
| 40 |
|
| 41 |
def test_resi_b_chain_tol(self): |
| 42 |
r = RESI(None, 'RESI b:-10 TOL'.split()) |
| 43 |
self.assertEqual(('TOL', -10, 'b', None), (r.residue_class, r.residue_number, r.chain_id, r.alias)) |
| 44 |
|
| 45 |
|
| 46 |
class TestFMAP(TestCase): |
| 47 |
|
| 48 |
def test_fmap_number(self): |
| 49 |
shx = Shelxfile() |
| 50 |
shx.read_file('tests/resources/p21c.res') |
| 51 |
self.assertEqual(2.0, shx.fmap.code) |
| 52 |
|
| 53 |
|
| 54 |
class TestACTA(TestCase): |
| 55 |
def setUp(self) -> None: |
| 56 |
self.shx = Shelxfile() |
| 57 |
self.shx.read_file('tests/resources/p21c.res') |
| 58 |
self.ref = ShelxlRefine(self.shx, Path('./tests/resources/p21c.res')) |
| 59 |
|
| 60 |
def test_acta(self): |
| 61 |
self.assertEqual('ACTA 45', self.shx._reslist[12].__repr__()) |
| 62 |
self.assertEqual('ACTA 45', self.shx.acta.__repr__()) |
| 63 |
|
| 64 |
def test_remove_acta(self): |
| 65 |
self.ref.remove_acta_card(self.shx.acta) |
| 66 |
self.assertEqual('SIZE 0.12 0.23 0.33', self.shx._reslist[12].__repr__()) |
| 67 |
|
| 68 |
def test_restore_acta(self): |
| 69 |
self.ref.remove_acta_card(self.shx.acta) |
| 70 |
self.ref.restore_acta_card() |
| 71 |
self.assertEqual(8, self.shx.index_of(self.shx.acta)) |
| 72 |
self.assertEqual( |
| 73 |
['UNIT 1 2 3 4 5 6', 'ACTA 45', 'LIST 4 ! automatically inserted. Change 6 to 4 for CHECKCIF!!'], |
| 74 |
[str(x) for x in self.shx._reslist[7:10]]) |
| 75 |
|
| 76 |
|
| 77 |
class TestPART(TestCase): |
| 78 |
def setUp(self) -> None: |
| 79 |
self.shx = None |
| 80 |
|
| 81 |
def test_part_n(self): |
| 82 |
"""Test the result from PART.as_symbol""" |
| 83 |
self.assertEqual(PART(self.shx, ['PART', '1']).n, 1) |
| 84 |
self.assertEqual(PART(self.shx, ['PART', '-1']).n, -1) |
| 85 |
|
| 86 |
def test_part_resiclass(self): |
| 87 |
self.assertEqual(21.0, PART(self.shx, ['PART', '9', '21']).sof) |
| 88 |
self.assertEqual(9, PART(self.shx, ['PART', '9', '21']).n) |
| 89 |
self.assertEqual(-21.0, PART(self.shx, ['PART', '9', '-21']).sof) |
| 90 |
|
| 91 |
def test_part_sof(self): |
| 92 |
self.assertEqual(11.0, PART(self.shx, ['PART', '-2']).sof) |
| 93 |
|
| 94 |
|
| 95 |
class TestLSCycles(TestCase): |
| 96 |
def setUp(self) -> None: |
| 97 |
self.shx = Shelxfile(debug=True) |
| 98 |
self.shx.read_file('tests/resources/p21c.res') |
| 99 |
self.ref = ShelxlRefine(self.shx, Path('./resources/p21c.res')) |
| 100 |
|
| 101 |
def test_set_refine_cycles(self): |
| 102 |
self.shx.cycles.set_refine_cycles(44) |
| 103 |
self.assertEqual('L.S. 44', self.shx._reslist[self.shx.cycles.index].__repr__()) |
| 104 |
|
| 105 |
|
| 106 |
class TestSFACTable(TestCase): |
| 107 |
def setUp(self) -> None: |
| 108 |
self.shx = Shelxfile(debug=True) |
| 109 |
self.shx.read_file('tests/resources/p21c.res') |
| 110 |
|
| 111 |
def test_sfac_table(self): |
| 112 |
self.assertEqual('SFAC C H O F Al Ga', str(self.shx.sfac_table)) |
| 113 |
|
| 114 |
def test_unit_table(self): |
| 115 |
self.assertEqual('UNIT 1 2 3 4 5 6', str(self.shx.unit)) |
| 116 |
|
| 117 |
def test_sfac_add_element(self): |
| 118 |
self.shx.sfac_table.add_element('Au') |
| 119 |
self.assertEqual('SFAC C H O F Al Ga Au', str(self.shx.sfac_table)) |
| 120 |
|
| 121 |
def test_unit_add_element(self): |
| 122 |
self.shx.sfac_table.add_element('Au') |
| 123 |
self.assertEqual('UNIT 1 2 3 4 5 6 1', self.shx.unit.__repr__()) |
| 124 |
|
| 125 |
def test_unit_add_element_string_repr(self): |
| 126 |
self.shx.sfac_table.add_element('Au') |
| 127 |
self.assertEqual('UNIT 1 2 3 4 5 6 1', str(self.shx.unit)) |
| 128 |
|
| 129 |
|
| 130 |
class TestWGHT(TestCase): |
| 131 |
def setUp(self) -> None: |
| 132 |
self.shx = Shelxfile() |
| 133 |
self.shx.read_file('tests/resources/p21c.res') |
| 134 |
|
| 135 |
def test_wght(self): |
| 136 |
self.assertEqual('WGHT 0.049 0.0', self.shx.wght.__repr__()) |
| 137 |
|
| 138 |
def test_wght_differene(self): |
| 139 |
self.assertEqual([0.0, 0.0], self.shx.wght.difference()) |
| 140 |
|
| 141 |
def test_wght_set(self): |
| 142 |
self.shx.wght.set('WGHT 0.1 99') |
| 143 |
self.assertEqual('WGHT 0.1 99.0', str(self.shx.wght)) |
| 144 |
self.assertEqual(0.1, self.shx.wght.a) |
| 145 |
self.assertEqual(99.0, self.shx.wght.b) |
| 146 |
|
| 147 |
|
| 148 |
class TestANIS(TestCase): |
| 149 |
|
| 150 |
def test_anis_n(self): |
| 151 |
a = ANIS(Shelxfile(), 'ANIS 12'.split()) |
| 152 |
self.assertEqual([], a.atoms) |
| 153 |
self.assertEqual(12, a.n) |
| 154 |
|
| 155 |
def test_anis_atoms(self): |
| 156 |
a = ANIS(Shelxfile(), 'ANIS C12 C23 Al3a'.split()) |
| 157 |
self.assertEqual(['C12', 'C23', 'Al3a'], a.atoms) |
| 158 |
self.assertEqual(False, hasattr(a, 'n')) |
| 159 |
|
| 160 |
|
| 161 |
class TestABIN(TestCase): |
| 162 |
def test_abin(self): |
| 163 |
a = ABIN(Shelxfile(), 'ABIN 1.234 4'.split()) |
| 164 |
self.assertEqual(1.234, a.n1) |
| 165 |
self.assertEqual(4, a.n2) |
| 166 |
|
| 167 |
|
| 168 |
class TestSADI(TestCase): |
| 169 |
def setUp(self) -> None: |
| 170 |
self.shx = Shelxfile() |
| 171 |
self.shx.read_file('tests/resources/I-43d.res') |
| 172 |
|
| 173 |
def test_sadi_normal(self): |
| 174 |
a = SADI(self.shx, 'SADI C1 C2 C2 C3 C3 C4'.split()) |
| 175 |
self.assertEqual(['C1', 'C2', 'C2', 'C3', 'C3', 'C4'], a.atoms) |
| 176 |
self.assertEqual('', a.residue_class) |
| 177 |
self.assertEqual([0], a.residue_number) |
| 178 |
|
| 179 |
def test_sadi_normal_with_resinum(self): |
| 180 |
a = SADI(self.shx, 'SADI_1 C1 C2 C2 C3 C3 C4'.split()) |
| 181 |
self.assertEqual(['C1', 'C2', 'C2', 'C3', 'C3', 'C4'], a.atoms) |
| 182 |
self.assertEqual('', a.residue_class) |
| 183 |
self.assertEqual([1], a.residue_number) |
| 184 |
|
| 185 |
def test_sadi_normal_with_resinum_on_atoms(self): |
| 186 |
a = SADI(self.shx, 'SADI C1_1 C2_1 C2_2 C3_2 C3_2 C4_2'.split()) |
| 187 |
self.assertEqual(['C1_1', 'C2_1', 'C2_2', 'C3_2', 'C3_2', 'C4_2'], a.atoms) |
| 188 |
self.assertEqual('', a.residue_class) |
| 189 |
self.assertEqual([0], a.residue_number) |
| 190 |
|
| 191 |
def test_sadi_normal_with_name(self): |
| 192 |
a = SADI(self.shx, 'SADI_CCF3 C1 C2 C2 C3 C3 C4'.split()) |
| 193 |
self.assertEqual(['C1', 'C2', 'C2', 'C3', 'C3', 'C4'], a.atoms) |
| 194 |
|
| 195 |
def test_sadi_with_name_2(self): |
| 196 |
a = SADI(self.shx, 'SADI_CCF3 C1 C2 C2 C3 C3 C4'.split()) |
| 197 |
self.assertEqual('CCF3', a.residue_class) |
| 198 |
self.assertEqual([0], a.residue_number) |
| 199 |
|
| 200 |
def test_sadi_with_name_3(self): |
| 201 |
a = SADI(self.shx, 'SADI_CCF3 C1 C2 C2 C3 C3 C4'.split()) |
| 202 |
self.assertEqual(0.02, a.s) |
| 203 |
|
| 204 |
def test_sadi_with_name_4(self): |
| 205 |
a = SADI(self.shx, 'SADI_CCF3 0.001 C1 C2 C2 C3 C3 C4'.split()) |
| 206 |
self.assertEqual(0.001, a.s) |
| 207 |
|
| 208 |
|
| 209 |
class TestwithDEFS(TestCase): |
| 210 |
def setUp(self) -> None: |
| 211 |
self.shx = Shelxfile() |
| 212 |
self.shx.read_file('tests/resources/p21c.res') |
| 213 |
|
| 214 |
def test_sadi_with_s(self): |
| 215 |
a = SADI(self.shx, 'SADI_CCF3 0.001 C1 C2 C2 C3 C3 C4'.split()) |
| 216 |
self.assertEqual(0.001, a.s) |
| 217 |
|
| 218 |
def test_sadi_without_s(self): |
| 219 |
a = SADI(self.shx, 'SADI C1 C2 C2 C3 C3 C4'.split()) |
| 220 |
self.assertEqual(0.0234, a.s) |
| 221 |
|
| 222 |
def test_DFIX_without_s(self): |
| 223 |
a = DFIX(self.shx, 'DFIX 1.45 C1 C2 C2 C3 C3 C4'.split()) |
| 224 |
self.assertEqual(1.45, a.d) |
| 225 |
self.assertEqual(0.0234, a.s) |
| 226 |
self.assertEqual(0.0234, self.shx.defs.sd) |
| 227 |
|
| 228 |
def test_DFIX_with_s(self): |
| 229 |
a = DFIX(self.shx, 'DFIX 1.45 0.022 C1 C2 C2 C3 C3 C4'.split()) |
| 230 |
self.assertEqual(1.45, a.d) |
| 231 |
self.assertEqual(0.022, a.s) |
| 232 |
|
| 233 |
|
| 234 |
class TestDFIX(TestCase): |
| 235 |
""" |
| 236 |
DFIX d s[0.02] atom pairs |
| 237 |
|
| 238 |
Wrong argument order (d < s) is only warned about, never raises — unlike DANG. |
| 239 |
""" |
| 240 |
|
| 241 |
def setUp(self) -> None: |
| 242 |
|
| 243 |
self.shx = Shelxfile() |
| 244 |
self.shx.read_file('tests/resources/p21c.res') |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
def test_dfix_d(self): |
| 249 |
a = DFIX(self.shx, 'DFIX 1.45 C1 C2'.split()) |
| 250 |
self.assertEqual(1.45, a.d) |
| 251 |
|
| 252 |
def test_dfix_explicit_s(self): |
| 253 |
a = DFIX(self.shx, 'DFIX 1.45 0.022 C1 C2'.split()) |
| 254 |
self.assertEqual(1.45, a.d) |
| 255 |
self.assertEqual(0.022, a.s) |
| 256 |
|
| 257 |
def test_dfix_default_s_from_defs(self): |
| 258 |
"""s defaults to the value from the DEFS card when present.""" |
| 259 |
a = DFIX(self.shx, 'DFIX 1.45 C1 C2'.split()) |
| 260 |
self.assertEqual(0.0234, a.s) |
| 261 |
|
| 262 |
def test_dfix_default_s_without_defs(self): |
| 263 |
"""s falls back to the SHELXL default of 0.02 when no DEFS card exists.""" |
| 264 |
shx = Shelxfile() |
| 265 |
a = DFIX(shx, 'DFIX 1.45 C1 C2'.split()) |
| 266 |
self.assertEqual(0.02, a.s) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
def test_dfix_atoms_single_pair(self): |
| 271 |
a = DFIX(self.shx, 'DFIX 1.45 C1 C2'.split()) |
| 272 |
self.assertEqual(['C1', 'C2'], a.atoms) |
| 273 |
|
| 274 |
def test_dfix_atoms_multiple_pairs(self): |
| 275 |
a = DFIX(self.shx, 'DFIX 1.45 C1 C2 C2 C3 C3 C4'.split()) |
| 276 |
self.assertEqual(['C1', 'C2', 'C2', 'C3', 'C3', 'C4'], a.atoms) |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
def test_dfix_repr(self): |
| 281 |
a = DFIX(self.shx, 'DFIX 1.45 0.022 C1 C2'.split()) |
| 282 |
self.assertEqual('DFIX 1.45 0.022 C1 C2', repr(a)) |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
def test_dfix_residue_number_from_suffix(self): |
| 287 |
a = DFIX(self.shx, 'DFIX_1 1.45 C1 C2'.split()) |
| 288 |
self.assertEqual([1], a.residue_number) |
| 289 |
|
| 290 |
def test_dfix_residue_class_from_suffix(self): |
| 291 |
a = DFIX(self.shx, 'DFIX_TOL 1.45 C1 C2'.split()) |
| 292 |
self.assertEqual('TOL', a.residue_class) |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
def test_dfix_wrong_order_silent_no_exception(self): |
| 297 |
"""In silent mode, swapped d/s is stored as-is and no exception is raised.""" |
| 298 |
shx = Shelxfile() |
| 299 |
a = DFIX(shx, 'DFIX 0.01 1.45 C1 C2'.split()) |
| 300 |
self.assertEqual(0.01, a.d) |
| 301 |
self.assertEqual(1.45, a.s) |
| 302 |
|
| 303 |
def test_dfix_wrong_order_verbose_prints_warning(self): |
| 304 |
"""In verbose mode, a warning is printed but no exception is raised.""" |
| 305 |
shx = Shelxfile(verbose=True) |
| 306 |
buf = io.StringIO() |
| 307 |
with contextlib.redirect_stdout(buf): |
| 308 |
a = DFIX(shx, 'DFIX 0.01 1.45 C1 C2'.split()) |
| 309 |
self.assertIn('WRONG', buf.getvalue()) |
| 310 |
self.assertEqual(0.01, a.d) |
| 311 |
self.assertEqual(1.45, a.s) |
| 312 |
|
| 313 |
def test_dfix_wrong_order_debug_prints_warning(self): |
| 314 |
"""In debug mode, a warning is printed but no exception is raised.""" |
| 315 |
shx = Shelxfile(debug=True) |
| 316 |
buf = io.StringIO() |
| 317 |
with contextlib.redirect_stdout(buf): |
| 318 |
a = DFIX(shx, 'DFIX 0.01 1.45 C1 C2'.split()) |
| 319 |
self.assertIn('WRONG', buf.getvalue()) |
| 320 |
self.assertEqual(0.01, a.d) |
| 321 |
self.assertEqual(1.45, a.s) |
| 322 |
|
| 323 |
|
| 324 |
class TestPLAN(TestCase): |
| 325 |
def setUp(self) -> None: |
| 326 |
self.shx = Shelxfile() |
| 327 |
self.shx.read_file('tests/resources/p21c.res') |
| 328 |
|
| 329 |
def test_plan__repr__(self): |
| 330 |
self.assertEqual('PLAN 20', self.shx.plan.__repr__()) |
| 331 |
|
| 332 |
def test_set_plan_value(self): |
| 333 |
self.shx.plan.set('PLAN 23') |
| 334 |
self.assertEqual('PLAN 23', self.shx.plan.__repr__()) |
| 335 |
|
| 336 |
def test_set_plan_all_values(self): |
| 337 |
self.shx.plan.set('PLAN 23 1.34 1.1') |
| 338 |
self.assertEqual('PLAN 23 1.34 1.1', self.shx.plan.__repr__()) |
| 339 |
|
| 340 |
|
| 341 |
class TestAFIX(TestCase): |
| 342 |
|
| 343 |
def test_afix_n(self): |
| 344 |
a = AFIX(Shelxfile(), 'AFIX 33'.split()) |
| 345 |
self.assertEqual(33, a.mn) |
| 346 |
self.assertEqual(None, a.d) |
| 347 |
self.assertEqual(10.08, a.U) |
| 348 |
self.assertEqual(11.0, a.sof) |
| 349 |
self.assertEqual(True, a.__bool__()) |
| 350 |
|
| 351 |
def test_afix_zero(self): |
| 352 |
a = AFIX(Shelxfile(), 'AFIX 0'.split()) |
| 353 |
self.assertEqual(0, a.mn) |
| 354 |
self.assertEqual(False, a.__bool__()) |
| 355 |
|
| 356 |
def test_afix_empty(self): |
| 357 |
a = AFIX(Shelxfile(), 'AFIX'.split()) |
| 358 |
self.assertEqual(None, a.mn) |
| 359 |
self.assertEqual(False, a.__bool__()) |