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
|
//
// Copyright (C) 2001-2018 Greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <GraphMol/RDKitBase.h>
#include <GraphMol/RDKitQueries.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/MonomerInfo.h>
#include <GraphMol/StereoGroup.h>
#include <RDGeneral/utils.h>
#include <RDGeneral/RDLog.h>
#include <RDGeneral/StreamOps.h>
#include <RDGeneral/types.h>
#include <Query/QueryObjects.h>
#include <map>
#include <boost/cstdint.hpp>
#include <boost/algorithm/string.hpp>
#ifdef RDK_THREADSAFE_SSS
#include <mutex>
#endif
using boost::int32_t;
using boost::uint32_t;
namespace RDKit {
const int32_t MolPickler::versionMajor = 10;
const int32_t MolPickler::versionMinor = 0;
const int32_t MolPickler::versionPatch = 0;
const int32_t MolPickler::endianId = 0xDEADBEEF;
void streamWrite(std::ostream &ss, MolPickler::Tags tag) {
unsigned char tmp = static_cast<unsigned char>(tag);
streamWrite(ss, tmp);
}
template <typename T>
void streamWrite(std::ostream &ss, MolPickler::Tags tag, const T &what) {
streamWrite(ss, tag);
streamWrite(ss, what);
};
void streamRead(std::istream &ss, MolPickler::Tags &tag, int version) {
if (version < 7000) {
int32_t tmp;
streamRead(ss, tmp, version);
tag = static_cast<MolPickler::Tags>(tmp);
} else {
unsigned char tmp;
streamRead(ss, tmp, version);
tag = static_cast<MolPickler::Tags>(tmp);
}
}
namespace {
static unsigned int defaultProperties = PicklerOps::NoProps;
#ifdef RDK_THREADSAFE_SSS
std::mutex &propmutex_get() {
// create on demand
static std::mutex _mutex;
return _mutex;
}
void propmutex_create() {
std::mutex &mutex = propmutex_get();
std::lock_guard<std::mutex> test_lock(mutex);
}
std::mutex &GetPropMutex() {
static std::once_flag flag;
std::call_once(flag, propmutex_create);
return propmutex_get();
}
#endif
} // namespace
unsigned int MolPickler::getDefaultPickleProperties() {
#ifdef RDK_THREADSAFE_SSS
std::lock_guard<std::mutex> lock(GetPropMutex());
#endif
unsigned int props = defaultProperties;
return props;
}
void MolPickler::setDefaultPickleProperties(unsigned int props) {
#ifdef RDK_THREADSAFE_SSS
std::lock_guard<std::mutex> lock(GetPropMutex());
#endif
defaultProperties = props;
}
namespace {
using namespace Queries;
template <class T>
void pickleQuery(std::ostream &ss, const Query<int, T const *, true> *query) {
PRECONDITION(query, "no query");
streamWrite(ss, query->getDescription());
if (query->getNegation()) streamWrite(ss, MolPickler::QUERY_ISNEGATED);
int32_t queryVal;
// if (typeid(*query)==typeid(ATOM_BOOL_QUERY)){
// streamWrite(ss,QUERY_BOOL);
if (typeid(*query) == typeid(AndQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_AND);
} else if (typeid(*query) == typeid(OrQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_OR);
} else if (typeid(*query) == typeid(XOrQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_XOR);
} else if (typeid(*query) == typeid(EqualityQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_EQUALS);
queryVal = static_cast<const EqualityQuery<int, T const *, true> *>(query)
->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal = static_cast<const EqualityQuery<int, T const *, true> *>(query)
->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) == typeid(GreaterQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_GREATER);
queryVal = static_cast<const GreaterQuery<int, T const *, true> *>(query)
->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal = static_cast<const GreaterQuery<int, T const *, true> *>(query)
->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) ==
typeid(GreaterEqualQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_GREATEREQUAL);
queryVal =
static_cast<const GreaterEqualQuery<int, T const *, true> *>(query)
->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal =
static_cast<const GreaterEqualQuery<int, T const *, true> *>(query)
->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) == typeid(LessQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_LESS);
queryVal =
static_cast<const LessQuery<int, T const *, true> *>(query)->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal =
static_cast<const LessQuery<int, T const *, true> *>(query)->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) == typeid(LessEqualQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_LESSEQUAL);
queryVal = static_cast<const LessEqualQuery<int, T const *, true> *>(query)
->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal = static_cast<const LessEqualQuery<int, T const *, true> *>(query)
->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) == typeid(RangeQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_RANGE);
queryVal = static_cast<const RangeQuery<int, T const *, true> *>(query)
->getLower();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal = static_cast<const RangeQuery<int, T const *, true> *>(query)
->getUpper();
streamWrite(ss, queryVal);
queryVal =
static_cast<const RangeQuery<int, T const *, true> *>(query)->getTol();
streamWrite(ss, queryVal);
char ends;
bool lowerOpen, upperOpen;
boost::tie(lowerOpen, upperOpen) =
static_cast<const RangeQuery<int, T const *, true> *>(query)
->getEndsOpen();
ends = 0 | (rdcast<int>(lowerOpen) << 1) | rdcast<int>(upperOpen);
streamWrite(ss, ends);
} else if (typeid(*query) == typeid(SetQuery<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_SET);
queryVal =
static_cast<const SetQuery<int, T const *, true> *>(query)->size();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
typename SetQuery<int, T const *, true>::CONTAINER_TYPE::const_iterator cit;
for (cit = static_cast<const SetQuery<int, T const *, true> *>(query)
->beginSet();
cit !=
static_cast<const SetQuery<int, T const *, true> *>(query)->endSet();
++cit) {
queryVal = *cit;
streamWrite(ss, queryVal);
}
} else if (typeid(*query) == typeid(AtomRingQuery)) {
streamWrite(ss, MolPickler::QUERY_ATOMRING);
queryVal = static_cast<const EqualityQuery<int, T const *, true> *>(query)
->getVal();
streamWrite(ss, MolPickler::QUERY_VALUE, queryVal);
queryVal = static_cast<const EqualityQuery<int, T const *, true> *>(query)
->getTol();
streamWrite(ss, queryVal);
} else if (typeid(*query) == typeid(RecursiveStructureQuery)) {
streamWrite(ss, MolPickler::QUERY_RECURSIVE);
streamWrite(ss, MolPickler::QUERY_VALUE);
MolPickler::pickleMol(
((const RecursiveStructureQuery *)query)->getQueryMol(), ss);
} else if (typeid(*query) == typeid(Query<int, T const *, true>)) {
streamWrite(ss, MolPickler::QUERY_NULL);
} else {
throw MolPicklerException("do not know how to pickle part of the query.");
}
// now the children:
streamWrite(ss, MolPickler::QUERY_NUMCHILDREN,
static_cast<unsigned char>(query->endChildren() -
query->beginChildren()));
typename Query<int, T const *, true>::CHILD_VECT_CI cit;
for (cit = query->beginChildren(); cit != query->endChildren(); ++cit) {
pickleQuery(ss, cit->get());
}
}
void finalizeQueryFromDescription(Query<int, Atom const *, true> *query,
Atom const *owner) {
std::string descr = query->getDescription();
RDUNUSED_PARAM(owner);
if (boost::starts_with(descr, "range_")) {
descr = descr.substr(6);
} else if (boost::starts_with(descr, "less_")) {
descr = descr.substr(5);
} else if (boost::starts_with(descr, "greater_")) {
descr = descr.substr(8);
}
Query<int, Atom const *, true> *tmpQuery;
if (descr == "AtomRingBondCount") {
query->setDataFunc(queryAtomRingBondCount);
} else if (descr == "AtomHasRingBond") {
query->setDataFunc(queryAtomHasRingBond);
} else if (descr == "AtomRingSize") {
tmpQuery = makeAtomInRingOfSizeQuery(
static_cast<ATOM_EQUALS_QUERY *>(query)->getVal());
query->setDataFunc(tmpQuery->getDataFunc());
delete tmpQuery;
} else if (descr == "AtomMinRingSize") {
query->setDataFunc(queryAtomMinRingSize);
} else if (descr == "AtomRingBondCount") {
query->setDataFunc(queryAtomRingBondCount);
} else if (descr == "AtomImplicitValence") {
query->setDataFunc(queryAtomImplicitValence);
} else if (descr == "AtomTotalValence") {
query->setDataFunc(queryAtomTotalValence);
} else if (descr == "AtomAtomicNum") {
query->setDataFunc(queryAtomNum);
} else if (descr == "AtomExplicitDegree") {
query->setDataFunc(queryAtomExplicitDegree);
} else if (descr == "AtomTotalDegree") {
query->setDataFunc(queryAtomTotalDegree);
} else if (descr == "AtomHeavyAtomDegree") {
query->setDataFunc(queryAtomHeavyAtomDegree);
} else if (descr == "AtomHCount") {
query->setDataFunc(queryAtomHCount);
} else if (descr == "AtomImplicitHCount") {
query->setDataFunc(queryAtomImplicitHCount);
} else if (descr == "AtomHasImplicitH") {
query->setDataFunc(queryAtomHasImplicitH);
} else if (descr == "AtomIsAromatic") {
query->setDataFunc(queryAtomAromatic);
} else if (descr == "AtomIsAliphatic") {
query->setDataFunc(queryAtomAliphatic);
} else if (descr == "AtomUnsaturated") {
query->setDataFunc(queryAtomUnsaturated);
} else if (descr == "AtomMass") {
query->setDataFunc(queryAtomMass);
} else if (descr == "AtomIsotope") {
query->setDataFunc(queryAtomIsotope);
} else if (descr == "AtomFormalCharge") {
query->setDataFunc(queryAtomFormalCharge);
} else if (descr == "AtomHybridization") {
query->setDataFunc(queryAtomHybridization);
} else if (descr == "AtomInRing") {
query->setDataFunc(queryIsAtomInRing);
} else if (descr == "AtomInNRings") {
query->setDataFunc(queryIsAtomInNRings);
} else if (descr == "AtomHasHeteroatomNeighbors") {
query->setDataFunc(queryAtomHasHeteroatomNbrs);
} else if (descr == "AtomNumHeteroatomNeighbors") {
query->setDataFunc(queryAtomNumHeteroatomNbrs);
} else if (descr == "AtomHasAliphaticHeteroatomNeighbors") {
query->setDataFunc(queryAtomHasAliphaticHeteroatomNbrs);
} else if (descr == "AtomNumAliphaticHeteroatomNeighbors") {
query->setDataFunc(queryAtomNumAliphaticHeteroatomNbrs);
} else if (descr == "AtomNull") {
query->setDataFunc(nullDataFun);
query->setMatchFunc(nullQueryFun);
} else if (descr == "AtomType") {
query->setDataFunc(queryAtomType);
} else if (descr == "AtomInNRings" || descr == "RecursiveStructure") {
// don't need to do anything here because the classes
// automatically have everything set
} else if (descr == "AtomAnd" || descr == "AtomOr" || descr == "AtomXor") {
// don't need to do anything here because the classes
// automatically have everything set
} else {
throw MolPicklerException("Do not know how to finalize query: '" + descr +
"'");
}
}
void finalizeQueryFromDescription(Query<int, Bond const *, true> *query,
Bond const *owner) {
RDUNUSED_PARAM(owner);
std::string descr = query->getDescription();
Query<int, Bond const *, true> *tmpQuery;
if (descr == "BondRingSize") {
tmpQuery = makeBondInRingOfSizeQuery(
static_cast<BOND_EQUALS_QUERY *>(query)->getVal());
query->setDataFunc(tmpQuery->getDataFunc());
delete tmpQuery;
} else if (descr == "BondMinRingSize") {
query->setDataFunc(queryBondMinRingSize);
} else if (descr == "BondOrder") {
query->setDataFunc(queryBondOrder);
} else if (descr == "BondDir") {
query->setDataFunc(queryBondDir);
} else if (descr == "BondInRing") {
query->setDataFunc(queryIsBondInRing);
} else if (descr == "BondInNRings") {
query->setDataFunc(queryIsBondInNRings);
} else if (descr == "SingleOrAromaticBond") {
query->setDataFunc(queryBondIsSingleOrAromatic);
} else if (descr == "BondNull") {
query->setDataFunc(nullDataFun);
query->setMatchFunc(nullQueryFun);
} else if (descr == "BondAnd" || descr == "BondOr" || descr == "BondXor") {
// don't need to do anything here because the classes
// automatically have everything set
} else {
throw MolPicklerException("Do not know how to finalize query: '" + descr +
"'");
}
}
template <class T>
Query<int, T const *, true> *buildBaseQuery(std::istream &ss, T const *owner,
MolPickler::Tags tag, int version) {
PRECONDITION(owner, "no query");
std::string descr;
Query<int, T const *, true> *res = nullptr;
int32_t val;
int32_t nMembers;
char cval;
const unsigned int lowerOpen = 1 << 1;
const unsigned int upperOpen = 1;
switch (tag) {
case MolPickler::QUERY_AND:
res = new AndQuery<int, T const *, true>();
break;
case MolPickler::QUERY_OR:
res = new OrQuery<int, T const *, true>();
break;
case MolPickler::QUERY_XOR:
res = new XOrQuery<int, T const *, true>();
break;
case MolPickler::QUERY_EQUALS:
res = new EqualityQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<EqualityQuery<int, T const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<EqualityQuery<int, T const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_GREATER:
res = new GreaterQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<GreaterQuery<int, T const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<GreaterQuery<int, T const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_GREATEREQUAL:
res = new GreaterEqualQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<GreaterEqualQuery<int, T const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<GreaterEqualQuery<int, T const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_LESS:
res = new LessQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<LessQuery<int, T const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<LessQuery<int, T const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_LESSEQUAL:
res = new LessEqualQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<LessEqualQuery<int, T const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<LessEqualQuery<int, T const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_RANGE:
res = new RangeQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<RangeQuery<int, T const *, true> *>(res)->setLower(val);
streamRead(ss, val, version);
static_cast<RangeQuery<int, T const *, true> *>(res)->setUpper(val);
streamRead(ss, val, version);
static_cast<RangeQuery<int, T const *, true> *>(res)->setTol(val);
streamRead(ss, cval, version);
static_cast<RangeQuery<int, T const *, true> *>(res)->setEndsOpen(
cval & lowerOpen, cval & upperOpen);
break;
case MolPickler::QUERY_SET:
res = new SetQuery<int, T const *, true>();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, nMembers);
while (nMembers > 0) {
streamRead(ss, val, version);
static_cast<SetQuery<int, T const *, true> *>(res)->insert(val);
--nMembers;
}
break;
case MolPickler::QUERY_NULL:
res = new Query<int, T const *, true>();
break;
default:
throw MolPicklerException("unknown query-type tag encountered");
}
POSTCONDITION(res, "no match found");
return res;
}
Query<int, Atom const *, true> *unpickleQuery(std::istream &ss,
Atom const *owner, int version) {
PRECONDITION(owner, "no query");
std::string descr;
bool isNegated = false;
Query<int, Atom const *, true> *res;
streamRead(ss, descr, version);
MolPickler::Tags tag;
streamRead(ss, tag, version);
if (tag == MolPickler::QUERY_ISNEGATED) {
isNegated = true;
streamRead(ss, tag, version);
}
int32_t val;
ROMol *tmpMol;
switch (tag) {
case MolPickler::QUERY_ATOMRING:
res = new AtomRingQuery();
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
streamRead(ss, val, version);
static_cast<EqualityQuery<int, Atom const *, true> *>(res)->setVal(val);
streamRead(ss, val, version);
static_cast<EqualityQuery<int, Atom const *, true> *>(res)->setTol(val);
break;
case MolPickler::QUERY_RECURSIVE:
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_VALUE) {
throw MolPicklerException(
"Bad pickle format: QUERY_VALUE tag not found.");
}
tmpMol = new ROMol();
MolPickler::molFromPickle(ss, tmpMol);
res = new RecursiveStructureQuery(tmpMol);
break;
default:
res = buildBaseQuery(ss, owner, tag, version);
break;
}
CHECK_INVARIANT(res, "no query!");
res->setNegation(isNegated);
res->setDescription(descr);
finalizeQueryFromDescription(res, owner);
// read in the children:
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_NUMCHILDREN) {
throw MolPicklerException(
"Bad pickle format: QUERY_NUMCHILDREN tag not found.");
}
unsigned char numChildren;
streamRead(ss, numChildren, version);
while (numChildren > 0) {
Query<int, Atom const *, true> *child = unpickleQuery(ss, owner, version);
res->addChild(Query<int, Atom const *, true>::CHILD_TYPE(child));
--numChildren;
}
return res;
}
Query<int, Bond const *, true> *unpickleQuery(std::istream &ss,
Bond const *owner, int version) {
PRECONDITION(owner, "no query");
std::string descr;
bool isNegated = false;
Query<int, Bond const *, true> *res;
streamRead(ss, descr, version);
MolPickler::Tags tag;
streamRead(ss, tag, version);
if (tag == MolPickler::QUERY_ISNEGATED) {
isNegated = true;
streamRead(ss, tag, version);
}
res = buildBaseQuery(ss, owner, tag, version);
CHECK_INVARIANT(res, "no query!");
res->setNegation(isNegated);
res->setDescription(descr);
finalizeQueryFromDescription(res, owner);
// read in the children:
streamRead(ss, tag, version);
if (tag != MolPickler::QUERY_NUMCHILDREN) {
throw MolPicklerException(
"Bad pickle format: QUERY_NUMCHILDREN tag not found.");
}
unsigned char numChildren;
streamRead(ss, numChildren, version);
while (numChildren > 0) {
Query<int, Bond const *, true> *child = unpickleQuery(ss, owner, version);
res->addChild(Query<int, Bond const *, true>::CHILD_TYPE(child));
--numChildren;
}
return res;
}
void pickleAtomPDBResidueInfo(std::ostream &ss,
const AtomPDBResidueInfo *info) {
PRECONDITION(info, "no info");
if (info->getSerialNumber())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_SERIALNUMBER,
info->getSerialNumber());
if (info->getAltLoc() != "")
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_ALTLOC, info->getAltLoc());
if (info->getResidueName() != "")
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_RESIDUENAME,
info->getResidueName());
if (info->getResidueNumber())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_RESIDUENUMBER,
info->getResidueNumber());
if (info->getChainId() != "")
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_CHAINID, info->getChainId());
if (info->getInsertionCode() != "")
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_INSERTIONCODE,
info->getInsertionCode());
if (info->getOccupancy())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_OCCUPANCY,
info->getOccupancy());
if (info->getTempFactor())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_TEMPFACTOR,
info->getTempFactor());
if (info->getIsHeteroAtom())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_ISHETEROATOM,
static_cast<char>(info->getIsHeteroAtom()));
if (info->getSecondaryStructure())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE,
info->getSecondaryStructure());
if (info->getSegmentNumber())
streamWrite(ss, MolPickler::ATOM_PDB_RESIDUE_SEGMENTNUMBER,
info->getSegmentNumber());
}
void unpickleAtomPDBResidueInfo(std::istream &ss, AtomPDBResidueInfo *info,
int version) {
PRECONDITION(info, "no info");
std::string sval;
double dval;
char cval;
unsigned int uival;
int ival;
MolPickler::Tags tag = MolPickler::BEGIN_ATOM_MONOMER;
while (tag != MolPickler::END_ATOM_MONOMER) {
streamRead(ss, tag, version);
switch (tag) {
case MolPickler::ATOM_PDB_RESIDUE_SERIALNUMBER:
streamRead(ss, ival, version);
info->setSerialNumber(ival);
break;
case MolPickler::ATOM_PDB_RESIDUE_ALTLOC:
streamRead(ss, sval, version);
info->setAltLoc(sval);
break;
case MolPickler::ATOM_PDB_RESIDUE_RESIDUENAME:
streamRead(ss, sval, version);
info->setResidueName(sval);
break;
case MolPickler::ATOM_PDB_RESIDUE_RESIDUENUMBER:
streamRead(ss, ival, version);
info->setResidueNumber(ival);
break;
case MolPickler::ATOM_PDB_RESIDUE_CHAINID:
streamRead(ss, sval, version);
info->setChainId(sval);
break;
case MolPickler::ATOM_PDB_RESIDUE_INSERTIONCODE:
streamRead(ss, sval, version);
info->setInsertionCode(sval);
break;
case MolPickler::ATOM_PDB_RESIDUE_OCCUPANCY:
streamRead(ss, dval, version);
info->setOccupancy(dval);
break;
case MolPickler::ATOM_PDB_RESIDUE_TEMPFACTOR:
streamRead(ss, dval, version);
info->setTempFactor(dval);
break;
case MolPickler::ATOM_PDB_RESIDUE_ISHETEROATOM:
streamRead(ss, cval, version);
info->setIsHeteroAtom(cval);
break;
case MolPickler::ATOM_PDB_RESIDUE_SECONDARYSTRUCTURE:
streamRead(ss, uival, version);
info->setSecondaryStructure(uival);
break;
case MolPickler::ATOM_PDB_RESIDUE_SEGMENTNUMBER:
streamRead(ss, uival, version);
info->setSegmentNumber(uival);
break;
case MolPickler::END_ATOM_MONOMER:
break;
default:
throw MolPicklerException(
"unrecognized tag while parsing atom peptide residue info");
}
}
}
void pickleAtomMonomerInfo(std::ostream &ss, const AtomMonomerInfo *info) {
PRECONDITION(info, "no info");
streamWrite(ss, info->getName());
streamWrite(ss, static_cast<unsigned int>(info->getMonomerType()));
switch (info->getMonomerType()) {
case AtomMonomerInfo::UNKNOWN:
case AtomMonomerInfo::OTHER:
break;
case AtomMonomerInfo::PDBRESIDUE:
pickleAtomPDBResidueInfo(ss,
static_cast<const AtomPDBResidueInfo *>(info));
break;
default:
throw MolPicklerException("unrecognized MonomerType");
}
}
AtomMonomerInfo *unpickleAtomMonomerInfo(std::istream &ss, int version) {
MolPickler::Tags tag;
std::string nm;
streamRead(ss, nm, version);
unsigned int typ;
streamRead(ss, typ, version);
AtomMonomerInfo *res;
switch (typ) {
case AtomMonomerInfo::UNKNOWN:
case AtomMonomerInfo::OTHER:
res =
new AtomMonomerInfo(RDKit::AtomMonomerInfo::AtomMonomerType(typ), nm);
streamRead(ss, tag, version);
if (tag != MolPickler::END_ATOM_MONOMER)
throw MolPicklerException(
"did not find expected end of atom monomer info");
break;
case AtomMonomerInfo::PDBRESIDUE:
res = static_cast<AtomMonomerInfo *>(new AtomPDBResidueInfo(nm));
unpickleAtomPDBResidueInfo(ss, static_cast<AtomPDBResidueInfo *>(res),
version);
break;
default:
throw MolPicklerException("unrecognized MonomerType");
}
return res;
}
} // end of anonymous namespace
void MolPickler::pickleMol(const ROMol *mol, std::ostream &ss) {
pickleMol(mol, ss, MolPickler::getDefaultPickleProperties());
}
void MolPickler::pickleMol(const ROMol *mol, std::ostream &ss,
unsigned int propertyFlags) {
PRECONDITION(mol, "empty molecule");
streamWrite(ss, endianId);
streamWrite(ss, static_cast<int>(VERSION));
streamWrite(ss, versionMajor);
streamWrite(ss, versionMinor);
streamWrite(ss, versionPatch);
#ifndef OLD_PICKLE
if (mol->getNumAtoms() > 255) {
_pickle<int32_t>(mol, ss, propertyFlags);
} else {
_pickle<unsigned char>(mol, ss, propertyFlags);
}
#else
_pickleV1(mol, ss);
#endif
}
void MolPickler::pickleMol(const ROMol &mol, std::ostream &ss) {
pickleMol(&mol, ss, MolPickler::getDefaultPickleProperties());
}
void MolPickler::pickleMol(const ROMol *mol, std::string &res) {
pickleMol(mol, res, MolPickler::getDefaultPickleProperties());
}
void MolPickler::pickleMol(const ROMol *mol, std::string &res,
unsigned int pickleFlags) {
PRECONDITION(mol, "empty molecule");
std::stringstream ss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
MolPickler::pickleMol(mol, ss, pickleFlags);
res = ss.str();
}
void MolPickler::pickleMol(const ROMol &mol, std::string &ss) {
pickleMol(&mol, ss, MolPickler::getDefaultPickleProperties());
}
// NOTE: if the mol passed in here already has atoms and bonds, they will
// be left intact. The side effect is that ALL atom and bond bookmarks
// will be blown out by the end of this process.
void MolPickler::molFromPickle(std::istream &ss, ROMol *mol) {
PRECONDITION(mol, "empty molecule");
int32_t tmpInt;
mol->clearAllAtomBookmarks();
mol->clearAllBondBookmarks();
streamRead(ss, tmpInt);
if (tmpInt != endianId) {
throw MolPicklerException(
"Bad pickle format: bad endian ID or invalid file format");
}
streamRead(ss, tmpInt);
if (static_cast<Tags>(tmpInt) != VERSION) {
throw MolPicklerException("Bad pickle format: no version tag");
}
int32_t majorVersion, minorVersion, patchVersion;
streamRead(ss, majorVersion);
streamRead(ss, minorVersion);
streamRead(ss, patchVersion);
if (majorVersion > versionMajor ||
(majorVersion == versionMajor && minorVersion > versionMinor)) {
BOOST_LOG(rdWarningLog)
<< "Depickling from a version number (" << majorVersion << "."
<< minorVersion << ")"
<< "that is higher than our version (" << versionMajor << "."
<< versionMinor << ").\nThis probably won't work." << std::endl;
}
majorVersion = 1000 * majorVersion + minorVersion * 10 + patchVersion;
if (majorVersion == 1) {
_depickleV1(ss, mol);
} else {
int32_t numAtoms;
streamRead(ss, numAtoms, majorVersion);
if (numAtoms > 255) {
_depickle<int32_t>(ss, mol, majorVersion, numAtoms);
} else {
_depickle<unsigned char>(ss, mol, majorVersion, numAtoms);
}
}
mol->clearAllAtomBookmarks();
mol->clearAllBondBookmarks();
if (majorVersion < 4000) {
// FIX for issue 220 - probably better to change the pickle format later
MolOps::assignStereochemistry(*mol, true);
}
}
void MolPickler::molFromPickle(const std::string &pickle, ROMol *mol) {
PRECONDITION(mol, "empty molecule");
std::stringstream ss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
ss.write(pickle.c_str(), pickle.length());
MolPickler::molFromPickle(ss, mol);
}
//--------------------------------------
//
// Molecules
//
//--------------------------------------
template <typename T>
void MolPickler::_pickle(const ROMol *mol, std::ostream &ss,
unsigned int propertyFlags) {
PRECONDITION(mol, "empty molecule");
int32_t tmpInt;
bool includeAtomCoords = true;
std::map<int, int> atomIdxMap;
tmpInt = static_cast<int32_t>(mol->getNumAtoms());
streamWrite(ss, tmpInt);
tmpInt = static_cast<int32_t>(mol->getNumBonds());
streamWrite(ss, tmpInt);
char flag = 0;
if (includeAtomCoords) flag |= 0x1 << 7;
streamWrite(ss, flag);
// -------------------
//
// Write Atoms
//
// -------------------
streamWrite(ss, BEGINATOM);
ROMol::ConstAtomIterator atIt;
int nWritten = 0;
for (atIt = mol->beginAtoms(); atIt != mol->endAtoms(); ++atIt) {
_pickleAtom<T>(ss, *atIt);
atomIdxMap[(*atIt)->getIdx()] = nWritten;
nWritten++;
}
// -------------------
//
// Write Bonds
//
// -------------------
streamWrite(ss, BEGINBOND);
for (unsigned int i = 0; i < mol->getNumBonds(); i++) {
_pickleBond<T>(ss, mol->getBondWithIdx(i), atomIdxMap);
}
// -------------------
//
// Write Rings (if present)
//
// -------------------
const RingInfo *ringInfo = mol->getRingInfo();
if (ringInfo && ringInfo->isInitialized()) {
streamWrite(ss, BEGINSSSR);
_pickleSSSR<T>(ss, ringInfo, atomIdxMap);
}
// Write Stereo Groups
{
auto &stereo_groups = mol->getStereoGroups();
if (stereo_groups.size() > 0u) {
streamWrite(ss, BEGINSTEREOGROUP);
_pickleStereo<T>(ss, stereo_groups, atomIdxMap);
}
}
// pickle the conformations if necessary
if (includeAtomCoords) {
streamWrite(ss, BEGINCONFS);
tmpInt = static_cast<int32_t>(mol->getNumConformers());
streamWrite(ss, tmpInt);
ROMol::ConstConformerIterator ci;
for (ci = mol->beginConformers(); ci != mol->endConformers(); ++ci) {
const Conformer *conf = ci->get();
_pickleConformer<T>(ss, conf);
}
}
if (propertyFlags & PicklerOps::MolProps) {
streamWrite(ss, BEGINPROPS);
_pickleProperties(ss, *mol, propertyFlags);
streamWrite(ss, ENDPROPS);
}
if (propertyFlags & PicklerOps::AtomProps) {
streamWrite(ss, BEGINATOMPROPS);
for (ROMol::ConstAtomIterator atIt = mol->beginAtoms();
atIt != mol->endAtoms(); ++atIt) {
_pickleProperties(ss, **atIt, propertyFlags);
}
streamWrite(ss, ENDPROPS);
}
if (propertyFlags & PicklerOps::BondProps) {
streamWrite(ss, BEGINBONDPROPS);
for (ROMol::ConstBondIterator bondIt = mol->beginBonds();
bondIt != mol->endBonds(); ++bondIt) {
_pickleProperties(ss, **bondIt, propertyFlags);
}
streamWrite(ss, ENDPROPS);
}
streamWrite(ss, ENDMOL);
}
template <typename T>
void MolPickler::_depickle(std::istream &ss, ROMol *mol, int version,
int numAtoms) {
PRECONDITION(mol, "empty molecule");
bool directMap = mol->getNumAtoms() == 0;
Tags tag;
int32_t tmpInt;
// int numAtoms,numBonds;
int numBonds;
bool haveQuery = false;
streamRead(ss, tmpInt, version);
numBonds = tmpInt;
// did we include coordinates
bool includeCoords = false;
if (version >= 3000) {
char flag;
streamRead(ss, flag, version);
if (flag & 0x1 << 7) includeCoords = true;
}
// -------------------
//
// Read Atoms
//
// -------------------
streamRead(ss, tag, version);
if (tag != BEGINATOM) {
throw MolPicklerException("Bad pickle format: BEGINATOM tag not found.");
}
Conformer *conf = nullptr;
if ((version >= 2000 && version < 3000) && includeCoords) {
// there can only one conformation - since the poositions were stored on
// the atoms themselves in this version
conf = new Conformer(numAtoms);
mol->addConformer(conf, true);
}
for (int i = 0; i < numAtoms; i++) {
RDGeom::Point3D pos;
Atom *atom = _addAtomFromPickle<T>(ss, mol, pos, version, directMap);
if ((version >= 2000 && version < 3000) && includeCoords) {
// this is a older pickle so we go the pos
conf->setAtomPos(i, pos);
}
if (!directMap) {
mol->setAtomBookmark(atom, i);
}
if (atom->hasQuery()) {
haveQuery = true;
}
}
// -------------------
//
// Read Bonds
//
// -------------------
streamRead(ss, tag, version);
if (tag != BEGINBOND) {
throw MolPicklerException("Bad pickle format: BEGINBOND tag not found.");
}
for (int i = 0; i < numBonds; i++) {
Bond *bond = _addBondFromPickle<T>(ss, mol, version, directMap);
if (!directMap) {
mol->setBondBookmark(bond, i);
}
}
// -------------------
//
// Read Rings (if needed)
//
// -------------------
streamRead(ss, tag, version);
if (tag == BEGINSSSR) {
_addRingInfoFromPickle<T>(ss, mol, version, directMap);
streamRead(ss, tag, version);
}
if (tag == BEGINSTEREOGROUP) {
_depickleStereo<T>(ss, mol, version);
streamRead(ss, tag, version);
}
if (tag == BEGINCONFS) {
// read in the conformation
streamRead(ss, tmpInt, version);
int i;
for (i = 0; i < tmpInt; i++) {
Conformer *conf = _conformerFromPickle<T>(ss, version);
mol->addConformer(conf);
}
streamRead(ss, tag, version);
}
while (tag != ENDMOL) {
if (tag == BEGINPROPS) {
_unpickleProperties(ss, *mol);
streamRead(ss, tag, version);
} else if (tag == BEGINATOMPROPS) {
for (ROMol::AtomIterator atIt = mol->beginAtoms();
atIt != mol->endAtoms(); ++atIt) {
_unpickleProperties(ss, **atIt);
}
streamRead(ss, tag, version);
} else if (tag == BEGINBONDPROPS) {
for (ROMol::BondIterator bdIt = mol->beginBonds();
bdIt != mol->endBonds(); ++bdIt) {
_unpickleProperties(ss, **bdIt);
}
streamRead(ss, tag, version);
} else if (tag == BEGINQUERYATOMDATA) {
for (ROMol::AtomIterator atIt = mol->beginAtoms();
atIt != mol->endAtoms(); ++atIt) {
_unpickleAtomData(ss, *atIt, version);
}
streamRead(ss, tag, version);
} else {
break; // break to tag != ENDMOL
}
if (tag != ENDPROPS) {
throw MolPicklerException("Bad pickle format: ENDPROPS tag not found.");
}
streamRead(ss, tag, version);
}
if (tag != ENDMOL) {
throw MolPicklerException("Bad pickle format: ENDMOL tag not found.");
}
if (haveQuery) {
// we didn't read any property info for atoms with associated
// queries. update their property caches
// (was sf.net Issue 3316407)
for (ROMol::AtomIterator atIt = mol->beginAtoms(); atIt != mol->endAtoms();
++atIt) {
Atom *atom = *atIt;
if (atom->hasQuery()) {
atom->updatePropertyCache(false);
}
}
}
}
//--------------------------------------
//
// Atoms
//
//--------------------------------------
namespace {
bool getAtomMapNumber(const Atom *atom, int &mapNum) {
PRECONDITION(atom, "bad atom");
if (!atom->hasProp(common_properties::molAtomMapNumber)) return false;
bool res = true;
int tmpInt;
try {
atom->getProp(common_properties::molAtomMapNumber, tmpInt);
} catch (boost::bad_any_cast &) {
const std::string &tmpSVal =
atom->getProp<std::string>(common_properties::molAtomMapNumber);
try {
tmpInt = boost::lexical_cast<int>(tmpSVal);
} catch (boost::bad_lexical_cast &) {
res = false;
}
}
if (res) mapNum = tmpInt;
return res;
}
} // namespace
int32_t MolPickler::_pickleAtomData(std::ostream &tss, const Atom *atom) {
int32_t propFlags = 0;
char tmpChar;
signed char tmpSchar;
// tmpFloat=atom->getMass()-PeriodicTable::getTable()->getAtomicWeight(atom->getAtomicNum());
// if(fabs(tmpFloat)>.0001){
// propFlags |= 1;
// streamWrite(tss,tmpFloat);
// }
tmpSchar = static_cast<signed char>(atom->getFormalCharge());
if (tmpSchar != 0) {
propFlags |= 1 << 1;
streamWrite(tss, tmpSchar);
}
tmpChar = static_cast<char>(atom->getChiralTag());
if (tmpChar != 0) {
propFlags |= 1 << 2;
streamWrite(tss, tmpChar);
}
tmpChar = static_cast<char>(atom->getHybridization());
if (tmpChar != static_cast<char>(Atom::SP3)) {
propFlags |= 1 << 3;
streamWrite(tss, tmpChar);
}
tmpChar = static_cast<char>(atom->getNumExplicitHs());
if (tmpChar != 0) {
propFlags |= 1 << 4;
streamWrite(tss, tmpChar);
}
if (atom->d_explicitValence > 0) {
tmpChar = static_cast<char>(atom->d_explicitValence);
propFlags |= 1 << 5;
streamWrite(tss, tmpChar);
}
if (atom->d_implicitValence > 0) {
tmpChar = static_cast<char>(atom->d_implicitValence);
propFlags |= 1 << 6;
streamWrite(tss, tmpChar);
}
tmpChar = static_cast<char>(atom->getNumRadicalElectrons());
if (tmpChar != 0) {
propFlags |= 1 << 7;
streamWrite(tss, tmpChar);
}
unsigned int tmpuint = atom->getIsotope();
if (tmpuint > 0) {
propFlags |= 1 << 8;
streamWrite(tss, tmpuint);
}
return propFlags;
}
void MolPickler::_unpickleAtomData(std::istream &ss, Atom *atom, int version) {
int propFlags;
char tmpChar;
signed char tmpSchar;
streamRead(ss, propFlags, version);
if (propFlags & 1) {
float tmpFloat;
streamRead(ss, tmpFloat, version);
int iso = static_cast<int>(floor(tmpFloat + atom->getMass() + .0001));
atom->setIsotope(iso);
}
if (propFlags & (1 << 1)) {
streamRead(ss, tmpSchar, version);
} else {
tmpSchar = 0;
}
atom->setFormalCharge(static_cast<int>(tmpSchar));
if (propFlags & (1 << 2)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = 0;
}
atom->setChiralTag(static_cast<Atom::ChiralType>(tmpChar));
if (propFlags & (1 << 3)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = Atom::SP3;
}
atom->setHybridization(static_cast<Atom::HybridizationType>(tmpChar));
if (propFlags & (1 << 4)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = 0;
}
atom->setNumExplicitHs(tmpChar);
if (propFlags & (1 << 5)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = 0;
}
atom->d_explicitValence = tmpChar;
if (propFlags & (1 << 6)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = 0;
}
atom->d_implicitValence = tmpChar;
if (propFlags & (1 << 7)) {
streamRead(ss, tmpChar, version);
} else {
tmpChar = 0;
}
atom->d_numRadicalElectrons = static_cast<unsigned int>(tmpChar);
atom->d_isotope = 0;
if (propFlags & (1 << 8)) {
unsigned int tmpuint;
streamRead(ss, tmpuint, version);
atom->setIsotope(tmpuint);
}
}
// T refers to the type of the atom indices written
template <typename T>
void MolPickler::_pickleAtom(std::ostream &ss, const Atom *atom) {
PRECONDITION(atom, "empty atom");
char tmpChar;
unsigned char tmpUchar;
int tmpInt;
char flags;
tmpUchar = atom->getAtomicNum() % 256;
streamWrite(ss, tmpUchar);
flags = 0;
if (atom->getIsAromatic()) flags |= 0x1 << 6;
if (atom->getNoImplicit()) flags |= 0x1 << 5;
if (atom->hasQuery()) flags |= 0x1 << 4;
if (getAtomMapNumber(atom, tmpInt)) flags |= 0x1 << 3;
if (atom->hasProp(common_properties::dummyLabel)) flags |= 0x1 << 2;
if (atom->getMonomerInfo()) flags |= 0x1 << 1;
streamWrite(ss, flags);
std::stringstream tss(std::ios_base::binary | std::ios_base::out |
std::ios_base::in);
int32_t propFlags = _pickleAtomData(tss, atom);
streamWrite(ss, propFlags);
ss.write(tss.str().c_str(), tss.str().size());
if (atom->hasQuery()) {
streamWrite(ss, BEGINQUERY);
pickleQuery(ss, static_cast<const QueryAtom *>(atom)->getQuery());
streamWrite(ss, ENDQUERY);
}
if (getAtomMapNumber(atom, tmpInt)) {
if (tmpInt < 128) {
tmpChar = static_cast<char>(tmpInt % 128);
streamWrite(ss, ATOM_MAPNUMBER, tmpChar);
} else {
tmpChar = static_cast<char>(255);
streamWrite(ss, ATOM_MAPNUMBER, tmpChar);
streamWrite(ss, tmpInt);
}
}
if (atom->hasProp(common_properties::dummyLabel)) {
streamWrite(ss, ATOM_DUMMYLABEL,
atom->getProp<std::string>(common_properties::dummyLabel));
}
if (atom->getMonomerInfo()) {
streamWrite(ss, BEGIN_ATOM_MONOMER);
pickleAtomMonomerInfo(ss, atom->getMonomerInfo());
streamWrite(ss, END_ATOM_MONOMER);
}
}
template <typename T>
void MolPickler::_pickleConformer(std::ostream &ss, const Conformer *conf) {
PRECONDITION(conf, "empty conformer");
char tmpChr = static_cast<int>(conf->is3D());
streamWrite(ss, tmpChr);
int32_t tmpInt = static_cast<int32_t>(conf->getId());
streamWrite(ss, tmpInt);
T tmpT = static_cast<T>(conf->getNumAtoms());
streamWrite(ss, tmpT);
const RDGeom::POINT3D_VECT &pts = conf->getPositions();
for (const auto &pt : pts) {
float tmpFloat;
tmpFloat = static_cast<float>(pt.x);
streamWrite(ss, tmpFloat);
tmpFloat = static_cast<float>(pt.y);
streamWrite(ss, tmpFloat);
tmpFloat = static_cast<float>(pt.z);
streamWrite(ss, tmpFloat);
}
}
template <typename T>
Conformer *MolPickler::_conformerFromPickle(std::istream &ss, int version) {
float tmpFloat;
bool is3D = true;
if (version > 4000) {
char tmpChr;
streamRead(ss, tmpChr, version);
is3D = static_cast<bool>(tmpChr);
}
int tmpInt;
streamRead(ss, tmpInt, version);
unsigned int cid = static_cast<unsigned int>(tmpInt);
T tmpT;
streamRead(ss, tmpT, version);
unsigned int numAtoms = static_cast<unsigned int>(tmpT);
auto *conf = new Conformer(numAtoms);
conf->setId(cid);
conf->set3D(is3D);
for (unsigned int i = 0; i < numAtoms; i++) {
streamRead(ss, tmpFloat, version);
conf->getAtomPos(i).x = static_cast<double>(tmpFloat);
streamRead(ss, tmpFloat, version);
conf->getAtomPos(i).y = static_cast<double>(tmpFloat);
streamRead(ss, tmpFloat, version);
conf->getAtomPos(i).z = static_cast<double>(tmpFloat);
}
return conf;
}
template <typename T>
Atom *MolPickler::_addAtomFromPickle(std::istream &ss, ROMol *mol,
RDGeom::Point3D &pos, int version,
bool directMap) {
RDUNUSED_PARAM(directMap);
PRECONDITION(mol, "empty molecule");
float x, y, z;
char tmpChar;
unsigned char tmpUchar;
signed char tmpSchar;
char flags;
Tags tag;
Atom *atom = nullptr;
int atomicNum = 0;
streamRead(ss, tmpUchar, version);
atomicNum = tmpUchar;
bool hasQuery = false;
streamRead(ss, flags, version);
if (version > 5000) {
hasQuery = flags & 0x1 << 4;
}
if (!hasQuery) {
atom = new Atom(atomicNum);
} else {
atom = new QueryAtom();
if (atomicNum) {
// can't set this in the constructor because that builds a
// query and we're going to take care of that later:
atom->setAtomicNum(atomicNum);
}
}
atom->setIsAromatic(flags & 0x1 << 6);
atom->setNoImplicit(flags & 0x1 << 5);
bool hasAtomMap = 0, hasDummyLabel = 0;
if (version >= 6020) {
hasAtomMap = flags & 0x1 << 3;
hasDummyLabel = flags & 0x1 << 2;
}
bool hasMonomerInfo = 0;
if (version >= 7020) {
hasMonomerInfo = flags & 0x1 << 1;
}
// are coordinates present?
if (flags & 0x1 << 7) {
streamRead(ss, x, version);
pos.x = static_cast<double>(x);
streamRead(ss, y, version);
pos.y = static_cast<double>(y);
streamRead(ss, z, version);
pos.z = static_cast<double>(z);
}
if (version <= 5000 || !hasQuery) {
if (version < 7000) {
if (version < 6030) {
streamRead(ss, tmpSchar, version);
// FIX: technically should be handling this in order to maintain true
// backwards compatibility
// atom->setMass(PeriodicTable::getTable()->getAtomicWeight(atom->getAtomicNum())+
// static_cast<int>(tmpSchar));
} else {
float tmpFloat;
streamRead(ss, tmpFloat, version);
// FIX: technically should be handling this in order to maintain true
// backwards compatibility
// atom->setMass(tmpFloat);
}
streamRead(ss, tmpSchar, version);
atom->setFormalCharge(static_cast<int>(tmpSchar));
streamRead(ss, tmpChar, version);
atom->setChiralTag(static_cast<Atom::ChiralType>(tmpChar));
streamRead(ss, tmpChar, version);
atom->setHybridization(static_cast<Atom::HybridizationType>(tmpChar));
streamRead(ss, tmpChar, version);
atom->setNumExplicitHs(static_cast<int>(tmpChar));
streamRead(ss, tmpChar, version);
atom->d_explicitValence = tmpChar;
streamRead(ss, tmpChar, version);
atom->d_implicitValence = tmpChar;
if (version > 6000) {
streamRead(ss, tmpChar, version);
atom->d_numRadicalElectrons = static_cast<unsigned int>(tmpChar);
}
} else {
_unpickleAtomData(ss, atom, version);
}
} else if (version > 5000) {
// we have a query
if (version >= 9000) {
_unpickleAtomData(ss, atom, version);
}
streamRead(ss, tag, version);
if (tag != BEGINQUERY) {
throw MolPicklerException("Bad pickle format: BEGINQUERY tag not found.");
}
static_cast<QueryAtom *>(atom)->setQuery(unpickleQuery(ss, atom, version));
streamRead(ss, tag, version);
if (tag != ENDQUERY) {
throw MolPicklerException("Bad pickle format: ENDQUERY tag not found.");
}
// atom->setNumExplicitHs(0);
}
if (version > 5000) {
if (version < 6020) {
unsigned int sPos = rdcast<unsigned int>(ss.tellg());
Tags tag;
streamRead(ss, tag, version);
if (tag == ATOM_MAPNUMBER) {
int32_t tmpInt;
streamRead(ss, tmpChar, version);
tmpInt = tmpChar;
atom->setProp(common_properties::molAtomMapNumber, tmpInt);
} else {
ss.seekg(sPos);
}
} else {
if (hasAtomMap) {
Tags tag;
streamRead(ss, tag, version);
if (tag != ATOM_MAPNUMBER) {
throw MolPicklerException(
"Bad pickle format: ATOM_MAPNUMBER tag not found.");
}
int tmpInt;
streamRead(ss, tmpChar, version);
if (tmpChar < 0 && version > 9000) {
streamRead(ss, tmpInt, version);
} else {
tmpInt = tmpChar;
}
atom->setProp(common_properties::molAtomMapNumber, tmpInt);
}
if (hasDummyLabel) {
streamRead(ss, tag, version);
if (tag != ATOM_DUMMYLABEL) {
throw MolPicklerException(
"Bad pickle format: ATOM_DUMMYLABEL tag not found.");
}
std::string tmpStr;
streamRead(ss, tmpStr, version);
atom->setProp(common_properties::dummyLabel, tmpStr);
}
}
}
if (version >= 7020) {
if (hasMonomerInfo) {
streamRead(ss, tag, version);
if (tag != BEGIN_ATOM_MONOMER) {
throw MolPicklerException(
"Bad pickle format: BEGIN_ATOM_MONOMER tag not found.");
}
atom->setMonomerInfo(unpickleAtomMonomerInfo(ss, version));
}
}
mol->addAtom(atom, false, true);
return atom;
}
//--------------------------------------
//
// Bonds
//
//--------------------------------------
template <typename T>
void MolPickler::_pickleBond(std::ostream &ss, const Bond *bond,
std::map<int, int> &atomIdxMap) {
PRECONDITION(bond, "empty bond");
T tmpT;
char tmpChar;
char flags;
tmpT = static_cast<T>(atomIdxMap[bond->getBeginAtomIdx()]);
streamWrite(ss, tmpT);
tmpT = static_cast<T>(atomIdxMap[bond->getEndAtomIdx()]);
streamWrite(ss, tmpT);
flags = 0;
if (bond->getIsAromatic()) flags |= 0x1 << 6;
if (bond->getIsConjugated()) flags |= 0x1 << 5;
if (bond->hasQuery()) flags |= 0x1 << 4;
if (bond->getBondType() != Bond::SINGLE) flags |= 0x1 << 3;
if (bond->getBondDir() != Bond::NONE) flags |= 0x1 << 2;
if (bond->getStereo() != Bond::STEREONONE) flags |= 0x1 << 1;
streamWrite(ss, flags);
if (bond->getBondType() != Bond::SINGLE) {
tmpChar = static_cast<char>(bond->getBondType());
streamWrite(ss, tmpChar);
}
if (bond->getBondDir() != Bond::NONE) {
tmpChar = static_cast<char>(bond->getBondDir());
streamWrite(ss, tmpChar);
}
// write info about the stereochemistry:
if (bond->getStereo() != Bond::STEREONONE) {
tmpChar = static_cast<char>(bond->getStereo());
streamWrite(ss, tmpChar);
const INT_VECT &stereoAts = bond->getStereoAtoms();
tmpChar = rdcast<unsigned int>(stereoAts.size());
streamWrite(ss, tmpChar);
for (int stereoAt : stereoAts) {
tmpT = static_cast<T>(stereoAt);
streamWrite(ss, tmpT);
}
}
if (bond->hasQuery()) {
streamWrite(ss, BEGINQUERY);
pickleQuery(ss, static_cast<const QueryBond *>(bond)->getQuery());
streamWrite(ss, ENDQUERY);
}
}
template <typename T>
Bond *MolPickler::_addBondFromPickle(std::istream &ss, ROMol *mol, int version,
bool directMap) {
PRECONDITION(mol, "empty molecule");
char tmpChar;
char flags;
int begIdx, endIdx;
T tmpT;
Bond *bond = nullptr;
streamRead(ss, tmpT, version);
if (directMap) {
begIdx = tmpT;
} else {
begIdx = mol->getAtomWithBookmark(static_cast<int>(tmpT))->getIdx();
}
streamRead(ss, tmpT, version);
if (directMap) {
endIdx = tmpT;
} else {
endIdx = mol->getAtomWithBookmark(static_cast<int>(tmpT))->getIdx();
}
streamRead(ss, flags, version);
bool hasQuery = flags & 0x1 << 4;
if (version <= 5000 || (version <= 7000 && !hasQuery) || version > 7000) {
bond = new Bond();
bond->setIsAromatic(flags & 0x1 << 6);
bond->setIsConjugated(flags & 0x1 << 5);
if (version < 7000) {
streamRead(ss, tmpChar, version);
bond->setBondType(static_cast<Bond::BondType>(tmpChar));
streamRead(ss, tmpChar, version);
bond->setBondDir(static_cast<Bond::BondDir>(tmpChar));
if (version > 3000) {
streamRead(ss, tmpChar, version);
Bond::BondStereo stereo = static_cast<Bond::BondStereo>(tmpChar);
bond->setStereo(stereo);
if (stereo != Bond::STEREONONE) {
streamRead(ss, tmpChar, version);
for (char i = 0; i < tmpChar; ++i) {
streamRead(ss, tmpT, version);
bond->getStereoAtoms().push_back(static_cast<int>(tmpT));
}
}
}
} else {
if (flags & (0x1 << 3)) {
streamRead(ss, tmpChar, version);
bond->setBondType(static_cast<Bond::BondType>(tmpChar));
} else {
bond->setBondType(Bond::SINGLE);
}
if (flags & (0x1 << 2)) {
streamRead(ss, tmpChar, version);
bond->setBondDir(static_cast<Bond::BondDir>(tmpChar));
} else {
bond->setBondDir(Bond::NONE);
}
if (flags & (0x1 << 1)) {
streamRead(ss, tmpChar, version);
Bond::BondStereo stereo = static_cast<Bond::BondStereo>(tmpChar);
streamRead(ss, tmpChar, version);
for (char i = 0; i < tmpChar; ++i) {
streamRead(ss, tmpT, version);
bond->getStereoAtoms().push_back(static_cast<int>(tmpT));
}
bond->setStereo(stereo);
} else {
bond->setStereo(Bond::STEREONONE);
}
}
}
if (version > 5000 && hasQuery) {
Tags tag;
if (bond) {
Bond *tbond = bond;
bond = new QueryBond(*bond);
delete tbond;
} else {
bond = new QueryBond();
}
// we have a query:
streamRead(ss, tag, version);
if (tag != BEGINQUERY) {
throw MolPicklerException("Bad pickle format: BEGINQUERY tag not found.");
}
static_cast<QueryBond *>(bond)->setQuery(unpickleQuery(ss, bond, version));
streamRead(ss, tag, version);
if (tag != ENDQUERY) {
throw MolPicklerException("Bad pickle format: ENDQUERY tag not found.");
}
}
if (bond) {
bond->setBeginAtomIdx(begIdx);
bond->setEndAtomIdx(endIdx);
mol->addBond(bond, true);
}
return bond;
}
//--------------------------------------
//
// Rings
//
//--------------------------------------
template <typename T>
void MolPickler::_pickleSSSR(std::ostream &ss, const RingInfo *ringInfo,
std::map<int, int> &atomIdxMap) {
PRECONDITION(ringInfo, "missing ring info");
T tmpT;
tmpT = ringInfo->numRings();
streamWrite(ss, tmpT);
for (unsigned int i = 0; i < ringInfo->numRings(); i++) {
INT_VECT ring;
ring = ringInfo->atomRings()[i];
tmpT = static_cast<T>(ring.size());
streamWrite(ss, tmpT);
for (int &j : ring) {
tmpT = static_cast<T>(atomIdxMap[j]);
streamWrite(ss, tmpT);
}
#if 0
ring = ringInfo->bondRings()[i];
tmpT = static_cast<T>(ring.size());
streamWrite(ss,tmpT);
for(unsigned int j=0;j<ring.size();j++){
tmpT = static_cast<T>(ring[j]);
streamWrite(ss,tmpT);
}
#endif
}
}
template <typename T>
void MolPickler::_addRingInfoFromPickle(std::istream &ss, ROMol *mol,
int version, bool directMap) {
PRECONDITION(mol, "empty molecule");
RingInfo *ringInfo = mol->getRingInfo();
if (!ringInfo->isInitialized()) ringInfo->initialize();
T numRings;
streamRead(ss, numRings, version);
if (numRings > 0) {
ringInfo->preallocate(mol->getNumAtoms(), mol->getNumBonds());
for (unsigned int i = 0; i < static_cast<unsigned int>(numRings); i++) {
T tmpT;
T ringSize;
streamRead(ss, ringSize, version);
INT_VECT atoms(static_cast<int>(ringSize));
INT_VECT bonds(static_cast<int>(ringSize));
for (unsigned int j = 0; j < static_cast<unsigned int>(ringSize); j++) {
streamRead(ss, tmpT, version);
if (directMap) {
atoms[j] = static_cast<int>(tmpT);
} else {
atoms[j] = mol->getAtomWithBookmark(static_cast<int>(tmpT))->getIdx();
}
}
if (version < 7000) {
for (unsigned int j = 0; j < static_cast<unsigned int>(ringSize); j++) {
streamRead(ss, tmpT, version);
if (directMap) {
bonds[j] = static_cast<int>(tmpT);
} else {
bonds[j] =
mol->getBondWithBookmark(static_cast<int>(tmpT))->getIdx();
}
}
} else {
for (unsigned int j = 1; j < static_cast<unsigned int>(ringSize); ++j) {
bonds[j - 1] =
mol->getBondBetweenAtoms(atoms[j - 1], atoms[j])->getIdx();
}
bonds[ringSize - 1] =
mol->getBondBetweenAtoms(atoms[0], atoms[ringSize - 1])->getIdx();
}
ringInfo->addRing(atoms, bonds);
}
}
}
template <typename T>
void MolPickler::_pickleStereo(std::ostream &ss,
const std::vector<StereoGroup> &groups,
std::map<int, int> &atomIdxMap) {
T tmpT = static_cast<T>(groups.size());
streamWrite(ss, tmpT);
for (auto &&group : groups) {
streamWrite(ss, static_cast<T>(group.getGroupType()));
auto& atoms = group.getAtoms();
streamWrite(ss, static_cast<T>(atoms.size()));
for (auto &&atom : atoms) {
tmpT = static_cast<T>(atomIdxMap[atom->getIdx()]);
streamWrite(ss, tmpT);
}
}
}
template <typename T>
void MolPickler::_depickleStereo(std::istream &ss, ROMol *mol, int version) {
T tmpT;
streamRead(ss, tmpT, version);
const unsigned numGroups = static_cast<unsigned>(tmpT);
if (numGroups > 0u) {
std::vector<StereoGroup> groups;
for (unsigned group = 0u; group < numGroups; ++group) {
T tmpT;
streamRead(ss, tmpT, version);
const auto groupType = static_cast<RDKit::StereoGroupType>(tmpT);
streamRead(ss, tmpT, version);
const unsigned numAtoms = static_cast<unsigned>(tmpT);
std::vector<Atom *> atoms;
atoms.reserve(numAtoms);
for (unsigned i = 0u; i < numAtoms; ++i) {
streamRead(ss, tmpT, version);
atoms.push_back(mol->getAtomWithIdx(tmpT));
}
groups.emplace_back(groupType, std::move(atoms));
}
mol->setStereoGroups(std::move(groups));
}
}
void MolPickler::_pickleProperties(std::ostream &ss, const RDProps &props,
unsigned int pickleFlags) {
if (!pickleFlags) return;
streamWriteProps(ss, props, pickleFlags & PicklerOps::PrivateProps,
pickleFlags & PicklerOps::ComputedProps);
}
//! unpickle standard properties
void MolPickler::_unpickleProperties(std::istream &ss, RDProps &props) {
streamReadProps(ss, props);
}
//--------------------------------------
//
// Version 1 Pickler:
//
// NOTE: this is not 64bit clean, but it shouldn't be used anymore anyway
//
//--------------------------------------
void MolPickler::_pickleV1(const ROMol *mol, std::ostream &ss) {
PRECONDITION(mol, "empty molecule");
ROMol::ConstAtomIterator atIt;
const Conformer *conf = nullptr;
if (mol->getNumConformers() > 0) {
conf = &(mol->getConformer());
}
for (atIt = mol->beginAtoms(); atIt != mol->endAtoms(); ++atIt) {
const Atom *atom = *atIt;
streamWrite(ss, BEGINATOM);
streamWrite(ss, ATOM_NUMBER, atom->getAtomicNum());
streamWrite(ss, ATOM_INDEX, atom->getIdx());
streamWrite(ss, ATOM_POS);
RDGeom::Point3D p;
if (conf) {
p = conf->getAtomPos(atom->getIdx());
}
streamWrite(ss, p.x);
streamWrite(ss, p.y);
streamWrite(ss, p.z);
if (atom->getFormalCharge() != 0) {
streamWrite(ss, ATOM_CHARGE, atom->getFormalCharge());
}
if (atom->getNumExplicitHs() != 0) {
streamWrite(ss, ATOM_NEXPLICIT, atom->getNumExplicitHs());
}
if (atom->getChiralTag() != 0) {
streamWrite(ss, ATOM_CHIRALTAG, atom->getChiralTag());
}
if (atom->getIsAromatic()) {
streamWrite(ss, ATOM_ISAROMATIC,
static_cast<char>(atom->getIsAromatic()));
}
streamWrite(ss, ENDATOM);
}
ROMol::ConstBondIterator bondIt;
for (bondIt = mol->beginBonds(); bondIt != mol->endBonds(); ++bondIt) {
const Bond *bond = *bondIt;
streamWrite(ss, BEGINBOND);
streamWrite(ss, BOND_INDEX, bond->getIdx());
streamWrite(ss, BOND_BEGATOMIDX, bond->getBeginAtomIdx());
streamWrite(ss, BOND_ENDATOMIDX, bond->getEndAtomIdx());
streamWrite(ss, BOND_TYPE, bond->getBondType());
if (bond->getBondDir()) {
streamWrite(ss, BOND_DIR, bond->getBondDir());
}
streamWrite(ss, ENDBOND);
}
streamWrite(ss, ENDMOL);
}
void MolPickler::_depickleV1(std::istream &ss, ROMol *mol) {
PRECONDITION(mol, "empty molecule");
Tags tag;
auto *conf = new Conformer();
mol->addConformer(conf);
streamRead(ss, tag, 1);
while (tag != ENDMOL) {
switch (tag) {
case BEGINATOM:
_addAtomFromPickleV1(ss, mol);
break;
case BEGINBOND:
_addBondFromPickleV1(ss, mol);
break;
default:
UNDER_CONSTRUCTION("bad tag in pickle");
}
streamRead(ss, tag, 1);
}
mol->clearAllAtomBookmarks();
mol->clearAllBondBookmarks();
}
void MolPickler::_addAtomFromPickleV1(std::istream &ss, ROMol *mol) {
PRECONDITION(mol, "empty molecule");
Tags tag;
int intVar;
double dblVar;
char charVar;
int version = 1;
streamRead(ss, tag, version);
auto *atom = new Atom();
Conformer &conf = mol->getConformer();
RDGeom::Point3D pos;
while (tag != ENDATOM) {
switch (tag) {
case ATOM_INDEX:
streamRead(ss, intVar, version);
mol->setAtomBookmark(atom, intVar);
break;
case ATOM_NUMBER:
streamRead(ss, intVar, version);
atom->setAtomicNum(intVar);
break;
case ATOM_POS:
streamRead(ss, pos.x, version);
streamRead(ss, pos.y, version);
streamRead(ss, pos.z, version);
break;
case ATOM_CHARGE:
streamRead(ss, intVar, version);
atom->setFormalCharge(intVar);
break;
case ATOM_NEXPLICIT:
streamRead(ss, intVar, version);
atom->setNumExplicitHs(intVar);
break;
case ATOM_CHIRALTAG:
streamRead(ss, intVar, version);
atom->setChiralTag(static_cast<Atom::ChiralType>(intVar));
break;
case ATOM_MASS:
streamRead(ss, dblVar, version);
// we don't need to set this anymore, but we do need to read it in order
// to
// maintain backwards compatibility
break;
case ATOM_ISAROMATIC:
streamRead(ss, charVar, version);
atom->setIsAromatic(charVar);
break;
default:
ASSERT_INVARIANT(0, "bad tag in atom block of pickle");
}
streamRead(ss, tag, version);
}
unsigned int id = mol->addAtom(atom, false, true);
conf.setAtomPos(id, pos);
}
void MolPickler::_addBondFromPickleV1(std::istream &ss, ROMol *mol) {
PRECONDITION(mol, "empty molecule");
Tags tag;
int intVar, idx = -1;
int version = 1;
Bond::BondType bt;
Bond::BondDir bd;
streamRead(ss, tag, version);
auto *bond = new Bond();
while (tag != ENDBOND) {
switch (tag) {
case BOND_INDEX:
streamRead(ss, idx, version);
break;
case BOND_BEGATOMIDX:
streamRead(ss, intVar, version);
bond->setBeginAtomIdx(mol->getAtomWithBookmark(intVar)->getIdx());
break;
case BOND_ENDATOMIDX:
streamRead(ss, intVar, version);
bond->setEndAtomIdx(mol->getAtomWithBookmark(intVar)->getIdx());
break;
case BOND_TYPE:
streamRead(ss, bt, version);
bond->setBondType(bt);
break;
case BOND_DIR:
streamRead(ss, bd, version);
bond->setBondDir(bd);
break;
default:
ASSERT_INVARIANT(0, "bad tag in bond block of pickle");
}
streamRead(ss, tag, version);
}
mol->addBond(bond, true);
}
}; // namespace RDKit
|