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 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#include <BALL/DOCKING/COMMON/poseClustering.h>
#include <BALL/STRUCTURE/structureMapper.h>
#include <BALL/STRUCTURE/RMSDMinimizer.h>
#include <BALL/STRUCTURE/geometricProperties.h>
#include <BALL/FORMAT/lineBasedFile.h>
// TEST
//#include <BALL/MATHS/angle.h>
#include <stack>
#include <queue>
#include <boost/version.hpp>
#include <boost/graph/iteration_macros.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/adj_list_serialize.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/variant.hpp>
using namespace std;
using namespace Eigen;
namespace BALL
{
const String PoseClustering::Option::CLUSTER_METHOD = "cluster_method";
const Index PoseClustering::Default::CLUSTER_METHOD = PoseClustering::CLINK_DEFAYS;
const String PoseClustering::Option::RMSD_LEVEL_OF_DETAIL = "rmsd_level_of_detail";
const Index PoseClustering::Default::RMSD_LEVEL_OF_DETAIL = PoseClustering::C_ALPHA;
const String PoseClustering::Option::DISTANCE_THRESHOLD = "pose_clustering_rmsd_threshold";
const float PoseClustering::Default::DISTANCE_THRESHOLD = 3;
const String PoseClustering::Option::RMSD_TYPE = "pose_clustering_rmsd_type";
const Index PoseClustering::Default::RMSD_TYPE = PoseClustering::SNAPSHOT_RMSD;
const String PoseClustering::Option::RUN_PARALLEL = "pose_clustering_run_parallel";
const bool PoseClustering::Default::RUN_PARALLEL = true;
PoseClustering::PoseClustering()
: current_set_(0),
has_rigid_transformations_(false),
delete_conformation_set_(false)
{
setDefaultOptions();
}
PoseClustering::PoseClustering(ConformationSet* poses, float rmsd)
{
options[Option::DISTANCE_THRESHOLD] = rmsd;
current_set_ = poses;
storeSnapShotReferences_();
has_rigid_transformations_ = false;
delete_conformation_set_ = false;
base_system_ = poses->getSystem();
}
PoseClustering::PoseClustering(System const& base_system, String transformation_file_name)
{
has_rigid_transformations_ = false;
delete_conformation_set_ = false;
setBaseSystemAndTransformations(base_system, transformation_file_name);
}
PoseClustering::~PoseClustering()
{
if (delete_conformation_set_)
delete current_set_;
}
Eigen::Matrix3f PoseClustering::computeCovarianceMatrix(System const& system, Index rmsd_level_of_detail)
{
Eigen::Matrix3f covariance_matrix = Matrix3f::Zero();
Vector3f base_com(0, 0, 0);
// note: we need two loops: one to compute the center of mass, the other for the covariance matrix
Size num_points = 0;
for (AtomConstIterator at_it = system.beginAtom(); +at_it; ++at_it)
{
if (!isExcludedByLevelOfDetail_(&*at_it, rmsd_level_of_detail))
{
Vector3 const& pos = at_it->getPosition();
base_com += ((Eigen::Vector3f() << pos.x, pos.y, pos.z).finished());
++num_points;
}
}
base_com /= num_points;
for (AtomConstIterator at_it = system.beginAtom(); +at_it; ++at_it)
{
if (!isExcludedByLevelOfDetail_(&*at_it, rmsd_level_of_detail))
{
Vector3 const& pos = at_it->getPosition();
covariance_matrix.selfadjointView<Eigen::Upper>().rankUpdate(Eigen::Vector3f(pos.x, pos.y, pos.z) - base_com);
}
}
covariance_matrix /= num_points;
// for efficiency, we have only computed the upper triangle so far
covariance_matrix(1,0) = covariance_matrix(0,1);
covariance_matrix(2,0) = covariance_matrix(0,2);
covariance_matrix(2,1) = covariance_matrix(1,2);
return covariance_matrix;
}
std::vector<std::set<Index> > PoseClustering::extractClustersForThreshold(float threshold, Size min_size)
{
// cout << " extract clusters for dist = " << threshold << endl;
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
// for debug purposes, we might have to reset our cluster id
#ifdef POSECLUSTERING_DEBUG
BGL_FORALL_VERTICES(current_vertex, cluster_tree_, ClusterTree)
{
cluster_tree_[current_vertex].current_cluster_id = -1;
}
#endif
std::stack<ClusterTreeNode> stack;
ClusterTreeNode current_node;
ClusterTreeNode cluster_tree_root = cluster_tree_[boost::graph_bundle];
if (cluster_tree_[cluster_tree_root].merged_at < threshold)
{
clusters_.push_back(collectClusterBelow_(cluster_tree_root));
cluster_scores_.push_back(cluster_tree_[cluster_tree_root].merged_at);
#ifdef POSECLUSTERING_DEBUG
cluster_tree_[cluster_tree_root].current_cluster_id = clusters_.size()-1;
#endif
}
else
{
stack.push(cluster_tree_root);
while (!stack.empty())
{
current_node = stack.top();
stack.pop();
BGL_FORALL_ADJ(current_node, child, cluster_tree_, ClusterTree)
{
// is child itself a leaf?
if ( (cluster_tree_[child].merged_at < threshold)
|| (out_degree(child, cluster_tree_) == 0) )
{
clusters_.push_back(collectClusterBelow_(child));
cluster_scores_.push_back(cluster_tree_[child].merged_at);
#ifdef POSECLUSTERING_DEBUG
cluster_tree_[child].current_cluster_id = clusters_.size()-1;
#endif
}
else
{
stack.push(child);
}
}
}
}
if (min_size > 1)
{
std::vector< std::set<Index> >::iterator iter = clusters_.begin();
std::vector< float >::iterator score_iter = cluster_scores_.begin();
while (iter != clusters_.end())
{
if ((*iter).size() < min_size)
{
iter = clusters_.erase(iter);
score_iter = cluster_scores_.erase(score_iter);
}
else
{
++iter;
++score_iter;
}
}
}
return clusters_;
}
// up to n best clusters
std::vector<std::set<Index> > PoseClustering::extractNBestClusters(Size n)
{
//cout << "extractNBestClusters n=" << n << "/" << getNumberOfPoses() << endl;
// we cannot rely on getNumberOfPoses() as we may have been given a tree...
/*
if (n > getNumberOfPoses())
throw(Exception::OutOfRange(__FILE__, __LINE__));
*/
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
ClusterTreeNodeComparator comp(cluster_tree_);
std::priority_queue< ClusterTreeNode, std::vector<ClusterTreeNode>, ClusterTreeNodeComparator > prio(comp);
ClusterTreeNode current_node;
ClusterTreeNode cluster_tree_root = cluster_tree_[boost::graph_bundle];
prio.push(cluster_tree_root);
// read ahead
current_node = prio.top();
// first we look out for the first n best clusters
// we traverse the tree from the root in descending order
// of merged_at values
while ( (prio.size() + clusters_.size() < n ) &&
(!prio.empty()) //&& (out_degree(current_node, cluster_tree_) > 0)
)
{
prio.pop();
// check all children
BGL_FORALL_ADJ(current_node, child, cluster_tree_, ClusterTree)
{
// no leaf, traverse further
if (out_degree(child, cluster_tree_) > 0)
{
prio.push(child);
}
else // a leaf --> store as "cluster"
{
//std::set<Index> new_cluster = collectClusterBelow_(child);
std::set<Index> new_cluster;
new_cluster.insert(*(cluster_tree_[child].poses.begin()));
clusters_.push_back(new_cluster);
cluster_scores_.push_back(cluster_tree_[current_node].merged_at);
}
}
current_node = prio.top();
}
//cout << "found clusters: " << clusters_.size() << " size prio: " << prio.size() << " check min_size filter..." << endl;
// then we collect all clusters that pass the min_size filter
// --> we may end up with less than n clusters
while (!prio.empty())
{
// no leaves have been added to the priority queue
std::set<Index> new_cluster = collectClusterBelow_(prio.top());
/*for (std::set<Index>::iterator s_it = new_cluster.begin(); s_it != new_cluster.end(); ++s_it)
cout << *s_it << " ";
cout << endl;
*/
if (new_cluster.size() > 0) //= min_size)
{
clusters_.push_back(new_cluster);
cluster_scores_.push_back(cluster_tree_[prio.top()].merged_at);
}
prio.pop();
//cout << clusters_.size() << " " << prio.size() << endl;
}
//cout << "after min size filter we finally get: " << clusters_.size() << endl;
return clusters_;
}
std::vector<std::set<Index> > PoseClustering::filterClusters(Size min_size)
{
//cout << "Got " << clusters_.size() << " clusters and min_size= " << min_size << endl;
std::vector< std::set<Index> > temp_clusters;
std::vector< float > temp_cluster_scores;
//int c_counter = 0;
for (Size i = 0; i < clusters_.size(); i++)
{
if (clusters_[i].size() >= min_size)
{
/*
c_counter++;
cout << " ++ " << c_counter << " (" << clusters_[i].size() << "): ";
for (std::set<Index>::iterator s_it = clusters_[i].begin(); s_it != clusters_[i].end(); ++s_it)
cout << *s_it << " ";
cout << endl;
*/
temp_clusters.push_back(clusters_[i]);
temp_cluster_scores.push_back(cluster_scores_[i]);
}
}
swap(clusters_, temp_clusters);
swap(cluster_scores_, temp_cluster_scores);
cluster_representatives_.clear();
return clusters_;
}
void PoseClustering::setConformationSet(ConformationSet* new_set, bool precompute_atombijection)
{
if (delete_conformation_set_)
delete current_set_;
has_rigid_transformations_ = false;
transformations_.clear();
current_set_ = new_set;
base_system_ = new_set->getSystem();
storeSnapShotReferences_();
if (precompute_atombijection)
precomputeAtomBijection_();
}
void PoseClustering::setBaseSystemAndPoses(System const& base_system, std::vector<PosePointer> const& poses)
{
base_system_ = base_system;
poses_ = poses;
// make sure we don't have old transformations or poses flying around
transformations_.clear();
if (delete_conformation_set_)
{
delete current_set_;
}
current_set_ = 0;
}
void PoseClustering::setBaseSystemAndTransformations(System const& base_system, String transformation_file_name)
{
// clear any potential pointers to old poses
if (delete_conformation_set_)
{
delete current_set_;
}
current_set_ = NULL;
has_rigid_transformations_ = true;
rmsd_level_of_detail_ = options.getInteger(Option::RMSD_LEVEL_OF_DETAIL);
base_system_ = System(base_system);
base_conformation_.takeSnapShot(base_system_);
covariance_matrix_ = computeCovarianceMatrix(base_system, rmsd_level_of_detail_);
readTransformationsFromFile_(transformation_file_name);
}
bool PoseClustering::compute()
{
//cout << " compute() t=" << true << "/f=" << false << " r=" << has_rigid_transformations_ << " t=" << rmsd_type << endl;
Size num_poses = getNumberOfPoses();
if (num_poses == 0)
{
Log.warn() << "Given input is empty (getNumberOfPoses() == 0)! Nothing to cluster, abort." << endl;
return false;
}
// first, we should figure out if we have been fed the right type of input for the choice of rmsd...
// TODO: center of mass distance should really not only be defined for snapshots...
Index rmsd_type = options.getInteger(Option::RMSD_TYPE);
if ( (rmsd_type == SNAPSHOT_RMSD) || (rmsd_type == CENTER_OF_MASS_DISTANCE) )
{
// obviously, we need snapshots
if (poses_[0].snap == 0)
{
// but we don't have them... so let's produce some...
convertTransformations2Snaphots();
}
}
else if (rmsd_type == RIGID_RMSD)
{
// obviously, we need transforms
if (poses_[0].trafo == 0)
{
// but we don't have them... so let's produce some...
convertSnaphots2Transformations();
}
}
// precompute the atom bijection, that handles the
// option RMSD_LEVEL_OF_DETAIL
precomputeAtomBijection_();
bool result = false;
// decide which algorithm to apply
switch (options.getInteger(Option::CLUSTER_METHOD))
{
case TRIVIAL_COMPLETE_LINKAGE:
result = trivialCompute_();
break;
case SLINK_SIBSON:
case CLINK_DEFAYS:
result = linearSpaceCompute_();
break;
case NEAREST_NEIGHBOR_CHAIN_WARD:
result = nearestNeighborChainCompute_();
break;
case CLINK_ALTHAUS:
result = althausCompute_();
break;
default:
Log.error() << "Unknown parameter for option CLUSTER_METHOD " << options.get(Option::CLUSTER_METHOD) << endl;
result = false;
}
return result;
}
bool PoseClustering::readTransformationsFromFile_(String filename)
{
poses_.clear();
transformations_.clear();
if (delete_conformation_set_)
delete current_set_;
current_set_ = NULL;
LineBasedFile file(filename, std::ios::in);
vector<String> fields;
while (file.LineBasedFile::readLine())
{
// get the line
String current_transformation = file.getLine();
if (current_transformation.countFields(" ") == 19)
{
fields.clear();
current_transformation.split(fields);
RigidTransformation rd;
rd.translation << fields[6].toFloat(), fields[10].toFloat(), fields[14].toFloat();
rd.rotation << fields[ 3].toFloat(), fields[ 4].toFloat(), fields[ 5].toFloat(),
fields[ 7].toFloat(), fields[ 8].toFloat(), fields[ 9].toFloat(),
fields[11].toFloat(), fields[12].toFloat(), fields[13].toFloat();
transformations_.push_back(rd);
}
else
{
// Log.info() << "Skipped line " << current_transformation << " while reading file " << filename << endl;
}
}
// now, store the transformations as poses (we cannot do this in the former loop, as the vector
// may be resized, which would change the pointers)
for (Position i=0; i<transformations_.size(); ++i)
{
poses_.push_back(PosePointer(&(transformations_[i])));
}
return true;
}
bool PoseClustering::trivialCompute_()
{
// in the beginning each pose is a cluster
// this causes O(n^2) space!!
Size num_poses = getNumberOfPoses();
pairwise_scores_.resize(num_poses, num_poses);
// clean up old clusters
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
// reset the cluster tree
cluster_tree_.clear();
// compute all pairwise RMSDs
Index rmsd_type = options.getInteger(Option::RMSD_TYPE);
if (rmsd_type == PoseClustering::CENTER_OF_MASS_DISTANCE)
{
computeCenterOfMasses_();
}
// for the next step we need to determine the minimal maximal
// distance between two clusters
float min_max_cluster_dist = std::numeric_limits<float>::max();
Index min_cluster_a_idx = 0;
Index min_cluster_b_idx = 0;
set<Index>* min_cluster_a = NULL;
set<Index>* min_cluster_b = NULL;
// in the beginning each pose is a cluster itself
for (Size i=0; i<num_poses; i++)
{
// add as new cluster
set<Index> new_set;
new_set.insert((Index) i);
clusters_.push_back(new_set);
cluster_scores_.push_back(0.);
// and add it into the tree
ClusterTreeNode v = boost::add_vertex(cluster_tree_);
cluster_tree_[v].poses.insert((Index) i);
cluster_tree_[v].size = 1;
// TODO: continue with the cluster tree... add center if required ...
// compute the rmsd
if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
poses_[i].snap->applySnapShot(system_i_);
}
pairwise_scores_(i,i) = 0;
for (Size j=i+1; j<num_poses; j++)
{
if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
poses_[j].snap->applySnapShot(system_j_);
}
float rmsd = getRMSD_(i, j, rmsd_type);
pairwise_scores_(i,j) = rmsd;
pairwise_scores_(j,i) = rmsd;
if (rmsd < min_max_cluster_dist)
{
min_max_cluster_dist = rmsd;
// store
min_cluster_a_idx = i;
min_cluster_b_idx = j;
}
}
}
// iterate as long as the maximal cluster distance
// gets lower than the DISTANCE_THRESHOLD
float rmsd_cutoff = options.getReal(Option::DISTANCE_THRESHOLD);
bool hit = (clusters_.size() > 1);
while ((min_max_cluster_dist < rmsd_cutoff) && hit)
{
// Invariant: cluster_a will always be the smaller one
min_cluster_a = &clusters_[min_cluster_a_idx];
min_cluster_b = &clusters_[min_cluster_b_idx];
if (min_cluster_b->size() > min_cluster_a->size())
{
//swap
swap(min_cluster_a_idx, min_cluster_b_idx);
swap(min_cluster_a, min_cluster_b);
}
// merge
std::set<Index> temp_set;
std::set_union(min_cluster_a->begin(), min_cluster_a->end(),
min_cluster_b->begin(), min_cluster_b->end(),
std::inserter(temp_set, temp_set.begin()));
min_cluster_a->swap(temp_set);
// the max rmsd between all pairs in the new cluster
// is the clusters score
cluster_scores_[min_cluster_a_idx] = min_max_cluster_dist;
// mark as merged
min_cluster_b->clear();
pairwise_scores_(min_cluster_b_idx, min_cluster_b_idx) = std::numeric_limits<float>::max();
//cout << "found a new cluster by merging " << min_cluster_a_idx << " with " << min_cluster_b_idx << " with rmsd " << min_max_cluster_dist << " / " << rmsd_cutoff << endl;
//printCluster_(min_cluster_a_idx);
// did we end up in a single cluster?
hit = false;
// and update the pairwise rmsd scores
for (Size j=0; j < clusters_.size(); j++)
{
if ((min_cluster_a_idx != (Index)j) && (clusters_[j].size() > 0))
{
hit = true;
float rmsd = getClusterRMSD_(min_cluster_a_idx, j, rmsd_type);
pairwise_scores_(min_cluster_a_idx, j) = rmsd;
pairwise_scores_(j, min_cluster_a_idx) = rmsd;
}
}
// now find the new minimum
min_max_cluster_dist = std::numeric_limits<float>::max();
min_cluster_b_idx = 0;
min_cluster_a_idx = 0;
min_cluster_b = NULL;
min_cluster_a = NULL;
for (Size i=0; i<num_poses; i++)
{
// skip already seen clusters
if (clusters_[i].size() > 0)
{
for (Size j=i+1; j<num_poses; j++)
{
// skip already seen clusters
if (clusters_[j].size() > 0)
{
// check if new minimum
if (pairwise_scores_(i,j) < min_max_cluster_dist)
{
hit = true;
min_max_cluster_dist = pairwise_scores_(i,j);
// store
min_cluster_a_idx = i;
min_cluster_b_idx = j;
//cout << "new min " << min_max_cluster_dist << " btw " << i << " " << j << endl;
}
}
}
}
}
}
// collect all final clusters
std::vector< std::set<Index> > temp_clusters;
std::vector< float > temp_cluster_scores;
for (Size i = 0; i < clusters_.size(); i++)
{
if (clusters_[i].size() > 0)
{
temp_clusters.push_back(clusters_[i]);
temp_cluster_scores.push_back(cluster_scores_[i]);
}
}
swap(clusters_, temp_clusters);
swap(cluster_scores_, temp_cluster_scores);
//cout << "Final num of clusters " << clusters_.size() << endl;
//printClusters();
//printClusterScores();
return true;
}
bool PoseClustering::linearSpaceCompute_()
{
Size num_poses = getNumberOfPoses();
float threshold = options.getReal(Option::DISTANCE_THRESHOLD);
Index rmsd_type = options.getInteger(Option::RMSD_TYPE);
if (rmsd_type == PoseClustering::CENTER_OF_MASS_DISTANCE)
{
computeCenterOfMasses_();
}
// we will need arrays pi, lambda, and mu
lambda_.resize(num_poses);
pi_.resize(num_poses);
mu_.resize(num_poses);
//cout << " num poses: " << num_poses << endl;
// and initialize them for the first point
pi_[0] = 0;
lambda_[0] = numeric_limits<double>::max();
for (Size current_level=1; current_level < num_poses; ++current_level)
{
// TEST
if (current_level % 1000 == 0)
{
double percentage = current_level / (double)num_poses;
percentage *= 100. * percentage;
std::cout << current_level << " " << num_poses << " " << percentage << std::endl;
}
// END TEST
pi_[current_level] = current_level;
lambda_[current_level] = numeric_limits<double>::max();
if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
poses_[current_level].snap->applySnapShot(system_i_);
}
for (Size j=0; j<current_level; ++j)
{
if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
poses_[j].snap->applySnapShot(system_j_);
}
// note: we don't need the rmsd matrix here - that's the whole point: the algorithm
// only requires O(n) data, not (O(n^2))
mu_[j] = getRMSD_(current_level, j, rmsd_type);
//std::cout << current_level << " <=> " << j << " " << pi_[j] << ": " << mu_[j] << " " << lambda_[j] << " " << rmsd_type << std::endl;
}
if (options.getInteger(Option::CLUSTER_METHOD) == SLINK_SIBSON)
{
slinkInner_(current_level);
}
else //(options.getInteger(Option::CLUSTER_METHOD) == CLINK_DEFAYS)
{
clinkInner_(current_level);
}
}
// convert lambda, pi, and mu to clusters datastructure
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
//for (int i=0; i<pi_.size(); ++i)
// std::cout << i << " " << pi_[i] << " " << lambda_[i] << std::endl;
// we need a helper for the clusters
std::vector<std::set<Index> > cluster_helper;
std::vector<float> score_helper(num_poses);
// first, each point is its own cluster
for (Position i=0; i < num_poses; ++i)
{
cluster_helper.push_back(std::set<Index>());
cluster_helper.back().insert(i);
}
// now, join clusters if needed
for (Position current_cluster=0; current_cluster < cluster_helper.size(); ++current_cluster)
{
if (Maths::isLessOrEqual(lambda_[current_cluster], threshold))
{
// merge this cluster with its destination
cluster_helper[pi_[current_cluster]].insert(cluster_helper[current_cluster].begin(), cluster_helper[current_cluster].end());
// store the value at which this was merged
score_helper[pi_[current_cluster]] = lambda_[current_cluster];
// std::cout << "merging into " << pi_[current_cluster] << " with " << lambda_[current_cluster] << std::endl;
// save some memory
cluster_helper[current_cluster].clear();
}
else
{
// this cluster is done => put it into the result
clusters_.push_back(cluster_helper[current_cluster]);
// save some memory
cluster_helper[current_cluster].clear();
cluster_scores_.push_back(score_helper[current_cluster]);
}
}
//printClusterScores();
return true;
}
void PoseClustering::slinkInner_(int current_level)
{
for (int i=0; i<current_level; ++i)
{
if (Maths::isGreaterOrEqual(lambda_[i], mu_[i]))
{
mu_[pi_[i]] = min(mu_[pi_[i]], lambda_[i]);
lambda_[i] = mu_[i];
pi_[i] = current_level;
}
else
{
mu_[pi_[i]] = min(mu_[pi_[i]], mu_[i]);
}
}
for (int i=0; i<current_level; ++i)
{
if (Maths::isGreaterOrEqual(lambda_[i], lambda_[pi_[i]]))
pi_[i] = current_level;
}
}
void PoseClustering::clinkInner_(int current_level)
{
for (int i=0; i<current_level; ++i)
{
if (Maths::isLess(lambda_[i], mu_[i]))
{
mu_[pi_[i]] = max(mu_[pi_[i]], mu_[i]);
mu_[i] = numeric_limits<double>::max();
}
}
// step 4
int a = current_level-1;
// step 5
for (int i=0; i<current_level; ++i)
{
int j = (current_level-1)-i;
if (Maths::isGreaterOrEqual(lambda_[j], mu_[pi_[j]]))
{
if (Maths::isLess(mu_[j], mu_[a]))
{
a = j;
}
}
else
{
mu_[j] = numeric_limits<double>::max();
}
}
//printVariables_(a, -5, -5., -5, -5, current_level);
// step 6
int b = pi_[a];
double c = lambda_[a];
pi_[a] = current_level;
lambda_[a] = mu_[a];
int d;
double e;
//printVariables_(a, b, c, -5, -5, current_level);
// step 7
while ((a < current_level-1) && (b < current_level-1))
{
d = pi_[b];
e = lambda_[b];
pi_[b] = current_level;
lambda_[b] = c;
b = d;
c = e;
}
if ((a < current_level - 1) && (b == current_level-1))
{
pi_[b] = current_level;
lambda_[b] = c;
}
//printVariables_(a, b, c, d, e, current_level);
// step 8
for (int i=0; i<current_level; ++i)
{
if (pi_[pi_[i]] == current_level)
{
if (Maths::isGreaterOrEqual(lambda_[i], lambda_[pi_[i]]))
pi_[i] = current_level;
}
}
/* cout << "------------------------------" << endl;
printVariables_(a, b, c, d, e, current_level);
cout << "------------------------------" << endl;
*/
}
bool PoseClustering::althausCompute_()
{
return false;
}
bool PoseClustering::nearestNeighborChainCompute_()
{
Size num_poses = getNumberOfPoses();
// we might need the number of atoms to which our ward distances apply later on
// this number depends on the choice of RMSD_LEVEL_OF_DETAIL
Index rmsd_level_of_detail = options.getInteger(Option::RMSD_LEVEL_OF_DETAIL);
number_of_selected_atoms_ = 0;
for (AtomConstIterator at_it = getSystem().beginAtom(); +at_it; ++at_it)
{
if (!isExcludedByLevelOfDetail_(&*at_it, rmsd_level_of_detail))
{
++number_of_selected_atoms_;
}
}
// clean up old clusters
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
// reset the cluster tree
cluster_tree_.clear();
Index rmsd_type = options.getInteger(PoseClustering::Option::RMSD_TYPE);
bool run_parallel = options.getBool(PoseClustering::Option::RUN_PARALLEL);
float min_cluster_dist = std::numeric_limits<float>::max();
// the active clusters (as nodes in the cluster tree)
std::vector<ClusterTreeNode> active_clusters;
active_clusters.reserve(num_poses);
// stack of clusters (as indices into the active_cluster-vector) storing nearest neighbor chains
std::deque<Index> cluster_stack;
// in the beginning each pose is a cluster itself
for (Size i=0; i<num_poses; i++)
{
// add it as a new cluster to the tree
ClusterTreeNode v = boost::add_vertex(cluster_tree_);
cluster_tree_[v].poses.insert((Index) i);
cluster_tree_[v].size = 1;
cluster_tree_[v].merged_at = 0.;
#ifdef POSECLUSTERING_DEBUG
cluster_tree_[v].current_cluster_id = -1;
#endif
active_clusters.push_back(v);
}
//cout << active_clusters.size() << " " << cluster_stack.size() << std::endl;
initWardDistance_(rmsd_type);
Index current_cluster, nearest_cluster;
Size num_active_clusters = num_poses;
// while there is more than one final cluster
while (num_active_clusters > 1)
{
// TEST
if (num_active_clusters % 1000 == 0)
{
cout << "active clusters: " << num_active_clusters << " (" << 100.*num_active_clusters / (double)num_poses << ")" <<endl;
}
// END TEST
min_cluster_dist = std::numeric_limits<float>::max();
nearest_cluster = num_active_clusters - 1;
// if stack is empty add randomly
if (cluster_stack.empty())
{
// we simply take the first
cluster_stack.push_back(0);
}
current_cluster = cluster_stack.back();
// Compute the distances to all other clusters
// TODO: can this be guaranteed?
// If there may be multiple equal nearest neighbors to a cluster, the algorithm requires a consistent
// tie-breaking rule: for instance, in this case, the nearest neighbor may be chosen, among the clusters
// at equal minimum distance from current_cluster, by numbering the clusters arbitrarily and choosing
// the one with the smallest index
#ifdef BALL_HAS_TBB
if (run_parallel)
{
// std::cout << "Parallel computation...";
ComputeNearestClusterTask_ root_task(this, active_clusters, current_cluster, rmsd_type);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, num_active_clusters), root_task);
// std::cout << "...done." << std::endl;
nearest_cluster = root_task.getMinIndex();
min_cluster_dist = root_task.getMinValue();
}
else
{
#endif
for (Index i=0; i<(Index)num_active_clusters; ++i)
{
// check all pairs between all_clusters[*clust_it] and all_clusters[current_cluster]
if (i != current_cluster)
{
float rmsd = computeWardDistance_(active_clusters[current_cluster], active_clusters[i], rmsd_type);
// for stability, make sure that "really" smaller cases are always taken...
if (Maths::isLess(rmsd, min_cluster_dist))
{
nearest_cluster = i;
min_cluster_dist = rmsd;
}
}
}
#ifdef BALL_HAS_TBB
}
#endif
// have we put this cluster to the stack before ?
// due to the reducibility of Ward's distance, this would imply that the cluster
// was our predecessor.
if (cluster_stack.size() == 1) // we don't have a predecessor => just push
{
cluster_stack.push_back(nearest_cluster);
continue;
}
// look at the predecessor
cluster_stack.pop_back(); // already saved in current_cluster
Index predecessor = cluster_stack.back();
if (predecessor == nearest_cluster)
{
cluster_stack.pop_back(); // pop the predecessor
ClusterTreeNode current_node = active_clusters[current_cluster];
ClusterTreeNode nearest_node = active_clusters[nearest_cluster];
// add a new parent node to the tree
ClusterTreeNode new_parent = boost::add_vertex(cluster_tree_);
// make this parent the new root of the cluster tree
// (the final root will be the last parent we add)
cluster_tree_[boost::graph_bundle] = new_parent;
// add an edge from the parent to both children
boost::add_edge(new_parent, current_node, cluster_tree_);
boost::add_edge(new_parent, nearest_node, cluster_tree_);
// remember the value at which these were merged
// NOTE: min_cluster_dist is the Ward distance, which is quadratic in the
// rmsds. We can, e.g., store RMSDs of the centroids, or the square root
// of the Ward distance. What we *will* store here, is the square root
// of the Ward distance per atom, i.e., the square root of the
// squared average distance from the centroid per atom
cluster_tree_[new_parent].merged_at = sqrt(min_cluster_dist / number_of_selected_atoms_);
cluster_tree_[new_parent].size = cluster_tree_[current_node].size
+ cluster_tree_[nearest_node].size;
#ifdef POSECLUSTERING_DEBUG
cluster_tree_[new_parent].current_cluster_id = -1;
#endif
//cout << cluster_tree_[new_parent].size << " " << num_poses << std::endl;
//cout << " merged at: " << cluster_tree_[new_parent].merged_at << endl;
// TODO: here, we could decide if we want to merge the sets from our
// children and explictly store them => save runtime, pay memory
// (this would look similar to the following...)
/*
min_cluster_a = &clusters_[current_node];
min_cluster_b = &clusters_[nearest_node];
// merge
std::set<Index> temp_set;
std::set_union(min_cluster_a->begin(), min_cluster_a->end(),
min_cluster_b->begin(), min_cluster_b->end(),
std::inserter(temp_set, temp_set.begin()));
*/
// Note: this needs to be done before clearing and swapping!
updateWardDistance_(new_parent, current_node, nearest_node, rmsd_type);
// make the old clusters inactive and add the new one
//
// in order to avoid unnecessary erases from the middle of the vector, we
// do the following:
//
// =====x====y=====k
//
// x and y are supposed to be erased, and we want to attach a new element behind k.
// instead, we now put k in x, and the new thing in y, and remove the last element => O(1)
//
// NOTE: this might invalidate our consistent tie breaking rule!
//
active_clusters[std::max(current_cluster, nearest_cluster)] = active_clusters.back();
active_clusters[std::min(current_cluster, nearest_cluster)] = new_parent;
// stupidly, we have to update all references to the formerly last element in the stack
for (std::deque<Index>::iterator s_it = cluster_stack.begin(); s_it != cluster_stack.end(); ++s_it)
{
if (*s_it == (Index)(num_active_clusters-1))
{
*s_it = std::max(current_cluster, nearest_cluster);
}
}
active_clusters.pop_back();
num_active_clusters--;
cluster_stack.push_back(std::min(current_cluster, nearest_cluster));
}
else
{
cluster_stack.push_back(current_cluster);
cluster_stack.push_back(nearest_cluster);
}
}
// we automatically extract clusters if a reasonable threshold is provided.
// distance threshold equal zero prohibits that to save runtime
float threshold = options.getReal(Option::DISTANCE_THRESHOLD);
if (Maths::isGreaterOrEqual(threshold, 0.0))
extractClustersForThreshold(threshold);
return true;
}
void PoseClustering::initWardDistance_(Index rmsd_type)
{
Index rmsd_level_of_detail = options.getInteger(Option::RMSD_LEVEL_OF_DETAIL);
Index num_atoms = 0;
if (rmsd_type == SNAPSHOT_RMSD)
{
// and the number of atoms used for rmsd computation
for (AtomConstIterator atom_it = system_i_.beginAtom(); +atom_it; ++atom_it)
{
if (!isExcludedByLevelOfDetail_(&(*atom_it), rmsd_level_of_detail))
{
++num_atoms;
}
}
}
if (rmsd_type == CENTER_OF_MASS_DISTANCE)
{
// precompute centers of mass
computeCenterOfMasses_();
}
// iterate over all nodes in the graph (for now, only the leaves)
BGL_FORALL_VERTICES(v, cluster_tree_, ClusterTree)
{
// get the index of the snapshot stored in the node
// (there is exactly one pose in each node, so this works...)
Index snapshot_index = *(cluster_tree_[v].poses.begin());
switch (rmsd_type)
{
case RIGID_RMSD:
{
cluster_tree_[v].center = *(poses_[snapshot_index].trafo);
break;
}
case SNAPSHOT_RMSD:
{
poses_[snapshot_index].snap->applySnapShot(system_i_);
Eigen::VectorXf center(3*number_of_selected_atoms_);
// consider the choice of option RMSD_LEVEL_OF_DETAIL
Index current_atom = 0;
for (AtomConstIterator atom_it = system_i_.beginAtom(); +atom_it; ++atom_it)
{
if (!isExcludedByLevelOfDetail_(&(*atom_it), rmsd_level_of_detail))
{
Vector3 const& current_pos = atom_it->getPosition();
center[3*current_atom ] = current_pos.x;
center[3*current_atom+1] = current_pos.y;
center[3*current_atom+2] = current_pos.z;
current_atom++;
}
}
cluster_tree_[v].center = center;
break;
}
case CENTER_OF_MASS_DISTANCE:
{
// CENTER_OF_MASS_DISTANCE ignores the option RMSD_LEVEL_OF_DETAIL
// NOTE: it would look nicer to center the translations, such that com_[0] == 0
// however, this is not really necessary, as we just look at differences of
// positions, which are independent of this constant shift
RigidTransformation com_transform;
(com_transform.translation << com_[snapshot_index].x,
com_[snapshot_index].y,
com_[snapshot_index].z).finished();
com_transform.rotation = Eigen::Matrix3f::Zero();
cluster_tree_[v].center = com_transform;
break;
}
default:
Log.info() << "initWardDistance_(): unknown rmsd score type " << __FILE__ << " " << __LINE__ << endl;
}
}
}
void PoseClustering::updateWardDistance_(ClusterTreeNode parent, ClusterTreeNode i, ClusterTreeNode j, Index rmsd_type)
{
Size cluster_size_a = cluster_tree_[i].size;
Size cluster_size_b = cluster_tree_[j].size;
Size cluster_size_ab = cluster_tree_[parent].size;
switch (rmsd_type)
{
case RIGID_RMSD:
{
RigidTransformation& transform_i = boost::get<RigidTransformation>(cluster_tree_[i].center);
RigidTransformation& transform_j = boost::get<RigidTransformation>(cluster_tree_[j].center);
Eigen::Vector3f new_translation = ( (transform_i.translation * cluster_size_a)
+ (transform_j.translation * cluster_size_b)
)/cluster_size_ab;
Eigen::Matrix3f new_rotation = ( (transform_i.rotation * cluster_size_a)
+ (transform_j.rotation * cluster_size_b)
)/cluster_size_ab;
cluster_tree_[parent].center = RigidTransformation(new_translation, new_rotation);
break;
}
case SNAPSHOT_RMSD:
{
Eigen::VectorXf& cluster_a = boost::get<Eigen::VectorXf>(cluster_tree_[i].center);
Eigen::VectorXf& cluster_b = boost::get<Eigen::VectorXf>(cluster_tree_[j].center);
Eigen::VectorXf new_centroid = (cluster_a * cluster_size_a + cluster_b * cluster_size_b) / cluster_size_ab;
cluster_tree_[parent].center = new_centroid;
break;
}
case CENTER_OF_MASS_DISTANCE:
{
RigidTransformation& transform_i = boost::get<RigidTransformation>(cluster_tree_[i].center);
RigidTransformation& transform_j = boost::get<RigidTransformation>(cluster_tree_[j].center);
Eigen::Vector3f new_translation = ( (transform_i.translation * cluster_size_a)
+ (transform_j.translation * cluster_size_b)
)/cluster_size_ab;
cluster_tree_[parent].center = RigidTransformation(new_translation, Matrix3f::Zero());
break;
}
default:
Log.info() << "updateWardDistance_(): unknown rmsd score type " << __FILE__ << " " << __LINE__ << endl;
}
}
float PoseClustering::computeWardDistance_(ClusterTreeNode i, ClusterTreeNode j, Index rmsd_type)
{
Size cluster_size_a = cluster_tree_[i].size;
Size cluster_size_b = cluster_tree_[j].size;
float result = -1.;
switch (rmsd_type)
{
case RIGID_RMSD:
{
RigidTransformation& transform_i = boost::get<RigidTransformation>(cluster_tree_[i].center);
RigidTransformation& transform_j = boost::get<RigidTransformation>(cluster_tree_[j].center);
result = getSquaredRigidRMSD(transform_i.translation - transform_j.translation,
transform_i.rotation - transform_j.rotation,
covariance_matrix_) / ( 1./cluster_size_a + 1./cluster_size_b)
* number_of_selected_atoms_;
break;
}
case SNAPSHOT_RMSD:
{
Eigen::VectorXf& cluster_a = boost::get<Eigen::VectorXf>(cluster_tree_[i].center);
Eigen::VectorXf& cluster_b = boost::get<Eigen::VectorXf>(cluster_tree_[j].center);
result = (cluster_a - cluster_b).squaredNorm() / ( 1./cluster_size_a + 1./cluster_size_b);
break;
}
case CENTER_OF_MASS_DISTANCE:
{
RigidTransformation& transform_i = boost::get<RigidTransformation>(cluster_tree_[i].center);
RigidTransformation& transform_j = boost::get<RigidTransformation>(cluster_tree_[j].center);
result = (transform_i.translation - transform_j.translation).squaredNorm() / ( 1./cluster_size_a + 1./cluster_size_b)
*number_of_selected_atoms_;
break;
}
default:
Log.info() << "getWardDistance_(): unknown rmsd score type " << __FILE__ << " " << __LINE__ << endl;
}
return result;
}
std::set<Index> PoseClustering::collectClusterBelow_(ClusterTreeNode const& v)
{
// collect the respective leaves
std::set<Index> result;
// is v itself a leaf?
if (out_degree(v, cluster_tree_) == 0)
{
result.insert(*(cluster_tree_[v].poses.begin()));
}
else
{
std::stack<ClusterTreeNode> stack;
stack.push(v);
ClusterTreeNode current_node;
while (!stack.empty())
{
current_node = stack.top();
stack.pop();
BGL_FORALL_ADJ(current_node, child, cluster_tree_, ClusterTree)
{
if (out_degree(child, cluster_tree_) == 0) // we found a leaf
{
// currently, each leaf represents one single pose
result.insert(*(cluster_tree_[child].poses.begin()));
}
else // something inbetween
{
stack.push(child);
}
}
}
}
return result;
}
void PoseClustering::computeCenterOfMasses_()
{
GeometricCenterProcessor center;
com_.resize(getNumberOfPoses());
for (Size i=0; i<com_.size(); ++i)
{
poses_[i].snap->applySnapShot(system_i_);
system_i_.apply(center);
com_[i] = center.getCenter();
}
}
// precompute the atom bijection
void PoseClustering::precomputeAtomBijection_()
{
Index rmsd_level_of_detail = options.getInteger(Option::RMSD_LEVEL_OF_DETAIL);
system_i_ = System(base_system_);
system_j_ = System(base_system_);
StructureMapper mapper(system_i_, system_j_);
switch (rmsd_level_of_detail)
{
case PoseClustering::C_ALPHA:
atom_bijection_.assignCAlphaAtoms(system_i_, system_j_);
break;
case PoseClustering::BACKBONE:
atom_bijection_.assignBackboneAtoms(system_i_, system_j_);
break;
case PoseClustering::ALL_ATOMS:
mapper.calculateDefaultBijection();
atom_bijection_ = mapper.getBijection();
break;
case PoseClustering::PROPERTY_BASED_ATOM_BIJECTION:
atom_bijection_.assignAtomsByProperty(system_i_, system_j_);
break;
case PoseClustering::HEAVY_ATOMS:
default:
Log.info() << "Option RMSDLevelOfDetaill::HEAVY_ATOMS not yet implemented" << endl;
}
}
bool PoseClustering::refineClustering(Options const& refined_options)
{
if (getNumberOfClusters() == 0)
{
// ok, nothing to do here...
return true;
}
// this is going to be a little cumbersome... hence, to simplify handling of the member
// variables, we will use a new PoseClustering instance
PoseClustering inner_pc;
// in principle, things are simple: we iterate over all existing clusters, perform a new
// clustering, and store the results
//
// in reality, things are less simple: for each of the inner clustering calls, we need
// to generate suitable input without having too many copies of the poses or transformations
// flying around at any point in time to avoid memory issues; also, indices will have to
// be mapped back and forth
Size num_curr_clusters = 0;
// store the inner clusters temporarily
std::vector< std::set<Index> > temp_clusters;
std::vector< float > temp_cluster_scores;
// now iterate over all clusters
for (Size i=0; i<getNumberOfClusters(); i++)
{
//cout << "\n Inner compute no " << i << " of " << getNumberOfClusters() << " #members:" << getClusterSize(i) << endl;
// if the cluster has only one element, there is no need to split it
if (getClusterSize(i)<=1)
{
temp_clusters.push_back(getCluster(i));
temp_cluster_scores.push_back(getClusterScore(i));
}
else // multiple-element cluster
{
// temporarily convert the poses to be re-clustered to index set 0 ... getClusterSize(i)
std::vector<Index> current_cluster_vector(getClusterSize(i));
std::vector<PosePointer> current_pose_vector;
Position temp_idx = 0;
for (std::set<Index>::iterator clust_it = clusters_[i].begin();
clust_it != clusters_[i].end();
++clust_it, temp_idx++)
{
current_cluster_vector[temp_idx] = *clust_it;
current_pose_vector.push_back(poses_[*clust_it]);
}
// run a clustering separately on each pre cluster as described by the options
inner_pc.clear_();
// set the refinement options
inner_pc.options = refined_options;
// set the poses for the inner pc
inner_pc.setBaseSystemAndPoses(base_system_, current_pose_vector);
//cout << " # given poses: " << current_pose_vector.size() << endl;
inner_pc.compute();
// add each cluster of the current inner run
num_curr_clusters = inner_pc.getNumberOfClusters();
//cout << " split into " << num_curr_clusters << " inner clusters " << endl;
for (Size j=0; j<num_curr_clusters; ++j)
{
// convert the indices back to original pose indices
std::set<Index> temp_indices = inner_pc.getCluster(j);
std::set<Index> converted_indices;
for (std::set<Index>::iterator ind_it = temp_indices.begin(); ind_it != temp_indices.end(); ++ind_it)
{
converted_indices.insert(current_cluster_vector[*ind_it]);
}
// add to the temp set
temp_clusters.push_back(converted_indices);
temp_cluster_scores.push_back(inner_pc.getClusterScore(j));
}
//cout << "++++++++++++++++++++++++++++++***" << endl;
// inner_pc.printClusterScores();
//cout << " cluster " << i << "( " << getClusterSize(i) << " ) was split into " << num_curr_clusters
// << " clusters." << endl;
//cout << "***++++++++++++++++++++++++++++++***" << endl;
}
}
// switch the clusters
clusters_ = temp_clusters;
cluster_scores_ = temp_cluster_scores;
cluster_representatives_.clear();
//printClusters();
return true;
}
bool PoseClustering::isExcludedByLevelOfDetail_(Atom const* atom, Index rmsd_level_of_detail)
{
bool result = true;
if ( (rmsd_level_of_detail == ALL_ATOMS)
|| ((rmsd_level_of_detail == C_ALPHA) && (atom->getName() == "CA"))
|| ((rmsd_level_of_detail == BACKBONE) && ( (atom->getName() == "CA")
|| (atom->getName() == "C")
|| (atom->getName() == "O")
|| (atom->getName() == "N")
|| (atom->getName() == "H")))
|| ( (rmsd_level_of_detail == PROPERTY_BASED_ATOM_BIJECTION)
&& (atom->hasProperty("ATOMBIJECTION_RMSD_SELECTION"))))
{
result = false;
}
return result;
}
void PoseClustering::printClusterScores(std::ostream& out)
{
Index rmsd_type = options.getInteger(Option::RMSD_TYPE);
#ifdef POSECLUSTERING_DEBUG
Index cluster_alg = options.getInteger(Option::CLUSTER_METHOD);
#endif
/*
cout << endl << "For method " << options.getInteger(Option::CLUSTER_METHOD)
<< " and RMSDtype " << rmsd_type
<< " and cutoff " << options.getInteger(Option::DISTANCE_THRESHOLD)
<< " we get: " << endl;
*/
for (Position i=0; i<clusters_.size(); ++i)
{
out << "=======================================" << endl;
out << "= Cluster " << i << " (cluster score: " << getClusterScore(i) << ")" << endl;
#ifdef POSECLUSTERING_DEBUG
if (cluster_alg == NEAREST_NEIGHBOR_CHAIN_WARD)
{
// find the cluster node with this id
BGL_FORALL_VERTICES(current_node, cluster_tree_, ClusterTree)
{
if (cluster_tree_[current_node].current_cluster_id == i)
{
out << "Merged at Ward distance: " << cluster_tree_[current_node].merged_at << std::endl;
}
}
}
#endif
std::set<Index>& current_cluster = clusters_[i];
out << " ";
for (std::set<Index>::iterator it_k=current_cluster.begin(); it_k!=current_cluster.end(); ++it_k)
{
out << *it_k << " ";
}
out << endl;
for (std::set<Index>::iterator it_j=current_cluster.begin(); it_j!=current_cluster.end(); ++it_j)
{
if (rmsd_type == SNAPSHOT_RMSD)
{
poses_[*it_j].snap->applySnapShot(system_i_);
}
out << " " << *it_j << " | ";
for (std::set<Index>::iterator it_k=current_cluster.begin(); it_k!=current_cluster.end(); ++it_k)
{
if (rmsd_type == SNAPSHOT_RMSD)
{
poses_[*it_k].snap->applySnapShot(system_j_);
}
out << getRMSD_(*it_j, *it_k, rmsd_type) << " ";
}
out << endl;
}
out << "=======================================" << endl << endl;
} //next cluster
}
void PoseClustering::exportWardClusterTreeToGraphViz(std::ostream& out)
{
boost::write_graphviz(out, cluster_tree_, ClusterTreeWriter_(&cluster_tree_));
}
void PoseClustering::exportToJSONDFS_(ClusterTreeNode const& current, String& result)
{
// what kind of node is this?
if (out_degree(current, cluster_tree_) == 0)
{
// we are in a leaf => simplified formatting, terminate recursion
result += String("{ \"name\": \"") + String(*(cluster_tree_[current].poses.begin())) + "\" }\n";
}
else
{
result += "{\n";
result += String("\t\"name\": \"") + String(cluster_tree_[current].merged_at) + "\",\n";
result += "\t\"children\": [\n";
BGL_FORALL_ADJ(current, child, cluster_tree_, ClusterTree)
{
exportToJSONDFS_(child, result);
result += ",";
}
result.back() = '\n';
result += "\t]\n";
result += "}\n";
}
}
void PoseClustering::exportClusterTreeToJSON(std::ostream& out)
{
String result = "";
exportToJSONDFS_(cluster_tree_[boost::graph_bundle], result);
out << result;
}
void PoseClustering::serializeWardClusterTree(std::ostream& out, bool binary)
{
if (binary)
{
boost::archive::binary_oarchive oa(out);
oa << cluster_tree_;
}
else
{
boost::archive::text_oarchive oa(out);
oa << cluster_tree_;
}
}
void PoseClustering::deserializeWardClusterTree(std::istream& in, bool binary)
{
if (binary)
{
boost::archive::binary_iarchive ia(in);
ia >> cluster_tree_;
}
else
{
boost::archive::text_iarchive ia(in);
ia >> cluster_tree_;
}
// unfortunately, old versions of boost don't serialize / deserialize graph_bundle properties... *sigh*
// so, be nice to them...
#if BOOST_VERSION < 105100
// iterate over all nodes in the graph and find the one which has no in_edges
// this would be much simpler if boost would store in_degrees for directed graphs... we don't want a bidirectional graph, though
HashMap<ClusterTreeNode, Size> in_degrees;
BGL_FORALL_VERTICES(current_vertex, cluster_tree_, ClusterTree)
{
boost::graph_traits<ClusterTree>::out_edge_iterator e, e_end;
for (boost::tie(e, e_end) = boost::out_edges(current_vertex, cluster_tree_); e != e_end; ++e)
{
ClusterTreeNode target = boost::target(*e, cluster_tree_);
if (in_degrees.find(target) == in_degrees.end())
{
in_degrees[target] = 1;
}
else
{
in_degrees[target]++;
}
}
}
// now, iterate over the tree again to find the one node without a parent
BGL_FORALL_VERTICES(current_vertex, cluster_tree_, ClusterTree)
{
if (in_degrees[current_vertex] == 0)
{
cluster_tree_[boost::graph_bundle] = current_vertex;
break;
}
}
#endif
}
#ifdef BALL_HAS_TBB
PoseClustering::ComputeNearestClusterTask_::ComputeNearestClusterTask_(PoseClustering* parent,
const std::vector<ClusterTreeNode>& active_clusters,
Position current_cluster, Index rmsd_type)
: parent_(parent),
active_clusters_(active_clusters),
current_cluster_(current_cluster),
rmsd_type_(rmsd_type),
my_min_value_(std::numeric_limits<float>::max())
{
}
PoseClustering::ComputeNearestClusterTask_::ComputeNearestClusterTask_(PoseClustering::ComputeNearestClusterTask_& cnct, tbb::split)
: parent_(cnct.parent_),
active_clusters_(cnct.active_clusters_),
current_cluster_(cnct.current_cluster_),
rmsd_type_(cnct.rmsd_type_),
my_min_value_(std::numeric_limits<float>::max())
{
}
void PoseClustering::ComputeNearestClusterTask_::join(ComputeNearestClusterTask_ const& cnct)
{
if (my_min_value_ > cnct.my_min_value_)
{
my_min_value_ = cnct.my_min_value_;
my_min_index_ = cnct.my_min_index_;
}
}
void PoseClustering::ComputeNearestClusterTask_::operator() (const tbb::blocked_range<size_t>& r)
{
// we use a local variable to squeeze out a little more efficiency, if possible
float min_value = my_min_value_;
Position min_index = my_min_index_;
size_t end = r.end();
for (size_t i = r.begin(); i != end; ++i)
{
// check all pairs between all_clusters[*clust_it] and all_clusters[current_cluster]
if (i != current_cluster_)
{
float rmsd = parent_->computeWardDistance_(active_clusters_[current_cluster_], active_clusters_[i], rmsd_type_);
// for stability, make sure that "really" smaller cases are always taken...
if (Maths::isLess(rmsd, min_value))
{
min_index = i;
min_value = rmsd;
}
}
}
my_min_index_ = min_index;
my_min_value_ = min_value;
}
#endif
PoseClustering::ClusterProperties::ClusterProperties()
: size(0),
merged_at(0.0f)
#ifdef POSECLUSTERING_DEBUG
, current_cluster_id(0.0f)
#endif
{
}
PoseClustering::ClusterProperties::ClusterProperties(const PoseClustering::ClusterProperties& o)
: poses(o.poses),
size(o.size),
center(o.center),
merged_at(o.merged_at)
#ifdef POSECLUSTERING_DEBUG
, current_cluster_id(o.current_cluster_id)
#endif
{
}
PoseClustering::ClusterProperties&
PoseClustering::ClusterProperties::operator=(const PoseClustering::ClusterProperties& o)
{
if(&o != this)
{
poses = o.poses;
size = o.size;
center = o.center;
merged_at = o.merged_at;
#ifdef POSECLUSTERING_DEBUG
current_cluster_id = o.current_cluster_id;
#endif
}
return *this;
}
PoseClustering::ClusterProperties::ClusterProperties(ClusterProperties&& o) BALL_NOEXCEPT
: poses(std::move(o.poses)),
size(o.size),
center(std::move(o.center)),
merged_at(o.merged_at)
#ifdef POSECLUSTERING_DEBUG
, current_cluster_id(o.current_cluster_id)
#endif
{
}
PoseClustering::ClusterProperties&
PoseClustering::ClusterProperties::operator=(ClusterProperties&& o) BALL_NOEXCEPT
{
poses = std::move(o.poses);
size = o.size;
center = std::move(o.center);
merged_at = o.merged_at;
#ifdef POSECLUSTERING_DEBUG
current_cluster_id = o.current_cluster_id;
#endif
return *this;
}
template <class Archive>
void PoseClustering::ClusterProperties::serialize(Archive& ar, const unsigned int /*version*/)
{
ar & poses;
ar & size;
// TODO: handle serialization of eigen matrix
// ar & center;
ar & merged_at;
}
void PoseClustering::ClusterTreeWriter_::operator() (std::ostream& out, const ClusterTreeNode& v) const
{
out << "[label=\"";
if ((*cluster_tree_)[v].poses.size() > 0)
out << *((*cluster_tree_)[v].poses.begin());
else
{
out << (*cluster_tree_)[v].merged_at;
#ifdef POSECLUSTERING_DEBUG
out << "\t" << (*cluster_tree_)[v].current_cluster_id;
#endif
}
out << "\"]";
}
const System& PoseClustering::getSystem() const
{
return base_system_;
}
System& PoseClustering::getSystem()
{
return base_system_;
}
const set<Index>& PoseClustering::getCluster(Index i) const
{
if (i >= (Index) clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
return clusters_[i];
}
set<Index>& PoseClustering::getCluster(Index i)
{
if (i >= (Index) clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
return clusters_[i];
}
Size PoseClustering::getClusterSize(Index i) const
{
if (i >= (Index)clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
return clusters_[i].size();
}
float PoseClustering::getClusterScore(Index i) const
{
if (i >= (Index)cluster_scores_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
return cluster_scores_[i];
}
float PoseClustering::getScore(System sys_a, System sys_b, Options temp_options) const
{
// basically reimplement getRMSD_(Index i, j, rmsd_type)
// but more general
float rmsd = 0.;
Index rmsd_type = temp_options.getInteger(Option::RMSD_TYPE);
Index rmsd_level_of_detail = temp_options.getInteger(Option::RMSD_LEVEL_OF_DETAIL);
// compute an atombijection
// Note: we cannot use precomputeAtomBijection as we might have
// different systems sys_a and sys_b given
AtomBijection temp_atom_bijection;
StructureMapper mapper(sys_a, sys_b);
switch (rmsd_level_of_detail)
{
case PoseClustering::C_ALPHA:
temp_atom_bijection.assignCAlphaAtoms(sys_a, sys_b);
break;
case PoseClustering::BACKBONE:
temp_atom_bijection.assignBackboneAtoms(sys_a, sys_b);
break;
case PoseClustering::ALL_ATOMS:
mapper.calculateDefaultBijection();
temp_atom_bijection = mapper.getBijection();
break;
case PoseClustering::PROPERTY_BASED_ATOM_BIJECTION:
temp_atom_bijection.assignAtomsByProperty(sys_a, sys_b);
break;
case PoseClustering::HEAVY_ATOMS:
default:
Log.info() << "Option RMSDLevelOfDetaill::HEAVY_ATOMS not yet implemented" << endl;
}
// center of masses
GeometricCenterProcessor center;
sys_a.apply(center);
Vector3 com_a = center.getCenter();
sys_b.apply(center);
Vector3 com_b = center.getCenter();
if (rmsd_type == PoseClustering::RIGID_RMSD)
{
// we will also need the covariance matrix
Eigen::Matrix3f temp_covariance_matrix = computeCovarianceMatrix(sys_a, rmsd_level_of_detail);
RMSDMinimizer::Result transform = RMSDMinimizer::computeTransformation(temp_atom_bijection);
Matrix4x4& bm = transform.first;
// convert the contained matrix to Eigen translation vector & rotation matrix
RigidTransformation rd_a;
// unfortunately, we cannot directly use the translation vector contained in the matrix:
// the way the transformation is set up, we will first move the second protein in the origin,
// then rotate it, and finally move it to the center of mass of the first protein. for our
// RIGID_RMSD-computation, however, we need the difference of the centers of mass, without
// the influence of the rotation
rd_a.rotation << bm.m11, bm.m12, bm.m13, bm.m21, bm.m22, bm.m23, bm.m31, bm.m32, bm.m33;
Eigen::Vector3f eigen_com_a;
eigen_com_a << com_a.x, com_a.y, com_a.z;
Eigen::Vector3f eigen_com_b;
eigen_com_b << com_b.x, com_b.y, com_b.z;
Eigen::Vector3f t_ab = eigen_com_a - eigen_com_b;
Eigen::Matrix3f M_ab = rd_a.rotation - Eigen::Matrix3f::Identity();
rmsd = getRigidRMSD(t_ab, M_ab, temp_covariance_matrix);
}
else if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
rmsd = mapper.calculateRMSD(temp_atom_bijection);
}
else if (rmsd_type == PoseClustering::CENTER_OF_MASS_DISTANCE)
{
// just query the center distance
rmsd = com_a.getDistance(com_b);
}
return rmsd;
}
float PoseClustering::computeCompleteLinkageRMSD(Index i, Options temp_options, bool initialize)
{
if (i >= (Index)cluster_scores_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
// we have to compute the maximal RMSD between all pairs in the cluster i
float rmsd = 0.;
Index rmsd_type = temp_options.getInteger(Option::RMSD_TYPE);
// do we have to recompute the atombijection?
if (initialize)
{
// prepare whatever has to be prepared...
if ( (rmsd_type == SNAPSHOT_RMSD) || (rmsd_type == CENTER_OF_MASS_DISTANCE) )
{
// obviously, we need snapshots
if (poses_[0].snap == 0)
{
// but we don't have them... so let's produce some...
convertTransformations2Snaphots();
}
}
else if (rmsd_type == RIGID_RMSD)
{
// obviously, we need transforms
if (poses_[0].trafo == 0)
{
// but we don't have them... so let's produce some...
convertSnaphots2Transformations();
}
}
// precompute the atom bijection, that handles the
// option RMSD_LEVEL_OF_DETAIL
precomputeAtomBijection_();
}
// now do the pairwise checking
for (set<Index>::iterator conf_it_i = clusters_[i].begin();
conf_it_i != clusters_[i].end(); conf_it_i++)
{
poses_[*conf_it_i].snap->applySnapShot(system_i_);
for (set<Index>::iterator conf_it_j = conf_it_i; conf_it_j != clusters_[i].end(); conf_it_j++)
{
if (conf_it_i != conf_it_j)
{
if (rmsd_type == SNAPSHOT_RMSD)
{
poses_[*conf_it_j].snap->applySnapShot(system_j_);
}
float temp_rmsd = getRMSD_(*conf_it_i, *conf_it_j, rmsd_type);
rmsd = std::max(rmsd, temp_rmsd);
}
}
}
return rmsd;
}
boost::shared_ptr<System> PoseClustering::getPose(Index i) const
{
if (i >= (Index)poses_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
boost::shared_ptr<System> new_system(new System(getSystem()));
if (has_rigid_transformations_)
{
GeometricCenterProcessor center;
new_system->apply(center);
Vector3 toOrigin = center.getCenter().negate();
TranslationProcessor translation;
translation.setTranslation(toOrigin);
new_system->apply(translation);
// apply transformation and rotation
RigidTransformation const& rigid_trafo = *(poses_[i].trafo);
Eigen::Matrix3f const& rd_rotation = rigid_trafo.rotation;
Eigen::Vector3f const& rd_translation = rigid_trafo.translation;
TransformationProcessor transformation;
transformation.setTransformation(Matrix4x4(rd_rotation(0,0), rd_rotation(0,1), rd_rotation(0,2), rd_translation(0),
rd_rotation(1,0), rd_rotation(1,1), rd_rotation(1,2), rd_translation(1),
rd_rotation(2,0), rd_rotation(2,1), rd_rotation(2,2), rd_translation(2),
0, 0, 0, 1));
new_system->apply(transformation);
// move back to its original center
translation.setTranslation(toOrigin.negate());
new_system->apply(translation);
}
else
{
poses_[i].snap->applySnapShot(*(new_system.get()));
}
return new_system;
}
Index PoseClustering::findClusterRepresentative(Index i)
{
if (i >= (Index)clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
Index rmsd_type = options.getInteger(Option::RMSD_TYPE);
// as cluster representative we simply compute the pose that
// has the smallest deviation from all other cluster members
// take the first
//Index conf_set_idx = *(clusters_[i].begin());
//std::set<Index> current_cluster = clusters_[i];
Index current_best = std::numeric_limits<Index>::max();
float min_distance = std::numeric_limits<float>::max();
for (set<Index>::iterator current_pose = clusters_[i].begin(); current_pose != clusters_[i].end(); current_pose++)
{
float sum_dist = 0;
for (set<Index>::iterator to_test_pose = clusters_[i].begin(); to_test_pose != clusters_[i].end(); to_test_pose++)
{
if (*current_pose != *to_test_pose)
{
sum_dist += getRMSD_(*current_pose, *to_test_pose, rmsd_type);
}
}
if (min_distance > sum_dist)
{
min_distance = sum_dist;
current_best = *current_pose;
}
}
return current_best;
}
boost::shared_ptr<System> PoseClustering::getClusterRepresentative(Index i)
{
if (i >= (Index)clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
return getPose(findClusterRepresentative(i));
}
void PoseClustering::setDefaultOptions()
{
options.setDefault(PoseClustering::Option::CLUSTER_METHOD,
PoseClustering::Default::CLUSTER_METHOD);
options.setDefaultReal(PoseClustering::Option::DISTANCE_THRESHOLD,
PoseClustering::Default::DISTANCE_THRESHOLD);
options.setDefault(PoseClustering::Option::RMSD_LEVEL_OF_DETAIL,
PoseClustering::Default::RMSD_LEVEL_OF_DETAIL);
options.setDefault(PoseClustering::Option::RMSD_TYPE,
PoseClustering::Default::RMSD_TYPE);
options.setDefault(PoseClustering::Option::RUN_PARALLEL,
PoseClustering::Default::RUN_PARALLEL);
// options.setDefault(PoseClustering::Option::FULL_CLUSTER_DENDOGRAM,
// PoseClustering::Default::FULL_CLUSTER_DENDOGRAM);
}
boost::shared_ptr<ConformationSet> PoseClustering::getClusterConformationSet(Index i)
{
if (i >= (Index)clusters_.size())
throw(Exception::OutOfRange(__FILE__, __LINE__));
if (current_set_==NULL)
{
// originally we were given transformations, not snapshots
// lets create some
convertTransformations2Snaphots();
}
// create a new ConformationSet
boost::shared_ptr<ConformationSet> new_set(new ConformationSet());
new_set->setup(base_system_);
new_set->setParent(current_set_);
for (set<Index>::iterator conf_it = clusters_[i].begin();
conf_it != clusters_[i].end(); conf_it++)
{
System conf(base_system_);
poses_[*conf_it].snap->applySnapShot(conf);
new_set->add(0, conf);
}
return new_set;
}
boost::shared_ptr<ConformationSet> PoseClustering::getReducedConformationSet()
{
if (clusters_.size()==0)
throw(Exception::OutOfRange(__FILE__, __LINE__));
if (current_set_==NULL)
{
// originally we were given transformations, not snapshots
// lets create some
convertTransformations2Snaphots();
}
cluster_representatives_.clear();
cluster_representatives_.resize(clusters_.size());
// create the set to be returned
boost::shared_ptr<ConformationSet> new_set(new ConformationSet());
new_set->setup(getSystem());
new_set->setParent(current_set_);
// iterate over all clusters
for (Size i=0; i<clusters_.size(); i++)
{
System conf(new_set->getSystem());
// get the cluster Representative
Index current_representative = findClusterRepresentative(i);
poses_[current_representative].snap->applySnapShot(conf);
cluster_representatives_[i] = current_representative;
new_set->add(0, conf);
}
return new_set;
}
// distance between cluster i and cluster j
// this method is used by the trivial complete linkage clustering and computes
// the maximum distance between cluster members
float PoseClustering::getClusterRMSD_(Index i, Index j, Index rmsd_type)
{
// we have to compute the maximal distance
float rmsd = std::numeric_limits<float>::min();
float temp_rmsd = std::numeric_limits<float>::max();
Index cluster_method = options.getInteger(Option::CLUSTER_METHOD);
for (set<Index>::iterator conf_it_i = clusters_[i].begin();
conf_it_i != clusters_[i].end();
conf_it_i ++)
{
for (set<Index>::iterator conf_it_j = clusters_[j].begin();
conf_it_j != clusters_[j].end();
conf_it_j ++)
{
if (rmsd_type == PoseClustering::CENTER_OF_MASS_DISTANCE)
{
temp_rmsd = com_[*conf_it_i].getDistance(com_[*conf_it_j]);
}
else if (cluster_method == TRIVIAL_COMPLETE_LINKAGE)
{
temp_rmsd = pairwise_scores_(*conf_it_i, *conf_it_j);
}
else if ( (cluster_method == PoseClustering::CLINK_DEFAYS)
|| (cluster_method == PoseClustering::SLINK_SIBSON))
{
Log.info() << " This should not happen!! " << __FILE__ << " " << __LINE__<< endl;
}
else
{
Log.info() << "getRMSD() for current option setting not yet implemented! "
<< __FILE__ << " " << __LINE__ << endl;
}
// std::cout << "duddeldumm: " << *conf_it_i << " " << *conf_it_j << " " << temp_rmsd << std::endl;
if (temp_rmsd > rmsd)
rmsd = temp_rmsd;
}
}
return rmsd;
}
float PoseClustering::getRigidRMSD(Eigen::Vector3f const& t_ab, Eigen::Matrix3f const& M_ab, Eigen::Matrix3f const& covariance_matrix)
{
// based on the given transformation we can simply compute the rmsd
//
// 2 |-----------------------------------------------------------
// - | 2 1 ( t / t \ )
// | || t_(a,b) || + - tr ( M M * sum (x_i x_i ) )
// rmsd(a,b) = | n ( i \ / )
// |
return sqrt(t_ab.squaredNorm() + ((M_ab.transpose() * M_ab) * covariance_matrix.selfadjointView<Eigen::Upper>()).trace());
}
float PoseClustering::getSquaredRigidRMSD(Eigen::Vector3f const& t_ab, Eigen::Matrix3f const& M_ab, Eigen::Matrix3f const& covariance_matrix)
{
// based on the given transformation we can simply compute the rmsd
//
// 2 |-----------------------------------------------------------
// - | 2 1 ( t / t \ )
// | || t_(a,b) || + - tr ( M M * sum (x_i x_i ) )
// rmsd(a,b) = | n ( i \ / )
// |
return t_ab.squaredNorm() + ((M_ab.transpose() * M_ab) * covariance_matrix.selfadjointView<Eigen::Upper>()).trace();
}
// compute the RMSD between two "poses",
// i.e. snapshots, transformations, or center of masses
float PoseClustering::getRMSD_(Index i, Index j, Index rmsd_type)
{
float rmsd = 0.;
if (rmsd_type == PoseClustering::RIGID_RMSD)
{
RigidTransformation const& trafo_i = *(poses_[i].trafo);
RigidTransformation const& trafo_j = *(poses_[j].trafo);
Eigen::Vector3f t_ab = (trafo_j.translation - trafo_i.translation);
Eigen::Matrix3f M_ab = (trafo_j.rotation - trafo_i.rotation);
rmsd = getRigidRMSD(t_ab, M_ab, covariance_matrix_);
// std::cout << t_ab << " " << M_ab << " " << rmsd << std::endl << covariance_matrix_ << std::endl;
}
else if (rmsd_type == PoseClustering::SNAPSHOT_RMSD)
{
// we are were given Conformations that have been applied to sys_i and sys_j
// TODO: we should probably use RMSDMinimizer instead!
// or even better, merge StructureMapper and RMSDMinimizer
StructureMapper mapper(system_i_, system_j_);
rmsd = mapper.calculateRMSD(atom_bijection_);
}
else if (rmsd_type == PoseClustering::CENTER_OF_MASS_DISTANCE)
{
// just query the center distance
rmsd = com_[i].getDistance(com_[j]);
}
// std::cout << "rmsd between " << i << " " << j << ": " << rmsd << std::endl;
return rmsd;
}
void PoseClustering::storeSnapShotReferences_()
{
// make sure that we have a sensible default state
poses_.clear();
for (Position i=0; i<current_set_->size(); ++i)
{
poses_.push_back(&((*current_set_)[i]));
}
}
void PoseClustering::applyTransformation2System(Index i, System& target_system)
{
// Please note, for efficiency reasons this code is copied
// from function convertTransformations2Snaphots() below
if (i < (Index) poses_.size())
{
// compute the translation vector to the origin
GeometricCenterProcessor center;
target_system.apply(center);
Vector3 toOrigin = center.getCenter().negate();
// move to origin
TranslationProcessor translation;
translation.setTranslation(toOrigin);
target_system.apply(translation);
TransformationProcessor transformation;
RigidTransformation const& rigid_trafo = *(poses_[i].trafo);
Eigen::Vector3f const& rd_translation = rigid_trafo.translation;
Eigen::Matrix3f const& rd_rotation = rigid_trafo.rotation;
// apply transformation and rotation
// NOTE: transformations given as input are always relative to the original pose
// thus substraction of toOrigin is correct and more efficient than
// the intuitive action sequence
// <toOrigin, pose rotate, negation of toOrigin, pose translation> !
transformation.setTransformation(Matrix4x4(rd_rotation(0,0), rd_rotation(0,1), rd_rotation(0,2), rd_translation(0) - toOrigin.x,
rd_rotation(1,0), rd_rotation(1,1), rd_rotation(1,2), rd_translation(1) - toOrigin.y,
rd_rotation(2,0), rd_rotation(2,1), rd_rotation(2,2), rd_translation(2) - toOrigin.z,
0, 0, 0, 1));
target_system.apply(transformation);
}
else
{
throw(Exception::OutOfRange(__FILE__, __LINE__));
}
}
void PoseClustering::convertTransformations2Snaphots()
{
if (current_set_ != NULL)
{
Log.info() << "Warning: previous snapshots are overwritten!" << __FILE__ << " " << __LINE__ << endl;
}
else
{
// create a new ConformationSet
current_set_ = new ConformationSet();
delete_conformation_set_= true;
current_set_->setup(base_system_);
}
// pre compute some general transformations
System new_template_system(base_system_);
// apply the reference state
base_conformation_.applySnapShot(new_template_system);
// compute the translation vector to the origin
GeometricCenterProcessor center;
new_template_system.apply(center);
Vector3 toOrigin = center.getCenter().negate();
// move to origin
TranslationProcessor translation;
translation.setTranslation(toOrigin);
new_template_system.apply(translation);
TransformationProcessor transformation;
// create one snapshot for each transformation
for (Size i=0; i<poses_.size(); i++)
{
RigidTransformation const& rigid_trafo = *(poses_[i].trafo);
Eigen::Vector3f const& rd_translation = rigid_trafo.translation;
Eigen::Matrix3f const& rd_rotation = rigid_trafo.rotation;
// create a copy
System new_system(new_template_system);
// apply transformation and rotation
// NOTE: transformations given as input are always relative to the original pose
// thus substraction of toOrigin is correct!
transformation.setTransformation(Matrix4x4(rd_rotation(0,0), rd_rotation(0,1), rd_rotation(0,2), rd_translation(0) - toOrigin.x,
rd_rotation(1,0), rd_rotation(1,1), rd_rotation(1,2), rd_translation(1) - toOrigin.y,
rd_rotation(2,0), rd_rotation(2,1), rd_rotation(2,2), rd_translation(2) - toOrigin.z,
0, 0, 0, 1));
new_system.apply(transformation);
// add the conformation
current_set_->add(0, new_system);
}
for (Position i=0; i<current_set_->size(); ++i)
{
poses_[i].snap = &((*current_set_)[i]);
}
}
void PoseClustering::convertSnaphots2Transformations()
{
// just to be sure...
transformations_.clear();
// we will also need the covariance matrix
covariance_matrix_ = computeCovarianceMatrix(base_system_, options.getInteger(Option::RMSD_LEVEL_OF_DETAIL));
Size num_poses = getNumberOfPoses();
system_i_ = base_system_;
system_j_ = base_system_;
poses_[0].snap->applySnapShot(system_i_);
// TODO: return and parameters?
precomputeAtomBijection_();
computeCenterOfMasses_();
// we iterate over all the snapshots
for (Position current_snap = 0; current_snap < num_poses; ++current_snap)
{
// apply the snapshot (we compute all transformations relative to system_i_)
poses_[current_snap].snap->applySnapShot(system_j_);
// compute the best mapping of the base system to the transformed one
RMSDMinimizer::Result transform = RMSDMinimizer::computeTransformation(atom_bijection_);
Matrix4x4& bm = transform.first;
// convert the contained matrix to Eigen translation vector & rotation matrix
RigidTransformation rd;
// unfortunately, we cannot directly use the translation vector contained in the matrix:
// the way the transformation is set up, we will first move the second protein in the origin,
// then rotate it, and finally move it to the center of mass of the first protein. for our
// RIGID_RMSD-computation, however, we need the difference of the centers of mass, without
// the influence of the rotation
rd.rotation << bm.m11, bm.m12, bm.m13, bm.m21, bm.m22, bm.m23, bm.m31, bm.m32, bm.m33;
Vector3 current_com = com_[current_snap] - com_[0];
rd.translation << current_com.x, current_com.y, current_com.z;
transformations_.push_back(rd);
}
for (Position i=0; i<poses_.size(); ++i)
{
poses_[i].trafo = &(transformations_[i]);
}
has_rigid_transformations_ = true;
}
void PoseClustering::printCluster_(Size i, std::ostream& out) const
{
out << "++++ cluster " << i << " score " << getClusterScore(i) << " members " << clusters_[i].size();
if (i < cluster_representatives_.size())
out << " rep " << cluster_representatives_[i];
out << " ++++" << endl;
std::copy(clusters_[i].begin(), clusters_[i].end(), std::ostream_iterator<Index>(out, " "));
out << endl;
out << "+++++++++++++++++++" << endl;
}
void PoseClustering::printClusters(std::ostream& out) const
{
out << "+\n+\n+ FINAL CLUSTERS \n+\n+" << endl;
for (Size i = 0; i < clusters_.size(); i++)
{
printCluster_(i, out);
}
}
void PoseClustering::printVariables_(int a, int b, double c, int d, double e, int current_level)
{
cout << "***************************" << endl;
cout << " a:" << a << " b:" << b << " c:" << c << " d:" << d << " e:" << e << " cl: " << current_level << endl;
cout << " pi:" << endl;
for (int i=0; i < current_level +1; ++i)
{
cout << "(i," << pi_[i] << ") ";
}
cout << endl;
cout << " lambda:" << endl;
for (int i=0; i < current_level+1; ++i)
{
cout << "(i," << lambda_[i] << ") ";
}
cout << endl;
cout << " mu:" << endl;
for (int i=0; i < current_level; ++i)
{
cout << "(i," << mu_[i] << ") ";
}
cout << endl;
cout << "******************" << endl;
}
void PoseClustering::clear_()
{
// Note: options should not be reset
pairwise_scores_.setZero();
if (delete_conformation_set_)
delete current_set_;
has_rigid_transformations_ = false;
delete_conformation_set_ = false;
current_set_ = NULL;
poses_.clear();
transformations_.clear();
covariance_matrix_.setZero();
base_system_.clear();
base_conformation_.clear();
clusters_.clear();
cluster_representatives_.clear();
cluster_scores_.clear();
//rmsd_level_of_detail_;
lambda_.clear();
pi_.clear();
mu_.clear();
com_.clear();
//atom_bijection_;
//system_i_;
//system_j_;
number_of_selected_atoms_ = 0;
}
}
|