1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904
|
import re
from math import cos, radians, sqrt, sin
from typing import List, Union, TYPE_CHECKING, Optional, Iterator, Tuple, Dict, Generator
from shelxfile.atoms.pairs import AtomPair
from shelxfile.misc.dsrmath import my_isnumeric, SymmetryElement, OrthogonalMatrix, Matrix
from shelxfile.misc.misc import chunks, ParseParamError, ParseNumError, \
ParseOrderError, ParseSyntaxError
if TYPE_CHECKING:
from shelxfile import Shelxfile
from shelxfile.atoms.atom import Atom
"""
SHELXL cards:
ABIN n1 n2
ACTA 2θfull[#]
AFIX mn d[#] sof[11] U[10.08]
ANIS n
ANIS names
ANSC six coefficients
ANSR anres[0.001]
BASF scale factors
BIND atom1 atom2
BIND m n
BLOC n1 n2 atomnames
BOND atomnames
BUMP s [0.02]
CELL λ a b c α β γ
CGLS nls[0] nrf[0] nextra[0]
CHIV V[0] s[0.1] atomnames
CONF atomnames max_d[1.9] max_a[170]
CONN bmax[12] r[#] atomnames or CONN bmax[12]
DAMP damp[0.7] limse[15]
DANG d s[0.04] atom pairs
DEFS sd[0.02] sf[0.1] su[0.01] ss[0.04] maxsof[1]
DELU s1[0.01] s2[0.01] atomnames
DFIX d s[0.02] atom pairs
DISP E f' f"[#] mu[#]
EADP atomnames
END
EQIV $n symmetry operation
EXTI x[0]
EXYZ atomnames
FEND
FLAT s[0.1] four or more atoms
FMAP code[2] axis[#] nl[53]
FRAG code[17] a[1] b[1] c[1] α[90] β[90] γ[90]
FREE atom1 atom2
FVAR osf[1] free variables
GRID sl[#] sa[#] sd[#] dl[#] da[#] dd[#]
HFIX mn U[#] d[#] atomnames
HKLF N[0] S[1] r11...r33[1 0 0 0 1 0 0 0 1] sm[1] m[0]
HTAB dh[2.0]
HTAB donor-atom acceptor-atom
ISOR s[0.1] st[0.2] atomnames
LATT N[1]
LAUE E
LIST m[#] mult[1]
L.S. nls[0] nrf[0] nextra[0]
MERG n[2]
MORE m[1]
MOVE dx[0] dy[0] dz[0] sign[1]
MPLA na atomnames
NCSY DN sd[0.1] su[0.05] atoms
NEUT
OMIT atomnames
OMIT s[-2] 2θ(lim)[180]
OMIT h k l
PART n sof
PLAN npeaks[20] d1[#] d2[#]
PRIG p[#]
REM
RESI class[ ] number[0] alias
RIGU s1[0.004] s2[0.004] atomnames
RTAB codename atomnames
SADI s[0.02] pairs of atoms
SAME s1[0.02] s2[0.04] atomnames
SFAC elements
SFAC E a1 b1 a2 b2 a3 b3 a4 b4 c f' f" mu r wt
SHEL lowres[infinite] highres[0]
SIMU s[0.04] st[0.08] dmax[2.0] atomnames
SIZE dx dy dz
SPEC del[0.2]
STIR sres step[0.01]
SUMP c sigma c1 m1 c2 m2 ...
SWAT g[0] U[2]
SYMM symmetry operation
TEMP T[20]
TITL [ ]
TWIN 3x3 matrix [-1 0 0 0 -1 0 0 0 -1] N[2]
TWST N[0]
UNIT n1 n2 ...
WGHT a[0.1] b[0] c[0] d[0] e[0] f[.33333]
WIGL del[0.2] dU[0.2]
WPDB n[1]
XNPD Umin[-0.001]
ZERR Z esd(a) esd(b) esd(c) esd(α) esd(β) esd(γ)
"""
class Residue():
def __init__(self):
self.shx: Optional['Shelxfile'] = None
self._spline: str = ''
self.residue_class: str = '' # '' is the default class (with residue number 0)
@property
def residue_number(self) -> List[int]:
if '_' in self._spline[0] and '$' not in self._spline[0]:
_, suffix = self._spline[0].upper().split('_')
if suffix.isdigit():
# TODO: implement _+, _- and _*
if '*' in suffix:
return list(self.shx.residues.residue_numbers.keys()) # type: ignore
else:
return [int(suffix)]
return self.shx.residues.residue_classes.get(self.residue_class, [0]) # type: ignore
class Restraint(Residue):
def __init__(self, shx: 'Shelxfile', spline: list):
"""
Base class for parsing restraints.
TODO: resolve ranges like SADI_CCF3 O1 > F9
"""
super().__init__()
self.shx: 'Shelxfile' = shx
self.textline: str = ' '.join(spline)
self.name: Optional[str] = None
self.atoms: List[Atom] = []
@property
def index(self) -> int:
return self.shx.index_of(self)
def _parse_line(self, spline: List[str]):
"""
Residues may be referenced by any instruction that allows atom names; the reference takes
the form of the character '_' followed by either the residue class or number without intervening
spaces.
Individual atom names in an instruction may be followed by '_' and a residue number, but not by '_* ' or '_'
and a residue class. If an atom name is not followed by a residue number, the current residue is
assumed (unless overridden by a global residue number or class appended to the instruction
codeword).
"""
self._spline = spline
if '_' in spline[0]:
self.name, suffix = spline[0].upper().split('_')
# a residue class has to start with a letter, but can contain numbers:
if len(suffix) > 0 and re.match(r'[a-zA-Z]', suffix):
self.residue_class: str = suffix
else:
self.name = spline[0].upper()
self._set_defs_values()
self._check_if_class_name_fits_to_command()
params = []
atoms = []
for x in spline[1:]:
if my_isnumeric(x):
params.append(float(x))
else:
atoms.append(x)
# if pairs:
# return params, self.get_atompairs(atoms)
# else:
return params, atoms
def _get_atompairs(self, atoms: List[str]) -> List[AtomPair]:
pairs = []
for p in chunks(atoms, 2):
pairs.append(AtomPair(*p))
return pairs
def _check_if_class_name_fits_to_command(self) -> None:
if self.shx.debug and self.__class__.__name__ != self.name:
print('*** Trying to parse restraint with wrong class ***')
raise ParseSyntaxError(debug=self.shx.debug, verbose=self.shx.verbose)
def _set_defs_values(self) -> None:
# Beware! DEFS changes only the non-defined default values:
# DEFS sd[0.02] sf[0.1] su[0.01] ss[0.04] maxsof[1]
if self.shx.defs: # and self.shx.defs.active:
if self.name == 'DFIX':
self.s = self.shx.defs.sd
if self.name == 'SAME':
self.s1 = self.shx.defs.sd
self.s2 = self.shx.defs.sd * 2
if self.name == 'SADI':
self.s = self.shx.defs.sd
if self.name == 'CHIV':
self.s = self.shx.defs.sf
if self.name == 'FLAT':
self.s = self.shx.defs.sf
if self.name == 'DELU':
self.s1 = self.shx.defs.su
self.s2 = self.shx.defs.su
if self.name == 'SIMU':
self.s = self.shx.defs.ss
self.st = self.shx.defs.ss * 2
def _paircheck(self):
if not self.atoms:
return
if len(self.atoms) % 2 != 0 and (self.shx.debug or self.shx.verbose):
print('*** Wrong number of numerical parameters ***')
print('Instruction: {}'.format(self.textline))
if self.shx.debug:
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
def __iter__(self) -> Iterator[str]:
for x in self.textline.split():
yield x
def __repr__(self) -> str:
return self.textline
def __str__(self) -> str:
return self.textline
'''# print(self.atoms, self.residue_number)
s = self.s if hasattr(self, 's') else ''
s1 = self.s1 if hasattr(self, 's1') else ''
s2 = self.s2 if hasattr(self, 's2') else ''
st = self.st if hasattr(self, 'st') else ''
return f"{self.name}" \
f"{' ' if s else ''}{s}" \
f"{' ' if s1 else ''}{s1}" \
f"{' ' if s2 else ''}{s2}" \
f"{' ' if st else ''}{st} " \
f"{' '.join(self.atoms)}"'''
def split(self):
return self.textline.split()
class Command():
"""
A class to parse all general commands except restraints.
"""
def __init__(self, shx: 'Shelxfile', spline: List[str]):
self._shx: 'Shelxfile' = shx
self._spline: List[str] = spline
self.residue_class: str = ''
self._textline: str = ' '.join(spline)
def _parse_line(self, spline: List[str], intnums: bool = False) -> Tuple[List[Union[int, float]], List[str]]:
"""
:param spline: Split shelxl line
:param intnums: if numerical parameters should be integer
:return: Tuple of (numerical parameter, words)
"""
if '_' in spline[0]:
self._card_name, suffix = spline[0].upper().split('_')
else:
self._card_name = spline[0].upper()
numparams = []
words = []
for x in spline[1:]: # all values after SHELX card
if str.isdigit(x[0]) or x[0] in '+-':
if intnums:
numparams.append(int(x))
else:
numparams.append(float(x))
else:
words.append(x)
return numparams, words
def set(self, value):
self.__init__(self._shx, value.split())
@property
def index(self):
return self._shx.index_of(self)
@property
def position(self) -> int:
return self.index
def __iter__(self):
for x in self.__repr__().split():
yield x
def split(self):
return self._textline.split()
def __str__(self):
return self._textline
def __repr__(self):
return self._textline
class ABIN(Command):
def __init__(self, shx, spline):
"""
ABIN n1 n2
"""
super(ABIN, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
if len(p) > 0:
self.n1 = p[0]
if len(p) > 1:
self.n2 = p[1]
class ANIS(Command):
def __init__(self, shx, spline: List):
"""
ANIS
ANIS n
ANIS names
Make either all atoms anisotropic (with just ANIS) or specific atoms with ANIS names.
Also, wildcards like 'ANIS $CL' would make all chlorine atoms anisotropic.
Since ANIS, like other instructions, applies to the current residue unless otherwise specified,
ANIS_* $S would be required to make the sulfur atoms in all residues anisotropic.
ANIS n makes the next n isotropic non-hydrogen atoms anisotropic.
"""
super(ANIS, self).__init__(shx, spline)
p, self.atoms = self._parse_line(spline)
self.over_all = True
if len(p) > 0:
self.over_all = False
self.n = p[0]
if len(self.atoms) > 0:
self.over_all = False
def __bool__(self):
return True
class MPLA(Command):
def __init__(self, shx, spline: List):
"""
MPLA na atomnames
"""
super(MPLA, self).__init__(shx, spline)
p, self.atoms = self._parse_line(spline, intnums=True)
if len(p) > 0:
self.na = p[0]
class MORE(Command):
def __init__(self, shx, spline: List):
"""
MORE m[1]
"""
super(MORE, self).__init__(shx, spline)
self.m = 1
p, _ = self._parse_line(spline, intnums=True)
self.m = p[0]
class CELL(Command):
def __init__(self, shx, spline: List):
"""
CELL λ a b c α β γ
"""
super(CELL, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self._cell_list = []
if len(p) > 0:
self.wavelen = float(p[0])
if len(p) > 6:
self._cell_list = p[1:]
self.a = p[1]
self.b = p[2]
self.c = p[3]
self.alpha = p[4]
self.beta = p[5]
self.gamma = p[6]
self.cosal = cos(radians(self.alpha))
self.cosbe = cos(radians(self.beta))
self.cosga = cos(radians(self.gamma))
self.V = self.volume
self.o = OrthogonalMatrix(self.a, self.b, self.c, self.alpha, self.beta, self.gamma)
# calculate reciprocal lattice vectors:
self.astar = (self.b * self.c * sin(radians(self.alpha))) / self.V
self.bstar = (self.a * self.c * sin(radians(self.beta))) / self.V
self.cstar = (self.a * self.b * sin(radians(self.gamma))) / self.V
# matrix with the reciprocal lattice vectors:
self.N = Matrix([[self.astar, 0, 0],
[0, self.bstar, 0],
[0, 0, self.cstar]])
else:
raise ParseSyntaxError(debug=shx.debug, verbose=shx.verbose)
@property
def volume(self) -> float:
"""
calculates the volume of a unit cell
"""
try:
ca, cb, cg = cos(radians(self.alpha)), cos(radians(self.beta)), cos(radians(self.gamma))
v = self.a * self.b * self.c * sqrt(1 + 2 * ca * cb * cg - ca ** 2 - cb ** 2 - cg ** 2)
except AttributeError:
# No valid celll
v = 0.0
return v
def __iter__(self):
return iter(self._cell_list)
def __getitem__(self, item: Union[slice, int]) -> Union[List[float], float]:
return self._cell_list[item]
class ZERR(Command):
def __init__(self, shx, spline: List):
"""
ZERR Z esd(a) esd(b) esd(c) esd(α) esd(β) esd(γ)
"""
super(ZERR, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.Z = 1
if len(p) > 0:
self.Z = p[0]
if len(p) > 6:
self.esd_list = p[1:]
self.esd_a = p[0]
self.esd_b = p[1]
self.esd_c = p[2]
self.esd_al = p[3]
self.esd_be = p[4]
self.esd_ga = p[5]
class AFIX(Command):
def __init__(self, shx, spline: list):
"""
AFIX mn d[#] sof[11] U[10.08]
"""
super(AFIX, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.U = 10.08
self.sof = 11.0
self.mn = None
self.d = None
if len(p) > 0:
self.mn = int(p[0])
if len(p) > 1:
self.d = p[1]
if len(p) > 2:
self.sof = p[2]
if len(p) > 3:
self.U = p[3]
def __bool__(self):
if self.mn and self.mn > 0:
return True
else:
return False
class Residues():
def __init__(self, shx):
self.shx = shx
self.all_residues: list = []
self.residue_classes: dict = {} # class: numbers
def append(self, resi: 'RESI') -> None:
"""
Adds a new residues to the list of residues.
"""
self.all_residues.append(resi)
# Collect dict with class: numbers
if resi.residue_class in self.residue_classes:
self.residue_classes[resi.residue_class].append(resi.residue_number)
else:
self.residue_classes[resi.residue_class] = [resi.residue_number]
@property
def residue_numbers(self):
return dict((x.residue_number, x.residue_class) for x in self.shx.residues.all_residues)
class RESI(Command):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
RESI class[ ] number[0] alias
"""
super().__init__(shx, spline)
self.shx = shx
self.residue_class = ''
self.residue_number: int = 0
self.alias: Optional[int] = None
self.chain_id: Optional[int] = None
self._textline: str = ' '.join(spline)
if len(spline) < 2 and (self.shx.debug or self.shx.verbose):
print('*** Wrong RESI definition found! Check your RESI instructions ***')
raise ParseParamError(debug=self.shx.debug, verbose=self.shx.verbose)
self._get_resi_definition(spline)
if self.residue_number < -999 or self.residue_number > 9999:
if self.shx.debug or self.shx.verbose:
print('*** Invalid residue number given. ****')
if self.shx.debug:
raise ParseSyntaxError(debug=self.shx.debug, verbose=self.shx.verbose)
def _get_resi_definition(self, resi: List[str]) -> Tuple[str, int, str, int]:
"""
RESI class[ ] number[0] alias
Returns the residue number and class of a string like 'RESI TOL 1'
or 'RESI 1 TOL'
Residue names may now begin with a digit.
They must however contain at least one letter
Allowed residue numbers is now from -999 to 9999 (2017/1)
"""
alpha = re.compile('[a-zA-Z]')
for x in resi:
if alpha.search(x):
if ':' in x:
# contains ":" thus must be a chain-id+number
self.chain_id, self.residue_number = x.split(':')[0], int(x.split(':')[1])
else:
# contains letters, must be a name (class)
self.residue_class = x
else:
# everything else can only be a number
if self.residue_number > 0:
self.alias = int(x)
else:
try:
self.residue_number = int(x)
except ValueError:
self.residue_number = 0
return self.residue_class, self.residue_number, self.chain_id, self.alias
def __bool__(self) -> bool:
if self.residue_number > 0:
return True
else:
return False
class PART(Command):
def __init__(self, shx, spline: list):
"""
PART n sof
"""
super(PART, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.sof = 11.0
self.n = 0
try:
self.n = int(p[0])
except(ValueError, IndexError):
if self.shx.debug or self.shx.verbose:
print('*** Wrong PART definition in line {} found! '
'Check your PART instructions ***'.format(shx.error_line_num))
if self.shx.debug:
raise ParseSyntaxError(debug=True)
self.n = 0
if len(p) > 1:
self.sof = float(p[1])
def __bool__(self):
if self.n > 0:
return True
else:
return False
class XNPD(Command):
def __init__(self, shx, spline: list):
"""
XNPD Umin[-0.001]
"""
super(XNPD, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.Umin = -0.001
if len(p) > 0:
self.Umin = p[0]
class SIZE(Command):
def __init__(self, shx, spline: list):
"""
SIZE dx dy dz
"""
super(SIZE, self).__init__(shx, spline)
self.dx, self.dy, self.dz = None, None, None
p, _ = self._parse_line(spline)
if len(p) > 0:
self.dx = p[0]
if len(p) > 1:
self.dy = p[1]
if len(p) > 2:
self.dz = p[2]
def _as_text(self):
if all([self.dx, self.dy, self.dz]):
return "SIZE {:,g} {:,g} {:,g}".format(self.dx, self.dy, self.dz)
else:
return ""
@property
def max(self):
return max(self.dx, self.dy, self.dz)
@property
def mid(self):
return sorted([self.dx, self.dy, self.dz])[1]
@property
def min(self):
return min(self.dx, self.dy, self.dz)
def __repr__(self):
return self._as_text()
def __str__(self):
return self._as_text()
class SHEL(Command):
def __init__(self, shx, spline: list):
"""
SHEL lowres[infinite] highres[0]
"""
super(SHEL, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
self.lowres = None
self.highres = None
if len(params) > 0:
self.lowres = params[0]
if len(params) > 1:
self.highres = params[1]
class WIGL(Command):
def __init__(self, shx, spline: list):
"""
WIGL del[0.2] dU[0.2]
"""
super(WIGL, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.d = 0.2
self.dU = 0.2
if len(p) > 0:
self.d = p[0]
if len(p) > 1:
self.dU = p[1]
class WPDB(Command):
def __init__(self, shx, spline: list):
"""
WPDB n[1]
"""
super(WPDB, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.n = 1
if len(p) > 0:
self.n = p[0]
class SPEC(Command):
def __init__(self, shx, spline: list):
"""
SPEC d[0.2]
"""
super(SPEC, self).__init__(shx, spline)
self.d = None
p, _ = self._parse_line(spline)
if len(p) > 0:
self.d = p[0]
class STIR(Command):
def __init__(self, shx, spline: list):
"""
STIR sres step[0.01]
"""
super(STIR, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.step = 0.01
self.sres = None
if len(p) > 0:
self.sres = p[0]
if len(p) > 1:
self.step = p[1]
def __repr__(self):
return "STIR {} {}".format(self.sres if self.sres else '', self.step)
def __str__(self):
return "STIR {} {}".format(self.sres if self.sres else '', self.step)
class TWST(Command):
def __init__(self, shx, spline: list):
"""
TWST N[0] (N[1] after SHELXL-2018/3)
Twin component number to be used for the completeness and Friedel completeness statistics.
"""
super(TWST, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.N = 1
if len(p) > 0:
self.N = p[0]
class RTAB(Command):
def __init__(self, shx, spline: list):
"""
RTAB codename atomnames
"""
super(RTAB, self).__init__(shx, spline)
self.code = spline.pop(1)
_, self.atoms = self._parse_line(spline)
class PRIG(Command):
def __init__(self, shx, spline: list):
"""
PRIG p[#]
"""
super(PRIG, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
if len(params) > 0:
self.p = params[0]
class PLAN(Command):
def __init__(self, shx, spline: list):
"""
PLAN npeaks[20] d1[#] d2[#]
"""
self.shx = shx
super(PLAN, self).__init__(shx, spline)
self.npeaks = 20
self.d1 = None
self.d2 = None
params, _ = self._parse_line(spline)
if len(params) > 0:
self.npeaks = int(params[0])
if len(params) > 1:
self.d1 = params[1]
if len(params) > 2:
self.d2 = params[2]
def __repr__(self):
return f'PLAN {self.npeaks:,g}{" " if self.d1 else ""}{self.d1 if self.d1 is not None else ""}' \
f'{" " if self.d2 else ""}{self.d2 if self.d2 is not None else ""}'
class FRAG(Command):
def __init__(self, shx, spline: list):
"""
FRAG code[17] a[1] b[1] c[1] α[90] β[90] γ[90]
"""
super(FRAG, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
self.code = params[0]
self.cell = params[1:7]
class FREE(Command):
def __init__(self, shx, spline: list):
"""
FREE atom1 atom2
"""
super(FREE, self).__init__(shx, spline)
_, atoms = self._parse_line(spline)
self.atom1 = None
self.atom2 = None
try:
self.atom1 = atoms[0]
self.atom2 = atoms[1]
except IndexError:
raise ParseParamError(debug=shx.debug, verbose=shx.verbose)
class FMAP(Command):
"""
FMAP code[2] axis[#] nl[53]
"""
def __init__(self, shx, spline: list):
super(FMAP, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
self.code = None
self.axis = None
self.nl = None
if len(params) > 0:
self.code = params[0]
if len(params) > 1:
self.axis = params[1]
if len(params) > 2:
self.nl = params[2]
class MOVE(Command):
def __init__(self, shx, spline: list):
"""
MOVE dx[0] dy[0] dz[0] sign[1]
"""
super(MOVE, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
self.dxdydz = None
self.sign = None
if len(params) > 2:
self.dxdydz = params[:3]
if len(params) > 3:
self.sign = params[3]
class MERG(Command):
def __init__(self, shx, spline: list):
"""
MERG n[2]
"""
super(MERG, self).__init__(shx, spline)
self.n = None
_n, _ = self._parse_line(spline)
if len(_n) > 0:
self.n = _n[0]
class HTAB(Command):
def __init__(self, shx, spline: list):
"""
HTAB dh[2.0]
HTAB donor-atom acceptor-atom
"""
super(HTAB, self).__init__(shx, spline)
self.dh = None
self.donor = None
self.acceptor = None
dh, atoms = self._parse_line(spline)
if dh:
self.dh = dh[0]
if len(atoms) == 2:
self.donor = atoms[0]
self.acceptor = atoms[1]
class GRID(Command):
def __init__(self, shx, spline: list):
"""
GRID sl[#] sa[#] sd[#] dl[#] da[#] dd[#]
"""
super(GRID, self).__init__(shx, spline)
params, _ = self._parse_line(spline)
if len(params) > 0:
self.sl = params[0]
if len(params) > 1:
self.sa = params[1]
if len(params) > 2:
self.sd = params[2]
if len(params) > 3:
self.dl = params[3]
if len(params) > 4:
self.da = params[4]
if len(params) > 5:
self.dd = params[5]
class ACTA(Command):
def __init__(self, shx, spline: list):
"""
ACTA 2θfull[#] (NOHKL)
"""
super(ACTA, self).__init__(shx, spline)
self.twotheta, self.nohkl = self._parse_line(spline)
self.shx = shx
def _as_str(self):
if self.twotheta:
return f"ACTA {self.twotheta[0]:,g}"
else:
return "ACTA"
def __repr__(self):
return self._as_str()
def __str__(self):
return self._as_str()
class BLOC(Command):
def __init__(self, shx, spline: list):
"""
BLOC n1 n2 atomnames
"""
super(BLOC, self).__init__(shx, spline)
params, self.atoms = self._parse_line(spline)
self.n1 = None
self.n2 = None
if len(params) > 0:
self.n1 = params[0]
if len(params) > 1:
self.n2 = params[1]
self.shx = shx
class FVAR():
def __init__(self, number: int = 1, value: float = 0.0):
"""
FVAR osf[1] free variables
"""
self.fvar_value = value # value
self.number = number # occurence inside of FVAR instructions
self.usage = 1
def __str__(self):
return str(float(self.fvar_value))
def __repr__(self):
return str(float(self.fvar_value))
class FVARs():
def __init__(self, shx):
super(FVARs, self).__init__()
self.fvars = [] # free variables
self.shx = shx
self._fvarline = 0
def __iter__(self):
"""
Must be defined for __repr__() to work.
"""
for x in self.fvars:
yield x
def __getitem__(self, item: int) -> str:
# SHELXL counts fvars from 1 to x:
item = abs(item) - 1
return self.fvars[item].fvar_value
def __setitem__(self, key, fvar_value):
self.fvars[key] = fvar_value
def __len__(self) -> int:
return len(self.fvars)
def __str__(self) -> str:
# returnes FVAR as list of FVAR instructions with seven numbers in one line
lines = chunks(self.as_stringlist, 7)
fvars = [' '.join(i) for i in lines]
fvars = ['FVAR ' + i for i in fvars]
return "\n".join(fvars)
def __repr__(self):
return str([x for x in self.fvars])
@property
def position(self) -> int:
return self.shx.index_of(self)
def set_free_variables(self, fvar: int, dummy_fvar: float = 0.5):
"""
Inserts additional free variables according to the fvar number.
"""
if fvar > 99:
print('*** SHELXL allows only 99 free variables! ***')
raise ParseParamError(debug=self.shx.debug, verbose=self.shx.verbose)
varlen = len(self.fvars)
difference = (abs(fvar) - varlen)
if difference > 0:
for n in range(int(difference)):
fv = FVAR(varlen + n, dummy_fvar)
self.fvars.append(fv)
def append(self, fvar) -> None:
self.fvars.append(fvar)
def set_fvar_usage(self, fvarnum: int, times: int = 1) -> None:
"""
Incerements the usage count of a free variable by times.
"""
fvarnum = abs(fvarnum)
if len(self.fvars) >= abs(fvarnum):
self.fvars[fvarnum - 1].usage += times
elif fvarnum > 1 and (self.shx.debug or self.shx.verbose):
print('*** Free variable {} is not defined but used! ***'.format(fvarnum))
if self.shx.debug:
raise ParseParamError(debug=self.shx.debug, verbose=self.shx.verbose)
def get_fvar_usage(self, fvarnum):
"""
Returns the usage (count) of a certain free variable.
"""
try:
usage = self.fvars[fvarnum - 1].usage
except IndexError:
return 0
return usage
def fvars_used(self):
"""
Retruns a dictionary with the usage of all free variables.
"""
used = {}
for num, fv in enumerate(self.fvars):
used[num + 1] = fv.usage
return used
@property
def as_stringlist(self):
return [str(x.fvar_value) for x in self.fvars]
class CONF(Command):
def __init__(self, shx, spline: list) -> None:
"""
CONF atomnames max_d[1.9] max_a[170]
"""
super(CONF, self).__init__(shx, spline)
class CONN(Command):
def __init__(self, shx, spline: list) -> None:
"""
CONN bmax[12] r[#] atomnames or CONN bmax[12]
"""
super(CONN, self).__init__(shx, spline)
class REM(Command):
def __init__(self, shx, spline: list) -> None:
"""
Parses REM lines
"""
super(REM, self).__init__(shx, spline)
class BIND(Command):
def __init__(self, shx, spline: list) -> None:
"""
BIND atom1 atom2
BIND m n
"""
super(BIND, self).__init__(shx, spline)
self.parts, self.atoms = self._parse_line(spline)
class BOND(Command):
def __init__(self, shx, spline: list) -> None:
"""
BOND atomnames
"""
super(BOND, self).__init__(shx, spline)
_, self.atoms = self._parse_line(spline)
class DISP(Command):
"""
DISP E f' f"[#] mu[#]
"""
def __init__(self, shx, spline: list) -> None:
super(DISP, self).__init__(shx, spline)
self.element, self.parameter = self._parse_line(spline)
class Restraints():
"""
Base class for the list of restraints.
"""
def __init__(self) -> None:
"""
"""
self._restraints: List[Restraint] = []
def append(self, restr: Restraint):
self._restraints.append(restr)
def __iter__(self) -> Generator:
x: Restraint
for x in self._restraints:
yield x
def __getitem__(self, item: int) -> Restraint:
return self._restraints[item]
def __repr__(self) -> str:
if self._restraints:
return "\n".join([str(x) for x in self._restraints])
else:
return 'No Restraints in file.'
class DEFS(Restraint):
def __init__(self, shx, spline: list):
"""
DEFS sd[0.02] sf[0.1] su[0.01] ss[0.04] maxsof[1]
Changes the *default* effective standard deviations for the following
DFIX, SAME, SADI, CHIV, FLAT, DELU and SIMU restraints.
"""
super(DEFS, self).__init__(shx, spline)
self.sf = 0.1
self.su = 0.01
self.ss = 0.04
self.maxsof = 1
self.sd = 0.02
self.active = True
p, _ = self._parse_line(spline)
if _:
raise ParseParamError(debug=shx.debug, verbose=shx.verbose)
if len(p) > 0:
self.sd = p[0]
if len(p) > 1:
self.sf = p[1]
if len(p) > 2:
self.su = p[2]
if len(p) > 3:
self.ss = p[3]
if len(p) > 4:
self.maxsof = p[4]
@property
def all(self):
return self.sd, self.sf, self.su, self.ss, self.maxsof
class NCSY(Restraint):
"""
NCSY DN sd[0.1] su[0.05] atoms
"""
def __init__(self, shx, spline: list):
super(NCSY, self).__init__(shx, spline)
self.sd = 0.1
self.su = 0.05
self.DN = None
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.DN = p[0]
if len(p) > 1:
self.sd = p[1]
if len(p) > 2:
self.su = p[2]
if not self.DN:
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
class ISOR(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
ISOR s[0.1] st[0.2] atomnames
"""
super(ISOR, self).__init__(shx, spline)
self.s = 0.1
self.st = 0.2
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s = p[0]
if len(p) > 1:
self.st = p[1]
class FLAT(Restraint):
"""
FLAT s[0.1] four or more atoms
"""
def __init__(self, shx, spline: list):
super(FLAT, self).__init__(shx, spline)
self.s = 0.1
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s = p[0]
# TODO: Have to resolve ranges first:
# if len(self.atoms) < 4:
# raise ParseParamError
class BUMP(Restraint):
"""
'Anti-bumping' restraints are generated automatically for all distances involving
two non-bonded C, N, O and S atoms (based on the SFAC type) that are shorter than
the expected shortest non-bonded distances, allowing for the possibility of hydrogen bonds.
"""
def __init__(self, shx, spline):
"""
BUMP s [0.02]
"""
super(BUMP, self).__init__(shx, spline)
self.s = 0.02
p, _ = self._parse_line(spline)
if len(p) > 0:
self.s = p[0]
if _:
raise ParseParamError(debug=shx.debug, verbose=shx.verbose)
class DFIX(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
DFIX d s[0.02] atom pairs
"""
super(DFIX, self).__init__(shx, spline)
self.s = 0.02
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.d = p[0]
if len(p) > 1:
self.s = p[1]
self._paircheck()
if not self.d:
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
if (self.shx.debug or self.shx.verbose) and 0.0001 < self.d <= self.s:
print('*** WRONG ODER of INSTRUCTIONS. d is smaller than s ***')
print("{}".format(self.textline))
class DANG(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
DANG d s[0.04] atom pairs
"""
super(DANG, self).__init__(shx, spline)
self.s = 0.04
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.d = p[0]
if len(p) > 1:
self.s = p[1]
self._paircheck()
if not self.d:
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
if 0.0001 < self.d <= self.s: # Raise exception if d is smaller than s
raise ParseOrderError(debug=shx.debug, verbose=shx.verbose)
class SADI(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
SADI s[0.02] pairs of atoms
Instructions with only two atoms are ignored by SHELXL: SADI C3 C4
SADI_3 C3 C4 C3_4 C4_4 creates SADI C3_3 C4_3 C3_4 C4_4
SADI_CCF3 C3 C4 C3_4 C4_4 creates SADI C3_1 C4_1 C3_2 C4_2 C3_4 C4_4 if there are residues 1, 2 and 4
"""
super(SADI, self).__init__(shx, spline)
self.s = 0.02
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s = p[0]
self._paircheck()
class SAME(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
SAME s1[0.02] s2[0.04] atomnames
"""
super(SAME, self).__init__(shx, spline)
self.s1 = 0.02
self.s2 = 0.04
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s1 = p[0]
if len(p) > 1:
self.s2 = p[1]
class RIGU(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
RIGU s1[0.004] s2[0.004] atomnames
"""
super(RIGU, self).__init__(shx, spline)
self.s1 = 0.004
self.s2 = 0.004
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s1 = p[0]
if len(p) > 1:
self.s2 = p[1]
class SIMU(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
SIMU s[0.04] st[0.08] dmax[2.0] atomnames
"""
super(SIMU, self).__init__(shx, spline)
self.s = 0.04
self.st = 0.08
self.dmax = 2.0
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s = p[0]
if len(p) > 1:
self.st = p[1]
if len(p) > 2:
self.dmax = p[2]
class DELU(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
DELU s1[0.01] s2[0.01] atomnames
"""
super(DELU, self).__init__(shx, spline)
self.s1 = 0.01
self.s2 = 0.01
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.s1 = p[0]
if len(p) > 1:
self.s2 = p[1]
class CHIV(Restraint):
def __init__(self, shx: 'Shelxfile', spline: List[str]):
"""
CHIV V[0] s[0.1] atomnames
"""
super(CHIV, self).__init__(shx, spline)
self.s = 0.1
self.V = 0.0
p, self.atoms = self._parse_line(spline)
if len(p) > 0:
self.V = p[0]
if len(p) > 1:
self.s = p[1]
class EADP(Restraint):
"""
EADP atomnames
"""
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
super(EADP, self).__init__(shx, spline)
_, self.atoms = self._parse_line(spline)
class EXYZ(Restraint):
"""
EADP atomnames
"""
def __init__(self, shx, spline: list) -> None:
super(EXYZ, self).__init__(shx, spline)
_, self.atoms = self._parse_line(spline)
class DAMP(Command):
"""
DAMP damp[0.7] limse[15]
"""
def __init__(self, shx, spline: list):
super(DAMP, self).__init__(shx, spline)
values, _ = self._parse_line(spline, intnums=False)
self.damp, self.limse = 0, 0
if len(values) > 0:
self.damp = values[0]
if len(values) > 1:
self.damp, self.limse = values
def __repr__(self) -> str:
if self.limse == 0:
return "DAMP {:,g}".format(self.damp)
else:
return "DAMP {:,g} {:,g}".format(self.damp, self.limse)
class HFIX(Command):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
HFIX mn U[#] d[#] atomnames
"""
super(HFIX, self).__init__(shx, spline)
self.params, self.atoms = self._parse_line(spline, intnums=True)
def __repr__(self):
return f"HFIX {' '.join([str(x) for x in self.params]) if self.params else ''} " \
f"{' '.join(self.atoms) if self.atoms else ''}"
class HKLF(Command):
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
HKLF N[0] S[1] r11...r33[1 0 0 0 1 0 0 0 1] sm[1] m[0]
"""
super(HKLF, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.n = 0
self.s = 1
self.matrix = [1, 0, 0, 0, 1, 0, 0, 0, 1]
self.sm = 1
self.m = 0
if len(p) > 0:
self.n = int(p[0])
if len(p) > 1:
self.s = p[1]
if len(p) > 10:
self.matrix = p[3:11]
if len(p) > 11:
self.sm = p[12]
if len(p) > 12:
self.m = p[13]
def __repr__(self) -> str:
return "HKLF {:,g} {:,g} {} {:,g} {:,g}".format(self.n, self.s, ' '.join([str(i) for i in self.matrix]),
self.sm, self.m)
class SUMP(Command):
"""
SUMP for linear equation eypressions with free variables.
SUMP c sigma c1 m1 c2 m2 ...
"""
def __init__(self, shx, spline: list):
super(SUMP, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.c = p.pop(0)
self.sigma = p.pop(0)
# this is to have integer free variables
_fvars: List[int] = [int(x) for x in p[1::2]]
_times: List[Union[int, float]] = [x for x in p[0::2]]
self.fvars: List[list[Union[int, float]]] = [[x, y] for x, y in zip(_times, _fvars)]
def __getitem__(self, item: int) -> Union[List[int], List[float]]:
return self.fvars[item]
class SWAT(Command):
"""
SWAT g[0] U[2]
Allows two variables g and U to be refined in order to model diffuse solvent
"""
def __init__(self, shx: 'Shelxfile', spline: List[str]):
super().__init__(shx, spline)
p, _ = self._parse_line(spline)
if len(p) > 1:
self.g = p.pop(0)
self.U = p.pop(0)
class LATT(Command):
lattdict = {1: [], # Primitive
2: [SymmetryElement(['0.5', '0.5', '0.5'])], # I-centered
3: [SymmetryElement(['1/3', '2/3', '2/3']), # Rhombohedral
SymmetryElement(['2/3', '1/3', '1/3'])],
4: [SymmetryElement(['0.0', '0.5', '0.5']), # F-centered
SymmetryElement(['0.5', '0.0', '0.5']),
SymmetryElement(['0.5', '0.5', '0.0'])],
5: [SymmetryElement(['0.0', '0.5', '0.5'])], # A-centered
6: [SymmetryElement(['0.5', '0.0', '0.5'])], # B-centered
7: [SymmetryElement(['0.5', '0.5', '0.0'])]} # C-centered
lattint_to_str = {1: 'P', 2: 'I', 3: 'R', 4: 'F', 5: 'A', 6: 'B', 7: 'C'}
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
"""
LATT N[1]
"""
super(LATT, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self.centric = False
try:
self.N = int(p[0])
except ValueError:
self.N = -1
self.N_str = self.lattint_to_str[abs(self.N)]
if self.N > 0: # centrosymmetric space group:
self.centric = True
self.latt_ops = LATT.lattdict[abs(self.N)]
class SYMM(Command):
def __init__(self, shx, spline: list):
"""
SYMM symmetry operation
"""
super(SYMM, self).__init__(shx, spline)
self.symmcard = self._parse_line(spline)
def _parse_line(self, spline: list, intnums: bool = False) -> list:
symmcard = ''.join(spline[1:]).split(',') # removes whitespace
return symmcard
def _as_str(self) -> str:
return "SYMM " + ", ".join(self.symmcard)
def __repr__(self) -> str:
return self._as_str()
def __str__(self) -> str:
return self._as_str()
class SymmCards():
"""
Contains the list of SYMM cards
"""
def __init__(self, shx: 'Shelxfile') -> None:
self.shx = shx
self._symmcards = [SymmetryElement(['X', 'Y', 'Z'])]
self.latt_ops = []
def _as_str(self) -> str:
return "\n".join([str(x) for x in self._symmcards])
def __repr__(self) -> str:
return self._as_str()
def __str__(self) -> str:
return self._as_str()
def __len__(self) -> int:
return len(self._symmcards)
def __getitem__(self, item: int) -> SymmetryElement:
return self._symmcards[item]
def __iter__(self) -> Generator:
for x in self._symmcards:
yield x
def append(self, symm_data: list) -> None:
"""
Add the content of a Shelxl SYMM command to generate the appropriate SymmetryElement instance.
:param symm_data: list of strings. eg.['1/2+X', '1/2+Y', '1/2+Z']
:return: None
"""
new_symm = SymmetryElement(symm_data)
self._symmcards.append(new_symm)
for symm in self.shx.latt.latt_ops:
latt_symm = new_symm.apply_latt_symm(symm)
if latt_symm not in self._symmcards:
self._symmcards.append(latt_symm)
if self.shx.latt.centric:
self._symmcards.append(SymmetryElement(symm_data, centric=True))
for symm in self.shx.latt.latt_ops:
latt_symm = new_symm.apply_latt_symm(symm)
latt_symm.centric = True
self._symmcards.append(latt_symm)
def set_centric(self, value: bool) -> None:
"""
Defines the instance as representing a centrosymmetric structure. Generates the appropriate SymmetryElement
instances automatically if called before adding further SYMM commands via self.addSymm().
"""
self.shx.latt.centric = value
self._symmcards.append(SymmetryElement(['-X', '-Y', '-Z']))
self._symmcards[-1].centric = True
def set_latt_ops(self, lattops: list) -> None:
"""
Adds lattice operations. If called before adding SYMM commands, the appropriate lattice operations are used
automatically to generate further SymmetryElements.
:param lattops: list of SymmetryElement instances.
"""
self.latt_ops = lattops
class LSCycles(Command):
def __init__(self, shx, spline: list):
"""
L.S. nls[0] nrf[0] nextra[0]
#TODO: support nls parameter
If nrf is positive, it is the number of these cycles that should be performed before applying ANIS.
Negative nrf indicates which reflections should be ignored during the refinement but used instead for
the calculation of free R-factors in the final structure factor summation.
nextra is the number of additional parameters that were derived from the data when 'squeezing' the
structure etc.
"""
super(LSCycles, self).__init__(shx, spline)
p, _ = self._parse_line(spline)
self._shx = shx
self.cgls = False
self._cycles = 0
self._nrf = ''
self._nextra = ''
try:
self._cycles = int(p[0])
except (IndexError, NameError, ValueError):
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
try:
self._nrf = int(p[1])
except IndexError:
pass
try:
self._nextra = int(p[2])
except IndexError:
pass
if spline[0].upper() == 'CGLS':
self.cgls = True
@property
def number(self):
return self._cycles
@number.setter
def number(self, n: int) -> None:
self._cycles = int(n)
self.__init__(self._shx, self._as_str().split())
def set_refine_cycles(self, number: int) -> None:
"""
Sets the number of refinement cycles for the current res file.
"""
self.number = int(number)
@property
def text(self) -> str:
"""
'CGLS 10 2 '
"""
return self.__repr__()
def __iter__(self) -> Generator:
for x in self.__repr__().split():
yield x
def _as_str(self) -> str:
return '{} {} {} {}'.format('CGLS' if self.cgls else 'L.S.', self._cycles,
self._nrf if self._nrf else '', self._nextra if self._nextra else '').strip()
def __repr__(self) -> str:
return self._as_str()
class SFACTable():
def __init__(self, shx: 'Shelxfile') -> None:
"""
Holds the information of SFAC instructions. Either with default values and only elements
SFAC elements
or as explicit scattering factor in the form of an exponential series, followed by real and
imaginary dispersion terms, linear absorption coefficient, covalent radius and atomic weight.
SFAC elements or SFAC E a1 b1 a2 b2 a3 b3 a4 b4 c f' f" mu r wt
"""
self.sfac_table: List[Union[Dict[str, str], Dict[str, int]]] = []
self.shx = shx
self.elements_list = []
def __iter__(self) -> Generator:
for x in self.sfac_table:
yield x['element'].capitalize()
def __repr__(self) -> str:
sftext = ''
elements = []
for sf in self.sfac_table:
if not self.is_exp(sf) and sf['element'].capitalize() not in elements:
elements.append(sf['element'].capitalize())
else:
if elements:
sftext = self._extend_sfac_text(elements, sftext)
elements = []
values = []
for x in ('element', 'a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4', 'c',
'fprime', 'fdprime', 'mu', 'r', 'wt'):
values.append(sf[x])
sftext = self._extend_sfac_text(elements, sftext)
if elements:
sftext = self._extend_sfac_text(elements, sftext)
return sftext[1:]
def _extend_sfac_text(self, elements: List[str], sftext: str) -> str:
sftext += f"\nSFAC {' '.join(elements)}"
return sftext
def __getitem__(self, index: int) -> str:
"""
Returns the n-th element in the sfac table, beginning with 1.
"""
if index == 0:
raise IndexError
if index < 0:
index = len(self.sfac_table) + index + 1
return self.sfac_table[index - 1]['element'].capitalize()
def parse_element_line(self, spline: List[str]) -> None:
"""
Adds a new SFAC card to the list of cards.
"""
if not ''.join(spline[1:]).isalpha(): # joining, because space is not alphabetical
# Excplicit with all values
sfdic = {}
for n, x in enumerate(['element', 'a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'b4', 'c',
'fprime', 'fdprime', 'mu', 'r', 'wt']):
if n == 0:
self.elements_list.append(spline[n + 1].upper())
try:
sfdic[x] = spline[n + 1]
except IndexError:
raise ParseNumError(debug=self.shx.debug, verbose=self.shx.verbose)
self.sfac_table.append(sfdic)
else:
# Just the elements
for x in spline[1:]:
self.elements_list.append(x.upper())
self.sfac_table.append({'element': x.upper()})
def has_element(self, element: str) -> bool:
return element.upper() in self.elements_list
@staticmethod
def is_exp(item: Dict[str, str]) -> bool:
return 'a1' in item
def add_element(self, element: str) -> None:
"""
Adds an element to the SFAC list.
"""
if self.has_element(element):
return
self.elements_list.append(element.upper())
self.sfac_table.append({'element': element.upper(), 'line_number': None})
self.shx.unit.add_number(1.0)
def remove_element(self, element: str):
del self.sfac_table[self.shx.elem2sfac(element.upper()) - 1]
del self.elements_list[self.elements_list.index(element.upper())]
class UNIT(Command):
values: List[Union[int, float]]
def __init__(self, shx, spline: List):
"""
UNIT n1 n2 ...
"""
super(UNIT, self).__init__(shx, spline)
self.values, _ = self._parse_line(spline)
def add_number(self, number: float):
self.values.append(number)
def __iter__(self):
yield [x for x in self.values]
def __repr__(self) -> str:
return "UNIT " + " ".join(["{:,g}".format(x) for x in self.values])
def __str__(self) -> str:
return self.__repr__()
def __setitem__(self, key: int, value: Union[int, float]) -> None:
self.values[key] = value
def __getitem__(self, item: int) -> Union[int, float]:
return self.values[item]
def __add__(self, other: Union[int, float]) -> None:
self.values.append(other)
class BASF(Command):
"""
BASF scale factors
BASF can occour in multiple lines.
"""
scale_factors: List[Union[int, float]]
def __init__(self, shx: 'Shelxfile', spline: List[str]) -> None:
super(BASF, self).__init__(shx, spline)
self.scale_factors, _ = self._parse_line(spline)
def __iter__(self):
yield self.scale_factors
class TWIN(Command):
def __init__(self, shx, spline: List):
"""
TWIN 3x3 matrix [-1 0 0 0 -1 0 0 0 -1] N[2]
+N -N m = |N|
m-1 to 2m-1
m-1 (2*abs(m)/2)-1
"""
super(TWIN, self).__init__(shx, spline)
self.matrix = [-1, 0, 0, 0, -1, 0, 0, 0, -1]
self.allowed_N = 2
self.n_value = 2
if len(spline) > 1:
p, _ = self._parse_line(spline, intnums=False)
if len(p) == 9:
self.matrix = p
elif len(p) == 10:
self.matrix = p[:9]
self.n_value = int(p[9])
else:
raise ParseNumError("*** Check TWIN instruction. ***")
m = abs(self.n_value) / 2
if self.n_value > 0:
self.allowed_N = abs(self.n_value) - 1
else:
self.allowed_N = (2 * m) - 1
class WGHT(Command):
def __init__(self, shx, spline: list):
"""
The weighting scheme is defined as follows:
w = q / [ σ²(Fo²) + (a*P)² + b*P + d + e*sin(θ)/$lambda; ]
WGHT a[0.1] b[0] c[0] d[0] e[0] f[.33333]
Usually only 'WGHT a b' is used
"""
super(WGHT, self).__init__(shx, spline)
self.shx = shx
self.a = 0.1
self.b = 0.0
self.c = 0.0
self.d = 0.0
self.e = 0.0
self.f = 0.33333
p, _ = self._parse_line(spline)
if len(p) > 0:
self.a = p[0]
if len(p) > 1:
self.b = p[1]
if len(p) > 2:
self.c = p[2]
if len(p) > 3:
self.d = p[3]
if len(p) > 4:
self.e = p[4]
if len(p) > 5:
self.f = p[5]
def _as_string(self):
wght = 'WGHT {} {}'.format(self.a, self.b)
# It is very unlikely that someone changes other parameter than a and b:
if (self.c + self.d + self.e + self.f) != 0.33333:
wght += ' {} {} {} {}'.format(self.c, self.d, self.e, self.f)
return wght
def difference(self) -> List[float]:
"""
Returns a list with the weight differences of the parameters a and b.
"""
try:
adiff = abs(self.shx.wght.a - self.shx.wght_suggested.a)
bdiff = abs(self.shx.wght.b - self.shx.wght_suggested.b)
except AttributeError:
print("No suggested weighting scheme found. Unable to proceed.")
return [0.0, 0.0]
return [round(adiff, 3), round(bdiff, 3)]
def __repr__(self) -> str:
return self._as_string()
def __str__(self) -> str:
return self._as_string()
|