File size: 94,735 Bytes
6aab6b3 | 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 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 | from collections.abc import Mapping
from dataclasses import dataclass
from dataclasses import replace, astuple
from collections import defaultdict
from pathlib import Path
import random
import re
from typing import Optional
from copy import deepcopy
import numpy as np
from rdkit import Chem, rdBase
from rdkit.Chem import AllChem
from rdkit.Chem.rdchem import Conformer, Mol
from rdkit.Chem.rdMolDescriptors import CalcNumHeavyAtoms
from scipy.spatial.distance import cdist
import yaml
from boltzgen.data import const
from boltzgen.data.mol import load_molecules
from boltzgen.data.parse.mmcif import parse_mmcif
from boltzgen.data.data import (
Atom,
Bond,
Chain,
ChainInfo,
Coords,
DesignInfo,
Ensemble,
Interface,
Record,
Residue,
Structure,
StructureInfo,
Target,
Token,
Tokenized,
)
from boltzgen.data.parse.pdb_parser import parse_pdb
from boltzgen.data.tokenize.tokenizer import TokenData
from dataclasses import replace
####################################################################################################
# DATACLASSES
####################################################################################################
@dataclass(frozen=True)
class ParsedAtom:
"""A parsed atom object."""
name: str
element: int
charge: int
coords: tuple[float, float, float]
conformer: tuple[float, float, float]
is_present: bool
chirality: int
@dataclass(frozen=True)
class ParsedBond:
"""A parsed bond object."""
atom_1: int
atom_2: int
type: int
@dataclass(frozen=True)
class ParsedRDKitBoundsConstraint:
"""A parsed RDKit bounds constraint object."""
atom_idxs: tuple[int, int]
is_bond: bool
is_angle: bool
upper_bound: float
lower_bound: float
@dataclass(frozen=True)
class ParsedChiralAtomConstraint:
"""A parsed chiral atom constraint object."""
atom_idxs: tuple[int, int, int, int]
is_reference: bool
is_r: bool
@dataclass(frozen=True)
class ParsedStereoBondConstraint:
"""A parsed stereo bond constraint object."""
atom_idxs: tuple[int, int, int, int]
is_check: bool
is_e: bool
@dataclass(frozen=True)
class ParsedPlanarBondConstraint:
"""A parsed planar bond constraint object."""
atom_idxs: tuple[int, int, int, int, int, int]
@dataclass(frozen=True)
class ParsedPlanarRing5Constraint:
"""A parsed planar bond constraint object."""
atom_idxs: tuple[int, int, int, int, int]
@dataclass(frozen=True)
class ParsedPlanarRing6Constraint:
"""A parsed planar bond constraint object."""
atom_idxs: tuple[int, int, int, int, int, int]
@dataclass(frozen=True)
class ParsedResidue:
"""A parsed residue object."""
name: str
type: int
idx: int
atoms: list[ParsedAtom]
bonds: list[ParsedBond]
orig_idx: Optional[int]
atom_center: int
atom_disto: int
is_standard: bool
is_present: bool
@dataclass(frozen=True)
class ParsedChain:
"""A parsed chain object."""
entity: str
type: int
residues: list[ParsedResidue]
res_design_mask: list[bool]
cyclic_period: int
sequence: Optional[str] = None
sampleidx_to_specidx: Optional[np.ndarray] = None
symmetric_group: int = 0
@dataclass(frozen=True)
class Alignment:
"""A parsed alignment object."""
query_st: int
query_en: int
template_st: int
template_en: int
####################################################################################################
# HELPERS
####################################################################################################
def compute_3d_conformer(mol: Mol, version: str = "v3") -> bool:
"""Generate 3D coordinates using EKTDG method.
Taken from `pdbeccdutils.core.component.Component`.
Parameters
----------
mol: Mol
The RDKit molecule to process
version: str, optional
The ETKDG version, defaults ot v3
Returns
-------
bool
Whether computation was successful.
"""
if version == "v3":
options = AllChem.ETKDGv3()
elif version == "v2":
options = AllChem.ETKDGv2()
else:
options = AllChem.ETKDGv2()
options.clearConfs = False
conf_id = -1
try:
conf_id = AllChem.EmbedMolecule(mol, options)
if conf_id == -1:
print(
f"WARNING: RDKit ETKDGv3 failed to generate a conformer for molecule "
f"{Chem.MolToSmiles(AllChem.RemoveHs(mol))}, so the program will start with random coordinates. "
f"Note that the performance of the model under this behaviour was not tested."
)
options.useRandomCoords = True
conf_id = AllChem.EmbedMolecule(mol, options)
AllChem.UFFOptimizeMolecule(mol, confId=conf_id, maxIters=1000)
except RuntimeError:
pass # Force field issue here
except ValueError:
pass # sanitization issue here
if conf_id != -1:
conformer = mol.GetConformer(conf_id)
conformer.SetProp("name", "Computed")
conformer.SetProp("coord_generation", f"ETKDG{version}")
return True
return False
def get_conformer(mol: Mol) -> Conformer:
"""Retrieve an rdkit object for a deemed conformer.
Inspired by `pdbeccdutils.core.component.Component`.
Parameters
----------
mol: Mol
The molecule to process.
Returns
-------
Conformer
The desired conformer, if any.
Raises
------
ValueError
If there are no conformers of the given tyoe.
"""
# Try using the computed conformer
for c in mol.GetConformers():
try:
if c.GetProp("name") == "Computed":
return c
except KeyError: # noqa: PERF203
pass
# Fallback to the ideal coordinates
for c in mol.GetConformers():
try:
if c.GetProp("name") == "Ideal":
return c
except KeyError: # noqa: PERF203
pass
# Fallback to boltz2 format
conf_ids = [int(conf.GetId()) for conf in mol.GetConformers()]
if len(conf_ids) > 0:
conf_id = conf_ids[0]
conformer = mol.GetConformer(conf_id)
return conformer
msg = "Conformer does not exist."
raise ValueError(msg)
def get_mol(ccd: str, mols: dict, moldir: str) -> Mol:
"""Get mol from CCD code.
Return mol with ccd from mols if it is in mols. Otherwise load it from moldir,
add it to mols, and return the mol.
"""
mol = mols.get(ccd)
if mol is None:
mol = load_molecules(moldir, [ccd])[ccd]
mols[ccd] = mol # cache for future calls
return mol
####################################################################################################
# PARSING
####################################################################################################
yaml_keys = [
"entities",
"protein",
"dna",
"rna",
"id",
"sequence",
"ligand",
"ccd",
"secondary_structure",
"file",
"path",
"msa",
"include",
"chain",
"include_proximity",
"res_index",
"radius",
"binding_types",
"binding",
"not_binding",
"structure_groups",
"group",
"visibility",
"design",
"loop",
"helix",
"sheet",
"design_insertions",
"insertion",
"num_residues",
"fuse",
"exclude",
"smiles",
"cyclic",
"bonds",
"bond",
"atom1",
"atom2",
"bondtype",
"structure_group",
"constraints",
"total_len",
"min",
"max",
"reset_res_index",
"not_design",
"leaving_atoms",
"atom",
"use_assembly",
"symmetric_group",
# Per-residue amino acid constraints
"residue_constraints",
"position",
"allowed",
"disallowed",
]
def parse_ccd_residue(
name: str,
ref_mol: Mol,
res_idx: int,
) -> Optional[ParsedResidue]:
"""Parse an MMCIF ligand.
First tries to get the SMILES string from the RCSB.
Then, tries to infer atom ordering using RDKit.
Parameters
----------
name: str
The name of the molecule to parse.
ref_mol: Mol
The reference molecule to parse.
res_idx : int
The residue index.
Returns
-------
ParsedResidue, optional
The output ParsedResidue, if successful.
"""
unk_chirality = const.chirality_type_ids[const.unk_chirality_type]
# Check if this is a single heavy atom CCD residue
if CalcNumHeavyAtoms(ref_mol) == 1:
# Remove hydrogens
ref_mol = AllChem.RemoveHs(ref_mol, sanitize=False)
pos = (0, 0, 0)
ref_atom = ref_mol.GetAtoms()[0]
chirality_type = const.chirality_type_ids.get(
str(ref_atom.GetChiralTag()), unk_chirality
)
atom = ParsedAtom(
name=ref_atom.GetProp("name"),
element=ref_atom.GetAtomicNum(),
charge=ref_atom.GetFormalCharge(),
coords=pos,
conformer=(0, 0, 0),
is_present=True,
chirality=chirality_type,
)
unk_prot_id = const.unk_token_ids["PROTEIN"]
residue = ParsedResidue(
name=name,
type=unk_prot_id,
atoms=[atom],
bonds=[],
idx=res_idx,
orig_idx=None,
atom_center=0, # Placeholder, no center
atom_disto=0, # Placeholder, no center
is_standard=False,
is_present=True,
)
return residue
# Get reference conformer coordinates
conformer = get_conformer(ref_mol)
# Parse each atom in order of the reference mol
atoms = []
atom_idx = 0
idx_map = {} # Used for bonds later
for i, atom in enumerate(ref_mol.GetAtoms()):
# Ignore Hydrogen atoms
if atom.GetAtomicNum() == 1:
continue
# Get atom name, charge, element and reference coordinates
atom_name = atom.GetProp("name")
charge = atom.GetFormalCharge()
element = atom.GetAtomicNum()
ref_coords = conformer.GetAtomPosition(atom.GetIdx())
ref_coords = (ref_coords.x, ref_coords.y, ref_coords.z)
chirality_type = const.chirality_type_ids.get(
str(atom.GetChiralTag()), unk_chirality
)
# Get PDB coordinates, if any
coords = (0, 0, 0)
atom_is_present = True
# Add atom to list
atoms.append(
ParsedAtom(
name=atom_name,
element=element,
charge=charge,
coords=coords,
conformer=ref_coords,
is_present=atom_is_present,
chirality=chirality_type,
)
)
idx_map[i] = atom_idx
atom_idx += 1 # noqa: SIM113
# Load bonds
bonds = []
unk_bond = const.bond_type_ids[const.unk_bond_type]
for bond in ref_mol.GetBonds():
idx_1 = bond.GetBeginAtomIdx()
idx_2 = bond.GetEndAtomIdx()
# Skip bonds with atoms ignored
if (idx_1 not in idx_map) or (idx_2 not in idx_map):
continue
idx_1 = idx_map[idx_1]
idx_2 = idx_map[idx_2]
start = min(idx_1, idx_2)
end = max(idx_1, idx_2)
bond_type = bond.GetBondType().name
bond_type = const.bond_type_ids.get(bond_type, unk_bond)
bonds.append(ParsedBond(start, end, bond_type))
unk_prot_id = const.unk_token_ids["PROTEIN"]
return ParsedResidue(
name=name,
type=unk_prot_id,
atoms=atoms,
bonds=bonds,
idx=res_idx,
atom_center=0,
atom_disto=0,
orig_idx=None,
is_standard=False,
is_present=True,
)
def parse_polymer(
sequence: list[str],
raw_sequence: str,
entity: str,
chain_type: str,
components: dict[str, Mol],
cyclic: bool,
mol_dir: Path,
symmetric_group: int = 0,
) -> Optional[ParsedChain]:
"""Process a sequence into a chain object.
Performs alignment of the full sequence to the polymer
residues. Loads coordinates and masks for the atoms in
the polymer, following the ordering in const.atom_order.
Parameters
----------
sequence : list[str]
The full sequence of the polymer.
entity : str
The entity id.
entity_type : str
The entity type.
components : dict[str, Mol]
The preprocessed PDB components dictionary.
Returns
-------
ParsedChain, optional
The output chain, if successful.
Raises
------
ValueError
If the alignment fails.
"""
ref_res = set(const.tokens)
unk_chirality = const.chirality_type_ids[const.unk_chirality_type]
# Make sequence and distinguish between design and non-design
seq_processed = []
res_design_mask = []
sampleidx_to_specidx = []
count = 0
for token in sequence:
if isinstance(token, str):
seq_processed.append(token)
res_design_mask.append(False)
sampleidx_to_specidx.append(count)
count += 1
elif isinstance(token, tuple):
if len(token) == 1:
num = start = token[0]
sampleidx_to_specidx.extend(range(count, count + num))
elif len(token) == 2:
start, end = token
num = np.random.randint(start, end + 1)
mapping = list(range(count, count + start))
mapping += [count + start - 1] * (num - start)
sampleidx_to_specidx.extend(mapping)
res_design_mask.extend([True] * num)
seq_processed.extend(["GLY"] * num)
count += start
else:
raise ValueError("Token must be tuple of int or string")
sampleidx_to_specidx = np.array(sampleidx_to_specidx)
# Get coordinates and masks
parsed = []
for res_idx, res_name in enumerate(seq_processed):
# Check if modified residue
# Map MSE to MET
res_corrected = res_name if res_name != "MSE" else "MET"
# Handle non-standard residues
if res_corrected not in ref_res:
ref_mol = get_mol(res_corrected, components, mol_dir)
residue = parse_ccd_residue(
name=res_corrected,
ref_mol=ref_mol,
res_idx=res_idx,
)
parsed.append(residue)
continue
# Load ref residue
ref_mol = get_mol(res_corrected, components, mol_dir)
ref_mol = AllChem.RemoveHs(ref_mol, sanitize=False)
ref_conformer = get_conformer(ref_mol)
# Only use reference atoms set in constants
ref_name_to_atom = {a.GetProp("name"): a for a in ref_mol.GetAtoms()}
ref_atoms = [ref_name_to_atom[a] for a in const.ref_atoms[res_corrected]]
# Iterate, always in the same order
atoms: list[ParsedAtom] = []
for ref_atom in ref_atoms:
# Get atom name
atom_name = ref_atom.GetProp("name")
idx = ref_atom.GetIdx()
# Get conformer coordinates
ref_coords = ref_conformer.GetAtomPosition(idx)
ref_coords = (ref_coords.x, ref_coords.y, ref_coords.z)
# Set 0 coordinate
atom_is_present = True
coords = (0, 0, 0)
# Add atom to list
atoms.append(
ParsedAtom(
name=atom_name,
element=ref_atom.GetAtomicNum(),
charge=ref_atom.GetFormalCharge(),
coords=coords,
conformer=ref_coords,
is_present=atom_is_present,
chirality=const.chirality_type_ids.get(
str(ref_atom.GetChiralTag()), unk_chirality
),
)
)
atom_center = const.res_to_center_atom_id[res_corrected]
atom_disto = const.res_to_disto_atom_id[res_corrected]
parsed.append(
ParsedResidue(
name=res_corrected,
type=const.token_ids[res_corrected],
atoms=atoms,
bonds=[],
idx=res_idx,
atom_center=atom_center,
atom_disto=atom_disto,
is_standard=True,
is_present=True,
orig_idx=None,
)
)
if cyclic:
cyclic_period = len(seq_processed)
else:
cyclic_period = 0
# Return polymer object
return ParsedChain(
entity=entity,
residues=parsed,
res_design_mask=res_design_mask,
type=chain_type,
cyclic_period=cyclic_period,
sequence=raw_sequence,
sampleidx_to_specidx=sampleidx_to_specidx,
symmetric_group=symmetric_group,
)
# Define helper
def parse_range(ranges, c_start=0, c_end=None):
ranges = str(ranges)
if "," in ranges:
spec_list = ranges.split(",")
else:
spec_list = [ranges]
indices = []
for spec in spec_list:
if re.fullmatch(r"\d+", spec):
# Single number. Convert it from 1 indexed to 0 indexed.
start = int(spec) - 1
end = int(spec) - 1
indices.append(c_start + start)
elif re.fullmatch(r"\d+..\d+", spec):
# Range with start and end. Convert the start from 1 indexed to 0 indexed. Leave the end untouched because the specification is inclusive (+1) but 1 indexed (-1).
start, end = map(int, spec.split(".."))
start -= 1
indices += list(range(c_start + start, c_start + end))
elif re.fullmatch(r"..\d+", spec):
# Range that is inclusive of the specified end (which is specified in a 1 indexed fashion).
end = int(spec.replace("..", ""))
start = 0
indices += list(range(c_start, c_start + end))
elif re.fullmatch(r"\d+..", spec):
assert c_end is not None
# Range that is inclusive of the specified start (which is specified in a 1 indexed fashion).
start = int(spec.replace("..", ""))
start -= 1
end = c_end - c_start
indices += list(range(c_start + start, c_end))
else:
msg = f"Malformed residue range specification '{spec}' in '{ranges}'."
raise ValueError(msg)
if start < 0:
msg = f"There is a 0 in the specified range(s) {ranges}. Residue indices are 1 indexed."
raise ValueError(msg)
if c_end is not None and end > c_end - c_start:
msg = f"Specified end {ranges} is higher than the length of the chain."
raise ValueError(msg)
return indices
def _normalize_aa_spec(aa_spec) -> list[str]:
"""Normalize amino acid specification to a list of individual codes.
Supports both BoltzGen conventions:
- String format: "AGS" (concatenated 1-letter codes, consistent with sequence/binding_types)
- List format: [A, G, S] or [ALA, GLY, SER]
Parameters
----------
aa_spec : str or list
Amino acid specification in string or list format
Returns
-------
list[str]
List of individual amino acid codes
"""
if isinstance(aa_spec, str):
# String format: "AGS" -> ["A", "G", "S"]
# Handle both "AGS" and "ALA" (single 3-letter code)
aa_spec = aa_spec.strip().upper()
if len(aa_spec) <= 3 and aa_spec.isalpha():
# Could be single 3-letter code like "ALA" or 1-3 single letters like "A", "AG", "AGS"
# Check if it's a valid 3-letter code
if len(aa_spec) == 3 and aa_spec in ["ALA", "ARG", "ASN", "ASP", "CYS", "GLN", "GLU", "GLY", "HIS", "ILE", "LEU", "LYS", "MET", "PHE", "PRO", "SER", "THR", "TRP", "TYR", "VAL"]:
return [aa_spec]
# Otherwise treat as concatenated 1-letter codes
return list(aa_spec)
else:
# Longer string: treat as concatenated 1-letter codes
return list(aa_spec)
elif isinstance(aa_spec, list):
# List format: [A, G, S] or [ALA, GLY, SER]
return [str(x).strip().upper() for x in aa_spec]
else:
raise ValueError(f"Invalid amino acid specification: {aa_spec}")
def _convert_aa_names_to_indices(
aa_names: list,
canonical_tokens: list[str],
prot_letter_to_token: dict[str, str],
) -> list[int]:
"""Convert amino acid names (1-letter or 3-letter) to canonical token indices.
Parameters
----------
aa_names : list
List of amino acid names (1-letter like 'A' or 3-letter like 'ALA')
canonical_tokens : list[str]
List of canonical 3-letter amino acid codes
prot_letter_to_token : dict[str, str]
Mapping from 1-letter to 3-letter codes
Returns
-------
list[int]
List of indices into canonical_tokens
"""
indices = []
for name in aa_names:
name = str(name).strip().upper()
# Convert 1-letter to 3-letter if needed
if len(name) == 1:
if name not in prot_letter_to_token:
raise ValueError(f"Unknown amino acid code: {name}")
name = prot_letter_to_token[name]
# Find index in canonical_tokens
if name not in canonical_tokens:
raise ValueError(f"Unknown amino acid: {name}")
indices.append(canonical_tokens.index(name))
return indices
def parse_residue_constraints(
constraints_spec: list,
chain_length: int,
canonical_tokens: list[str],
prot_letter_to_token: dict[str, str],
) -> np.ndarray:
"""Parse residue_constraints into a per-residue constraint mask.
Parameters
----------
constraints_spec : list
List of constraint specifications from YAML
chain_length : int
Length of the chain (number of residues)
canonical_tokens : list[str]
List of canonical 3-letter amino acid codes (20 AAs)
prot_letter_to_token : dict[str, str]
Mapping from 1-letter to 3-letter codes
Returns
-------
np.ndarray
Shape (chain_length, 20) where:
- 0.0 means allowed
- 1.0 means disallowed (will be converted to -inf logit bias in model)
Notes
-----
Overlapping constraints use **intersection** semantics: if multiple
constraints cover the same position, only amino acids allowed by ALL
of them survive. For example, ``allowed: AG`` at pos 1..10 followed
by ``allowed: GS`` at pos 5..15 results in only G being allowed at
positions 5-10 (the intersection of {A,G} and {G,S}).
"""
num_aa = len(canonical_tokens) # Should be 20
constraint_mask = np.zeros((chain_length, num_aa), dtype=np.float32)
for constraint in constraints_spec:
# Parse position(s)
position_spec = constraint.get("position")
if position_spec is None:
raise ValueError("residue_constraints: 'position' is required")
# Use parse_range to handle single positions and ranges (1-indexed)
positions = parse_range(str(position_spec), c_start=0, c_end=chain_length)
# Validate positions are within bounds
for pos in positions:
if pos < 0 or pos >= chain_length:
raise ValueError(
f"Position {pos + 1} is out of bounds for chain of length {chain_length}"
)
# Parse amino acid specification
allowed = constraint.get("allowed", None)
disallowed = constraint.get("disallowed", None)
# Validate: cannot have both allowed and disallowed
if allowed is not None and disallowed is not None:
raise ValueError(
f"Position {position_spec}: cannot specify both 'allowed' and 'disallowed'"
)
if allowed is None and disallowed is None:
raise ValueError(
f"Position {position_spec}: must specify either 'allowed' or 'disallowed'"
)
if allowed is not None:
# Whitelist mode: block all except specified AAs
# Uses np.maximum to accumulate with existing constraints (intersection semantics):
# if a position already has constraints, only AAs allowed by BOTH survive.
aa_list = _normalize_aa_spec(allowed)
if len(aa_list) == 0:
raise ValueError(
f"Position {position_spec}: 'allowed' cannot be empty"
)
aa_indices = _convert_aa_names_to_indices(
aa_list, canonical_tokens, prot_letter_to_token
)
new_block = np.ones(num_aa, dtype=np.float32)
for idx in aa_indices:
new_block[idx] = 0.0
for pos in positions:
constraint_mask[pos, :] = np.maximum(constraint_mask[pos, :], new_block)
elif disallowed is not None:
# Blacklist mode: only block specified
# Normalize input: supports both "CM" (string) and [C, M] (list)
aa_list = _normalize_aa_spec(disallowed)
aa_indices = _convert_aa_names_to_indices(
aa_list, canonical_tokens, prot_letter_to_token
)
for pos in positions:
for idx in aa_indices:
constraint_mask[pos, idx] = 1.0 # Block specified
return constraint_mask
def parse_entity(item, mols, mol_dir, ligand_id, is_msa_custom, is_msa_auto):
extra_mols: dict[str, Mol] = {}
parsed_chains: dict[str, ParsedChain] = {}
res_bind_type: list[int] = []
ss_type: list[int] = []
chain_to_msa: dict[str, str] = {}
# Get entity type and sequence
entity_type = next(iter(item.keys())).lower()
# Ensure all the items share the same msa
msa = -1
if entity_type == "protein":
designed = bool(re.search(r"\d", str(item[entity_type]["sequence"])))
if designed:
# Get the msa, default to -1, meaning no msa.
msa = item[entity_type].get("msa", -1)
if (msa is None) or (msa == ""):
msa = -1
else:
# Get the msa, default to 0, meaning auto-generated
msa = item[entity_type].get("msa", 0)
if (msa is None) or (msa == ""):
msa = 0
# Check if all MSAs are the same within the same entity
item_msa = item[entity_type].get("msa", 0)
if (item_msa is None) or (item_msa == ""):
item_msa = 0
if item_msa != msa and not designed:
msg = "All proteins with the same sequence must share the same MSA!"
raise ValueError(msg)
# Set the MSA, warn if passed in single-sequence mode
if msa == "empty":
msa = -1
msg = (
"Found explicit empty MSA for some proteins, will run "
"these in single sequence mode."
)
print(msg)
if msa not in (0, -1):
is_msa_custom = True
elif msa == 0:
is_msa_auto = True
# Parse a polymer
if entity_type in {"protein", "dna", "rna"}:
# Get token map
if entity_type == "rna":
token_map = const.rna_letter_to_token
elif entity_type == "dna":
token_map = const.dna_letter_to_token
elif entity_type == "protein":
token_map = const.prot_letter_to_token
else:
msg = f"Unknown polymer type: {entity_type}"
raise ValueError(msg)
# Get polymer info
chain_type = const.chain_type_ids[entity_type.upper()]
unk_token = const.unk_token[entity_type.upper()]
# Extract sequence
raw_seq = str(item[entity_type]["sequence"])
# Convert sequence to standard and design tokens
seq = []
parts = re.split(r",\s*", raw_seq) # split by comma (optional whitespace after)
for part in parts:
# If a part is empty (e.g., from "1,,2"), skip it.
if not part:
continue
tokens = re.findall(r"\d+\.\.\d+|\d+|[a-zA-Z]", part)
for token in tokens:
if re.fullmatch(r"\d+\.\.\d+", token): # Case 2: range
start, end = map(int, token.split(".."))
seq.append((start, end))
elif re.fullmatch(r"\d+", token): # Case 1: single number
seq.append((int(token),))
else: # Case 3: characters
seq.extend([token_map.get(c, unk_token) for c in token])
# Apply modifications
for mod in item[entity_type].get("modifications", []):
code = mod["ccd"].upper()
idx = mod["position"] - 1 # 1-indexed
seq[idx] = code
cyclic = item[entity_type].get("cyclic", False)
symmetric_group = item[entity_type].get("symmetric_group", 0)
if symmetric_group is None:
symmetric_group = 0
# Parse a polymer
parsed_chain = parse_polymer(
sequence=seq,
raw_sequence=raw_seq,
entity=0,
chain_type=chain_type,
components=mols,
cyclic=cyclic,
mol_dir=mol_dir,
symmetric_group=symmetric_group,
)
# Parse a non-polymer
elif (entity_type == "ligand") and "ccd" in (item[entity_type]):
symmetric_group = item[entity_type].get("symmetric_group", 0)
if symmetric_group is None:
symmetric_group = 0
seq = item[entity_type]["ccd"]
if isinstance(seq, str):
seq = [seq]
residues = []
for res_idx, code in enumerate(seq):
code = code.upper()
# Get mol
ref_mol = get_mol(code, mols, mol_dir)
# Parse residue
residue = parse_ccd_residue(
name=code,
ref_mol=ref_mol,
res_idx=res_idx,
)
residues.append(residue)
# Create multi ligand chain
parsed_chain = ParsedChain(
entity=0,
residues=residues,
res_design_mask=[False] * len(residues),
type=const.chain_type_ids["NONPOLYMER"],
cyclic_period=0,
sequence=None,
symmetric_group=symmetric_group,
)
assert not item[entity_type].get("cyclic", False), (
"Cyclic flag is not supported for ligands"
)
elif (entity_type == "ligand") and ("smiles" in item[entity_type]):
symmetric_group = item[entity_type].get("symmetric_group", 0)
if symmetric_group is None:
symmetric_group = 0
seq = item[entity_type]["smiles"]
mol = AllChem.MolFromSmiles(seq)
mol = AllChem.AddHs(mol)
element_counts = defaultdict(int)
for i, atom in enumerate(mol.GetAtoms()):
symbol = atom.GetSymbol()
element_counts[symbol] += 1
atom_name = f"{symbol}{element_counts[symbol]}"
if len(atom_name) > 4:
raise ValueError(
f"{seq} has an atom with a name longer than 4 characters: {atom_name}"
)
atom.SetProp("name", atom_name)
success = compute_3d_conformer(mol)
if not success:
msg = f"Failed to compute 3D conformer for {seq}"
raise ValueError(msg)
mol_no_h = AllChem.RemoveHs(mol)
extra_mols[f"LIG{ligand_id}"] = mol_no_h
residue = parse_ccd_residue(
name=f"LIG{ligand_id}",
ref_mol=mol,
res_idx=0,
)
ligand_id += 1
parsed_chain = ParsedChain(
entity=0,
residues=[residue],
res_design_mask=[False],
type=const.chain_type_ids["NONPOLYMER"],
cyclic_period=0,
sequence=None,
symmetric_group=symmetric_group,
)
assert not item[entity_type].get("cyclic", False), (
"Cyclic flag is not supported for ligands"
)
elif entity_type == "file":
pass
else:
msg = f"Invalid entity type: {entity_type}"
raise ValueError(msg)
# Parse binding site specification
num = len(parsed_chain.residues)
entry = item[entity_type]
binding_spec = entry.get("binding_types", None)
ids = item[entity_type]["id"]
num_chains = 1 if isinstance(ids, str) else len(ids)
for _ in range(num_chains):
if binding_spec is not None:
if isinstance(binding_spec, str):
for char in binding_spec:
if char.lower() == "u":
res_bind_type.append(const.binding_type_ids["UNSPECIFIED"])
elif char.lower() == "b":
res_bind_type.append(const.binding_type_ids["BINDING"])
elif char.lower() == "n":
res_bind_type.append(const.binding_type_ids["NOT_BINDING"])
else:
msg = f"Invalid binding_type '{char}' in: {binding_spec}"
raise ValueError(msg)
# Fill missing specification with unspecified
if len(binding_spec) < num:
num_missing = num - len(binding_spec)
res_bind_type.extend(
[const.binding_type_ids["UNSPECIFIED"]] * num_missing
)
if len(binding_spec) > num:
msg = f"Misspecified bingin_types {binding_spec} which is shorter than the sequence."
raise ValueError(msg)
else:
types = np.ones(num) * const.binding_type_ids["UNSPECIFIED"]
if "binding" in binding_spec:
indices = parse_range(binding_spec["binding"], 0, num)
types[indices] = const.binding_type_ids["BINDING"]
if "not_binding" in binding_spec:
indices = parse_range(binding_spec["not_binding"], 0, num)
types[indices] = const.binding_type_ids["NOT_BINDING"]
res_bind_type.extend(types.tolist())
else:
res_bind_type.extend([const.binding_type_ids["UNSPECIFIED"]] * num)
# Parse ss conditioning specification
entry = item[entity_type]
ss_spec = entry.get("secondary_structure", None)
ids = item[entity_type]["id"]
num_chains = 1 if isinstance(ids, str) else len(ids)
for _ in range(num_chains):
if ss_spec is not None:
if isinstance(ss_spec, str):
for char in ss_spec:
if char.lower() == "u":
ss_type.append(const.ss_type_ids["UNSPECIFIED"])
elif char.lower() == "l":
ss_type.append(const.ss_type_ids["LOOP"])
elif char.lower() == "h":
ss_type.append(const.ss_type_ids["HELIX"])
elif char.lower() == "s":
ss_type.append(const.ss_type_ids["SHEET"])
else:
msg = f"Invalid secondary_structure '{char}' in: {ss_spec}"
raise ValueError(msg)
# Fill missing specification with unspecified
if len(ss_spec) < num:
num_missing = num - len(ss_spec)
ss_type.extend([const.ss_type_ids["UNSPECIFIED"]] * num_missing)
if len(ss_spec) > num:
msg = f"Misspecified secondary_structure {ss_spec} which is shorter than the sequence."
raise ValueError(msg)
else:
types = np.ones(num) * const.ss_type_ids["UNSPECIFIED"]
if "loop" in ss_spec:
indices = parse_range(ss_spec["loop"], 0, num)
types[indices] = const.ss_type_ids["LOOP"]
if "helix" in ss_spec:
indices = parse_range(ss_spec["helix"], 0, num)
types[indices] = const.ss_type_ids["HELIX"]
if "sheet" in ss_spec:
indices = parse_range(ss_spec["sheet"], 0, num)
types[indices] = const.ss_type_ids["SHEET"]
ss_type.extend(types.tolist())
else:
ss_type.extend([const.ss_type_ids["UNSPECIFIED"]] * num)
# Parse residue_constraints for per-residue amino acid restrictions
entry = item[entity_type]
constraints_spec = entry.get("residue_constraints", None)
ids = item[entity_type]["id"]
num_chains = 1 if isinstance(ids, str) else len(ids)
res_aa_constraint_list = []
for _ in range(num_chains):
if constraints_spec is not None and entity_type == "protein":
res_aa_constraints = parse_residue_constraints(
constraints_spec,
chain_length=num,
canonical_tokens=const.canonical_tokens,
prot_letter_to_token=const.prot_letter_to_token,
)
else:
# No constraints: all 20 amino acids allowed (zeros)
res_aa_constraints = np.zeros((num, len(const.canonical_tokens)), dtype=np.float32)
res_aa_constraint_list.append(res_aa_constraints)
# Concatenate constraint masks for all chain copies
if res_aa_constraint_list:
res_aa_constraint_mask = np.concatenate(res_aa_constraint_list, axis=0)
else:
res_aa_constraint_mask = np.zeros((0, len(const.canonical_tokens)), dtype=np.float32)
# Add as many parsed_chains as provided ids
if entity_type in {"protein", "dna", "rna", "ligand"}:
ids = item[entity_type]["id"]
if isinstance(ids, str):
ids = [ids]
for chain_name in ids:
parsed_chains[chain_name] = parsed_chain
chain_to_msa[chain_name] = msa
fuse = item[entity_type].get("fuse", None)
fuse_info = {}
if fuse is not None:
fuse_info["target_id"] = fuse
fuse_info["fuse"] = True
else:
fuse_info["fuse"] = False
if is_msa_custom and is_msa_auto:
msg = "Cannot mix custom and auto-generated MSAs in the same input!"
raise ValueError(msg)
return (
extra_mols,
parsed_chains,
res_bind_type,
ss_type,
chain_to_msa,
fuse_info,
ligand_id,
res_aa_constraint_mask,
)
def parse_redesign_schema(
schema: dict,
tokenized: Tokenized,
) -> Target:
"""parse a redesign schema"""
key = next(iter(schema["restrictions"].keys()))
if key not in ["not_design", "design"]:
msg = f"Invalid key: {key}"
raise ValueError(msg)
new_design_mask = [False] * len(tokenized.tokens)
for item in schema["restrictions"][key]:
# initialize binders to be all designed or num designed
for token in tokenized.tokens:
if (
tokenized.structure.chains[token["asym_id"]]["name"]
== item["chain"]["binder"]
):
new_design_mask[token["token_idx"]] = key == "not_design"
for item in schema["restrictions"][key]:
id = item["chain"]["id"]
c_start = tokenized.structure.chains[
np.where(tokenized.structure.chains["name"] == id)
][0]["res_idx"].item()
c_end = (
c_start
+ tokenized.structure.chains[
np.where(tokenized.structure.chains["name"] == id)
][0]["res_num"].item()
)
indicies = parse_range(item["chain"]["res_index"], c_start, c_end)
token_indices = []
for idx in range(len(tokenized.tokens)):
if tokenized.token_to_res[idx] in indicies:
token_indices.append(idx)
radius = item["chain"]["within_proximity"]
undesign_idx = []
for token in tokenized.tokens:
for idx in token_indices:
if (
cdist(
np.array([token["center_coords"]]),
np.array([tokenized.tokens["center_coords"][idx]]),
)[0][0]
< radius
and tokenized.structure.chains[token["asym_id"]]["name"]
== item["chain"]["binder"]
):
undesign_idx.append(token["token_idx"])
new_design_mask[token["token_idx"]] = key == "design"
new_design_mask = np.array(new_design_mask, dtype=bool)
return new_design_mask
def parse_redesign_yaml(
path: Path,
tokenized: Tokenized,
) -> Target:
"""parse a design mask override yaml file"""
with path.open("r") as file:
if path.suffix == ".yaml":
data = yaml.safe_load(file)
else:
raise ValueError(f"Unsupported file type: {str(path)}")
target = parse_redesign_schema(data, tokenized)
return target
####################################################################################################
# YAML PARSER WRAPPER (with caches)
####################################################################################################
class YamlDesignParser:
def __init__(
self,
mol_dir: Path | str,
) -> None:
self.mol_dir = Path(mol_dir)
self._struct_cache: dict[tuple[Path, bool], Structure] = {}
self._once_keys: set[str] = set()
def parse_yaml(
self,
path: Path,
mols: dict[str, Mol],
mol_dir: Path,
) -> Target:
"""Parse a Boltz input yaml / json."""
with path.open("r") as file:
if path.suffix == ".yaml":
data = yaml.safe_load(file)
elif path.suffix == ".pdb":
data = parse_pdb(file)
else:
raise ValueError(f"Unsupported file type: {str(path)}")
name = path.stem
target = self.parse_boltzgen_schema(
name, data, mols, mol_dir, base_file_path=path.parent
)
return target
def log_once(self, msg: str):
"""Print *msg* exactly once per *training job* (only on rank-0 if DDP)."""
try:
import torch.distributed as _dist
if _dist.is_available() and _dist.is_initialized():
is_rank0 = _dist.get_rank() == 0
else:
is_rank0 = True
except Exception:
is_rank0 = True
if is_rank0 and msg not in self._once_keys:
print(msg)
self._once_keys.add(msg)
def parse_boltzgen_schema(
self,
name: str,
schema: dict,
mols: Mapping[str, Mol],
mol_dir: Optional[Path] = None,
base_file_path: Optional[Path] = None,
) -> Target:
"""Parse a Boltz input yaml / json.
See examples/design_spec_refactored.yaml for the schema.
"""
# Check valididty of yaml file
for name in ["res_idx", "residue_idx", "residue_index"]:
if name in str(schema):
raise ValueError(f"Found {name} in yaml. Did you mean 'res_index'?")
invalid_keys = set()
def recursive_check(data):
if isinstance(data, dict):
for key, value in data.items():
if key not in yaml_keys:
invalid_keys.add(key)
recursive_check(value)
elif isinstance(data, list):
for item in data:
recursive_check(item)
recursive_check(schema)
if len(invalid_keys) > 0:
msg = f"Found invalid keys in yaml file: {invalid_keys}.\nValid keys are: {yaml_keys}"
raise ValueError(msg)
# Disable rdkit warnings
blocker = rdBase.BlockLogs() # noqa: F841
# First group items that have the same type, sequence and modifications
items_to_group = {}
file_path_count = {}
items_list = []
for item in schema["entities"]:
# Get entity type
entity_type = next(iter(item.keys())).lower()
if entity_type not in {
"protein",
"dna",
"rna",
"ligand",
"file",
}:
msg = f"Invalid entity type: {entity_type}"
raise ValueError(msg)
# Get sequence
if entity_type in {"protein", "dna", "rna"}:
seq = str(item[entity_type]["sequence"])
elif entity_type == "ligand":
assert "smiles" in item[entity_type] or "ccd" in item[entity_type]
assert (
"smiles" not in item[entity_type] or "ccd" not in item[entity_type]
)
if "smiles" in item[entity_type]:
seq = str(item[entity_type]["smiles"])
else:
seq = str(item[entity_type]["ccd"])
elif entity_type == "file":
identifier = str(item["file"]["path"])
file_path_count[identifier] = file_path_count.get(identifier, 0) + 1
seq = identifier + str(file_path_count[identifier])
items_list.append(item)
items_to_group.setdefault((entity_type, seq), []).append(item)
# Create tables
protein_chains = set()
covalents = []
constraints = schema.get("constraints", [[]])
if "total_len" in constraints[0]:
total_len = constraints[0]["total_len"]
if "min" in total_len:
min_len = total_len["min"]
if "max" in total_len:
max_len = total_len["max"]
# Convert parsed chains to tables
while True:
data = Structure.empty_protein(0)
chain_to_idx = {}
# Keep a mapping of (chain_name, residue_idx, atom_name) to atom_idx
atom_idx_map = {}
local_atom_idx_map = {}
total_renaming = {}
extra_mols = {}
res_bind_type = []
ss_type = []
chain_to_msa = {}
is_msa_custom = False
is_msa_auto = False
all_parsed_chains: dict[str, ParsedChain] = {}
ligand_id = 1
structure_groups = np.array([], dtype=np.int32)
res_design_mask = np.array([], dtype=bool)
res_bind_type = np.array([], dtype=np.int32)
ss_type = np.array([], dtype=np.int32)
res_aa_constraint_mask = np.zeros((0, len(const.canonical_tokens)), dtype=np.float32)
chain_to_msa = {}
global_asym_id = 0
for item in items_list:
sym_id = 0
entity_type = next(iter(item.keys())).lower()
if entity_type != "file":
atom_idx = 0
res_idx = 0
asym_id = 0
atom_data = []
bond_data = []
res_data = []
chain_data = []
new_res_design_mask = []
(
new_extra_mols,
parsed_chains,
new_res_bind_type,
new_ss_type,
entity_chain_to_msa,
fuse_info,
ligand_id,
new_res_aa_constraint_mask,
) = parse_entity(
item, mols, mol_dir, ligand_id, is_msa_custom, is_msa_auto
)
all_parsed_chains.update(parsed_chains)
extra_mols.update(new_extra_mols)
res_bind_type = np.concatenate([res_bind_type, new_res_bind_type])
ss_type = np.concatenate([ss_type, new_ss_type])
res_aa_constraint_mask = np.concatenate([res_aa_constraint_mask, new_res_aa_constraint_mask], axis=0)
for asym_id, (chain_name, chain) in enumerate(
parsed_chains.items()
):
# Compute number of atoms and residues
res_num = len(chain.residues)
atom_num = sum(len(res.atoms) for res in chain.residues)
# Extend res_design_mask
new_res_design_mask.extend(chain.res_design_mask)
# Save protein chains for later
if chain.type == const.chain_type_ids["PROTEIN"]:
protein_chains.add(chain_name)
# Find all copies of this chain in the assembly
chain_data.append(
(
chain_name,
chain.type,
0,
sym_id,
asym_id,
atom_idx,
atom_num,
res_idx,
res_num,
chain.cyclic_period,
chain.symmetric_group,
)
)
chain_to_idx[chain_name] = asym_id
sym_id += 1
# Add residue, atom, bond, data
for res in chain.residues:
atom_center = atom_idx + res.atom_center
atom_disto = atom_idx + res.atom_disto
res_data.append(
(
res.name,
res.type,
res.idx,
atom_idx,
len(res.atoms),
atom_center,
atom_disto,
res.is_standard,
res.is_present,
)
)
for bond in res.bonds:
atom_1 = atom_idx + bond.atom_1
atom_2 = atom_idx + bond.atom_2
bond_data.append(
(
asym_id,
asym_id,
res_idx,
res_idx,
atom_1,
atom_2,
bond.type,
)
)
for atom in res.atoms:
# Add atom to map
atom_idx_map[(chain_name, res.idx, atom.name)] = (
global_asym_id,
data.residues.shape[0]
+ asym_id * res_num
+ res_idx,
data.atoms.shape[0] + asym_id * atom_num + atom_idx,
)
local_atom_idx_map[(chain_name, res.idx, atom.name)] = (
asym_id,
asym_id * res_num + res_idx,
asym_id * atom_num + atom_idx,
)
# Add atom to data
atom_data.append(
(
atom.name,
atom.element,
atom.charge,
atom.coords,
atom.conformer,
atom.is_present,
atom.chirality,
)
)
atom_idx += 1
res_idx += 1
if chain.cyclic_period > 0:
bond_data.append(
(
asym_id,
asym_id,
0,
chain.cyclic_period - 1,
local_atom_idx_map[(chain_name, 0, "N")][2],
local_atom_idx_map[
(chain_name, chain.cyclic_period - 1, "C")
][2],
const.bond_type_ids["COVALENT"],
)
)
new_res_design_mask = np.array(new_res_design_mask)
residues = np.array(res_data, dtype=Residue)
chains = np.array(chain_data, dtype=Chain)
interfaces = np.array([], dtype=Interface)
mask = np.ones(len(chain_data), dtype=bool)
atom_data = [(a[0], a[3], a[5], 0.0, 1.0) for a in atom_data]
atoms = np.array(atom_data, dtype=Atom)
bonds = np.array(bond_data, dtype=Bond)
coords = [(x,) for x in atoms["coords"]]
coords = np.array(coords, Coords)
ensemble = np.array([(0, len(coords))], dtype=Ensemble)
new_data = Structure(
atoms=atoms,
bonds=bonds,
residues=residues,
chains=chains,
interfaces=interfaces,
mask=mask,
coords=coords,
ensemble=ensemble,
)
new_structure_groups = np.zeros(
len(new_data.residues), dtype=np.int32
)
structure_groups = np.concatenate(
[structure_groups, new_structure_groups]
)
res_design_mask = np.concatenate(
[res_design_mask, new_res_design_mask]
)
if fuse_info["fuse"]:
data = Structure.fuse(
data, new_data, fuse_info["target_id"], res_reindex=True
)
msg = f"fused chain{fuse_info['target_id']} with chain{new_data.chains[0]['name']}"
self.log_once(msg)
else:
data, renaming = Structure.concatenate(
data, new_data, return_renaming=True
)
total_renaming.update(renaming)
if len(renaming) > 0:
msg = f"\nChain ids in non-file sequence conflict with existing chain ids. Renaming them {renaming}."
self.log_once(msg)
global_asym_id += asym_id + 1
new_chain_to_msa = {}
for chain_id, msa in entity_chain_to_msa.items():
renamed_id = renaming.get(chain_id, chain_id)
if renamed_id in chain_to_msa:
raise KeyError(
f"Key '{renamed_id}' already exists in chain_to_msa."
)
new_chain_to_msa[renamed_id] = msa
chain_to_msa.update(new_chain_to_msa)
else:
path = item["file"]["path"]
(
new_data,
new_groups,
new_design_mask,
fbind_types,
fss_type,
file_chain_to_msa,
file_chain_symmetric_group,
fuse_info,
new_extra_mols,
file_msa_flag,
ligand_id,
) = self.parse_file(item, mols, mol_dir, ligand_id, base_file_path)
# Apply symmetric_group to chains from file
for chain_id, sym_group in file_chain_symmetric_group.items():
chain_mask = new_data.chains["name"] == chain_id
new_data.chains["symmetric_group"][chain_mask] = sym_group
if fuse_info["fuse"]:
if fuse_info["target_id"] in total_renaming.keys():
fuse_info["target_id"] = total_renaming[
fuse_info["target_id"]
]
data = Structure.fuse(
data, new_data, fuse_info["target_id"], res_reindex=True
)
msg = f"\nFused chain{fuse_info['target_id']} with chain{new_data.chains[0]['name']}."
self.log_once(msg)
else:
data, renaming = Structure.concatenate(
data, new_data, return_renaming=True
)
total_renaming.update(renaming)
global_asym_id += max(new_data.chains["asym_id"]) + 1
structure_groups = np.concatenate([structure_groups, new_groups])
res_design_mask = np.concatenate([res_design_mask, new_design_mask])
res_bind_type = np.concatenate([res_bind_type, fbind_types])
ss_type = np.concatenate([ss_type, fss_type])
# File entities have no residue constraints — pad with zeros (all AAs allowed)
file_constraint_mask = np.zeros((len(new_design_mask), len(const.canonical_tokens)), dtype=np.float32)
res_aa_constraint_mask = np.concatenate([res_aa_constraint_mask, file_constraint_mask], axis=0)
extra_mols.update(new_extra_mols)
if len(renaming) > 0:
msg = f"\nChain ids conflict with existing chain ids. Renaming with {renaming}. This is for the structure from '{path}'."
self.log_once(msg)
new_chain_to_msa = {}
for chain_id, msa in file_chain_to_msa.items():
renamed_id = renaming.get(chain_id, chain_id)
if renamed_id in chain_to_msa:
raise KeyError(
f"Key '{renamed_id}' already exists in chain_to_msa."
)
new_chain_to_msa[renamed_id] = msa
chain_to_msa.update(new_chain_to_msa)
# Update chain_to_msa dictionary. Set defaults given by file_msa_flag for proteins. Insert -1 (no msa) for {dna, rna, ligand}.
for chain in data.chains:
chain_id = chain["name"].item()
if chain_id not in chain_to_msa:
if chain["mol_type"] == const.chain_type_ids["PROTEIN"]:
chain_to_msa[chain_id] = file_msa_flag
else:
chain_to_msa[chain_id] = -1
if "total_len" in constraints[0]:
if len(res_bind_type) >= min_len and len(res_bind_type) <= max_len:
break
if "total_len" not in constraints[0]:
break
# Parse constraints
for constraint in constraints:
if "bond" in constraint:
if (
"atom1" not in constraint["bond"]
or "atom2" not in constraint["bond"]
):
msg = f"Bond constraint was not properly specified"
raise ValueError(msg)
c1, r1, a1 = tuple(constraint["bond"]["atom1"])
c2, r2, a2 = tuple(constraint["bond"]["atom2"])
r1 = r1 - 1 # 1-indexed
r2 = r2 - 1 # 1-indexed
if c1 in total_renaming.keys():
c1 = total_renaming[c1]
if c2 in total_renaming.keys():
c2 = total_renaming[c2]
if c1 not in all_parsed_chains.keys():
msg = f"Chain {c1} in the specified connection does not exist: {constraint}"
ValueError(msg)
if c2 not in all_parsed_chains.keys():
msg = f"Chain {c2} in the specified connection does not exist: {constraint}"
ValueError(msg)
# Map index
if (
c1 in all_parsed_chains.keys()
and all_parsed_chains[c1].sampleidx_to_specidx is not None
):
r1 = np.where(all_parsed_chains[c1].sampleidx_to_specidx == r1)[0][
0
].item()
c1, r1, a1 = atom_idx_map[(c1, r1, a1)]
else:
# we have a chain coming from a file where we just use the residue index
chain = data.chains[data.chains["name"] == c1]
c1 = chain["asym_id"].item()
res_start = chain["res_idx"].item()
res_end = chain["res_idx"].item() + chain["res_num"].item()
residues = data.residues[res_start:res_end]
residue = residues[residues["res_idx"] == r1]
r1 = res_start + residue["res_idx"].item()
atom_start = residue["atom_idx"].item()
atom_end = residue["atom_idx"].item() + residue["atom_num"].item()
atoms = data.atoms[atom_start:atom_end]
assert a1 in atoms["name"], (
f"Atom {a1} not found in residue {r1} of chain {c1}"
)
a1 = np.where(atoms["name"] == a1)[0].item()
a1 = (
residue["atom_idx"].item() + a1
) # THIS STILL NEEDS TO BE CORRECTED
if (
c2 in all_parsed_chains.keys()
and all_parsed_chains[c2].sampleidx_to_specidx is not None
):
r2 = np.where(all_parsed_chains[c2].sampleidx_to_specidx == r2)[0][
0
].item()
c2, r2, a2 = atom_idx_map[(c2, r2, a2)]
else:
# we have a chain coming from a file where we just use the residue index
chain = data.chains[data.chains["name"] == c2]
c2 = chain["asym_id"].item()
res_start = chain["res_idx"].item()
res_end = chain["res_idx"].item() + chain["res_num"].item()
residues = data.residues[res_start:res_end]
residue = residues[residues["res_idx"] == r2]
r2 = res_start + residue["res_idx"].item()
atom_start = residue["atom_idx"].item()
atom_end = residue["atom_idx"].item() + residue["atom_num"].item()
atoms = data.atoms[atom_start:atom_end]
assert a2 in atoms["name"], (
f"Atom {a2} not found in residue {r2} of chain {c2}"
)
a2 = np.where(atoms["name"] == a2)[0].item()
a2 = (
residue["atom_idx"].item() + a2
) # THIS STILL NEEDS TO BE CORRECTED
covalents.append((c1, c2, r1, r2, a1, a2))
elif "total_len" in constraints:
continue
covalents = [(*c, const.bond_type_ids["COVALENT"]) for c in covalents]
covalents = np.array(covalents, dtype=Bond)
data = replace(data, bonds=np.concatenate([data.bonds, covalents]))
# Parse leaving atoms
leaving_atoms = schema.get("leaving_atoms", [])
for leaving_atom in leaving_atoms:
cidx, ridx, aidx = tuple(leaving_atom["atom"])
ridx = ridx - 1
if all_parsed_chains[cidx].sampleidx_to_specidx is not None:
ridx = np.where(all_parsed_chains[cidx].sampleidx_to_specidx == ridx)[
0
][0].item()
if cidx in total_renaming.keys():
cidx = total_renaming[cidx]
chain = data.chains[np.where(data.chains["name"] == cidx)[0].item()]
residues = data.residues[
chain["res_idx"] : chain["res_idx"] + chain["res_num"]
]
res = residues[np.where(residues["res_idx"] == ridx)[0].item()]
atoms = data.atoms[res["atom_idx"] : res["atom_idx"] + res["atom_num"]]
atom_idx = res["atom_idx"] + np.where(atoms["name"] == aidx)[0].item()
data.atoms["is_present"][atom_idx] = False
# Create metadata
struct_info = StructureInfo(num_chains=len(data.chains))
chain_infos = []
for chain in data.chains:
chain_info = ChainInfo(
chain_id=int(chain["asym_id"]),
chain_name=chain["name"],
mol_type=int(chain["mol_type"]),
cluster_id=-1,
msa_id=chain_to_msa[chain["name"]],
num_residues=int(chain["res_num"]),
valid=True,
entity_id=int(chain["entity_id"]),
)
chain_infos.append(chain_info)
record = Record(
id=name,
structure=struct_info,
chains=chain_infos,
interfaces=[],
)
design_info = DesignInfo(
res_design_mask=res_design_mask,
res_structure_groups=structure_groups,
res_binding_type=res_bind_type,
res_ss_types=ss_type,
res_aa_constraint_mask=res_aa_constraint_mask,
)
DesignInfo.is_valid(design_info)
return Target(
record=record,
structure=data,
design_info=design_info,
extra_mols=extra_mols,
)
def parse_file(self, item, mols, mol_dir, ligand_id, base_file_path=Path(".")):
extra_mols: dict[str, Mol] = {}
file = item["file"]
# Check if file points to another yaml file. If so, then use the contents of that other yaml file
path = file["path"]
if isinstance(path, list) or Path(path).suffix == ".yaml":
if isinstance(path, list):
path = random.choice(path)
resolved_path = (base_file_path / path).resolve()
with resolved_path.open("r") as f:
file = yaml.safe_load(f)
base_file_path = resolved_path.parent
# Extract values of file
path = (base_file_path / Path(file["path"])).resolve()
use_assembly = file.get("use_assembly", False) # dont use assembly by default
include = file.get("include", "all") # include all by default
include_proximity = file.get("include_proximity", None)
exclude = file.get("exclude", None)
structure_spec = file.get("structure_groups", None)
design = file.get("design", None)
add_cyclization = file.get("add_cyclization", None)
reset_res_index = file.get("reset_res_index", None)
not_design = file.get("not_design", None)
file_msa_flag = file.get("msa", 0) # default to automatic MSA generation
if (file_msa_flag is None) or (file_msa_flag == ""):
file_msa_flag = 0
design_insertions = file.get("design_insertions", None)
fuse = file.get("fuse", None)
binding_types = file.get("binding_types", None)
secondary_structure = file.get("secondary_structure", None)
if isinstance(include, list):
for list_element in include:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in include with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if "smiles" in chain:
mol = AllChem.MolFromSmiles(chain["smiles"])
mol = AllChem.AddHs(mol)
element_counts = defaultdict(int)
for i, atom in enumerate(mol.GetAtoms()):
symbol = atom.GetSymbol()
element_counts[symbol] += 1
atom_name = f"{symbol.upper()}{element_counts[symbol]}"
atom.SetProp("name", atom_name)
mols[f"LIG{ligand_id}"] = mol
success = compute_3d_conformer(mol)
if not success:
msg = f"Failed to compute 3D conformer for given smiles string"
raise ValueError(msg)
extra_mols[f"LIG{ligand_id}"] = mol
ligand_id += 1
# Get structure
cache_key = (path.resolve(), use_assembly)
cached = self._struct_cache.get(cache_key)
if cached is not None:
parsed = deepcopy(cached)
else:
if path.suffix == ".pdb":
parsed = parse_pdb(
path,
mols=mols,
moldir=mol_dir,
use_assembly=use_assembly,
)
else:
parsed = parse_mmcif(
path,
mols=mols,
moldir=mol_dir,
use_assembly=use_assembly,
)
self._struct_cache[cache_key] = deepcopy(parsed)
structure = parsed.data
num_res = len(structure.residues)
# Construct include mask from include entries
file_chain_to_msa = {}
file_chain_symmetric_group = {}
if isinstance(include, str):
if include == "all":
include_mask = np.ones(num_res)
else:
msg = f"Include has to be a list or 'all' to include everything in the file."
raise ValueError(msg)
elif isinstance(include, list):
include_mask = np.zeros(num_res)
for list_element in include:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in include with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
if "msa" in chain:
file_chain_to_msa[chain_id] = chain["msa"]
if "symmetric_group" in chain:
file_chain_symmetric_group[chain_id] = chain["symmetric_group"]
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set include_mask values to 1
if "res_index" not in chain:
include_mask[c_start:c_end] = 1
else:
indices = parse_range(chain["res_index"], c_start, c_end)
include_mask[indices] = 1
else:
msg = "Include entry has to be a list of chains or 'all'."
raise ValueError(msg)
proximity_mask = np.ones(num_res)
if include_proximity is not None:
proximity_mask = np.zeros(num_res)
coords = np.array(
[
structure.atoms[r["atom_center"]]["coords"]
for r in structure.residues
]
)
for list_element in include_proximity:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in include_proximity with missing 'id' for file with path {path}."
raise ValueError(msg)
if "radius" not in chain:
msg = f"Misspecified chain in include_proximity with missing 'radius' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
radius = chain["radius"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
proximity_spec_mask = np.zeros(num_res)
if "res_index" not in chain:
proximity_spec_mask[c_start:c_end] = 1
else:
indices = parse_range(chain["res_index"], c_start, c_end)
proximity_spec_mask[indices] = 1
queries = coords[proximity_spec_mask.astype(bool)]
distances = cdist(coords, queries)
dist_mask = distances < radius
dist_mask = dist_mask.sum(-1) > 0
proximity_mask += dist_mask
include_mask *= proximity_mask
# Build exclude mask
exclude_mask = np.ones(num_res)
if exclude is not None:
for list_element in exclude:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in exclude with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set exclude_mask values to 0
if "res_index" not in chain:
include_mask[c_start:c_end] = 0
else:
indices = parse_range(chain["res_index"], c_start, c_end)
exclude_mask[indices] = 0
include_mask = (include_mask * exclude_mask).astype(bool)
# Construct missing_mask to remove leading and trailing unresolved residues for every chain.
missing_mask = []
for chain in structure.chains:
chain_start = chain["res_idx"]
chain_end = chain["res_idx"] + chain["res_num"]
chain_include_mask = include_mask[chain_start:chain_end]
if chain_include_mask.sum() == 0:
# Just append ones for the whole chain if the chain is not even included. This will leave the include_mask unaffected.
missing_mask.append(np.ones(chain["res_num"], dtype=bool))
else:
# Make missing_mask of trailing and leading residues in the included part of the chain
chain_res = structure.residues[chain_start:chain_end]
included_res = chain_res[chain_include_mask]
is_present = included_res["is_present"]
first_true = np.argmax(is_present)
last_true = len(is_present) - 1 - np.argmax(is_present[::-1])
included_missing_mask = np.ones_like(is_present, dtype=bool)
included_missing_mask[:first_true] = False
included_missing_mask[last_true + 1 :] = False
# Print a message if there are any trailing or leading missing residues.
if (~included_missing_mask).sum() > 0:
if included_missing_mask.sum() == 0:
msg = f"\nThere are no resolved residues for chain {chain['name']} in {str(path)}. We are removing the chain."
else:
leading = ",".join(map(str, included_res[:first_true]["name"]))
trailing = ",".join(
map(str, included_res[last_true + 1 :]["name"])
)
msg = (
f"\nRemoving leading and/or trailing unresolved residues from included part of chain {chain['name']} in {path}.\n"
f" Leading unresolved: {leading}\n"
f" Trailing unresolved: {trailing}"
)
self.log_once(msg)
# insert the missing mask of the included part of the chain into the missing mask of the whole chain
chain_missing_mask = np.ones(chain["res_num"], dtype=bool)
chain_missing_mask[chain_include_mask] = included_missing_mask
missing_mask.append(chain_missing_mask)
missing_mask = np.concatenate(missing_mask)
include_mask *= missing_mask
# Get structure groups
new_groups = np.zeros(num_res)
if structure_spec is None or structure_spec == "all" or structure_spec == 1:
new_groups = np.ones(num_res)
else:
for list_element in structure_spec:
group = list_element["group"]
if "id" not in group:
msg = f"Misspecified group in structure_groups with missing 'id' for file with path {path}."
raise ValueError(msg)
if "visibility" not in group:
msg = f"Misspecified group in structure_groups with missing 'visibility' for file with path {path}."
raise ValueError(msg)
chain_id = group["id"]
# Handle the "all" case where all chains are set to be specified
if chain_id == "all":
new_groups = np.ones(num_res)
continue
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
visibility = group["visibility"]
# Set structure group values to the correct visibility
if "res_index" not in group:
new_groups[c_start:c_end] = visibility
else:
indices = parse_range(group["res_index"], c_start, c_end)
new_groups[indices] = visibility
# Get design mask for file
new_design_mask = np.zeros(num_res)
if design is not None:
for list_element in design:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in design with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
# Handle the "all" case where all chains are set to be designed
if chain_id == "all":
# TODO: handle case where users specify non-protein residues to be designed.
new_design_mask = np.ones(num_res)
continue
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set values
if "res_index" not in chain:
new_design_mask[c_start:c_end] = 1
else:
indices = parse_range(chain["res_index"], c_start, c_end)
new_design_mask[indices] = 1
# Get modification mask to turn previous design regions into non-design regions
new_design_mask_mod = np.ones(num_res)
if not_design is not None:
for list_element in not_design:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in not_design with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set values
if "res_index" not in chain:
new_design_mask_mod[c_start:c_end] = 0
else:
indices = parse_range(chain["res_index"], c_start, c_end)
new_design_mask_mod[indices] = 0
new_design_mask = (new_design_mask * new_design_mask_mod).astype(bool)
# Get file's binding types called fbind_types
fbind_types = np.ones(num_res) * const.binding_type_ids["UNSPECIFIED"]
fbind_types = fbind_types.astype(np.int32)
if binding_types is not None:
for list_element in binding_types:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in binding_types with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set values
if "binding" in chain:
binding = chain["binding"]
if binding == "all":
fbind_types[c_start:c_end] = const.binding_type_ids["BINDING"]
else:
indices = parse_range(binding, c_start, c_end)
fbind_types[indices] = const.binding_type_ids["BINDING"]
if "not_binding" in chain:
not_binding = chain["not_binding"]
if not_binding == "all":
fbind_types[c_start:c_end] = const.binding_type_ids[
"NOT_BINDING"
]
else:
indices = parse_range(not_binding, c_start, c_end)
fbind_types[indices] = const.binding_type_ids["NOT_BINDING"]
# Get file's secondary structure types called fss_types
fss_type = np.ones(num_res) * const.ss_type_ids["UNSPECIFIED"]
fss_type = fss_type.astype(np.int32)
if secondary_structure is not None:
for list_element in secondary_structure:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in secondary_structure with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
data_chain = structure.chains[structure.chains["name"] == chain_id]
c_start = data_chain["res_idx"].item()
c_end = c_start + data_chain["res_num"].item()
# Set values
if "loop" in chain:
loop = chain["loop"]
if loop == "all":
fss_type[c_start:c_end] = const.ss_type_ids["LOOP"]
else:
indices = parse_range(loop, c_start, c_end)
fss_type[indices] = const.ss_type_ids["LOOP"]
if "helix" in chain:
helix = chain["helix"]
if helix == "all":
fss_type[c_start:c_end] = const.ss_type_ids["HELIX"]
else:
indices = parse_range(helix, c_start, c_end)
fss_type[indices] = const.ss_type_ids["HELIX"]
if "sheet" in chain:
sheet = chain["sheet"]
if sheet == "all":
fss_type[c_start:c_end] = const.ss_type_ids["SHEET"]
else:
indices = parse_range(sheet, c_start, c_end)
fss_type[indices] = const.ss_type_ids["SHEET"]
# Parse and apply design insertions
# First pass: collect insertions and coordinate lengths for symmetric chains
if design_insertions is not None:
num_inserted = defaultdict(int)
# Group insertions by (symmetric_group, res_index) to coordinate variable lengths
symmetric_length_cache = {} # (sym_group, res_index) -> sampled_length
for list_element in design_insertions:
insertion = list_element["insertion"]
if "id" not in insertion:
msg = f"Misspecified insertion in design_insertions with missing 'id' for file with path {path}."
raise ValueError(msg)
if "res_index" not in insertion:
msg = f"Misspecified insertion in design_insertions with missing 'res_index' for file with path {path}."
raise ValueError(msg)
chain_id = insertion["id"]
res_index = insertion["res_index"] - 1 # 1 index input to 0 indexed
res_index += num_inserted[chain_id]
ss_insert_type = insertion.get("secondary_structure", "UNSPECIFIED")
num_residues_spec = insertion["num_residues"]
num_residues_range = parse_range(num_residues_spec)
# Check if this chain has a symmetric_group
chain_sym_group = file_chain_symmetric_group.get(chain_id, 0)
# If chain has symmetric_group > 0, coordinate length with other symmetric chains
if chain_sym_group > 0:
cache_key = (chain_sym_group, res_index, str(num_residues_spec))
if cache_key in symmetric_length_cache:
num_residues = symmetric_length_cache[cache_key]
else:
num_residues = np.random.choice(num_residues_range).item()
symmetric_length_cache[cache_key] = num_residues
else:
num_residues = np.random.choice(num_residues_range).item()
# We add +1 because the parse_range function is usually used for indexing where we then convert the 1 based inputs to 0 indexing
num_residues += 1
num_inserted[chain_id] += num_residues
if chain_id not in structure.chains["name"]:
msg = f"Specified chain id {chain_id} not in file {path}."
raise ValueError(msg)
target_chain = structure.chains[structure.chains["name"] == chain_id]
res_insert_idx = target_chain["res_idx"] + res_index
# Insert into structure
structure = Structure.insert(
structure, chain_id, res_idx=res_index, num_residues=num_residues
)
# Insert into design specifications
include_mask = np.insert(
include_mask, res_insert_idx, np.ones(num_residues)
)
new_groups = np.insert(
new_groups, res_insert_idx, np.zeros(num_residues)
)
new_design_mask = np.insert(
new_design_mask, res_insert_idx, np.ones(num_residues)
)
fbind_types = np.insert(
fbind_types,
res_insert_idx,
np.ones(num_residues) * const.binding_type_ids["UNSPECIFIED"],
)
fss_type = np.insert(
fss_type,
res_insert_idx,
np.ones(num_residues) * const.ss_type_ids[ss_insert_type],
)
# Apply mask to new structure groups. Update structure_groups by concatenating existing and new one
new_groups = new_groups[include_mask].astype(np.int32)
# Apply mask to new design_mask. Update design by concatenating existing and new one.
new_design_mask = new_design_mask[include_mask]
# Apply mask to new binding_types. Update binding_types by concatenating existing and new one.
fbind_types = fbind_types[include_mask].astype(np.int32)
# Apply mask to new ss_type. Update ss_type by concatenating existing and new one.
fss_type = fss_type[include_mask].astype(np.int32)
# Apply mask to structrue
if not all(include_mask):
new_structure = Structure.extract_residues(
structure, include_mask.astype(bool), res_reindex=False
)
else:
new_structure = structure
# Handle cyclizations
if add_cyclization is not None:
additional_bonds = []
for list_element in add_cyclization:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in add_cyclization with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
chain_idx = np.where(chain_id == new_structure.chains["name"])[0].item()
struct_chain = new_structure.chains[chain_idx]
num_res = struct_chain["res_num"].item()
new_structure.chains[chain_idx]["cyclic_period"] = num_res
chain_res_idx = struct_chain["res_idx"].item()
# Get atom indices
res1 = new_structure.residues[chain_res_idx]
res2 = new_structure.residues[chain_res_idx + num_res - 1]
atoms1 = new_structure.atoms[
res1["atom_idx"] : res1["atom_idx"] + res1["atom_num"]
]
atoms2 = new_structure.atoms[
res2["atom_idx"] : res2["atom_idx"] + res2["atom_num"]
]
assert "N" in atoms1["name"]
assert "C" in atoms2["name"]
idx_in_res1 = np.where(atoms1["name"] == "N")[0].item()
idx_in_res2 = np.where(atoms2["name"] == "C")[0].item()
atom_idx1 = res1["atom_idx"] + idx_in_res1
atom_idx2 = res2["atom_idx"] + idx_in_res2
# Make new bond
additional_bonds.append(
(
struct_chain["asym_id"].item(),
struct_chain["asym_id"].item(),
chain_res_idx,
chain_res_idx + num_res - 1,
atom_idx1,
atom_idx2,
const.bond_type_ids["COVALENT"],
)
)
additional_bonds = np.array(additional_bonds, dtype=Bond)
new_bonds = np.concatenate([new_structure.bonds, additional_bonds])
new_structure = replace(new_structure, bonds=new_bonds)
# Reset residue indices of chains where it is desired
if reset_res_index is not None:
for list_element in reset_res_index:
chain = list_element["chain"]
if "id" not in chain:
msg = f"Misspecified chain in reset_res_index with missing 'id' for file with path {path}."
raise ValueError(msg)
chain_id = chain["id"]
chain_idx = np.where(chain_id == new_structure.chains["name"])[0].item()
struct_chain = new_structure.chains[chain_idx]
new_structure.residues[
struct_chain["res_idx"] : struct_chain["res_idx"]
+ struct_chain["res_num"]
]["res_idx"] = np.arange(struct_chain["res_num"])
# perform fusion or concatenation
fuse_info = {}
if fuse is not None:
fuse_info["target_id"] = file["fuse"]
fuse_info["fuse"] = True
else:
fuse_info["fuse"] = False
return (
new_structure,
new_groups,
new_design_mask,
fbind_types,
fss_type,
file_chain_to_msa,
file_chain_symmetric_group,
fuse_info,
extra_mols,
file_msa_flag,
ligand_id,
)
|