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
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728 | def fragment_dock(input_models: Iterable[ContainsEntities]) -> list[PoseJob] | list:
"""Perform the fragment docking routine described in Laniado, Meador, & Yeates, PEDS. 2021
Args:
input_models: The Structures to be used in docking
Returns:
The resulting Poses satisfying docking criteria
"""
frag_dock_time_start = time.time()
# Todo reimplement this feature to write a log to the Project directory?
# # Setup logger
# if logger is None:
# log_file_path = os.path.join(project_dir, f'{building_blocks}_log.txt')
# else:
# try:
# log_file_path = getattr(logger.handlers[0], 'baseFilename', None)
# except IndexError: # No handler attached to this logger. Probably passing to a parent logger
# log_file_path = None
#
# if log_file_path: # Start logging to a file in addition
# logger = start_log(name=building_blocks, handler=2, location=log_file_path, format_log=False, propagate=True)
# Retrieve symjob.JobResources for all flags
job = symjob.job_resources_factory.get()
sym_entry: SymEntry = job.sym_entry
"""The SymmetryEntry object describing the material"""
if sym_entry:
protocol_name = putils.nanohedra
else:
protocol_name = putils.fragment_docking
#
# protocol = Protocol(name=protocol_name)
job.fragment_db = fragment_factory(source=job.fragment_source)
euler_lookup = job.fragment_db.euler_lookup
# This is used in clustering algorithms to define an observation outside the found clusters
outlier = -1
initial_z_value = job.dock.initial_z_value
"""The acceptable standard deviation z score for initial fragment overlap identification. Smaller values lead to
more stringent matching criteria
"""
min_matched = job.dock.minimum_matched
"""How many high quality fragment pairs should be present before a pose is identified?"""
high_quality_match_value = job.dock.match_value
"""The value to exceed before a high quality fragment is matched. When z-value was used this was 1.0, however, 0.5
when match score is used
"""
rotation_step1 = job.dock.rotation_step1
rotation_step2 = job.dock.rotation_step2
# Todo 3 set below as parameters?
measure_interface_during_dock = True
low_quality_match_value = .2
"""The lower bounds on an acceptable match. Was upper bound of 2 using z-score"""
clash_dist: float = 2.1
"""The distance to measure for clashing atoms"""
cb_distance = 9. # change to 8.?
"""The distance to measure for interface atoms"""
# Testing if this is too strict when strict overlaps are used
cluster_translations = not job.dock.contiguous_ghosts # True
translation_cluster_epsilon = 1
# 1 works well at recapitulating the results without it while reducing number of checks
# More stringent -> 0.75
cluster_transforms = False # True
"""Whether the entire transformation space should be clustered. This was found to be redundant with a translation
clustering search only, and instead, decreases Pose solutions at the edge of oligomeric search slices
"""
transformation_cluster_epsilon = 1
# 1 seems to work well at recapitulating the results without it
# less stringent -> 0.75, removes about 20% found solutions
# stringent -> 0.5, removes about %50 found solutions
forward_reverse = False # True
# Todo 3 set above as parameters?
high_quality_z_value = z_value_from_match_score(high_quality_match_value)
low_quality_z_value = z_value_from_match_score(low_quality_match_value)
if job.dock.perturb_dof_tx:
if sym_entry.unit_cell:
logger.critical(f"{create_perturbation_transformations.__name__} hasn't been tested for lattice symmetries")
# Get score functions from input
if job.dock.weight and isinstance(job.dock.weight, dict):
# Todo actually use these during optimize_found_transformations_by_metrics()
# score_functions = metrics.pose.format_metric_functions(job.dock.weight.keys())
default_weight_metric = None
else:
# score_functions = {}
if job.dock.proteinmpnn_score:
weight_method = f'{putils.nanohedra}+{putils.proteinmpnn}'
else:
weight_method = putils.nanohedra
default_weight_metric = resources.config.default_weight_parameter[weight_method]
# Initialize incoming Structures
models = []
"""The Structure instances to be used in docking"""
# Assumes model is oriented with major axis of symmetry along z
entity_count = count(1)
for idx, (input_model, symmetry) in enumerate(zip(input_models, sym_entry.groups)):
for entity in input_model.entities:
if entity.is_symmetric():
pass
else:
# Remove any unstructured termini from the Entity to allow the best secondary structure docking
if job.trim_termini:
entity.delete_termini(how='unstructured')
# Ensure models are oligomeric
entity.make_oligomer(symmetry=symmetry)
# Make, then save a new model based on the symmetric version of each Entity in the Model
if input_model.number_of_entities > 2:
# If this was a Pose, this is essentially what is happening
# model = input_model.assembly
model = Model.from_chains([chain for entity in input_model.entities for chain in entity.chains],
name=input_model.name)
raise NotImplementedError(f"Can't dock 2 Model instances with > 2 total Entity instances")
else:
model = input_model.entities[0].assembly
model.name = input_model.name
model.fragment_db = job.fragment_db
# # Ensure the .metadata attribute is passed to each entity in the full assembly
# # This is crucial for sql usage
# for _entity, entity in zip(model.entities, input_model.entities):
# _entity.metadata = entity.metadata
models.append(model)
model1: Model
model2: Model
model1, model2 = models
del models
logger.info(f'DOCKING {model1.name} TO {model2.name}')
# Set up output mechanism
entry_string = f'NanohedraEntry{sym_entry.number}'
building_blocks = '-'.join(input_model.name for input_model in input_models)
if job.prefix:
project = f'{job.prefix}{building_blocks}'
else:
project = building_blocks
if job.suffix:
project = f'{project}{job.suffix}'
project = f'{entry_string}_{project}'
project_dir = os.path.join(job.projects, project)
putils.make_path(project_dir)
if job.output_trajectory:
if sym_entry.unit_cell:
logger.warning('No unit cell dimensions applicable to the trajectory file')
# Set up the TransformHasher to assist in scoring/pose output
radius1 = model1.radius # .distance_from_reference(measure='max')
radius2 = model2.radius # .distance_from_reference(measure='max')
# Assume the maximum distance the box could get is the radius of each plus the interface distance
box_width = radius1 + radius2 + cb_distance
model_transform_hasher = TransformHasher(box_width)
# Set up Building Block1
get_complete_surf_frags1_time_start = time.time()
surf_frags1 = model1.get_fragment_residues(residues=model1.surface_residues, fragment_db=model1.fragment_db)
# Calculate the initial match type by finding the predominant surface type
fragment_content1 = np.bincount([surf_frag.i_type for surf_frag in surf_frags1])
initial_surf_type1 = np.argmax(fragment_content1)
init_surf_frags1 = [surf_frag for surf_frag in surf_frags1 if surf_frag.i_type == initial_surf_type1]
# For reverse/forward matching these two arrays must be made
if forward_reverse:
init_surf_guide_coords1 = np.array([surf_frag.guide_coords for surf_frag in init_surf_frags1])
init_surf_residue_indices1 = np.array([surf_frag.index for surf_frag in init_surf_frags1])
# surf_frag1_indices = [surf_frag.index for surf_frag in surf_frags1]
idx = 1
# logger.debug(f'Found surface guide coordinates {idx} with shape {surf_guide_coords1.shape}')
# logger.debug(f'Found surface residue numbers {idx} with shape {surf_residue_numbers1.shape}')
# logger.debug(f'Found surface indices {idx} with shape {surf_i_indices1.shape}')
logger.debug(f'Found {len(init_surf_frags1)} initial surface {idx} fragments with type: {initial_surf_type1}')
# logger.debug('Found component 2 fragment content: %s' % fragment_content2)
# logger.debug('init_surf_frag_indices2: %s' % slice_variable_for_log(init_surf_frag_indices2))
# logger.debug('init_surf_guide_coords2: %s' % slice_variable_for_log(init_surf_guide_coords2))
# logger.debug('init_surf_residue_indices2: %s' % slice_variable_for_log(init_surf_residue_indices2))
# logger.debug('init_surf_guide_coords1: %s' % slice_variable_for_log(init_surf_guide_coords1))
# logger.debug('init_surf_residue_indices1: %s' % slice_variable_for_log(init_surf_residue_indices1))
logger.info(f'Retrieved component{idx}-{model1.name} surface fragments and guide coordinates took '
f'{time.time() - get_complete_surf_frags1_time_start:8f}s')
#################################
# Set up Building Block2
# Get Surface Fragments With Guide Coordinates Using COMPLETE Fragment Database
get_complete_surf_frags2_time_start = time.time()
surf_frags2 = \
model2.get_fragment_residues(residues=model2.surface_residues, fragment_db=model2.fragment_db)
# Calculate the initial match type by finding the predominant surface type
surf_guide_coords2 = np.array([surf_frag.guide_coords for surf_frag in surf_frags2])
surf_residue_indices2 = np.array([surf_frag.index for surf_frag in surf_frags2])
surf_i_indices2 = np.array([surf_frag.i_type for surf_frag in surf_frags2])
fragment_content2 = np.bincount(surf_i_indices2)
initial_surf_type2 = np.argmax(fragment_content2)
init_surf_frag_indices2 = \
[idx for idx, surf_frag in enumerate(surf_frags2) if surf_frag.i_type == initial_surf_type2]
init_surf_guide_coords2 = surf_guide_coords2[init_surf_frag_indices2]
init_surf_residue_indices2 = surf_residue_indices2[init_surf_frag_indices2]
idx = 2
logger.debug(f'Found surface guide coordinates {idx} with shape {surf_guide_coords2.shape}')
logger.debug(f'Found surface residue numbers {idx} with shape {surf_residue_indices2.shape}')
logger.debug(f'Found surface indices {idx} with shape {surf_i_indices2.shape}')
logger.debug(
f'Found {len(init_surf_residue_indices2)} initial surface {idx} fragments with type: {initial_surf_type2}')
logger.info(f'Retrieved component{idx}-{model2.name} surface fragments and guide coordinates took '
f'{time.time() - get_complete_surf_frags2_time_start:8f}s')
# logger.debug('init_surf_frag_indices2: %s' % slice_variable_for_log(init_surf_frag_indices2))
# logger.debug('init_surf_guide_coords2: %s' % slice_variable_for_log(init_surf_guide_coords2))
# logger.debug('init_surf_residue_indices2: %s' % slice_variable_for_log(init_surf_residue_indices2))
#################################
# Get component 1 ghost fragments and associated data from complete fragment database
component1_backbone_cb_tree = BallTree(model1.backbone_and_cb_coords)
get_complete_ghost_frags1_time_start = time.time()
ghost_frags_by_residue1 = \
[frag.get_ghost_fragments(clash_tree=component1_backbone_cb_tree) for frag in surf_frags1]
complete_ghost_frags1: list[GhostFragment] = \
[ghost for ghosts in ghost_frags_by_residue1 for ghost in ghosts]
ghost_guide_coords1 = np.array([ghost_frag.guide_coords for ghost_frag in complete_ghost_frags1])
ghost_rmsds1 = np.array([ghost_frag.rmsd for ghost_frag in complete_ghost_frags1])
ghost_residue_indices1 = np.array([ghost_frag.index for ghost_frag in complete_ghost_frags1])
ghost_j_indices1 = np.array([ghost_frag.j_type for ghost_frag in complete_ghost_frags1])
# Whether to use the overlap potential on the same component to filter ghost fragments
if job.dock.contiguous_ghosts:
# Prioritize search at those fragments which have same component, ghost fragment overlap
contiguous_ghost_indices1 = make_contiguous_ghosts(ghost_frags_by_residue1, surf_frags1,
# distance=cb_distance,
initial_z_value=initial_z_value)
initial_ghost_frags1 = [complete_ghost_frags1[idx] for idx in contiguous_ghost_indices1.tolist()]
init_ghost_guide_coords1 = np.array([ghost_frag.guide_coords for ghost_frag in initial_ghost_frags1])
init_ghost_rmsds1 = np.array([ghost_frag.rmsd for ghost_frag in initial_ghost_frags1])
init_ghost_residue_indices1 = np.array([ghost_frag.index for ghost_frag in initial_ghost_frags1])
# init_ghost_guide_coords1, init_ghost_rmsds1, init_ghost_residue_indices1 = \
# make_contiguous_ghosts(ghost_frags_by_residue1, surf_frags)
else:
init_ghost_frag_indices1 = \
[idx for idx, ghost_frag in enumerate(complete_ghost_frags1) if ghost_frag.j_type == initial_surf_type2]
init_ghost_guide_coords1: np.ndarray = ghost_guide_coords1[init_ghost_frag_indices1]
init_ghost_rmsds1: np.ndarray = ghost_rmsds1[init_ghost_frag_indices1]
init_ghost_residue_indices1: np.ndarray = ghost_residue_indices1[init_ghost_frag_indices1]
idx = 1
logger.debug(f'Found ghost guide coordinates {idx} with shape: {ghost_guide_coords1.shape}')
logger.debug(f'Found ghost residue numbers {idx} with shape: {ghost_residue_indices1.shape}')
logger.debug(f'Found ghost indices {idx} with shape: {ghost_j_indices1.shape}')
logger.debug(f'Found ghost rmsds {idx} with shape: {ghost_rmsds1.shape}')
logger.debug(f'Found {len(init_ghost_guide_coords1)} initial ghost {idx} fragments with type:'
f' {initial_surf_type2}')
logger.info(f'Retrieved component{idx}-{model1.name} ghost fragments and guide coordinates '
f'took {time.time() - get_complete_ghost_frags1_time_start:8f}s')
#################################
# Implemented for Todd to work on C1 instances
if job.only_write_frag_info:
# Whether to write fragment information to a directory (useful for fragment based docking w/o Nanohedra)
guide_file_ghost = os.path.join(project_dir, f'{model1.name}_ghost_coords.txt')
with open(guide_file_ghost, 'w') as f:
for coord_group in ghost_guide_coords1.tolist():
f.write('%s\n' % ' '.join('%f,%f,%f' % tuple(coords) for coords in coord_group))
guide_file_ghost_idx = os.path.join(project_dir, f'{model1.name}_ghost_coords_index.txt')
with open(guide_file_ghost_idx, 'w') as f:
f.write('%s\n' % '\n'.join(map(str, ghost_j_indices1.tolist())))
guide_file_ghost_res_num = os.path.join(project_dir, f'{model1.name}_ghost_coords_residue_number.txt')
with open(guide_file_ghost_res_num, 'w') as f:
f.write('%s\n' % '\n'.join(map(str, ghost_residue_indices1.tolist())))
guide_file_surf = os.path.join(project_dir, f'{model2.name}_surf_coords.txt')
with open(guide_file_surf, 'w') as f:
for coord_group in surf_guide_coords2.tolist():
f.write('%s\n' % ' '.join('%f,%f,%f' % tuple(coords) for coords in coord_group))
guide_file_surf_idx = os.path.join(project_dir, f'{model2.name}_surf_coords_index.txt')
with open(guide_file_surf_idx, 'w') as f:
f.write('%s\n' % '\n'.join(map(str, surf_i_indices2.tolist())))
guide_file_surf_res_num = os.path.join(project_dir, f'{model2.name}_surf_coords_residue_number.txt')
with open(guide_file_surf_res_num, 'w') as f:
f.write('%s\n' % '\n'.join(map(str, surf_residue_indices2.tolist())))
start_slice = 0
visualize_number = 15
indices_of_interest = [0, 3, 5, 10]
for idx, frags in enumerate(ghost_frags_by_residue1):
if idx in indices_of_interest:
number_of_fragments = len(frags)
step_size = number_of_fragments // visualize_number
# end_slice = start_slice * step_size
residue_number = frags[0].number
write_fragment_pairs_as_accumulating_states(
ghost_frags_by_residue1[idx][start_slice:number_of_fragments:step_size],
os.path.join(project_dir, f'{model1.name}_{residue_number}_paired_frags_'
f'{start_slice}:{number_of_fragments}:{visualize_number}.pdb'))
raise RuntimeError(
f'Suspending operation of {model1.name}/{model2.name} after write')
ij_type_match_lookup_table = compute_ij_type_lookup(ghost_j_indices1, surf_i_indices2)
# Axis 0 is ghost frag, 1 is surface frag
# ij_matching_ghost1_indices = \
# (ij_type_match_lookup_table * np.arange(len(ij_type_match_lookup_table)))[ij_type_match_lookup_table]
# ij_matching_surf2_indices = \
# (ij_type_match_lookup_table * np.arange(ij_type_match_lookup_table.shape[1])[:, None])[
# ij_type_match_lookup_table]
# Tod0 apparently this works to grab the flattened indices where there is overlap
# row_indices, column_indices = np.indices(ij_type_match_lookup_table.shape)
# # row index vary with ghost, column surf
# # transpose to index the first axis (axis=0) along the 1D row indices
# ij_matching_ghost1_indices = row_indices[ij_type_match_lookup_table.T]
# ij_matching_surf2_indices = column_indices[ij_type_match_lookup_table]
# >>> j = np.ones(22)
# >>> k = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
# >>> k.shape
# (2, 22)
# >>> j[k]
# array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
# 1., 1., 1., 1., 1., 1.],
# [1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
# 1., 1., 1., 1., 1., 1.]])
# This will allow pulling out the indices where there is overlap which may be useful
# for limiting scope of overlap checks
# Get component 2 ghost fragments and associated data from complete fragment database
bb_cb_coords2 = model2.backbone_and_cb_coords
# Whether to use the overlap potential on the same component to filter ghost fragments
if forward_reverse:
bb_cb_tree2 = BallTree(bb_cb_coords2)
get_complete_ghost_frags2_time_start = time.time()
ghost_frags_by_residue2 = \
[frag.get_ghost_fragments(clash_tree=bb_cb_tree2) for frag in surf_frags2]
complete_ghost_frags2: list[GhostFragment] = \
[ghost for ghosts in ghost_frags_by_residue2 for ghost in ghosts]
# complete_ghost_frags2 = []
# for frag in surf_frags2:
# complete_ghost_frags2.extend(frag.get_ghost_fragments(clash_tree=bb_cb_tree2))
if job.dock.contiguous_ghosts:
# Prioritize search at those fragments which have same component, ghost fragment overlap
contiguous_ghost_indices2 = make_contiguous_ghosts(ghost_frags_by_residue2, surf_frags2,
# distance=cb_distance,
initial_z_value=initial_z_value)
initial_ghost_frags2 = [complete_ghost_frags2[idx] for idx in contiguous_ghost_indices2.tolist()]
init_ghost_guide_coords2 = np.array([ghost_frag.guide_coords for ghost_frag in initial_ghost_frags2])
# init_ghost_rmsds2 = np.array([ghost_frag.rmsd for ghost_frag in initial_ghost_frags2])
init_ghost_residue_indices2 = np.array([ghost_frag.index for ghost_frag in initial_ghost_frags2])
# init_ghost_guide_coords1, init_ghost_rmsds1, init_ghost_residue_indices1 = \
# make_contiguous_ghosts(ghost_frags_by_residue1, surf_frags)
else:
# init_ghost_frag_indices2 = \
# [idx for idx, ghost_frag in enumerate(complete_ghost_frags2) if ghost_frag.j_type == initial_surf_type1]
# init_ghost_guide_coords2: np.ndarray = ghost_guide_coords2[init_ghost_frag_indices2]
# # init_ghost_rmsds2: np.ndarray = ghost_rmsds2[init_ghost_frag_indices2]
# init_ghost_residue_indices2: np.ndarray = ghost_residue_indices2[init_ghost_frag_indices2]
initial_ghost_frags2 = [ghost_frag for ghost_frag in complete_ghost_frags2 if
ghost_frag.j_type == initial_surf_type1]
init_ghost_guide_coords2 = np.array([ghost_frag.guide_coords for ghost_frag in initial_ghost_frags2])
init_ghost_residue_indices2 = np.array([ghost_frag.index for ghost_frag in initial_ghost_frags2])
idx = 2
logger.debug(
f'Found {len(init_ghost_guide_coords2)} initial ghost {idx} fragments with type {initial_surf_type1}')
# logger.debug('init_ghost_guide_coords2: %s' % slice_variable_for_log(init_ghost_guide_coords2))
# logger.debug('init_ghost_residue_indices2: %s' % slice_variable_for_log(init_ghost_residue_indices2))
# ghost2_residue_array = np.repeat(init_ghost_residue_indices2, len(init_surf_residue_indices1))
# surface1_residue_array = np.tile(init_surf_residue_indices1, len(init_ghost_residue_indices2))
logger.info(f'Retrieved component{idx}-{model2.name} ghost fragments and guide coordinates '
f'took {time.time() - get_complete_ghost_frags2_time_start:8f}s')
# logger.debug(f'Found ghost guide coordinates {idx} with shape {ghost_guide_coords2.shape}')
# logger.debug(f'Found ghost residue numbers {idx} with shape {ghost_residue_numbers2.shape}')
# logger.debug(f'Found ghost indices {idx} with shape {ghost_j_indices2.shape}')
# logger.debug(f'Found ghost rmsds {idx} with shape {ghost_rmsds2.shape}')
# Prepare precomputed arrays for fast pair lookup
# ghost1_residue_array = np.repeat(init_ghost_residue_indices1, len(init_surf_residue_indices2))
# surface2_residue_array = np.tile(init_surf_residue_indices2, len(init_ghost_residue_indices1))
logger.info('Obtaining rotation/degeneracy matrices')
translation_perturb_steps = tuple(.5 for _ in range(sym_entry.number_dof_translation))
"""The number of angstroms to increment the translation degrees of freedom search for each model"""
rotation_steps = [rotation_step1, rotation_step2]
"""The number of degrees to increment the rotational degrees of freedom search for each model"""
number_of_degens = []
number_of_rotations = []
rotation_matrices = []
for idx, rotation_step in enumerate(rotation_steps, 1):
if getattr(sym_entry, f'is_internal_rot{idx}'): # if rotation step required
if rotation_step is None:
rotation_step = 3 # Set rotation_step to default
# Set sym_entry.rotation_step
setattr(sym_entry, f'rotation_step{idx}', rotation_step)
else:
if rotation_step:
logger.warning(f"Specified rotation_step{idx} was ignored. Oligomer {idx} doesn't have rotational DOF")
rotation_step = 1 # Set rotation step to 1
rotation_steps[idx - 1] = rotation_step
degeneracy_matrices = getattr(sym_entry, f'degeneracy_matrices{idx}')
# Todo 3 make reliant on scipy...Rotation
# rotation_matrix = \
# scipy.spatial.transform.Rotation.from_euler('Z', [step * rotation_step
# for step in range(number_of_steps)],
# degrees=True).as_matrix()
# rotations = \
# scipy.spatial.transform.Rotation.from_euler('Z', np.linspace(0, getattr(sym_entry, f'rotation_range{idx}'),
# number_of_steps),
# degrees=True).as_matrix()
# rot_degen_matrices = []
# for idx in range(degeneracy_matrices):
# rot_degen_matrices = rotations * degeneracy_matrices[idx]
# rot_degen_matrices = rotations * degeneracy_matrices
# rotation_matrix = rotations.as_matrix()
rotation_matrix = get_rot_matrices(rotation_step, 'z', getattr(sym_entry, f'rotation_range{idx}'))
rot_degen_matrices = make_rotations_degenerate(rotation_matrix, degeneracy_matrices)
logger.debug(f'Degeneracy shape for component {idx}: {degeneracy_matrices.shape}')
logger.debug(f'Combined rotation/degeneracy shape for component {idx}: {rot_degen_matrices.shape}')
degen_len = len(degeneracy_matrices)
number_of_degens.append(degen_len)
# logger.debug(f'Rotation shape for component {idx}: {rot_degen_matrices.shape}')
number_of_rotations.append(len(rot_degen_matrices) // degen_len)
rotation_matrices.append(rot_degen_matrices)
set_mat1, set_mat2 = sym_entry.setting_matrix1, sym_entry.setting_matrix2
# def check_forward_and_reverse(test_ghost_guide_coords, stack_rot1, stack_tx1,
# test_surf_guide_coords, stack_rot2, stack_tx2,
# reference_rmsds):
# """Debug forward versus reverse guide coordinate fragment matching
#
# All guide_coords and reference_rmsds should be indexed to the same length and overlap
# """
# mismatch = False
# inv_set_mat1 = np.linalg.inv(set_mat1)
# for shift_idx in range(1):
# rot1 = stack_rot1[shift_idx]
# tx1 = stack_tx1[shift_idx]
# rot2 = stack_rot2[shift_idx]
# tx2 = stack_tx2[shift_idx]
#
# tnsfmd_ghost_coords = transform_coordinate_sets(test_ghost_guide_coords,
# rotation=rot1,
# translation=tx1,
# rotation2=set_mat1)
# tnsfmd_surf_coords = transform_coordinate_sets(test_surf_guide_coords,
# rotation=rot2,
# translation=tx2,
# rotation2=set_mat2)
# int_euler_matching_ghost_indices, int_euler_matching_surf_indices = \
# euler_lookup.check_lookup_table(tnsfmd_ghost_coords, tnsfmd_surf_coords)
#
# all_fragment_match = calculate_match(tnsfmd_ghost_coords[int_euler_matching_ghost_indices],
# tnsfmd_surf_coords[int_euler_matching_surf_indices],
# reference_rmsds[int_euler_matching_ghost_indices])
# high_qual_match_indices = np.flatnonzero(all_fragment_match >= high_quality_match_value)
# high_qual_match_count = len(high_qual_match_indices)
# if high_qual_match_count < min_matched:
# logger.info(
# f'\t{high_qual_match_count} < {min_matched} Which is Set as the Minimal Required Amount of '
# f'High Quality Fragment Matches')
#
# # Find the passing overlaps to limit the output to only those passing the low_quality_match_value
# passing_overlaps_indices = np.flatnonzero(all_fragment_match >= low_quality_match_value)
# number_passing_overlaps = len(passing_overlaps_indices)
# logger.info(
# f'\t{high_qual_match_count} High Quality Fragments Out of {number_passing_overlaps} '
# f'Matches Found in Complete Fragment Library')
#
# # now try inverse
# inv_rot_mat1 = np.linalg.inv(rot1)
# tnsfmd_surf_coords_inv = transform_coordinate_sets(tnsfmd_surf_coords,
# rotation=inv_set_mat1,
# translation=tx1 * -1,
# rotation2=inv_rot_mat1)
# int_euler_matching_ghost_indices_inv, int_euler_matching_surf_indices_inv = \
# euler_lookup.check_lookup_table(test_ghost_guide_coords, tnsfmd_surf_coords_inv)
#
# all_fragment_match = calculate_match(test_ghost_guide_coords[int_euler_matching_ghost_indices_inv],
# tnsfmd_surf_coords_inv[int_euler_matching_surf_indices_inv],
# reference_rmsds[int_euler_matching_ghost_indices_inv])
# high_qual_match_indices = np.flatnonzero(all_fragment_match >= high_quality_match_value)
# high_qual_match_count = len(high_qual_match_indices)
# if high_qual_match_count < min_matched:
# logger.info(
# f'\tINV {high_qual_match_count} < {min_matched} Which is Set as the Minimal Required Amount'
# f' of High Quality Fragment Matches')
#
# # Find the passing overlaps to limit the output to only those passing the low_quality_match_value
# passing_overlaps_indices = np.flatnonzero(all_fragment_match >= low_quality_match_value)
# number_passing_overlaps = len(passing_overlaps_indices)
#
# logger.info(
# f'\t{high_qual_match_count} High Quality Fragments Out of {number_passing_overlaps} '
# f'Matches Found in Complete Fragment Library')
#
# def investigate_mismatch():
# logger.info(f'Euler True ghost/surf indices forward and inverse don\'t match. '
# f'Shapes: Forward={int_euler_matching_ghost_indices.shape}, '
# f'Inverse={int_euler_matching_ghost_indices_inv.shape}')
# logger.debug(f'tnsfmd_ghost_coords.shape {tnsfmd_ghost_coords.shape}')
# logger.debug(f'tnsfmd_surf_coords.shape {tnsfmd_surf_coords.shape}')
# int_euler_matching_array = \
# euler_lookup.check_lookup_table(tnsfmd_ghost_coords, tnsfmd_surf_coords, return_bool=True)
# int_euler_matching_array_inv = \
# euler_lookup.check_lookup_table(test_ghost_guide_coords, tnsfmd_surf_coords_inv, return_bool=True)
# # Change the shape to allow for relation to guide_coords
# different = np.where(int_euler_matching_array != int_euler_matching_array_inv,
# True, False).reshape(len(tnsfmd_ghost_coords), -1)
# ghost_indices, surface_indices = np.nonzero(different)
# logger.debug(f'different.shape {different.shape}')
#
# different_ghosts = tnsfmd_ghost_coords[ghost_indices]
# different_surf = tnsfmd_surf_coords[surface_indices]
# tnsfmd_ghost_ints1, tnsfmd_ghost_ints2, tnsfmd_ghost_ints3 = \
# euler_lookup.get_eulint_from_guides(different_ghosts)
# tnsfmd_surf_ints1, tnsfmd_surf_ints2, tnsfmd_surf_ints3 = \
# euler_lookup.get_eulint_from_guides(different_surf)
# stacked_ints = np.stack([tnsfmd_ghost_ints1, tnsfmd_ghost_ints2, tnsfmd_ghost_ints3,
# tnsfmd_surf_ints1, tnsfmd_surf_ints2, tnsfmd_surf_ints3], axis=0).T
# logger.info(
# f'The mismatched forward Euler ints are\n{[ints for ints in list(stacked_ints)[:10]]}\n')
#
# different_ghosts_inv = test_ghost_guide_coords[ghost_indices]
# different_surf_inv = tnsfmd_surf_coords_inv[surface_indices]
# tnsfmd_ghost_ints_inv1, tnsfmd_ghost_ints_inv2, tnsfmd_ghost_ints_inv3 = \
# euler_lookup.get_eulint_from_guides(different_ghosts_inv)
# tnsfmd_surf_ints_inv1, tnsfmd_surf_ints_inv2, tnsfmd_surf_ints_inv3 = \
# euler_lookup.get_eulint_from_guides(different_surf_inv)
#
# stacked_ints_inv = \
# np.stack([tnsfmd_ghost_ints_inv1, tnsfmd_ghost_ints_inv2, tnsfmd_ghost_ints_inv3,
# tnsfmd_surf_ints_inv1, tnsfmd_surf_ints_inv2, tnsfmd_surf_ints_inv3], axis=0).T
# logger.info(
# f'The mismatched inverse Euler ints are\n{[ints for ints in list(stacked_ints_inv)[:10]]}\n')
#
# if not np.array_equal(int_euler_matching_ghost_indices, int_euler_matching_ghost_indices_inv):
# mismatch = True
#
# if not np.array_equal(int_euler_matching_surf_indices, int_euler_matching_surf_indices_inv):
# mismatch = True
#
# if mismatch:
# investigate_mismatch()
# if job.skip_transformation:
# transformation1 = unpickle(kwargs.get('transformation_file1'))
# full_rotation1, full_int_tx1, full_setting1, full_ext_tx1 = transformation1.values()
# transformation2 = unpickle(kwargs.get('transformation_file2'))
# full_rotation2, full_int_tx2, full_setting2, full_ext_tx2 = transformation2.values()
# else:
# Set up internal translation parameters
# zshift1/2 must be 2d array, thus the , 2:3].T instead of , 2].T
# [:, None, 2] would also work
if sym_entry.is_internal_tx1: # Add the translation to Z (axis=1)
full_int_tx1 = []
zshift1 = set_mat1[:, None, 2].T
else:
full_int_tx1 = zshift1 = None
if sym_entry.is_internal_tx2:
full_int_tx2 = []
zshift2 = set_mat2[:, None, 2].T
else:
full_int_tx2 = zshift2 = None
# Set up external translation parameters
full_optimal_ext_dof_shifts = []
if sym_entry.unit_cell:
positive_indices = None
else:
# Ensure to slice by nothing, as None alone creates a new axis
positive_indices = slice(None)
# Initialize the OptimalTx object
logger.debug(f'zshift1={zshift1}, zshift2={zshift2}, max_z_value={initial_z_value:2f}')
optimal_tx = resources.OptimalTx.from_dof(sym_entry.external_dof, zshift1=zshift1, zshift2=zshift2,
max_z_value=initial_z_value)
number_of_init_ghost = len(init_ghost_guide_coords1)
number_of_init_surf = len(init_surf_guide_coords2)
total_ghost_surf_combinations = number_of_init_ghost * number_of_init_surf
full_rotation1, full_rotation2 = [], []
rotation_matrices1, rotation_matrices2 = rotation_matrices
rotation_matrices_len1, rotation_matrices_len2 = len(rotation_matrices1), len(rotation_matrices2)
number_of_rotations1, number_of_rotations2 = number_of_rotations
# number_of_degens1, number_of_degens2 = number_of_degens
# Perform Euler integer extraction for all rotations
init_translation_time_start = time.time()
# Rotate Oligomer1 surface and ghost guide coordinates using rotation_matrices1 and set_mat1
# Must add a new axis so that the multiplication is broadcast
ghost_frag1_guide_coords_rot_and_set = \
transform_coordinate_sets(init_ghost_guide_coords1[None, :, :, :],
rotation=rotation_matrices1[:, None, :, :],
rotation2=set_mat1[None, None, :, :])
# Unstack the guide coords to be shape (N, 3, 3)
# eulerint_ghost_component1_1, eulerint_ghost_component1_2, eulerint_ghost_component1_3 = \
# euler_lookup.get_eulint_from_guides(ghost_frag1_guide_coords_rot_and_set.reshape((-1, 3, 3)))
eulerint_ghost_component1 = \
euler_lookup.get_eulint_from_guides_as_array(ghost_frag1_guide_coords_rot_and_set.reshape((-1, 3, 3)))
# Next, for component 2
surf_frags2_guide_coords_rot_and_set = \
transform_coordinate_sets(init_surf_guide_coords2[None, :, :, :],
rotation=rotation_matrices2[:, None, :, :],
rotation2=set_mat2[None, None, :, :])
# Reshape with the first axis (0) containing all the guide coordinate rotations stacked
eulerint_surf_component2 = \
euler_lookup.get_eulint_from_guides_as_array(surf_frags2_guide_coords_rot_and_set.reshape((-1, 3, 3)))
if forward_reverse:
surf_frag1_guide_coords_rot_and_set = \
transform_coordinate_sets(init_surf_guide_coords1[None, :, :, :],
rotation=rotation_matrices1[:, None, :, :],
rotation2=set_mat1[None, None, :, :])
ghost_frags2_guide_coords_rot_and_set = \
transform_coordinate_sets(init_ghost_guide_coords2[None, :, :, :],
rotation=rotation_matrices2[:, None, :, :],
rotation2=set_mat2[None, None, :, :])
eulerint_surf_component1 = \
euler_lookup.get_eulint_from_guides_as_array(surf_frag1_guide_coords_rot_and_set.reshape((-1, 3, 3)))
eulerint_ghost_component2 = \
euler_lookup.get_eulint_from_guides_as_array(ghost_frags2_guide_coords_rot_and_set.reshape((-1, 3, 3)))
stacked_surf_euler_int1 = eulerint_surf_component1.reshape((rotation_matrices_len1, -1, 3))
stacked_ghost_euler_int2 = eulerint_ghost_component2.reshape((rotation_matrices_len2, -1, 3))
# Improve indexing time by precomputing python objects
stacked_ghost_euler_int2 = list(stacked_ghost_euler_int2)
stacked_surf_euler_int1 = list(stacked_surf_euler_int1)
# eulerint_surf_component2_1, eulerint_surf_component2_2, eulerint_surf_component2_3 = \
# euler_lookup.get_eulint_from_guides(surf_frags2_guide_coords_rot_and_set.reshape((-1, 3, 3)))
# Reshape the reduced dimensional eulerint_components to again have the number_of_rotations length on axis 0,
# the number of init_guide_coords on axis 1, and the 3 euler intergers on axis 2
stacked_surf_euler_int2 = eulerint_surf_component2.reshape((rotation_matrices_len2, -1, 3))
stacked_ghost_euler_int1 = eulerint_ghost_component1.reshape((rotation_matrices_len1, -1, 3))
# Improve indexing time by precomputing python objects
stacked_surf_euler_int2 = list(stacked_surf_euler_int2)
stacked_ghost_euler_int1 = list(stacked_ghost_euler_int1)
# stacked_surf_euler_int2_1 = eulerint_surf_component2_1.reshape((rotation_matrices_len2, -1))
# stacked_surf_euler_int2_2 = eulerint_surf_component2_2.reshape((rotation_matrices_len2, -1))
# stacked_surf_euler_int2_3 = eulerint_surf_component2_3.reshape((rotation_matrices_len2, -1))
# stacked_ghost_euler_int1_1 = eulerint_ghost_component1_1.reshape((rotation_matrices_len1, -1))
# stacked_ghost_euler_int1_2 = eulerint_ghost_component1_2.reshape((rotation_matrices_len1, -1))
# stacked_ghost_euler_int1_3 = eulerint_ghost_component1_3.reshape((rotation_matrices_len1, -1))
# The fragments being added to the pose are different than the fragments generated on the pose. This function
# helped me elucidate that this was occurring
# def check_offset_index(title):
# if pose.entities[-1].offset_index == 0:
# raise RuntimeError('The offset_index has changed to 0')
# else:
# print(f'{title} offset_index: {pose.entities[-1].offset_index}')
if job.dock.quick: # job.development:
rotations_to_perform1 = min(len(rotation_matrices1), 13)
rotations_to_perform2 = min(len(rotation_matrices2), 12)
logger.critical(f'Development: Only sampling {rotations_to_perform1} by {rotations_to_perform2} rotations')
else:
rotations_to_perform1 = len(rotation_matrices1)
rotations_to_perform2 = len(rotation_matrices2)
# Todo 2 multiprocessing
def initial_euler_search():
pass
if job.multi_processing:
raise NotImplementedError(
f"Can't perform {fragment_dock.__name__} using {flags.multi_processing.long} yet")
rotation_pairs = None
results = utils.mp_map(initial_euler_search, rotation_pairs, processes=job.cores)
else:
pass
# results = []
# for rot_pair in rotation_pairs:
# results.append(initial_euler_search(rot_pair))
# Todo 3 resolve which mechanisms to use. guide coords or eulerints
# Below uses eulerints which work just fine.
# Timings on these from improved protocols shows about similar times to euler_lookup and calculate_overlap
# even with vastly different scales of the arrays. This ignores the fact that calculate_overlap uses a
# number of indexing steps including making the ij_match array formation, indexing against the ghost and
# surface arrays, the rmsd_reference construction
# |
# Given the lookups sort of irrelevance to the scoring (given very poor alignment), I could remove that
# step if it interfered with differentiability
# |
# Majority of time is spent indexing the 6D euler overlap arrays which should be quite easy to speed up given
# understanding of different computational efficiencies at this check
logger.info('Querying building blocks for initial fragment overlap')
# Get rotated component1 ghost fragment, component2 surface fragment guide coodinate pairs in the same Euler space
perturb_dof = job.dock.perturb_dof
total_dof_combinations = rotations_to_perform1 * rotations_to_perform2
progress_iter = iter(tqdm(range(total_dof_combinations), bar_format=TQDM_BAR_FORMAT))
for idx1 in range(rotations_to_perform1):
rot1_count = idx1%number_of_rotations1 + 1
degen1_count = idx1//number_of_rotations1 + 1
rot_mat1 = rotation_matrices1[idx1]
rotation_ghost_euler_ints1 = stacked_ghost_euler_int1[idx1]
if forward_reverse:
rotation_surf_euler_ints1 = stacked_surf_euler_int1[idx1]
for idx2 in range(rotations_to_perform2):
next(progress_iter) # Updates progress bar
# Rotate component2 surface and ghost fragment guide coordinates using rot_mat2 and set_mat2
rot2_count = idx2%number_of_rotations2 + 1
degen2_count = idx2//number_of_rotations2 + 1
rot_mat2 = rotation_matrices2[idx2]
logger.debug(f'***** OLIGOMER 1: Degeneracy {degen1_count} Rotation {rot1_count} | '
f'OLIGOMER 2: Degeneracy {degen2_count} Rotation {rot2_count} *****')
euler_start = time.time()
# euler_matched_surf_indices2, euler_matched_ghost_indices1 = \
# euler_lookup.lookup_by_euler_integers(stacked_surf_euler_int2_1[idx2],
# stacked_surf_euler_int2_2[idx2],
# stacked_surf_euler_int2_3[idx2],
# stacked_ghost_euler_int1_1[idx1],
# stacked_ghost_euler_int1_2[idx1],
# stacked_ghost_euler_int1_3[idx1],
# )
euler_matched_surf_indices2, euler_matched_ghost_indices1 = \
euler_lookup.lookup_by_euler_integers_as_array(stacked_surf_euler_int2[idx2],
rotation_ghost_euler_ints1)
# # euler_lookup.lookup_by_euler_integers_as_array(eulerint_ghost_component2.reshape(number_of_rotations2,
# # 1, 3),
# # eulerint_surf_component1.reshape(number_of_rotations1,
# # 1, 3))
if forward_reverse:
euler_matched_ghost_indices_rev2, euler_matched_surf_indices_rev1 = \
euler_lookup.lookup_by_euler_integers_as_array(stacked_ghost_euler_int2[idx2],
rotation_surf_euler_ints1)
# Todo 3 resolve. eulerints
# Todo 3 resolve. Below uses guide coords
# # for idx1 in range(rotation_matrices):
# # Iterating over more than 2 rotation matrix sets becomes hard to program dynamically owing to the permutations
# # of the rotations and the application of the rotation/setting to each set of fragment information. It would be a
# # bit easier if the same logic that is applied to the following routines, (similarity matrix calculation) putting
# # the rotation of the second set of fragment information into the setting of the first by applying the inverse
# # rotation and setting matrices to the second (or third...) set of fragments. Forget about this for now
# init_time_start = time.time()
# for idx1 in range(len(rotation_matrices1)): # min(len(rotation_matrices1), 5)): # Todo remove min
# # Rotate Oligomer1 Surface and Ghost Fragment Guide Coordinates using rot_mat1 and set_mat1
# rot1_count = idx1 % number_of_rotations1 + 1
# degen1_count = idx1 // number_of_rotations1 + 1
# rot_mat1 = rotation_matrices1[idx1]
# ghost_guide_coords_rot_and_set1 = \
# transform_coordinate_sets(init_ghost_guide_coords1, rotation=rot_mat1, rotation2=set_mat1)
# # surf_guide_coords_rot_and_set1 = \
# # transform_coordinate_sets(init_surf_guide_coords1, rotation=rot_mat1, rotation2=set_mat1)
#
# for idx2 in range(len(rotation_matrices2)): # min(len(rotation_matrices2), 5)): # Todo remove min
# # Rotate Oligomer2 Surface and Ghost Fragment Guide Coordinates using rot_mat2 and set_mat2
# rot2_count = idx2 % number_of_rotations2 + 1
# degen2_count = idx2 // number_of_rotations2 + 1
# rot_mat2 = rotation_matrices2[idx2]
# surf_guide_coords_rot_and_set2 = \
# transform_coordinate_sets(init_surf_guide_coords2, rotation=rot_mat2, rotation2=set_mat2)
# # ghost_guide_coords_rot_and_set2 = \
# # transform_coordinate_sets(init_ghost_guide_coords2, rotation=rot_mat2, rotation2=set_mat2)
#
# logger.info(f'***** OLIGOMER 1: Degeneracy {degen1_count} Rotation {rot1_count} | '
# f'OLIGOMER 2: Degeneracy {degen2_count} Rotation {rot2_count} *****')
#
# euler_start = time.time()
# # First returned variable has indices increasing 0,0,0,0,1,1,1,1,1,2,2,2,3,...
# # Second returned variable has indices increasing 2,3,4,14,...
# euler_matched_surf_indices2, euler_matched_ghost_indices1 = \
# euler_lookup.check_lookup_table(surf_guide_coords_rot_and_set2,
# ghost_guide_coords_rot_and_set1)
# # euler_matched_ghost_indices_rev2, euler_matched_surf_indices_rev1 = \
# # euler_lookup.check_lookup_table(ghost_guide_coords_rot_and_set2,
# # surf_guide_coords_rot_and_set1)
# Todo 3 resolve. guide coords
logger.debug(f'\tEuler search took {time.time() - euler_start:8f}s for '
f'{total_ghost_surf_combinations} ghost/surf pairs')
if forward_reverse:
# Ensure pairs are similar between euler_matched_surf_indices2 and euler_matched_ghost_indices_rev2
# by indexing the residue_numbers
# forward_reverse_comparison_start = time.time()
# logger.debug(f'Euler indices forward, index 0: {euler_matched_surf_indices2[:10]}')
forward_surface_indices2 = init_surf_residue_indices2[euler_matched_surf_indices2]
# logger.debug(f'Euler indices forward, index 1: {euler_matched_ghost_indices1[:10]}')
forward_ghosts_indices1 = init_ghost_residue_indices1[euler_matched_ghost_indices1]
# logger.debug(f'Euler indices reverse, index 0: {euler_matched_ghost_indices_rev2[:10]}')
reverse_ghosts_indices2 = init_ghost_residue_indices2[euler_matched_ghost_indices_rev2]
# logger.debug(f'Euler indices reverse, index 1: {euler_matched_surf_indices_rev1[:10]}')
reverse_surface_indices1 = init_surf_residue_indices1[euler_matched_surf_indices_rev1]
# Make an index indicating where the forward and reverse euler lookups have the same residue pairs
# Important! This method only pulls out initial fragment matches that go both ways, i.e. component1
# surface (type1) matches with component2 ghost (type1) and vice versa, so the expanded checks of
# for instance the surface loop (i type 3,4,5) with ghost helical (i type 1) matches is completely
# unnecessary during euler look up as this will never be included
# Also, this assumes that the ghost fragment display is symmetric, i.e. 1 (i) 1 (j) 10 (K) has an
# inverse transform at 1 (i) 1 (j) 230 (k) for instance
prior = 0
number_overlapping_pairs = len(euler_matched_ghost_indices1)
possible_overlaps = np.ones(number_overlapping_pairs, dtype=np.bool8)
# Residue numbers are in order for forward_surface_indices2 and reverse_ghosts_indices2
for residue_index in init_surf_residue_indices2:
# Where the residue number of component 2 is equal pull out the indices
forward_index = np.flatnonzero(forward_surface_indices2 == residue_index)
reverse_index = np.flatnonzero(reverse_ghosts_indices2 == residue_index)
# Next, use residue number indices to search for the same residue numbers in the extracted pairs
# The output array slice is only valid if the forward_index is the result of
# forward_surface_indices2 being in ascending order, which for check_lookup_table is True
current = prior + len(forward_index)
possible_overlaps[prior:current] = \
np.in1d(forward_ghosts_indices1[forward_index], reverse_surface_indices1[reverse_index])
prior = current
# # Use for residue number debugging
# possible_overlaps = np.ones(number_overlapping_pairs, dtype=np.bool8)
# forward_ghosts_indices1[possible_overlaps]
# forward_surface_indices2[possible_overlaps]
# indexing_possible_overlap_time = time.time() - indexing_possible_overlap_start
# number_of_successful = possible_overlaps.sum()
# logger.info(f'\tIndexing {number_overlapping_pairs * len(euler_matched_surf_indices2)} '
# f'possible overlap pairs found only {number_of_successful} possible out of '
# f'{number_overlapping_pairs} (took {time.time() - forward_reverse_comparison_start:8f}s)')
# passing_ghost_coords = ghost_guide_coords_rot_and_set1[possible_ghost_frag_indices]
# passing_surf_coords = surf_guide_coords_rot_and_set2[euler_matched_surf_indices2[possible_overlaps]]
# Get optimal shift parameters for initial (Ghost Fragment, Surface Fragment) guide coordinate pairs
# # Todo these are from Guides
# passing_ghost_coords = ghost_guide_coords_rot_and_set1[euler_matched_ghost_indices1]
# passing_surf_coords = surf_guide_coords_rot_and_set2[euler_matched_surf_indices2]
# # Todo these are from Guides
# Todo debug With EulerInteger calculation
if forward_reverse:
# Take the boolean index of the indices
possible_ghost_frag_indices = euler_matched_ghost_indices1[possible_overlaps]
# possible_surf_frag_indices = euler_matched_surf_indices2[possible_overlaps]
passing_ghost_coords = \
ghost_frag1_guide_coords_rot_and_set[idx1, possible_ghost_frag_indices]
passing_surf_coords = \
surf_frags2_guide_coords_rot_and_set[idx2, euler_matched_surf_indices2[possible_overlaps]]
reference_rmsds = init_ghost_rmsds1[possible_ghost_frag_indices]
else:
passing_ghost_coords = ghost_frag1_guide_coords_rot_and_set[idx1, euler_matched_ghost_indices1]
# passing_ghost_coords = transform_coordinate_sets(init_ghost_guide_coords1[euler_matched_ghost_indices1],
# rotation=rot_mat1, rotation2=set_mat1)
passing_surf_coords = surf_frags2_guide_coords_rot_and_set[idx2, euler_matched_surf_indices2]
# passing_surf_coords = transform_coordinate_sets(init_surf_guide_coords2[euler_matched_surf_indices2],
# rotation=rot_mat2, rotation2=set_mat2)
reference_rmsds = init_ghost_rmsds1[euler_matched_ghost_indices1]
# Todo debug With EulerInteger calculation
optimal_shifts_start = time.time()
transform_passing_shifts = \
optimal_tx.solve_optimal_shifts(passing_ghost_coords, passing_surf_coords, reference_rmsds)
optimal_shifts_time = time.time() - optimal_shifts_start
pre_cluster_passing_shifts = len(transform_passing_shifts)
if pre_cluster_passing_shifts == 0:
# logger.debug(f'optimal_shifts length: {len(optimal_shifts)}')
# logger.debug(f'transform_passing_shifts shape: {len(transform_passing_shifts)}')
logger.debug(f'\tNo transforms were found passing optimal shift criteria '
f'(took {optimal_shifts_time:8f}s)')
continue
elif cluster_translations:
cluster_time_start = time.time()
translation_cluster = \
DBSCAN(eps=translation_cluster_epsilon, min_samples=min_matched).fit(transform_passing_shifts)
if perturb_dof: # Later will be sampled more finely, so
# Get the core indices, i.e. the most dense translation regions only
transform_passing_shift_indexer = translation_cluster.core_sample_indices_
else: # Get any transform which isn't an outlier
transform_passing_shift_indexer = translation_cluster.labels_ != outlier
transform_passing_shifts = transform_passing_shifts[transform_passing_shift_indexer]
cluster_time = time.time() - cluster_time_start
logger.debug(f'Clustering {pre_cluster_passing_shifts} possible transforms (took {cluster_time:8f}s)')
# else: # Use all translations
# pass
if sym_entry.unit_cell:
# Must take the optimal_ext_dof_shifts and multiply the column number by the corresponding row
# in the sym_entry.external_dof#
# optimal_ext_dof_shifts[0] scalar * sym_entry.group_external_dof[0] (1 row, 3 columns)
# Repeat for additional DOFs, then add all up within each row.
# For a single DOF, multiplication won't matter as only one matrix element will be available
#
# Must find positive indices before external_dof1 multiplication in case negatives there
positive_indices = \
np.flatnonzero(np.all(transform_passing_shifts[:, :sym_entry.number_dof_external] >= 0, axis=1))
number_passing_shifts = len(positive_indices)
optimal_ext_dof_shifts = np.zeros((number_passing_shifts, 3), dtype=float)
optimal_ext_dof_shifts[:, :sym_entry.number_dof_external] = \
transform_passing_shifts[positive_indices, :sym_entry.number_dof_external]
# ^ I think for the sake of cleanliness, I need to make this matrix
full_optimal_ext_dof_shifts.append(optimal_ext_dof_shifts)
else:
number_passing_shifts = len(transform_passing_shifts)
# logger.debug(f'\tFound {number_passing_shifts} transforms after clustering from '
# f'{pre_cluster_passing_shifts} possible transforms (took '
# f'{time.time() - cluster_time_start:8f}s)')
# Prepare the transformation parameters for storage in full transformation arrays
# Use of [:, None] transforms the array into an array with each internal dof sored as a scalar in
# axis 1 and each successive index along axis 0 as each passing shift
# Stack each internal parameter along with a blank vector, this isolates the tx vector along z axis
if full_int_tx1 is not None:
# Store transformation parameters, indexing only those that are positive in the case of lattice syms
full_int_tx1.extend(
transform_passing_shifts[positive_indices, sym_entry.number_dof_external].tolist())
if full_int_tx2 is not None:
# Store transformation parameters, indexing only those that are positive in the case of lattice syms
full_int_tx2.extend(
transform_passing_shifts[positive_indices, sym_entry.number_dof_external + 1].tolist())
full_rotation1.append(np.tile(rot_mat1, (number_passing_shifts, 1, 1)))
full_rotation2.append(np.tile(rot_mat2, (number_passing_shifts, 1, 1)))
logger.debug(f'\tOptimal shift search took {optimal_shifts_time:8f}s for '
f'{len(euler_matched_ghost_indices1)} guide coordinate pairs')
logger.debug(f'\t{number_passing_shifts if number_passing_shifts else "No"} initial interface '
f'match{"es" if number_passing_shifts != 1 else ""} found '
f'(took {time.time() - euler_start:8f}s)')
try:
next(progress_iter) # Updates progress bar
except StopIteration:
pass
# -----------------------------------------------------------------------------------------------------------------
# Below creates vectors for cluster transformations
# Then asu clash testing, scoring, and symmetric clash testing are performed
# -----------------------------------------------------------------------------------------------------------------
if sym_entry.unit_cell:
# optimal_ext_dof_shifts[:, :, None] <- None expands the axis to make multiplication accurate
full_optimal_ext_dof_shifts = np.concatenate(full_optimal_ext_dof_shifts, axis=0)
unsqueezed_optimal_ext_dof_shifts = full_optimal_ext_dof_shifts[:, :, None]
external_dof1, external_dof2, *_ = sym_entry.external_dofs
full_ext_tx1 = np.sum(unsqueezed_optimal_ext_dof_shifts * external_dof1, axis=-2)
full_ext_tx2 = np.sum(unsqueezed_optimal_ext_dof_shifts * external_dof2, axis=-2)
full_ext_tx_sum = full_ext_tx2 - full_ext_tx1
del unsqueezed_optimal_ext_dof_shifts
else:
full_ext_tx1 = full_ext_tx2 = full_ext_tx_sum = full_optimal_ext_dof_shifts = None
# full_optimal_ext_dof_shifts = list(repeat(None, number_passing_shifts))
if not full_rotation1: # There were no successful transforms
logger.warning(f'No optimal translations found. Terminating {building_blocks} docking')
return []
# ------------------ TERMINATE DOCKING ------------------------
# Make full, numpy vectorized transformations overwriting individual variables for memory management
full_rotation1 = np.concatenate(full_rotation1, axis=0)
full_rotation2 = np.concatenate(full_rotation2, axis=0)
starting_transforms = len(full_rotation1)
logger.info(f'Initial optimal translation search found {starting_transforms} total transforms '
f'in {time.time() - init_translation_time_start:8f}s')
if sym_entry.is_internal_tx1:
stacked_internal_tx_vectors1 = np.zeros((starting_transforms, 3), dtype=float)
# Add the translation to Z (axis=1)
stacked_internal_tx_vectors1[:, -1] = full_int_tx1
full_int_tx1 = stacked_internal_tx_vectors1
del stacked_internal_tx_vectors1
if sym_entry.is_internal_tx2:
stacked_internal_tx_vectors2 = np.zeros((starting_transforms, 3), dtype=float)
# Add the translation to Z (axis=1)
stacked_internal_tx_vectors2[:, -1] = full_int_tx2
full_int_tx2 = stacked_internal_tx_vectors2
del stacked_internal_tx_vectors2
# Make inverted transformations
inv_setting1 = np.linalg.inv(set_mat1)
full_inv_rotation1 = np.linalg.inv(full_rotation1)
_full_rotation2 = full_rotation2.copy()
if sym_entry.is_internal_tx1:
# Invert by multiplying by -1
full_int_tx_inv1 = full_int_tx1 * -1
else:
full_int_tx_inv1 = None
if sym_entry.is_internal_tx2:
_full_int_tx2 = full_int_tx2.copy()
else:
_full_int_tx2 = None
# Define functions for making active transformation arrays and removing indices from them
def create_transformation_group() -> tuple[dict[str, np.ndarray | None], dict[str, np.ndarray | None]]:
"""Create the transformation mapping for each transformation in the current docking trajectory
Returns:
Every stacked transformation operation for the two separate models being docked in two separate dictionaries
"""
return (
dict(rotation=full_rotation1, translation=None if full_int_tx1 is None else full_int_tx1[:, None, :],
rotation2=set_mat1, translation2=None if full_ext_tx1 is None else full_ext_tx1[:, None, :]),
dict(rotation=full_rotation2, translation=None if full_int_tx2 is None else full_int_tx2[:, None, :],
rotation2=set_mat2, translation2=None if full_ext_tx2 is None else full_ext_tx2[:, None, :])
)
def remove_non_viable_indices_inverse(passing_indices: np.ndarray | list[int]):
"""Responsible for updating docking intermediate transformation parameters for inverse transform operations
These include: full_inv_rotation1, _full_rotation2, full_int_tx_inv1, _full_int_tx2, and full_ext_tx_sum
"""
nonlocal full_inv_rotation1, _full_rotation2, full_int_tx_inv1, _full_int_tx2, full_ext_tx_sum
full_inv_rotation1 = full_inv_rotation1[passing_indices]
_full_rotation2 = _full_rotation2[passing_indices]
if sym_entry.is_internal_tx1:
full_int_tx_inv1 = full_int_tx_inv1[passing_indices]
if sym_entry.is_internal_tx2:
_full_int_tx2 = _full_int_tx2[passing_indices]
if sym_entry.unit_cell:
full_ext_tx_sum = full_ext_tx_sum[passing_indices]
def filter_transforms_by_indices(passing_indices: np.ndarray | list[int]):
"""Responsible for updating docking transformation parameters for transform operations. Will set the
transformation in the order of the passing_indices
Includes:
full_rotation1, full_rotation2, full_int_tx1, full_int_tx2, full_optimal_ext_dof_shifts, full_ext_tx1, and
full_ext_tx2
Args:
passing_indices: The indices which should be kept
"""
nonlocal full_rotation1, full_rotation2, full_int_tx1, full_int_tx2
full_rotation1 = full_rotation1[passing_indices]
full_rotation2 = full_rotation2[passing_indices]
if sym_entry.is_internal_tx1:
full_int_tx1 = full_int_tx1[passing_indices]
if sym_entry.is_internal_tx2:
full_int_tx2 = full_int_tx2[passing_indices]
if sym_entry.unit_cell:
nonlocal full_optimal_ext_dof_shifts, full_ext_tx1, full_ext_tx2
full_optimal_ext_dof_shifts = full_optimal_ext_dof_shifts[passing_indices]
full_ext_tx1 = full_ext_tx1[passing_indices]
full_ext_tx2 = full_ext_tx2[passing_indices]
# Find the clustered transformations to expedite search of ASU clashing
if cluster_transforms:
clustering_start = time.time()
# Todo 3
# Can I use cluster.cluster_transformation_pairs distance graph to provide feedback on other aspects of the
# dock? Seems that I could use the distances to expedite clashing checks, especially for more time consuming
# expansion checks such as the full material...
# Must add a new axis to translations so the operations are broadcast together in transform_coordinate_sets()
transform_neighbor_tree, transform_cluster = \
cluster.cluster_transformation_pairs(*create_transformation_group(),
distance=transformation_cluster_epsilon,
minimum_members=min_matched
)
# cluster_representative_indices, cluster_labels = \
# find_cluster_representatives(transform_neighbor_tree, transform_cluster)
# representative_labels = cluster_labels[cluster_representative_indices]
# Todo 3
# _, cluster_labels = find_cluster_representatives(transform_neighbor_tree, transform_cluster)
# cluster_labels = transform_cluster.labels_
# logger.debug(f'Shape of cluster_labels: {cluster_labels.shape}')
# passing_transforms = cluster_labels != -1
sufficiently_dense_indices = np.flatnonzero(transform_cluster.labels_ != -1)
number_of_dense_transforms = len(sufficiently_dense_indices)
logger.info(f'After clustering, {starting_transforms - number_of_dense_transforms} are missing the minimum '
f'number of close transforms to be viable. {number_of_dense_transforms} transforms '
f'remain (took {time.time() - clustering_start:8f}s)')
if not number_of_dense_transforms: # There were no successful transforms
logger.warning(f'No viable transformations found. Terminating {building_blocks} docking')
return []
# ------------------ TERMINATE DOCKING ------------------------
# Update the transformation array and counts with the sufficiently_dense_indices
# Remove non-viable transforms by indexing sufficiently_dense_indices
remove_non_viable_indices_inverse(sufficiently_dense_indices)
del transform_neighbor_tree, transform_cluster
else:
sufficiently_dense_indices = np.arange(starting_transforms)
number_of_dense_transforms = starting_transforms
# if job.design.ignore_pose_clashes:
# logger.warning(f'Not checking for pose clashes per requested flag '
# f'{flags.format_args(flags.ignore_pose_clashes_args)}')
# else:
# Transform coords to query for clashes
# Set up chunks of coordinate transforms for clash testing
check_clash_coords_start = time.time()
calculation_size = number_of_dense_transforms
# Start with the assumption that all tested clashes are clashing
asu_clash_counts = np.ones(calculation_size)
batch_length = get_check_tree_for_query_overlap_batch_length(bb_cb_coords2)
batch_length = min((batch_length, calculation_size))
# Setup function is performed before the function is executed
def np_tile_wrap(length: int, coords: np.ndarray, *args, **kwargs):
return dict(query_points=np.tile(coords, (length, 1, 1)))
# Todo this is used in perturb_transformations with different 'size' and 'batch_length' that are going to snag
# at some point given the size of the perturb could be larger than the batch_length
# Create the balltree clash check as a batched function
@resources.ml.batch_calculation(size=calculation_size, batch_length=batch_length, setup=np_tile_wrap,
compute_failure_exceptions=(np.core._exceptions._ArrayMemoryError,))
def init_check_tree_for_query_overlap(*args, **kwargs):
return check_tree_for_query_overlap(*args, **kwargs)
logger.info(f'Testing found transforms for ASU clashes')
# Using the inverse transform of the model2 backbone and cb (surface fragment) coordinates, check for clashes
# with the model1 backbone and cb coordinates BinaryTree
ball_tree_kwargs = dict(binarytree=component1_backbone_cb_tree, clash_distance=clash_dist,
rotation=_full_rotation2, translation=_full_int_tx2,
rotation2=set_mat2, translation2=full_ext_tx_sum,
rotation3=inv_setting1, translation3=full_int_tx_inv1,
rotation4=full_inv_rotation1)
overlap_return = init_check_tree_for_query_overlap(
**ball_tree_kwargs, return_containers={'overlap_counts': asu_clash_counts}, setup_args=(bb_cb_coords2,))
# Extract the data
asu_clash_counts = overlap_return['overlap_counts']
# Find those indices where the asu_clash_counts is not zero (inverse of nonzero by using the array == 0)
asu_is_viable_indices = np.flatnonzero(asu_clash_counts == 0)
number_non_clashing_transforms = len(asu_is_viable_indices)
# Update the passing_transforms
# passing_transforms contains all the transformations that are still passing
# index the previously passing indices (sufficiently_dense_indices) by new pasing indices (asu_is_viable_indices)
# and set each of these indices to 1 (True)
# passing_transforms[sufficiently_dense_indices[asu_is_viable_indices]] = 1
logger.info(f"Clash testing for identified poses found {number_non_clashing_transforms} viable ASU's out of "
f'{number_of_dense_transforms} (took {time.time() - check_clash_coords_start:8f}s)')
if not number_non_clashing_transforms:
logger.warning(f'No viable asymmetric units. Terminating {building_blocks} docking')
return []
# ------------------ TERMINATE DOCKING ------------------------
# Clean memory
del asu_clash_counts, ball_tree_kwargs, overlap_return
# Remove non-viable transforms by indexing asu_is_viable_indices
remove_non_viable_indices_inverse(asu_is_viable_indices)
# Query PDB1 CB Tree for all PDB2 CB Atoms within "cb_distance" in A of a PDB1 CB Atom
# alternative route to measure clashes of each transform. Move copies of component2 to interact with model1 ORIGINAL
int_cb_and_frags_start = time.time()
# Transform the CB coords of component 2 to each identified transformation
# Transforming only surface frags has large speed benefits from not having to transform all ghosts
inverse_transformed_model2_tiled_cb_coords = \
transform_coordinate_sets(
transform_coordinate_sets(np.tile(model2.cb_coords, (number_non_clashing_transforms, 1, 1)),
rotation=_full_rotation2,
translation=None if full_int_tx2 is None else _full_int_tx2[:, None, :],
rotation2=set_mat2,
translation2=None if sym_entry.unit_cell is None
else full_ext_tx_sum[:, None, :]),
rotation=inv_setting1,
translation=None if full_int_tx1 is None else full_int_tx_inv1[:, None, :],
rotation2=full_inv_rotation1)
# Transform the surface guide coords of component 2 to each identified transformation
# Makes a shape (len(full_rotations), len(surf_guide_coords), 3, 3)
inverse_transformed_surf_frags2_guide_coords = \
transform_coordinate_sets(
transform_coordinate_sets(surf_guide_coords2[None, :, :, :],
rotation=_full_rotation2[:, None, :, :],
translation=None if full_int_tx2 is None else _full_int_tx2[:, None, None, :],
rotation2=set_mat2[None, None, :, :],
translation2=None if sym_entry.unit_cell is None
else full_ext_tx_sum[:, None, None, :]),
rotation=inv_setting1[None, None, :, :],
translation=None if full_int_tx1 is None else full_int_tx_inv1[:, None, None, :],
rotation2=full_inv_rotation1[:, None, :, :])
logger.info('Transformation of viable component 2 CB atoms and surface fragments '
f'(took {time.time() - int_cb_and_frags_start:8f}s)')
del full_inv_rotation1, _full_rotation2, full_int_tx_inv1, _full_int_tx2, full_ext_tx_sum
del surf_guide_coords2
# Use below instead of this until can Todo 3 vectorize asu_interface_residue_processing
# asu_interface_residues = \
# np.array([component1_backbone_cb_tree.query_radius(inverse_transformed_model2_tiled_cb_coords[idx],
# cb_distance)
# for idx in range(len(inverse_transformed_model2_tiled_cb_coords))])
# Full Interface Fragment Match
# Gather the data for efficient querying of model1 and model2 interactions
model1_cb_balltree = BallTree(model1.cb_coords)
model1_cb_indices = model1.cb_indices
model1_coords_indexed_residues = model1.coords_indexed_residues
model2_cb_indices = model2.cb_indices
model2_coords_indexed_residues = model2.coords_indexed_residues
zero_counts = []
# Save all the indices were matching fragments are identified
interface_is_viable = []
# all_passing_ghost_indices = []
# all_passing_surf_indices = []
# all_passing_z_scores = []
# Get residue number for all model1, model2 CB Pairs that interact within cb_distance
for idx in tqdm(range(number_non_clashing_transforms), bar_format=TQDM_BAR_FORMAT):
# query/contact pairs/isin - 0.028367 <- I predict query is about 0.015
# indexing guide_coords - 0.000389
# total get_int_frags_time - 0.028756 s
# indexing guide_coords - 0.000389
# Euler Lookup - 0.008161 s for 71435 fragment pairs
# Overlap Score Calculation - 0.000365 s for 2949 fragment pairs
# Total Match time - 0.008915 s
# query - 0.000895 s <- 100 fold shorter than predict
# contact pairs - 0.019595
# isin indexing - 0.008992 s
# indexing guide_coords - 0.000438
# get_int_frags_time - 0.029920 s
# indexing guide_coords - 0.000438
# Euler Lookup - 0.005603 s for 35400 fragment pairs
# Overlap Score Calculation - 0.000209 s for 887 fragment pairs
# Total Match time - 0.006250 s
# int_frags_time_start = time.time()
model2_query = model1_cb_balltree.query_radius(inverse_transformed_model2_tiled_cb_coords[idx], cb_distance)
# model1_cb_balltree_time = time.time() - int_frags_time_start
contacting_residue_idx_pairs = [(model1_coords_indexed_residues[model1_cb_indices[model1_idx]].index,
model2_coords_indexed_residues[model2_cb_indices[model2_idx]].index)
for model2_idx, model1_contacts in enumerate(model2_query.tolist())
for model1_idx in model1_contacts.tolist()]
try:
interface_residue_indices1, interface_residue_indices2 = \
map(list, map(set, zip(*contacting_residue_idx_pairs)))
except ValueError: # Interface contains no residues, so not enough values to unpack
logger.warning('Interface contains no residues')
continue
# Find the indices where the fragment residue numbers are found the interface residue numbers
# is_in_index_start = time.time()
# Since *_residue_numbers1/2 are the same index as the complete fragment arrays, these interface indices are the
# same index as the complete guide coords and rmsds as well
# Both residue numbers are one-indexed vv
# Todo 3 make ghost_residue_indices1 unique -> unique_ghost_residue_numbers1
# index selected numbers against per_residue_ghost_indices 2d (number surface frag residues,
ghost_indices_in_interface1 = \
np.flatnonzero(np.isin(ghost_residue_indices1, interface_residue_indices1))
surf_indices_in_interface2 = \
np.flatnonzero(np.isin(surf_residue_indices2, interface_residue_indices2, assume_unique=True))
# is_in_index_time = time.time() - is_in_index_start
all_fragment_match_time_start = time.time()
# unique_interface_frag_count_model1, unique_interface_frag_count_model2 = \
# len(ghost_indices_in_interface1), len(surf_indices_in_interface2)
# get_int_frags_time = time.time() - int_frags_time_start
# logger.debug(f'\tNewly formed interface contains {unique_interface_frag_count_model1} unique Fragments on '
# f'Oligomer 1 from {len(interface_residue_numbers1)} Residues and '
# f'{unique_interface_frag_count_model2} on Oligomer 2 from {len(interface_residue_numbers2)} '
# f'Residues\n\t(took {get_int_frags_time:8f}s to get interface fragments, including '
# f'{model1_cb_balltree_time:8f}s to query distances, '
# f'{is_in_index_time:8f}s to index residue numbers)')
number_int_surf = len(surf_indices_in_interface2)
number_int_ghost = len(ghost_indices_in_interface1)
# maximum_number_of_pairs = number_int_ghost*number_int_surf
# if maximum_number_of_pairs < euler_lookup_size_threshold:
# Tod0 at one point, there might have been a memory leak by Pose objects sharing memory with persistent objects
# that prevent garbage collection and stay attached to the run
# Skipping EulerLookup as it has issues with precision
index_ij_pairs_start_time = time.time()
ghost_indices_repeated = np.repeat(ghost_indices_in_interface1, number_int_surf)
surf_indices_tiled = np.tile(surf_indices_in_interface2, number_int_ghost)
ij_type_match = ij_type_match_lookup_table[ghost_indices_repeated, surf_indices_tiled]
# DEBUG: If ij_type_match needs to be removed for testing
# ij_type_match = np.array([True for _ in range(len(ij_type_match))])
# Surface selecting
# [0, 1, 3, 5, ...] with fancy indexing [0, 1, 5, 10, 12, 13, 34, ...]
# possible_fragments_pairs = len(ghost_indices_repeated)
passing_ghost_indices = ghost_indices_repeated[ij_type_match]
passing_surf_indices = surf_indices_tiled[ij_type_match]
# else: # Narrow candidates by EulerLookup
# Get (Oligomer1 Interface Ghost Fragment, Oligomer2 Interface Surface Fragment) guide coordinate pairs
# in the same Euler rotational space bucket
# DON'T think this is crucial! ###
# int_euler_matching_ghost_indices1, int_euler_matching_surf_indices2 = \
# euler_lookup.check_lookup_table(int_trans_ghost_guide_coords, int_trans_surf_guide_coords2)
# logger.debug('Euler lookup')
# logger.warning(f'The interface size is too large ({maximum_number_of_pairs} maximum pairs). '
# f'Trimming possible fragments by EulerLookup')
# eul_lookup_start_time = time.time()
# int_ghost_guide_coords1 = ghost_guide_coords1[ghost_indices_in_interface1]
# int_trans_surf_guide_coords2 = inverse_transformed_surf_frags2_guide_coords[idx, surf_indices_in_interface2]
# # Todo Debug skipping EulerLookup to see if issues with precision
# int_euler_matching_ghost_indices1, int_euler_matching_surf_indices2 = \
# euler_lookup.check_lookup_table(int_ghost_guide_coords1, int_trans_surf_guide_coords2)
# # logger.debug(f'int_euler_matching_ghost_indices1: {int_euler_matching_ghost_indices1[:5]}')
# # logger.debug(f'int_euler_matching_surf_indices2: {int_euler_matching_surf_indices2[:5]}')
# eul_lookup_time = time.time() - eul_lookup_start_time
#
# # Find the ij_type_match which is the same length as the int_euler_matching indices
# # this has data type bool so indexing selects al original
# index_ij_pairs_start_time = time.time()
# ij_type_match = \
# ij_type_match_lookup_table[
# ghost_indices_in_interface1[int_euler_matching_ghost_indices1],
# surf_indices_in_interface2[int_euler_matching_surf_indices2]]
# possible_fragments_pairs = len(int_euler_matching_ghost_indices1)
#
# # Get only euler matching fragment indices that pass ij filter. Then index their associated coords
# passing_ghost_indices = int_euler_matching_ghost_indices1[ij_type_match]
# # passing_ghost_coords = int_trans_ghost_guide_coords[passing_ghost_indices]
# # passing_ghost_coords = int_ghost_guide_coords1[passing_ghost_indices]
# passing_surf_indices = int_euler_matching_surf_indices2[ij_type_match]
# # passing_surf_coords = int_trans_surf_guide_coords2[passing_surf_indices]
# DON'T think this is crucial! ###
# Calculate z_value for the selected (Ghost Fragment, Interface Fragment) guide coordinate pairs
# Calculate match score for the selected (Ghost Fragment, Interface Fragment) guide coordinate pairs
overlap_score_time_start = time.time()
all_fragment_z_score = rmsd_z_score(ghost_guide_coords1[passing_ghost_indices],
inverse_transformed_surf_frags2_guide_coords[idx, passing_surf_indices],
ghost_rmsds1[passing_ghost_indices])
# all_fragment_match = calculate_match(ghost_guide_coords1[passing_ghost_indices],
# inverse_transformed_surf_frags2_guide_coords[idx, passing_surf_indices],
# ghost_rmsds1[passing_ghost_indices])
# number_of_checks = len(passing_ghost_indices)
logger.debug(
# f'\tEuler Lookup found {len(int_euler_matching_ghost_indices1)} passing overlaps '
# f'(took {eul_lookup_time:8f}s) for '
# f'{unique_interface_frag_count_model1 * unique_interface_frag_count_model2} fragment pairs and '
f'\tZ-score calculation took {time.time() - overlap_score_time_start:8f}s for '
f'{len(passing_ghost_indices)} successful ij type matches (indexing time '
f'{overlap_score_time_start - index_ij_pairs_start_time:8f}s) from '
f'{len(ghost_indices_repeated)} possible fragment pairs')
# Check if the pose has enough high quality fragment matches
# high_qual_match_indices = np.flatnonzero(all_fragment_match >= high_quality_match_value)
# high_qual_match_indices = np.flatnonzero(all_fragment_z_score <= high_quality_z_value)
# Using ar.size() - np.count_nonzero(ar) to count
# high_qual_match_count = number_of_checks - np.count_nonzero(all_fragment_z_score <= high_quality_z_value)
high_qual_match_count = np.count_nonzero(all_fragment_z_score <= high_quality_z_value)
all_fragment_match_time = time.time() - all_fragment_match_time_start
if high_qual_match_count < min_matched:
logger.debug(f'\t{high_qual_match_count} < {min_matched}, the minimal high quality fragment matches '
f'(took {all_fragment_match_time:8f}s)')
# Debug. Why are there no matches... cb_distance?
# I think it is the accuracy of binned euler_angle lookup
if high_qual_match_count == 0:
zero_counts.append(1)
continue
else:
# Find the passing overlaps to limit the output to only those passing the low_quality_match_value
# passing_overlaps_indices = np.flatnonzero(all_fragment_match >= low_quality_match_value)
# passing_overlaps_indices = np.flatnonzero(all_fragment_z_score <= low_quality_z_value)
# number_passing_overlaps = len(passing_overlaps_indices)
# number_passing_overlaps = number_of_checks - np.count_nonzero(all_fragment_z_score <= low_quality_z_value)
number_passing_overlaps = np.count_nonzero(all_fragment_z_score <= low_quality_z_value)
logger.debug(f'\t{high_qual_match_count} high quality fragments out of {number_passing_overlaps} matches '
f'found (took {all_fragment_match_time:8f}s)')
# Return the indices sorted by z_value in ascending order, truncated at the number of passing
# sorted_fragment_indices = np.argsort(all_fragment_z_score)[:number_passing_overlaps]
# sorted_match_scores = match_score_from_z_value(sorted_z_values)
# logger.debug('Overlapping Match Scores: %s' % sorted_match_scores)
# sorted_overlap_indices = passing_overlaps_indices[sorted_fragment_indices]
# interface_ghost_frags = \
# complete_ghost_frags1[interface_ghost_indices1][passing_ghost_indices[sorted_overlap_indices]]
# interface_surf_frags = \
# surf_frags2[surf_indices_in_interface2][passing_surf_indices[sorted_overlap_indices]]
# overlap_passing_ghosts = passing_ghost_indices[sorted_fragment_indices]
# all_passing_ghost_indices.append(passing_ghost_indices[sorted_fragment_indices])
# all_passing_surf_indices.append(passing_surf_indices[sorted_fragment_indices])
# all_passing_z_scores.append(all_fragment_z_score[sorted_fragment_indices])
interface_is_viable.append(idx)
# logger.debug(f'\tInterface fragment search time took {time.time() - int_frags_time_start:8f}')
continue
logger.debug(f'Found {len(zero_counts)} zero counts')
number_viable_pose_interfaces = len(interface_is_viable)
if number_viable_pose_interfaces == 0:
logger.warning(f'No interfaces have enough fragment matches. Terminating {building_blocks} docking')
return []
# ------------------ TERMINATE DOCKING ------------------------
# Clean memory
del inverse_transformed_model2_tiled_cb_coords, inverse_transformed_surf_frags2_guide_coords
del ghost_residue_indices1, surf_residue_indices2
del ghost_guide_coords1, ghost_rmsds1
del model1, model1_cb_indices, model1_coords_indexed_residues
del model2, model2_cb_indices, model2_coords_indexed_residues
del model1_cb_balltree, model2_query, contacting_residue_idx_pairs
del interface_residue_indices1, interface_residue_indices2
del ghost_indices_in_interface1, surf_indices_in_interface2
del ij_type_match, ghost_indices_repeated, surf_indices_tiled
del all_fragment_z_score, zero_counts
# del all_fragment_z_score, zero_counts, passing_overlaps_indices
logger.info(f'Found {number_viable_pose_interfaces} poses with viable interfaces')
# # Tod0 remove below. It is never accessed by pose. Perhaps downstream it could be but Entity instances are
# # already solved now with sql.ProteinMetadata
# # Generate the Pose for output handling
# entity_info = {entity_name: data for model in models
# for entity_name, data in model.entity_info.items()}
# chain_gen = chain_id_generator()
# for entity_name, data in entity_info.items():
# data['chains'] = [next(chain_gen)]
if sym_entry.unit_cell:
# Calculate the vectorized uc_dimensions
full_uc_dimensions = sym_entry.get_uc_dimensions(full_optimal_ext_dof_shifts)
uc_dimensions = full_uc_dimensions[0]
else:
uc_dimensions = None
pose = Pose.from_entities([entity for model in input_models for entity in model.entities],
log=True if job.debug else None,
sym_entry=sym_entry, name='asu', fragment_db=job.fragment_db, # entity_info=entity_info,
surrounding_uc=True, rename_chains=True, uc_dimensions=uc_dimensions)
# Ensure .metadata attribute is passed to each entity in the full assembly
# This is crucial for sql usage
entity_idx = count()
for input_model in input_models:
for entity in input_model.entities:
pose.entities[next(entity_idx)].metadata = entity.metadata
# Set up coordinates to transform the Pose with each Entity individually
entity_start_coords = [entity.coords for input_model in input_models for entity in input_model.entities]
entity_idx = count()
transform_indices = {next(entity_idx): transform_idx
for transform_idx, input_model in enumerate(input_models)
for _ in input_model.entities}
# Calculate thermophilicity
is_thermophilic = [entity.thermophilicity for idx, entity in enumerate(pose.entities, idx)]
pose_thermophilicity = sum(is_thermophilic) / pose.number_of_entities
# Define functions for updating the single Pose instance coordinates
# def create_specific_transformation(idx: int) -> tuple[dict[str, np.ndarray], ...]:
# """Take the current transformation index and create a mapping of the transformation operations
#
# Args:
# idx: The index of the transformation to select
# Returns:
# A tuple containing the transformation operations for each model
# """
# if sym_entry.is_internal_tx1:
# internal_tx_param1 = full_int_tx1[idx]
# else:
# internal_tx_param1 = None
#
# if sym_entry.is_internal_tx2:
# internal_tx_param2 = full_int_tx2[idx]
# else:
# internal_tx_param2 = None
#
# if sym_entry.unit_cell:
# external_tx1 = full_ext_tx1[idx]
# external_tx2 = full_ext_tx2[idx]
# else:
# external_tx1 = external_tx2 = None
#
# specific_transformation1 = dict(rotation=full_rotation1[idx], translation=internal_tx_param1,
# rotation2=set_mat1, translation2=external_tx1)
# specific_transformation2 = dict(rotation=full_rotation2[idx], translation=internal_tx_param2,
# rotation2=set_mat2, translation2=external_tx2)
# return specific_transformation1, specific_transformation2
# Todo 3 if using individual Poses
# def clone_pose(idx: int) -> Pose:
# # Create a copy of the base Pose
# new_pose = copy.copy(pose)
# if sym_entry.unit_cell:
# # Set the next unit cell dimensions
# new_pose.uc_dimensions = full_uc_dimensions[idx]
# # Update the Pose coords
# new_pose.coords = np.concatenate(new_coords)
# return new_pose
def update_pose_coords(idx: int):
"""Take the current transformation index and update the reference coordinates with the provided transforms
Args:
idx: The index of the transformation to select
"""
copy_model_start = time.time()
if sym_entry.is_internal_tx1:
internal_tx_param1 = full_int_tx1[idx]
else:
internal_tx_param1 = None
if sym_entry.is_internal_tx2:
internal_tx_param2 = full_int_tx2[idx]
else:
internal_tx_param2 = None
if sym_entry.unit_cell:
external_tx1 = full_ext_tx1[idx]
external_tx2 = full_ext_tx2[idx]
# uc_dimensions = full_uc_dimensions[idx]
# Set the next unit cell dimensions
pose.uc_dimensions = full_uc_dimensions[idx]
else:
external_tx1 = external_tx2 = None
specific_transformation1 = dict(rotation=full_rotation1[idx], translation=internal_tx_param1,
rotation2=set_mat1, translation2=external_tx1)
specific_transformation2 = dict(rotation=full_rotation2[idx], translation=internal_tx_param2,
rotation2=set_mat2, translation2=external_tx2)
specific_transformations = [specific_transformation1, specific_transformation2]
# Transform each starting coords to the candidate pose coords then update the Pose coords
new_coords = []
for entity_idx, entity in enumerate(pose.entities):
new_coords.append(transform_coordinate_sets(entity_start_coords[entity_idx],
**specific_transformations[transform_indices[entity_idx]]))
pose.coords = np.concatenate(new_coords)
logger.debug(f'\tCopy and Transform Oligomer1 and Oligomer2 (took {time.time() - copy_model_start:8f}s)')
def find_viable_symmetric_indices(viable_pose_indices: list[int]) -> np.ndarray:
"""Using the nonlocal Pose and transformation indices, check each transformation index for symmetric viability
Args:
viable_pose_indices: The indices from the transform array to test for clashes
Returns:
An array with the transformation indices that passed clash testing
"""
# Assume the pose will fail the clash test (0), otherwise, (1) for passing
_passing_symmetric_clashes = [0 for _ in range(len(viable_pose_indices))]
for result_idx, transform_idx in enumerate(viable_pose_indices):
# Find the pose
update_pose_coords(transform_idx)
if not pose.symmetric_assembly_is_clash(measure=job.design.clash_criteria,
distance=job.design.clash_distance):
_passing_symmetric_clashes[result_idx] = 1
return np.flatnonzero(_passing_symmetric_clashes)
# Make the indices into an array
interface_is_viable = np.array(interface_is_viable, dtype=int)
# Update the passing_transforms
# passing_transforms contains all the transformations that are still passing
# index the previously passing indices (sufficiently_dense_indices) and (asu_is_viable_indices)
# by new passing indices (interface_is_viable)
# and set each of these indices to 1 (True)
# passing_transforms[sufficiently_dense_indices[asu_is_viable_indices[interface_is_viable]]] = 1
# # Remove non-viable transforms from the original transformation parameters by indexing interface_is_viable
# passing_transforms_indices = np.flatnonzero(passing_transforms)
# # filter_transforms_by_indices(passing_transforms_indices)
passing_transforms_indices = sufficiently_dense_indices[asu_is_viable_indices[interface_is_viable]]
if job.design.ignore_symmetric_clashes:
logger.warning(f'Not checking for symmetric clashes per requested flag '
f'{flags.format_args(flags.ignore_symmetric_clashes_args)}')
passing_symmetric_clash_indices_perturb = slice(None)
else:
logger.info('Checking solutions for symmetric clashes')
# passing_symmetric_clash_indices = find_viable_symmetric_indices(number_viable_pose_interfaces)
passing_symmetric_clash_indices = find_viable_symmetric_indices(passing_transforms_indices.tolist())
number_passing_symmetric_clashes = len(passing_symmetric_clash_indices)
logger.info(f'After symmetric clash testing, found {number_passing_symmetric_clashes} viable poses')
if number_passing_symmetric_clashes == 0: # There were no successful transforms
logger.warning(f'No viable poses without symmetric clashes. Terminating {building_blocks} docking')
return []
# ------------------ TERMINATE DOCKING ------------------------
# Update the passing_transforms
# passing_transforms contains all the transformations that are still passing
# index the previously passing indices (sufficiently_dense_indices)
# and (asu_is_viable_indices) and (interface_is_viable)
# by new passing indices (passing_symmetric_clash_indices)
# and set each of these indices to 1 (True)
# passing_transforms_indices = \
# sufficiently_dense_indices[asu_is_viable_indices[interface_is_viable[passing_symmetric_clash_indices]]]
passing_transforms_indices = passing_transforms_indices[passing_symmetric_clash_indices]
# Todo could this be used?
# passing_transforms[passing_transforms_indices] = 1
# Remove non-viable transforms from the original transformations due to clashing
filter_transforms_by_indices(passing_transforms_indices)
number_of_transforms = len(passing_transforms_indices)
# Clean memory
del passing_transforms_indices, sufficiently_dense_indices, passing_symmetric_clash_indices
del asu_is_viable_indices, interface_is_viable
# # all_passing_ghost_indices = [all_passing_ghost_indices[idx] for idx in passing_symmetric_clash_indices.tolist()]
# # all_passing_surf_indices = [all_passing_surf_indices[idx] for idx in passing_symmetric_clash_indices.tolist()]
# # all_passing_z_scores = [all_passing_z_scores[idx] for idx in passing_symmetric_clash_indices.tolist()]
if sym_entry.unit_cell:
# Calculate the vectorized uc_dimensions
full_uc_dimensions = sym_entry.get_uc_dimensions(full_optimal_ext_dof_shifts)
# Calculate metrics on input Pose before any manipulation
pose_length = pose.number_of_residues
residue_indices = list(range(pose_length))
def add_fragments_to_pose():
"""Add observed fragments to the Pose or generate new observations given the Pose state
If no arguments are passed, the fragment observations will be generated new
"""
# First, clear any pose information and force identification of the interface
pose._interface_residue_indices_by_interface = {}
pose.find_and_split_interface(by_distance=True, distance=cb_distance)
# # Next, set the interface fragment info for gathering of interface metrics
# if overlap_ghosts is None or overlap_surf is None or sorted_z_scores is None:
# Remove old fragments
pose._fragment_info_by_entity_pair = {}
# Query fragments
pose.generate_interface_fragments()
# Load evolutionary profiles of interest for optimization/analysis
if job.use_evolution:
measure_evolution, measure_alignment = load_evolutionary_profile(job.api_db, pose)
evolutionary_profile_array = pssm_as_array(pose.evolutionary_profile)
with catch_warnings():
simplefilter('ignore', category=RuntimeWarning)
# Divide by zero encountered in log
corrected_log_evolutionary_profile = np.nan_to_num(
np.log(evolutionary_profile_array), copy=False, nan=np.nan,
neginf=metrics.zero_probability_evol_value)
batch_evolutionary_profile = \
torch.from_numpy(np.tile(evolutionary_profile_array, (batch_length, 1, 1)))
else: # Make an empty collapse_profile
# pose.add_profile(null=True) # <- Actually, don't use, keep pose.evolutionary_profile == {}
measure_evolution = measure_alignment = False
collapse_profile = np.empty(0)
evolutionary_profile_array = corrected_log_evolutionary_profile = None
# Calculate hydrophobic collapse for each dock using the collapse_profile if it was calculated
if measure_evolution:
hydrophobicity = 'expanded'
else:
hydrophobicity = 'standard'
contact_order_per_res_z, reference_collapse, collapse_profile = \
pose.get_folding_metrics(hydrophobicity=hydrophobicity)
if measure_evolution: # collapse_profile.size: # Not equal to zero, use the profile instead
reference_collapse = collapse_profile
# Todo
# enable precise metric acquisition
# def collect_dock_metrics(score_functions: dict[str, Callable]) -> dict[str, np.ndarray]:
# """Perform analysis on the docked Pose instances"""
# pose_functions = {}
# residue_functions = {}
# for score, function in score_functions:
# if getattr(sql.PoseMetrics, score, None):
# pose_functions[score] = function
# else:
# residue_functions[score] = function
#
# pose_metrics = []
# per_residue_metrics = []
# for idx in range(number_of_transforms):
# # Add the next set of coordinates
# update_pose_coords(idx)
#
# # if number_perturbations_applied > 1:
# add_fragments_to_pose()
#
# pose_metrics.append({score: function(pose) for score, function in pose_functions})
# per_residue_metrics.append({score: function(pose) for score, function in residue_functions})
#
# # Reset the fragment_map and fragment_profile for each Entity before calculate_fragment_profile
# for entity in pose.entities:
# entity.fragment_map = None
# # entity.alpha.clear()
#
# # Load fragment_profile (and fragment_metrics) into the analysis
# pose.calculate_fragment_profile()
# # This could be an empty array if no fragments were found
# fragment_profile_array = pose.fragment_profile.as_array()
# with catch_warnings():
# simplefilter('ignore', category=RuntimeWarning)
# # np.log causes -inf at 0, thus we correct these to a 'large' number
# corrected_frag_array = np.nan_to_num(np.log(fragment_profile_array), copy=False,
# nan=np.nan, neginf=metrics.zero_probability_frag_value)
# per_residue_fragment_profile_loss = \
# resources.ml.sequence_nllloss(torch_numeric_sequence, torch.from_numpy(corrected_frag_array))
#
# # Remove saved pose attributes from the prior iteration calculations
# pose.ss_sequence_indices.clear(), pose.ss_type_sequence.clear()
# pose.fragment_metrics.clear()
# for attribute in ['_design_residues', '_interface_residues']: # _assembly_minimally_contacting
# try:
# delattr(pose, attribute)
# except AttributeError:
# pass
#
# # Save pose metrics
# # pose_metrics[pose_id] = {
# pose_metrics.append({
# **pose.calculate_metrics(), # Also calculates entity.metrics
# 'dock_collapse_violation': collapse_violation[idx],
# })
# def format_docking_metrics(metrics_: dict[str, np.ndarray]) -> tuple[pd.DataFrame, pd.DataFrame]:
# """From the current pool of docked poses and their collected metrics, format the metrics for selection/output
#
# Args:
# metrics_: A dictionary of metric name to metric value where the values are per-residue measurements the
# length of the active transformation pool
# Returns:
# A tuple of DataFrames representing the per-pose and the per-residue metrics. Each has indices from 0-N
# """
idx_slice = pd.IndexSlice
def collect_dock_metrics(proteinmpnn_score: bool = False) -> tuple[pd.DataFrame, pd.DataFrame]: # -> dict[str,
# np.ndarray]:
"""Perform analysis on the docked Pose instances
Args:
proteinmpnn_score: Whether proteinmpnn scores should be collected
Returns:
A tuple of DataFrames representing the per-pose and the per-residue metrics. Each has indices from 0-N
"""
logger.info(f'Collecting metrics for {number_of_transforms} active Poses')
# Remove old DataFrames to save memory. These are going to be calculated fresh
nonlocal poses_df, residues_df
poses_df = residues_df = pd.DataFrame()
# nan_blank_data = list(repeat(np.nan, pose_length))
# unbound_errat = []
# for idx, entity in enumerate(pose.entities):
# _, oligomeric_errat = entity.assembly.errat(out_path=os.path.devnull)
# unbound_errat.append(oligomeric_errat[:entity.number_of_residues])
torch_numeric_sequence = torch.from_numpy(pose.sequence_numeric)
if evolutionary_profile_array is None:
profile_loss = {}
else:
torch_log_evolutionary_profile = torch.from_numpy(corrected_log_evolutionary_profile)
per_residue_evolutionary_profile_loss = \
resources.ml.sequence_nllloss(torch_numeric_sequence, torch_log_evolutionary_profile)
profile_loss = {
# 'sequence_loss_design': per_residue_design_profile_loss,
'sequence_loss_evolution': per_residue_evolutionary_profile_loss,
}
sequence_params = {
**pose.per_residue_contact_order(),
# 'errat_deviation': np.concatenate(unbound_errat),
'type': tuple(pose.sequence),
**profile_loss
# Todo 1 each pose...
# 'sequence_loss_fragment': per_residue_fragment_profile_loss
}
if proteinmpnn_score:
# Initialize and retrieve the ProteinMPNN model for dock analysis
mpnn_model = ml.proteinmpnn_factory(ca_only=job.design.ca_only, model_name=job.design.proteinmpnn_model)
# Set up model sampling type based on symmetry
if pose.is_symmetric():
number_of_residues = pose.number_of_symmetric_residues
else:
number_of_residues = pose_length
# Modulate memory requirements
# calculation_size = len(full_rotation1) # This is the number of transformations
# The batch_length indicates how many models could fit in the allocated memory
batch_length = ml.calculate_proteinmpnn_batch_length(mpnn_model, number_of_residues)
logger.info(f'Found ProteinMPNN batch_length={batch_length}')
# Set up parameters to run ProteinMPNN design
if job.design.ca_only:
coords_type = 'ca_coords'
num_model_residues = 1
else:
coords_type = 'backbone_coords'
num_model_residues = 4
# Set up Pose parameters
parameters = pose.get_proteinmpnn_params(ca_only=job.design.ca_only,
interface=measure_interface_during_dock)
# Todo 2 reinstate if conditional_log_probs
# # Todo
# # Must calculate randn individually if using some feature to describe order
# parameters['randn'] = pose.generate_proteinmpnn_decode_order() # to_device=device)
# Set up interface unbound coordinates
mpnn_null_idx = resources.ml.MPNN_NULL_IDX
if measure_interface_during_dock:
X_unbound = pose.get_proteinmpnn_unbound_coords(ca_only=job.design.ca_only)
unbound_batch = \
ml.setup_pose_batch_for_proteinmpnn(1, mpnn_model.device, X_unbound=X_unbound,
mask=parameters['mask'], residue_idx=parameters['residue_idx'],
chain_encoding=parameters['chain_encoding'])
with torch.no_grad():
unconditional_log_probs_unbound = \
mpnn_model.unconditional_probs(unbound_batch['X_unbound'], unbound_batch['mask'],
unbound_batch['residue_idx'],
unbound_batch['chain_encoding']).cpu()
asu_unconditional_softmax_seq_unbound = \
np.exp(unconditional_log_probs_unbound[:, :pose_length, :mpnn_null_idx])
# Remove any unnecessary reserved memory
del unbound_batch
else:
raise NotImplementedError(
f"{fragment_dock.__name__} isn't written to only measure the complex state")
asu_unconditional_softmax_seq_unbound = None
# Disregard X, chain_M_pos, and bias_by_res parameters return and use the pose specific data from below
# parameters.pop('X') # overwritten by X_unbound
parameters.pop('chain_M_pos')
parameters.pop('bias_by_res')
# tied_pos = parameters.pop('tied_pos')
# # Todo 2 if modifying the amount of weight given to each of the copies
# tied_beta = parameters.pop('tied_beta')
# # Set the design temperature
# temperature = job.design.temperatures[0]
batch_parameters = ml.setup_pose_batch_for_proteinmpnn(batch_length, mpnn_model.device, **parameters)
mask = batch_parameters['mask']
residue_idx = batch_parameters['residue_idx']
chain_encoding = batch_parameters['chain_encoding']
def proteinmpnn_score_batched_coords(batched_coords: list[np.ndarray]) -> list[dict[str, np.ndarray]]:
""""""
actual_batch_length = len(batched_coords)
if actual_batch_length != batch_length:
# if not actual_batch_length:
# return []
_mask = mask[:actual_batch_length]
_residue_idx = residue_idx[:actual_batch_length]
_chain_encoding = chain_encoding[:actual_batch_length]
else:
_mask = mask[:actual_batch_length]
_residue_idx = residue_idx[:actual_batch_length]
_chain_encoding = chain_encoding[:actual_batch_length]
# Format the bb coords for ProteinMPNN
if pose.is_symmetric():
# Make each set of coordinates symmetric
# Lattice cases have .uc_dimensions set in update_pose_coords()
perturbed_bb_coords = np.concatenate(
[pose.return_symmetric_coords(coords_) for coords_ in batched_coords])
# Todo 2 reinstate if conditional_log_probs
# # Symmetrize other arrays
# number_of_symmetry_mates = pose.number_of_symmetry_mates
# # (batch, number_of_sym_residues, ...)
# residue_mask_cpu = np.tile(residue_mask_cpu, (1, number_of_symmetry_mates))
# # bias_by_res = np.tile(bias_by_res, (1, number_of_symmetry_mates, 1))
else:
# When batched_coords are individually transformed, axis=0 works
perturbed_bb_coords = np.concatenate(batched_coords, axis=0)
# Reshape for ProteinMPNN
# Let -1 fill in the pose length dimension with the number of residues
# 4 is shape of backbone coords (N, Ca, C, O), 3 is x,y,z
# logger.debug(f'perturbed_bb_coords.shape: {perturbed_bb_coords.shape}')
X = perturbed_bb_coords.reshape((actual_batch_length, -1, num_model_residues, 3))
# logger.debug(f'X.shape: {X.shape}')
# Start a unit of work
# Taking the KL divergence would indicate how divergent the interfaces are from the
# surface. This should be simultaneously minimized (i.e. the lowest evolutionary divergence)
# while the aa frequency distribution cross_entropy compared to the fragment profile is
# minimized
# -------------------------------------------
with torch.no_grad():
X = torch.from_numpy(X).to(dtype=torch.float32, device=mpnn_model.device)
unconditional_log_probs = \
mpnn_model.unconditional_probs(X, _mask, _residue_idx, _chain_encoding).cpu()
# Use the sequence as an unknown token then guess the probabilities given the remaining
# information, i.e. the sequence and the backbone
# Calculations with this are done using cpu memory and numpy
# Todo 2 reinstate if conditional_log_probs
# S_design_null[residue_mask.type(torch.bool)] = mpnn_null_idx
# chain_residue_mask = chain_mask * residue_mask * mask
# decoding_order = \
# ml.create_decoding_order(randn, chain_residue_mask, tied_pos=tied_pos, to_device=device)
# conditional_log_probs_null_seq = \
# mpnn_model(X, S_design_null, mask, chain_residue_mask, residue_idx, chain_encoding,
# None, # This argument is provided but with below args, is not used
# use_input_decoding_order=True, decoding_order=decoding_order).cpu()
# asu_conditional_softmax_null_seq = \
# np.exp(conditional_log_probs_null_seq[:, :pose_length, :mpnn_null_idx])
# Remove the gaps index from the softmax input with :mpnn_null_idx]
asu_unconditional_softmax_seq = \
np.exp(unconditional_log_probs[:, :pose_length, :mpnn_null_idx])
# asu_unconditional_softmax_seq
# tensor([[[0.0273, 0.0125, 0.0200, ..., 0.0073, 0.0102, 0.0052],
# ...,
# [0.0091, 0.0078, 0.0101, ..., 0.0038, 0.0029, 0.0059]],
# ...
# [[0.0273, 0.0125, 0.0200, ..., 0.0073, 0.0102, 0.0052],
# ...,
# [0.0091, 0.0078, 0.0101, ..., 0.0038, 0.0029, 0.0059]]])
per_residue_dock_cross_entropy = \
metrics.cross_entropy(asu_unconditional_softmax_seq,
asu_unconditional_softmax_seq_unbound[:actual_batch_length],
per_entry=True)
# Set up per_residue metrics
# All have the shape (this batch length, pose.number_of_residues)
# Set up the interface indices where interface residues are 1, others are 0
per_residue_design_indices = np.zeros((actual_batch_length, pose_length), dtype=np.int32)
for chunk_idx, interface_residues in enumerate(interface_mask):
per_residue_design_indices[chunk_idx, interface_residues] = 1
# Set up various profiles for cross entropy against the softmax(complexed ProteinMPNN logits)
if pose.fragment_profile:
# Process the fragment_profiles into an array for cross entropy
fragment_profile_array = np.nan_to_num(np.array(fragment_profiles), copy=False, nan=np.nan)
# RuntimeWarning: divide by zero encountered in log
# np.log causes -inf at 0, thus we need to correct these to a very large number
batch_fragment_profile = torch.from_numpy(fragment_profile_array)
per_residue_fragment_cross_entropy = \
metrics.cross_entropy(asu_unconditional_softmax_seq,
batch_fragment_profile,
per_entry=True)
# per_residue_fragment_cross_entropy
# [[-3.0685883 -3.575249 -2.967545 ... -3.3111317 -3.1204746 -3.1201541]
# [-3.0685873 -3.5752504 -2.9675443 ... -3.3111336 -3.1204753 -3.1201541]
# [-3.0685952 -3.575687 -2.9675474 ... -3.3111277 -3.1428783 -3.1201544]]
else: # Populate with null data
per_residue_fragment_cross_entropy = np.empty_like(per_residue_design_indices, dtype=np.float32)
per_residue_fragment_cross_entropy[:] = np.nan
if pose.evolutionary_profile:
per_residue_evolution_cross_entropy = \
metrics.cross_entropy(asu_unconditional_softmax_seq,
batch_evolutionary_profile[:actual_batch_length],
per_entry=True)
else: # Populate with null data
per_residue_evolution_cross_entropy = np.empty_like(per_residue_fragment_cross_entropy)
per_residue_evolution_cross_entropy[:] = np.nan
if pose.profile:
# Process the design_profiles into an array for cross entropy
with catch_warnings():
# Divide by zero encountered in log
simplefilter('ignore', category=RuntimeWarning)
corrected_log_design_profile = np.nan_to_num(
np.log(np.array(design_profiles)), copy=False, nan=np.nan,
neginf=metrics.zero_probability_evol_value)
batch_design_profile = torch.from_numpy(corrected_log_design_profile)
per_residue_design_cross_entropy = \
metrics.cross_entropy(asu_unconditional_softmax_seq,
batch_design_profile,
per_entry=True)
else: # Populate with null data
per_residue_design_cross_entropy = np.empty_like(per_residue_fragment_cross_entropy)
per_residue_design_cross_entropy[:] = np.nan
# Convert to axis=0 list's for below indexing
per_residue_design_indices = list(per_residue_design_indices)
per_residue_dock_cross_entropy = list(per_residue_dock_cross_entropy)
per_residue_design_cross_entropy = list(per_residue_design_cross_entropy)
per_residue_evolution_cross_entropy = list(per_residue_evolution_cross_entropy)
per_residue_fragment_cross_entropy = list(per_residue_fragment_cross_entropy)
_per_residue_data_batched = []
for idx in range(actual_batch_length):
_per_residue_data_batched.append({
# This is required to save the interface_residues
'interface_residue': per_residue_design_indices[idx],
'proteinmpnn_dock_cross_entropy_loss': per_residue_dock_cross_entropy[idx],
'proteinmpnn_v_design_probability_cross_entropy_loss':
per_residue_design_cross_entropy[idx],
'proteinmpnn_v_evolution_probability_cross_entropy_loss':
per_residue_evolution_cross_entropy[idx],
'proteinmpnn_v_fragment_probability_cross_entropy_loss':
per_residue_fragment_cross_entropy[idx],
})
if collapse_profile.size: # Not equal to zero
# Include new axis for the sequence iteration to work on an array v
collapse_by_pose = \
metrics.collapse_per_residue(asu_unconditional_softmax_seq[:, None],
contact_order_per_res_z, reference_collapse,
alphabet_type=protein_letters_alph1,
hydrophobicity='expanded')
for _data_batched, collapse_metrics in zip(_per_residue_data_batched, collapse_by_pose):
_data_batched.update(collapse_metrics)
return _per_residue_data_batched
else:
batch_parameters = {}
mask = residue_idx = chain_encoding = None
# Initialize pose data
pose_metrics = []
per_residue_data = []
pose_ids = list(range(number_of_transforms))
# ProteinMPNN
batch_idx = 0
# pose_metrics_batched = []
per_residue_data_batched = []
design_profiles = []
fragment_profiles = []
interface_mask = []
# Stack the entity coordinates to make up a contiguous block for each pose
new_coords = []
# Get metrics for each Pose
for idx in tqdm(pose_ids, bar_format=TQDM_BAR_FORMAT):
# logger.info(f'Metrics for Pose {idx + 1}/{number_of_transforms}')
# Add the next set of coordinates
update_pose_coords(idx)
# if number_perturbations_applied > 1:
add_fragments_to_pose()
# Reset the fragment_map and fragment_profile for each Entity before calculate_fragment_profile
for entity in pose.entities:
entity.fragment_map = None
# entity.alpha.clear()
# Load fragment_profile (and fragment_metrics) into the analysis
pose.calculate_fragment_profile()
# This could be an empty array if no fragments were found
fragment_profile_array = pose.fragment_profile.as_array()
with catch_warnings():
simplefilter('ignore', category=RuntimeWarning)
# np.log causes -inf at 0, so correct these to a "large" number
corrected_frag_array = np.nan_to_num(np.log(fragment_profile_array), copy=False,
nan=np.nan, neginf=metrics.zero_probability_frag_value)
per_residue_fragment_profile_loss = \
resources.ml.sequence_nllloss(torch_numeric_sequence, torch.from_numpy(corrected_frag_array))
# per_residue_data[pose_id] = {
per_residue_data.append({
**sequence_params,
'sequence_loss_fragment': per_residue_fragment_profile_loss
})
# Remove saved pose attributes from the prior iteration calculations
pose.ss_sequence_indices.clear(), pose.ss_type_sequence.clear()
for attribute in ['_design_residues', '_interface_residues']: # _assembly_minimally_contacting
try:
delattr(pose, attribute)
except AttributeError:
pass
# Save pose metrics
pose_metrics.append(pose.calculate_metrics())
if proteinmpnn_score:
# Save profiles
fragment_profiles.append(pose.fragment_profile.as_array())
if measure_evolution:
pose.calculate_profile()
design_profiles.append(pssm_as_array(pose.profile))
# Add all interface residues
# if measure_interface_during_dock: # job.design.interface:
design_residues = []
for number, residues in pose.interface_residues_by_interface_unique.items():
design_residues.extend([residue.index for residue in residues])
# else:
# design_residues = list(range(pose_length))
interface_mask.append(design_residues)
# Set coords
new_coords.append(getattr(pose, coords_type))
batch_idx += 1
# If the current iteration marks a batch"-sized" unit of work, execute it
if batch_idx == batch_length: # or idx + 1 == number_of_transforms:
per_residue_data_batched.extend(proteinmpnn_score_batched_coords(new_coords))
# Set batch containers to zero for the next iteration
batch_idx = 0
design_profiles = []
fragment_profiles = []
interface_mask = []
new_coords = []
if proteinmpnn_score:
# Finish the routine with any remaining proteinmpnn calculations adding last batched dataset from
# proteinmpnn_score_batched_coords()
if new_coords:
per_residue_data_batched.extend(proteinmpnn_score_batched_coords(new_coords))
# Consolidate the iterative and batched data
for data, batched_data in zip(per_residue_data, per_residue_data_batched):
data.update(batched_data)
# for data, batched_data in zip(pose_metrics, pose_metrics_batched):
# data.update(batched_data)
# Construct the main DataFrames, poses_df and residues_df
poses_df = pd.DataFrame.from_dict(dict(zip(pose_ids, pose_metrics)), orient='index')
residues_df = pd.concat({pose_id: pd.DataFrame(data, index=residue_indices)
for pose_id, data in zip(pose_ids, per_residue_data)}) \
.unstack().swaplevel(0, 1, axis=1)
# Set up column renaming
# if proteinmpnn_score:
# per_res_columns = [
# 'proteinmpnn_v_design_probability_cross_entropy_loss',
# 'proteinmpnn_v_evolution_probability_cross_entropy_loss'
# ]
# mean_columns = [
# 'proteinmpnn_v_design_probability_cross_entropy_per_residue',
# 'proteinmpnn_v_evolution_probability_cross_entropy_per_residue'
# ]
# _rename = dict(zip(per_res_columns, mean_columns))
if proteinmpnn_score and collapse_profile.size:
# collapse_profile required
collapse_metrics = (
'collapse_deviation_magnitude',
'collapse_increase_significance_by_contact_order_z',
'collapse_increased_z',
'collapse_new_positions',
'collapse_new_position_significance',
'collapse_significance_by_contact_order_z',
'collapse_sequential_peaks_z',
'collapse_sequential_z',
'hydrophobic_collapse')
unique_columns = residues_df.columns.unique(level=-1)
_columns = unique_columns.tolist()
remap_columns = dict(zip(_columns, _columns))
remap_columns.update(dict(zip(collapse_metrics, (f'dock_{metric_}' for metric_ in collapse_metrics))))
residues_df.columns = residues_df.columns.set_levels(unique_columns.map(remap_columns), level=-1)
# 'dock' metrics aren't included by default
per_res_columns = ['dock_hydrophobic_collapse', 'dock_collapse_deviation_magnitude']
mean_columns = ['dock_hydrophobicity', 'dock_collapse_variance']
_rename = dict(zip(per_res_columns, mean_columns))
else:
mean_columns = []
_rename = {}
# Calculate new metrics from combinations of other metrics
# Add summed residue information to poses_df
summed_poses_df = metrics.sum_per_residue_metrics(
residues_df, rename_columns=_rename, mean_metrics=mean_columns)
# # Need to remove sequence as it is in pose.calculate_metrics()
# poses_df = poses_df.join(summed_poses_df.drop('sequence', axis=1))
if proteinmpnn_score:
poses_df = poses_df.join(summed_poses_df.drop('number_residues_interface', axis=1))
# .droplevel(-1, axis=1) operations are REQUIRED here or the calculations are messed up
interface_df = residues_df.loc[:, idx_slice[:, 'interface_residue']].droplevel(-1, axis=1)
# number_interface_residues_s = interface_df.sum(axis=1)
number_interface_residues_s = poses_df['number_residues_interface']
# Update the total loss according to those residues that were actually specified as designable
poses_df['proteinmpnn_dock_cross_entropy_per_residue'] = \
(residues_df.loc[:, idx_slice[:, 'proteinmpnn_dock_cross_entropy_loss']].droplevel(-1, axis=1)
* interface_df).sum(axis=1)
poses_df['proteinmpnn_dock_cross_entropy_per_residue'] /= number_interface_residues_s
poses_df['proteinmpnn_v_design_probability_cross_entropy_per_residue'] = \
(residues_df.loc[:, idx_slice[:, 'proteinmpnn_v_design_probability_cross_entropy_loss']]
.droplevel(-1, axis=1) * interface_df).sum(axis=1)
poses_df['proteinmpnn_v_design_probability_cross_entropy_per_residue'] /= number_interface_residues_s
poses_df['proteinmpnn_v_evolution_probability_cross_entropy_per_residue'] = \
(residues_df.loc[:, idx_slice[:, 'proteinmpnn_v_evolution_probability_cross_entropy_loss']]
.droplevel(-1, axis=1) * interface_df).sum(axis=1)
poses_df['proteinmpnn_v_evolution_probability_cross_entropy_per_residue'] /= number_interface_residues_s
# poses_df['proteinmpnn_v_fragment_probability_cross_entropy_per_residue'] = \
# (residues_df.loc[:, idx_slice[:, 'proteinmpnn_v_fragment_probability_cross_entropy_loss']]
# .droplevel(-1, axis=1) * interface_df).sum(axis=1)
# Update the per_residue loss according to fragment residues involved in the scoring
poses_df['proteinmpnn_v_fragment_probability_cross_entropy_per_residue'] = \
poses_df['proteinmpnn_v_fragment_probability_cross_entropy_loss'] \
/ poses_df['number_residues_interface_fragment_total']
if collapse_profile.size:
# Check if there are new collapse islands and count
poses_df['dock_collapse_new_positions'] = \
(residues_df.loc[:, idx_slice[:, 'dock_collapse_new_positions']].droplevel(-1, axis=1)
* interface_df).sum(axis=1)
# If there are any dock_collapse_new_positions there is a collapse violation
poses_df['dock_collapse_violation'] = poses_df['dock_collapse_new_positions'] > 0
poses_df['dock_collapse_significance_by_contact_order_z_mean'] = \
poses_df['dock_collapse_significance_by_contact_order_z'] / \
(residues_df.loc[:, idx_slice[:, 'dock_collapse_significance_by_contact_order_z']] != 0) \
.sum(axis=1)
# if measure_alignment:
dock_collapse_increased_df = residues_df.loc[:, idx_slice[:, 'dock_collapse_increased_z']]
total_increased_collapse = (dock_collapse_increased_df != 0).sum(axis=1)
poses_df['dock_collapse_increased_z_mean'] = \
dock_collapse_increased_df.sum(axis=1) / total_increased_collapse
poses_df['dock_collapse_sequential_peaks_z_mean'] = \
poses_df['dock_collapse_sequential_peaks_z'] / total_increased_collapse
poses_df['dock_collapse_sequential_z_mean'] = \
poses_df['dock_collapse_sequential_z'] / total_increased_collapse
else:
poses_df = poses_df.join(summed_poses_df)
# Finally add the precalculated pose_thermophilicity for completeness
poses_df['pose_thermophilicity'] = pose_thermophilicity
# logger.debug(f'Found poses_df with columns: {poses_df.columns.tolist()}')
# logger.debug(f'Found poses_df with index: {poses_df.index.tolist()}')
return poses_df, residues_df
def perturb_transformations() -> tuple[np.ndarray, list[int], int]:
"""From existing transformation parameters, sample parameters within a range of spatial perturbation
Returns:
A tuple consisting of the elements (
transformation hash - Integer mapping the possible 3D space for docking to each perturbed transformation,
size of each perturbation cluster - Number of perturbed transformations possible from starting transform,
degrees of freedom sampled - How many degrees of freedom were perturbed
)
"""
logger.info(f'Perturbing transformations')
perturb_rotation1, perturb_rotation2, perturb_int_tx1, perturb_int_tx2, perturb_optimal_ext_dof_shifts = \
[], [], [], [], []
# Define a function to stack the transforms
def stack_viable_transforms(passing_indices: np.ndarray | list[int]):
"""From indices with viable transformations, stack the corresponding transformations into full
perturbation transformations
Args:
passing_indices: The indices that should be selected from the full transformation sets
"""
# nonlocal perturb_rotation1, perturb_rotation2, perturb_int_tx1, perturb_int_tx2
logger.debug(f'Perturb expansion found {len(passing_indices)} passing_perturbations')
perturb_rotation1.append(full_rotation1[passing_indices])
perturb_rotation2.append(full_rotation2[passing_indices])
if sym_entry.is_internal_tx1:
perturb_int_tx1.extend(full_int_tx1[passing_indices, -1])
if sym_entry.is_internal_tx2:
perturb_int_tx2.extend(full_int_tx2[passing_indices, -1])
if sym_entry.unit_cell:
nonlocal full_optimal_ext_dof_shifts # , full_ext_tx1, full_ext_tx2
perturb_optimal_ext_dof_shifts.append(full_optimal_ext_dof_shifts[passing_indices])
# full_uc_dimensions = full_uc_dimensions[passing_indices]
# full_ext_tx1 = full_ext_tx1[passing_indices]
# full_ext_tx2 = full_ext_tx2[passing_indices]
# Expand successful poses from coarse search of transformational space to randomly perturbed offset
# By perturbing the transformation a random small amount, we generate transformational diversity from
# the already identified solutions.
perturbations, n_perturbed_dof = \
create_perturbation_transformations(sym_entry, number_of_rotations=job.dock.perturb_dof_steps_rot,
number_of_translations=job.dock.perturb_dof_steps_tx,
rotation_steps=rotation_steps,
translation_steps=translation_perturb_steps)
# Extract perturbation parameters and set the original transformation parameters to a new variable
nonlocal number_perturbations_applied
rotation_perturbations1 = perturbations['rotation1']
# Compute the length of each perturbation to separate into unique perturbation spaces
number_perturbations_applied = calculation_size_ = len(rotation_perturbations1)
# logger.debug(f'rotation_perturbations1.shape: {rotation_perturbations1.shape}')
# logger.debug(f'rotation_perturbations1[:5]: {rotation_perturbations1[:5]}')
batch_length_ = get_check_tree_for_query_overlap_batch_length(bb_cb_coords2)
batch_length_ = min((batch_length_, number_perturbations_applied))
# Define local check_tree_for_query_overlap function to check clashes
@resources.ml.batch_calculation(size=calculation_size_, batch_length=batch_length_, setup=np_tile_wrap,
compute_failure_exceptions=(np.core._exceptions._ArrayMemoryError,))
def perturb_check_tree_for_query_overlap(*args, **kwargs):
return check_tree_for_query_overlap(*args, **kwargs)
nonlocal number_of_transforms, full_rotation1, full_rotation2
# if sym_entry.is_internal_rot1: # Todo 2
original_rotation1 = full_rotation1
# if sym_entry.is_internal_rot2: # Todo 2
original_rotation2 = full_rotation2
rotation_perturbations2 = perturbations['rotation2']
# logger.debug(f'rotation_perturbations2.shape: {rotation_perturbations2.shape}')
# logger.debug(f'rotation_perturbations2[:5]: {rotation_perturbations2[:5]}')
# blank_parameter = list(repeat([None, None, None], number_of_transforms))
if sym_entry.is_internal_tx1:
nonlocal full_int_tx1
original_int_tx1 = full_int_tx1
translation_perturbations1 = perturbations['translation1']
# logger.debug(f'translation_perturbations1.shape: {translation_perturbations1.shape}')
# logger.debug(f'translation_perturbations1[:5]: {translation_perturbations1[:5]}')
# else:
# translation_perturbations1 = blank_parameter
if sym_entry.is_internal_tx2:
nonlocal full_int_tx2
original_int_tx2 = full_int_tx2
translation_perturbations2 = perturbations['translation2']
# logger.debug(f'translation_perturbations2.shape: {translation_perturbations2.shape}')
# logger.debug(f'translation_perturbations2[:5]: {translation_perturbations2[:5]}')
# else:
# translation_perturbations2 = blank_parameter
if sym_entry.unit_cell:
nonlocal full_optimal_ext_dof_shifts
nonlocal full_ext_tx1, full_ext_tx2
ext_dof_perturbations = perturbations['external_translations']
original_optimal_ext_dof_shifts = full_optimal_ext_dof_shifts
# original_ext_tx1 = full_ext_tx1
# original_ext_tx2 = full_ext_tx2
external_dof1, external_dof2, *_ = sym_entry.external_dofs
else:
full_ext_tx1 = full_ext_tx2 = full_ext_tx_sum = None
# Apply the perturbation to each existing transformation
logger.info(f'Perturbing each transform {number_perturbations_applied} times')
for idx in range(number_of_transforms):
logger.info(f'Perturbing transform {idx + 1}')
# Rotate the unique rotation by the perturb_matrix_grid and set equal to the full_rotation* array
full_rotation1 = np.matmul(original_rotation1[idx], rotation_perturbations1.swapaxes(-1, -2)) # rotation1
full_inv_rotation1 = np.linalg.inv(full_rotation1)
full_rotation2 = np.matmul(original_rotation2[idx], rotation_perturbations2.swapaxes(-1, -2)) # rotation2
# Translate the unique translation according to the perturb_translation_grid
if sym_entry.is_internal_tx1:
full_int_tx1 = original_int_tx1[idx] + translation_perturbations1 # translation1
if sym_entry.is_internal_tx2:
full_int_tx2 = original_int_tx2[idx] + translation_perturbations2 # translation2
if sym_entry.unit_cell:
# perturbed_optimal_ext_dof_shifts = full_optimal_ext_dof_shifts[None] + ext_dof_perturbations
# full_ext_tx_perturb1 = (perturbed_optimal_ext_dof_shifts[:, :, None] \
# * sym_entry.external_dof1).sum(axis=-2)
# full_ext_tx_perturb2 = (perturbed_optimal_ext_dof_shifts[:, :, None] \
# * sym_entry.external_dof2).sum(axis=-2)
# Below is for the individual perturbation
# optimal_ext_dof_shift = full_optimal_ext_dof_shifts[idx]
# perturbed_ext_dof_shift = optimal_ext_dof_shift + ext_dof_perturbations
unsqueezed_perturbed_ext_dof_shifts = \
(original_optimal_ext_dof_shifts[idx] + ext_dof_perturbations)[:, :, None]
# unsqueezed_perturbed_ext_dof_shifts = perturbed_ext_dof_shift[:, :, None]
full_ext_tx1 = np.sum(unsqueezed_perturbed_ext_dof_shifts * external_dof1, axis=-2)
full_ext_tx2 = np.sum(unsqueezed_perturbed_ext_dof_shifts * external_dof2, axis=-2)
full_ext_tx_sum = full_ext_tx2 - full_ext_tx1
# Check for ASU clashes again
# Using the inverse transform of the model2 backbone and cb coordinates, check for clashes with the model1
# backbone and cb coordinates BallTree
ball_tree_kwargs = dict(binarytree=component1_backbone_cb_tree, clash_distance=clash_dist,
rotation=full_rotation2, translation=full_int_tx2,
rotation2=set_mat2, translation2=full_ext_tx_sum,
rotation3=inv_setting1,
translation3=None if full_int_tx1 is None else full_int_tx1 * -1,
rotation4=full_inv_rotation1)
# Create a fresh asu_clash_counts
asu_clash_counts = np.ones(number_perturbations_applied)
# clash_time_start = time.time()
overlap_return = perturb_check_tree_for_query_overlap(
**ball_tree_kwargs, return_containers={'overlap_counts': asu_clash_counts}, setup_args=(bb_cb_coords2,))
# logger.debug(f'Perturb clash took {time.time() - clash_time_start:8f}s')
# Extract the data
asu_clash_counts = overlap_return['overlap_counts']
logger.debug(f'Perturb expansion found asu_clash_counts:\n{asu_clash_counts}')
passing_perturbations = np.flatnonzero(asu_clash_counts == 0)
# Check for symmetric clashes again
if not job.design.ignore_symmetric_clashes:
passing_symmetric_clash_indices_perturb = find_viable_symmetric_indices(passing_perturbations.tolist())
else:
passing_symmetric_clash_indices_perturb = slice(None)
# Index the passing ASU indices with the passing symmetric indices and keep all viable transforms
# Stack the viable perturbed transforms
stack_viable_transforms(passing_perturbations[passing_symmetric_clash_indices_perturb])
# Concatenate the stacked perturbations
full_rotation1 = np.concatenate(perturb_rotation1, axis=0)
full_rotation2 = np.concatenate(perturb_rotation2, axis=0)
number_of_transforms = len(full_rotation1)
logger.info(f'After perturbation, found {number_of_transforms} viable solutions')
if sym_entry.is_internal_tx1:
full_int_tx1 = np.zeros((number_of_transforms, 3), dtype=float)
# Add the translation to Z (axis=1)
full_int_tx1[:, -1] = perturb_int_tx1
# full_int_tx1 = stacked_internal_tx_vectors1
if sym_entry.is_internal_tx2:
full_int_tx2 = np.zeros((number_of_transforms, 3), dtype=float)
# Add the translation to Z (axis=1)
full_int_tx2[:, -1] = perturb_int_tx2
# full_int_tx2 = stacked_internal_tx_vectors2
if sym_entry.unit_cell:
# optimal_ext_dof_shifts[:, :, None] <- None expands the axis to make multiplication accurate
full_optimal_ext_dof_shifts = np.concatenate(perturb_optimal_ext_dof_shifts, axis=0)
unsqueezed_optimal_ext_dof_shifts = full_optimal_ext_dof_shifts[:, :, None]
full_ext_tx1 = np.sum(unsqueezed_optimal_ext_dof_shifts * external_dof1, axis=-2)
full_ext_tx2 = np.sum(unsqueezed_optimal_ext_dof_shifts * external_dof2, axis=-2)
transform_hashes = create_transformation_hash()
logger.debug(f'Found the TransformHasher.translation_bin_width={model_transform_hasher.translation_bin_width}, '
f'.rotation_bin_width={model_transform_hasher.rotation_bin_width}\n'
f'Current range of sampled translations={sum(translation_perturb_steps)}, '
f'rotations={sum(rotation_steps)}')
# print(model_transform_hasher.translation_bin_width > sum(translation_perturb_steps))
# print(model_transform_hasher.rotation_bin_width > sum(rotation_steps))
if model_transform_hasher.translation_bin_width > sum(translation_perturb_steps) or \
model_transform_hasher.rotation_bin_width > sum(rotation_steps):
# The translation/rotation is smaller than bins, so further exploration only possible without minimization
# Get the shape of the passing perturbations
perturbation_shape = [len(perturb) for perturb in perturb_rotation1]
# sorted_unique_transform_hashes = transform_hashes
else:
# Minimize perturbation space by unique transform hashes
# Using the current transforms, create a hash to uniquely label them and apply to the indices
sorted_unique_transform_hashes, unique_indices = np.unique(transform_hashes, return_index=True)
# Create array to mark which are unique
unique_transform_hashes = np.zeros_like(transform_hashes)
unique_transform_hashes[unique_indices] = 1
# Filter by unique_indices, sorting the indices to maintain the order of the transforms
# filter_transforms_by_indices(unique_indices)
unique_indices.sort()
filter_transforms_by_indices(unique_indices)
transform_hashes = transform_hashes[unique_indices]
# sorted_transform_hashes = np.sort(transform_hashes, axis=None)
# unique_sorted_transform_hashes = np.zeros_like(sorted_transform_hashes, dtype=bool)
# unique_sorted_transform_hashes[1:] = sorted_transform_hashes[1:] == sorted_transform_hashes[:-1]
# unique_sorted_transform_hashes[0] = True
# # Alternative
# unique_transform_hashes = pd.Index(transform_hashes).duplicated('first')
# total_number_of_perturbations = number_of_transforms * number_perturbations_applied
# Get the shape of the passing perturbations
perturbation_shape = []
# num_zeros = 0
last_perturb_start = 0
for perturb in perturb_rotation1:
perturb_end = last_perturb_start + len(perturb)
perturbation_shape.append(unique_transform_hashes[last_perturb_start:perturb_end].sum())
# Use if removing zero counts...
# shape = unique_transform_hashes[last_perturb_start:perturb_end].sum()
# if shape:
# perturbation_shape.append(shape)
# else:
# num_zeros += 1
last_perturb_start = perturb_end
number_of_transforms = len(full_rotation1)
logger.info(f'After culling duplicated transforms, found {number_of_transforms} viable solutions')
num_zeros = perturbation_shape.count(0)
if num_zeros:
logger.info(f'A total of {num_zeros} original transformations had no unique perturbations')
# Could use if removing zero counts... but probably less clear than above
# pop_zero_index = perturbation_shape.index(0)
# while pop_zero_index != -1:
# perturbation_shape.pop(pop_zero_index)
# pop_zero_index = perturbation_shape.index(0)
return transform_hashes, perturbation_shape, n_perturbed_dof
def optimize_found_transformations_by_metrics() -> tuple[pd.DataFrame, list[int]]:
"""Perform a cycle of (optional) transformation perturbation, and then score and select those which are ranked
highest
Returns:
A tuple containing the DataFrame containing the selected metrics for selected Poses and the identifiers for
those Poses
# The mean value of the acquired metric for all found poses
"""
nonlocal poses_df, residues_df
nonlocal total_dof_perturbed
# total_dof_perturbed = sym_entry.total_dof
if any((sym_entry.number_dof_rotation, sym_entry.number_dof_translation)):
nonlocal rotation_steps, translation_perturb_steps
# Perform perturbations to the allowed degrees of freedom
# Modify the perturbation amount by half as the space is searched to completion
# Reduce rotation_step before as the original step size was already searched
rotation_steps = tuple(step * .5 for step in rotation_steps)
current_transformation_ids, number_of_perturbs_per_cluster, total_dof_perturbed = perturb_transformations()
# Sets number_perturbations_applied, number_of_transforms,
# full_rotation1, full_rotation2, full_int_tx1, full_int_tx2,
# full_optimal_ext_dof_shifts, full_ext_tx1, full_ext_tx2
# Reduce translation_perturb_steps after as the original step size was never searched
translation_perturb_steps = tuple(step * .5 for step in translation_perturb_steps)
# elif sym_entry.external_dof: # The DOF are not such that perturbation would be of much benefit
else:
raise NotImplementedError(f"Can't perturb external dof only quite yet")
# Perform optimization by means of optimal_tx
def minimize_translations():
""""""
logger.info(f'Optimizing transformations')
# The application of total_dof_perturbed might not be necessary as this optimizes fully
total_dof_perturbed = sym_entry.total_dof
# Remake the optimal shifts given each of the passing ghost fragment/surface fragment pairs
optimal_ext_dof_shifts = np.zeros((number_of_transforms, 3), dtype=float)
pose_residues = pose.residues
for idx in range(number_of_transforms):
update_pose_coords(idx)
add_fragments_to_pose()
passing_ghost_coords = []
passing_surf_coords = []
reference_rmsds = []
for entity_pair, fragment_info in pose.fragment_info_by_entity_pair:
ghost_frag = pose_residues[fragment_info.paired]
surf_frag = pose_residues[fragment_info.mapped]
passing_ghost_coords.append(ghost_frag.guide_coords)
passing_surf_coords.append(surf_frag.guide_coords)
reference_rmsds.append(ghost_frag.rmsd)
transform_passing_shifts = \
optimal_tx.solve_optimal_shifts(passing_ghost_coords, passing_surf_coords, reference_rmsds)
mean_transform = transform_passing_shifts.mean(axis=0)
# Inherent in minimize_translations() call due to DOF requirements of preceding else:
if sym_entry.unit_cell:
# Must take the optimal_ext_dof_shifts and multiply the column number by the corresponding row
# in the sym_entry.external_dof#
# optimal_ext_dof_shifts[0] scalar * sym_entry.group_external_dof[0] (1 row, 3 columns)
# Repeat for additional DOFs, then add all up within each row.
# For a single DOF, multiplication won't matter as only one matrix element will be available
#
# # Must find positive indices before external_dof1 multiplication in case negatives there
# positive_indices = \
# np.flatnonzero(np.all(transform_passing_shifts[:, :sym_entry.number_dof_external] >= 0, axis=1))
# number_passing_shifts = len(positive_indices)
optimal_ext_dof_shifts[idx, :sym_entry.number_dof_external] = \
mean_transform[:sym_entry.number_dof_external]
# Using the current transforms, create a hash to uniquely label them and apply to the indices
current_transformation_ids = create_transformation_hash()
minimize_translations()
# Todo
# enable precise metric acquisition
# dock_metrics = collect_dock_metrics(score_functions, proteinmpnn_score=job.dock.proteinmpnn_score)
poses_df, residues_df = collect_dock_metrics(proteinmpnn_score=job.dock.proteinmpnn_score)
weighted_trajectory_df: pd.DataFrame = prioritize_transforms_by_selection(poses_df)
weighted_trajectory_df_index = weighted_trajectory_df.index
if number_perturbations_applied > 1:
# Sort each perturbation cluster members by the prioritized metric
top_transform_cluster_indices: list[int] = []
perturb_passing_indices: list[list[int]] = []
# Used to progressively limit search as clusters deepen
if optimize_round == 1:
top_perturb_hits = total_dof_perturbed
logger.info(f'Selecting the top {top_perturb_hits} transformations from each perturbation')
else:
top_perturb_hits = 1
logger.info(f'Selecting the top transformation from each perturbation')
# # Round down the sqrt of the number_perturbations_applied
# top_perturb_hits = int(math.sqrt(number_perturbations_applied) + .5)
# top_perturb_hits = int(total_dof_perturbed/optimize_round + .5)
lower_perturb_idx = 0
for cluster_idx, number_of_perturbs in enumerate(number_of_perturbs_per_cluster):
if not number_of_perturbs:
# All perturbations were culled due to overlap
continue
# Set up the cluster range
upper_perturb_idx = lower_perturb_idx + number_of_perturbs
perturb_indices = list(range(lower_perturb_idx, upper_perturb_idx))
lower_perturb_idx = upper_perturb_idx
# Grab the cluster range indices
perturb_indexer = np.isin(weighted_trajectory_df_index, perturb_indices)
if perturb_indexer.any():
if optimize_round == 1:
# Slice the cluster range indices by the top hits
selected_perturb_indices = \
weighted_trajectory_df_index[perturb_indexer][:top_perturb_hits].tolist()
# if selected_perturb_indices:
# Save the top transform and the top X transforms from each cluster
top_transform_cluster_indices.append(selected_perturb_indices[0])
perturb_passing_indices.append(selected_perturb_indices)
else: # Just grab the top hit
top_transform_cluster_indices.append(weighted_trajectory_df_index[perturb_indexer][0])
else: # Update that no perturb_indices present after filter
number_of_perturbs_per_cluster[cluster_idx] = 0
# perturb_passing_indices.append([])
if optimize_round == 1:
nonlocal round1_cluster_shape
round1_cluster_shape = [len(indices) for indices in perturb_passing_indices]
elif optimize_round == 2:
# This is only required if perturb_passing_indices.append([]) is used above
# Adjust the shape to account for any perturbations that were culled due to overlap
cluster_start = 0
for cluster_idx, cluster_shape in enumerate(round1_cluster_shape):
cluster_end = cluster_start + cluster_shape
for number_of_perturbs in number_of_perturbs_per_cluster[cluster_start:cluster_end]:
if not number_of_perturbs:
# All perturbations were culled due to overlap
cluster_shape -= 1
# for indices in perturb_passing_indices[cluster_start:cluster_end]:
# if not indices:
# cluster_shape -= 1
# Set the cluster shape with the results of the perturbation trials
round1_cluster_shape[cluster_idx] = cluster_shape
cluster_start = cluster_end
number_top_indices = len(top_transform_cluster_indices) # sum(round1_cluster_shape)
round1_number_of_clusters = len(round1_cluster_shape)
logger.info(f'Reducing round 1 expanded cluster search from {number_top_indices} '
f'to {round1_number_of_clusters} transformations')
top_scores_s = weighted_trajectory_df.loc[top_transform_cluster_indices,
metrics.selection_weight_column]
# Filter down to the size of the original transforms from the cluster expansion
top_index_of_cluster = []
cluster_lower_bound = 0
for cluster_idx, cluster_shape in enumerate(round1_cluster_shape):
if cluster_shape > 0:
cluster_upper_bound = cluster_lower_bound + cluster_shape
top_cluster_score = top_scores_s.iloc[cluster_lower_bound:cluster_upper_bound].argmax()
top_index_of_cluster.append(cluster_lower_bound + top_cluster_score)
# Set new lower bound
cluster_lower_bound = cluster_upper_bound
top_transform_cluster_indices = [top_transform_cluster_indices[idx] for idx in top_index_of_cluster]
else:
top_transform_cluster_indices = list(range(number_of_transforms))
# # Finally take from each of the top perturbation "kernels"
# # With each additional optimize_round, there is exponential increase in the number of transforms
# # unless there is filtering to take the top
# # Taking the sqrt (or equivalent function), needs to be incremented for each perturbation increase
# # so this doesn't get out of hand as the amount grows
# # For example, iteration 1 gets no sqrt, iteration 2 gets sqrt, 3 gets cube root
# # root_to_take = 1/iteration
# top_cluster_root_to_take = 1 / optimize_round
# # Take the metric at each of the top positions and sort these
# top_cluster_hits = int((number_of_transform_clusters**top_cluster_root_to_take) + .5)
# Operation NOTE:
# During the perturbation selection, this is the sqrt of the number_of_transform_clusters
# So, from 18 transforms (idx=1), expanded to 81 dof perturbs (idx=2), to get 1458 possible,
# 1392 didn't clash. 4 top_cluster_hits were selected and 9 transforms from each.
# This is a lot of sampling for the other 14 that were never chosen. They might've
# not been discovered without the perturb since the top score came from one of the 81
# possible perturb transforms
# if optimize_round > 1:
# cluster_divisor = (total_dof_perturbed / (optimize_round-1))
# else:
# cluster_divisor = total_dof_perturbed
# Divide the clusters by the total applied degrees of freedom
# top_cluster_hits = int(number_of_transform_clusters/cluster_divisor + .5)
#
# # Grab the cluster range indices
# cluster_representative_indexer = np.isin(weighted_trajectory_s.index, top_transform_cluster_indices)
# selected_cluster_indices = \
# weighted_trajectory_s[cluster_representative_indexer][:top_cluster_hits].index.tolist()
# # selected_cluster_hits = top_transform_cluster_indices[:top_cluster_overall_hits]
# # # Use .loc here as we have a list used to index...
# # selected_cluster_indices = weighted_trajectory_s.loc[selected_cluster_hits].index.tolist()
# # Grab the top cluster indices in the order of the weighted_trajectory_df
# cluster_representative_indexer = np.isin(weighted_trajectory_df_index, top_transform_cluster_indices)
# selected_cluster_indices = weighted_trajectory_df_index[cluster_representative_indexer].tolist()
# if number_perturbations_applied > 1 and optimize_round == 1:
# # For each of the top perturbation clusters, add all the indices picked from the above logic
# selected_indices = []
# for selected_idx in selected_cluster_indices:
# reference_idx = top_transform_cluster_indices.index(selected_idx)
# selected_indices.extend(perturb_passing_indices[reference_idx])
# else:
# selected_indices = selected_cluster_indices
# Grab the top cluster indices in the transformation order, not selected order
if number_perturbations_applied > 1 and optimize_round == 1:
# For each of the top perturbation clusters, add all the indices picked from the above logic
selected_indices = []
for cluster_idx, top_index in enumerate(top_transform_cluster_indices):
selected_indices.extend(perturb_passing_indices[cluster_idx])
else:
selected_indices = top_transform_cluster_indices
# Handle results
# # Using the current transforms, create a hash to uniquely label them and apply to the indices
# current_transformation_ids = create_transformation_hash()
# Filter hits down in the order of the selected indices (i.e. transformation order)
filter_transforms_by_indices(selected_indices)
# Narrow down the metrics by the selected_indices. If this is the last cycle, they will be written
poses_df = poses_df.loc[selected_indices]
residues_df = residues_df.loc[selected_indices]
# Reset the DataFrame.index given new ranking
poses_df.index = residues_df.index = pd.RangeIndex(len(selected_indices))
# Get the transformations that were selected ready to return
# selected_transformation_ids = weighted_trajectory_df_index[selected_indices].tolist()
selected_transformation_ids = [current_transformation_ids[idx] for idx in selected_indices]
# Filter weighted_trajectory_df by selected_indices, and update the .index with ordered transformation_ids
selected_indexer = np.isin(weighted_trajectory_df_index, selected_indices)
weighted_trajectory_df.index = pd.Index(current_transformation_ids[weighted_trajectory_df_index])
weighted_trajectory_df = weighted_trajectory_df[selected_indexer]
# Todo?
# Was returning this version of weighted_trajectory_df, but was only using the selected_transformation_ids
# So, now just returning selected_indices
# # Filter down the current_transformation_ids by the indices passing job.dock.filter and update the .index
# weighted_current_transforms = current_transformation_ids[weighted_trajectory_df_index]
# weighted_trajectory_df.index = pd.Index(weighted_current_transforms)
# Todo use append_total_results() for global search? Was removed over memory considerations
# # Add the weighted_trajectory_df to the total_results_df to keep global results
# append_total_results(weighted_trajectory_df)
return weighted_trajectory_df, selected_transformation_ids
def create_transformation_hash() -> np.ndarray:
"""Using the currently available transformation parameters for the two Model instances, create the
transformation hash to describe the orientation of the second model in relation to the first. This hash will be
unique over the sampling space when discrete differences exceed the TransformHasher.rotation_bin_width and
.translation_bin_width
Returns:
An integer hashing the currently active transforms to distinct orientational offset in the described space
"""
# Needs to be completed outside of individual naming function as it is stacked
# transforms = create_transformation_group()
# input(f'len(transforms[0]["rotation"]): {len(transforms[0]["rotation"])}')
guide_coordinates_model1, guide_coordinates_model2 = \
cluster.apply_transform_groups_to_guide_coordinates(*create_transformation_group())
# input(guide_coordinates_model1[:3])
rotations = [None for _ in range(len(guide_coordinates_model1))]
# input(len(rotations))
translations = rotations.copy()
# Only turn the outermost array into a list. Keep the guide coordinate 3x3 arrays as arrays for superposition3d
for transform_idx, (guide_coord2, guide_coord1) in enumerate(
zip(list(guide_coordinates_model2), list(guide_coordinates_model1))):
# Reverse the orientation so that the rot, tx indicate the movement of guide_coord1 onto guide_coord2
rmsd, rot, tx = superposition3d(guide_coord2, guide_coord1)
rotations[transform_idx] = rot # rotations.append(rot)
translations[transform_idx] = tx # translations.append(tx)
# logger.debug(f'before rotations[:3]: {rotations[:3]}')
# logger.debug(f'before translations[:3]: {translations[:3]}')
hashed_transforms = model_transform_hasher.transforms_to_hash(rotations, np.array(translations))
# rotations, translations = model_transform_hasher.hash_to_transforms(hashed_transforms)
# logger.debug(f'after rotations[:3]: {rotations[:3]}')
# logger.debug(f'after translations[:3]: {translations[:3]}')
return hashed_transforms
def prioritize_transforms_by_selection(df: pd.DataFrame) -> pd.DataFrame:
"""Using the active transformations, measure the Pose metrics and filter/weight according to defaults/provided
parameters
Args:
df: The Pose DataFrame which should be prioritized from docking
Returns:
The DataFrame that has been sorted according to the specified filters/weights
"""
weighted_df = metrics.prioritize_design_indices(
df, filters=job.dock.filter, weights=job.dock.weight, default_weight=default_weight_metric)
# Set the metrics_of_interest to the default weighting metric name as well as any weights that are specified
metrics_of_interest = [metrics.selection_weight_column]
if job.dock.weight:
metrics_of_interest += list(job.dock.weight.keys())
# weighted_df = weighted_trajectory_df.loc[:, list(job.dock.weight.keys())]
# else:
# weighted_df = weighted_trajectory_df.loc[:, metrics.selection_weight_column]
weighted_df = weighted_df.loc[:, metrics_of_interest]
# weighted_trajectory_s = metrics.pareto_optimize_trajectories(poses_df, weights=job.dock.weight,
# default_sort=default_weight_metric)
# weighted_trajectory_s is sorted with best transform in index 0, regardless of whether it is ascending or not
return weighted_df
# Todo use append_total_results() for global search
# def append_total_results(additional_trajectory_df: pd.DataFrame) -> pd.DataFrame:
# """Combine existing metrics with the new metrics
# Args:
# additional_trajectory_df: The additional DataFrame to add to the existing global metrics
# Returns:
# The full global metrics DataFrame
# """
# nonlocal total_results_df
# # Add new metrics to existing and keep the newly added if there are overlap
# total_results_df = pd.concat([total_results_df, additional_trajectory_df], axis=0)
# total_results_df = total_results_df[~total_results_df.index.duplicated(keep='last')]
# return total_results_df
# def calculate_results_for_stopping(target_df: pd.DataFrame, indices: list[int | str]) -> float | pd.Series:
# """Given a DataFrame with metrics from a round of optimization, calculate the optimization results to report on whether a stopping condition has been met
#
# Args:
# target_df: The DataFrame that resulted from the most recent optimization
# indices: The indices which have been selected from the target_df
# Returns:
# The resulting values from the DataFrame based on the target metrics
# """
# # Find the value of the new metrics in relation to the old to calculate the result from optimization
# if job.dock.weight:
# selected_columns = list(job.dock.weight.keys())
# else:
# selected_columns = metrics.selection_weight_column
#
# selected_metrics_df = target_df.loc[indices, selected_columns]
# # other_metrics_df = selected_metrics_df.drop(indices)
# # Find the difference between the selected and the other
# return selected_metrics_df.mean(axis=0)
# # other_metrics_df.mean(axis=1)
# Initialize output DataFrames
# Todo
# enable precise metric acquisition
# dock_metrics = collect_dock_metrics(score_functions, proteinmpnn_score=job.dock.proteinmpnn_score)
poses_df, residues_df = collect_dock_metrics(proteinmpnn_score=job.dock.proteinmpnn_score)
weighted_trajectory_df = prioritize_transforms_by_selection(poses_df)
# # Get selected indices (sorted in original order)
# selected_indices = weighted_trajectory_df.index.sort_values().tolist()
# Get selected indices (sorted in weighted_trajectory_df order)
selected_indices = weighted_trajectory_df.index.tolist()
# Filter/sort transforms and metrics by the selected_indices
filter_transforms_by_indices(selected_indices)
poses_df = poses_df.loc[selected_indices]
residues_df = residues_df.loc[selected_indices]
# Get the hash of the current transforms
passing_transform_ids = create_transformation_hash()
weighted_trajectory_df.index = pd.Index(passing_transform_ids)
# -----------------------------------------------------------------------------------------------------------------
# Below creates perturbations to sampled transformations and iteratively optimizes scores the resulting Pose
# -----------------------------------------------------------------------------------------------------------------
# Set nonlocal perturbation/metric variables that are used in optimize_found_transformations_by_metrics()
number_of_transforms = number_of_original_transforms = len(full_rotation1)
number_perturbations_applied = 1
if job.dock.perturb_dof:
# Set the weighted_trajectory_df as total_results_df to keep a record of global results
# total_results_df = poses_df <- this contains all filtered results too
total_results_df = weighted_trajectory_df
# Initialize docking score search
round1_cluster_shape = []
total_dof_perturbed = 1
optimize_round = 0
if job.dock.weight:
selected_columns = list(job.dock.weight.keys())
else:
selected_columns = [metrics.selection_weight_column]
result = total_results_df.loc[:, selected_columns].mean(axis=0)
# result = calculate_results_for_stopping(total_results_df, passing_transform_ids)
# threshold = 0.05 # 0.1 <- not convergent # 1 <- too lenient with pareto_optimize_trajectories
threshold_percent = 0.05
if isinstance(result, float):
def result_func(result_): return result_
thresholds = result * threshold_percent
last_result = 0.
else: # pd.Series
def result_func(result_): return result_.values
result = result_func(result)
thresholds = tuple(result * threshold_percent)
last_result = tuple(0. for _ in thresholds)
# The condition sum(translation_perturb_steps) < 0.1 is True after 4 optimization rounds...
# To ensure that the abs doesn't produce worse values, need to compare results in an unbounded scale
# (i.e. not between 0-1), which also indicates using a global scale. This way, iteration can tell if they are
# better. It is a feature of perturb_transformations() that the same transformation is always included in grid
# search at the moment, so the routine should never arrive at worse scores...
# Everything below could really be expedited with a Bayseian optimization search strategy
# while sum(translation_perturb_steps) > 0.1 and all(tuple(abs(last_result - result) > thresholds)):
# Todo the tuple(abs(last_result - result) > thresholds)) with a float won't convert to an iterable
logger.info(f'Starting {optimize_found_transformations_by_metrics.__name__} of {number_of_transforms} '
f'transformations with starting optimize target={result}')
while (optimize_round < 2 or all(tuple(abs(last_result - result) > thresholds))) \
and sum(translation_perturb_steps) > 0.1: # model_transform_hasher.translation_bin_width:
# and sum(rotation_steps) > model_transform_hasher.rotation_bin_width:
optimize_round += 1
logger.info(f'{optimize_found_transformations_by_metrics.__name__} round {optimize_round}')
last_result = result
# Perform scoring and a possible iteration of dock perturbation
weighted_trajectory_df, passing_transform_ids = optimize_found_transformations_by_metrics()
# IMPORTANT:
# - weighted_trajectory_df.index is in the order of the job.dock.weight/default_selection_weight
# - passing_transform_ids are in the order of the current transformations
# - De-duplication of overlapping passing_transform_ids (in case of TransformHasher resolution collapse) is
# handled after while loop
top_results_df = weighted_trajectory_df.loc[:, selected_columns]
# PREVIOUSLY, when weighted_trajectory_df contained all measurements passing filters from
# optimize_found_transformations_by_metrics()
# - If the last optimize_found_transformations_by_metrics() sampled from a TransformHasher with no
# resolvable bins (highest resolution reached), all the weighted_trajectory_df.index have the same
# transformation id and loc/selection of them provides every match, not just passing_transform_ids
# prescribed by optimization
# - Mean value DOES NOT accurately reflect dataset, given the issue with selection of the same index...
# top_results_df = weighted_trajectory_df.loc[passing_transform_ids, selected_columns]
# Todo? Could also index the
# top_results_df = total_results_df.loc[passing_transform_ids, selected_columns]
result = result_func(top_results_df.mean(axis=0))
# result = calculate_results_for_stopping(total_results_df, passing_transform_ids)
number_of_transforms = len(full_rotation1)
logger.info(f'Found {number_of_transforms} transformations after '
f'{optimize_found_transformations_by_metrics.__name__} round {optimize_round} '
f'with result={result}. last_result={last_result}')
else:
if optimize_round == 1:
number_top_indices = number_of_transforms
round1_number_of_clusters = len(round1_cluster_shape)
logger.info(f'Reducing round 1 expanded cluster search from {number_top_indices} '
f'to {round1_number_of_clusters} transformations')
# Reduce the top_transform_cluster_indices to the best remaining in each optimize_round 1 cluster
# Important that indexing happens by the passing_transform_ids order, as these are in the order of
# the transformation search, while the weighted_trajectory_df is in the order of the job.dock.weight
top_scores_s = weighted_trajectory_df.loc[passing_transform_ids, metrics.selection_weight_column]
# Todo? Could also use
# top_scores_s = total_results_df.loc[passing_transform_ids, metrics.selection_weight_column].mean(
# axis=0)
# top_indices_of_cluster = []
# for i in range(round1_number_of_clusters):
# # Slice by the expanded cluster amount
# cluster_lower_bound = total_dof_perturbed * i
# top_cluster_score = top_scores_s.iloc[cluster_lower_bound:
# cluster_lower_bound + total_dof_perturbed].argmax()
# top_indices_of_cluster.append(cluster_lower_bound + top_cluster_score)
# Filter down to the size of the original transforms from the cluster expansion
top_indices_of_cluster = []
cluster_lower_bound = 0
for cluster_idx, cluster_shape in enumerate(round1_cluster_shape):
# This can't be less than 1 here...
# if cluster_shape > 0:
cluster_upper_bound = cluster_lower_bound + cluster_shape
top_cluster_score = top_scores_s.iloc[cluster_lower_bound:cluster_upper_bound].argmax()
top_indices_of_cluster.append(cluster_lower_bound + top_cluster_score)
# Set new lower bound
cluster_lower_bound = cluster_upper_bound
passing_transform_ids = top_scores_s.iloc[top_indices_of_cluster].index.tolist()
# Filter hits down
filter_transforms_by_indices(top_indices_of_cluster)
# Narrow down the metrics by the selected_indices. If this is the last cycle, they will be written
poses_df = poses_df.loc[top_indices_of_cluster]
residues_df = residues_df.loc[top_indices_of_cluster]
# Reset the DataFrame.index
poses_df.index = residues_df.index = pd.RangeIndex(len(top_indices_of_cluster))
number_of_transforms = len(passing_transform_ids)
# Grab the passing_transform_ids according to the order of provided job.dock.weight, which follows the
# weighted_trajectory_df.index order, not transformations
# This sorts the final optimized indices and transfers this order to the pose outputs
# (current transform pool, poses_df, residues_df)
weighted_transform_ids = weighted_trajectory_df.index.tolist()
ordered_indices = [passing_transform_ids.index(_id) for _id in weighted_transform_ids]
# This calculation was used when weighted_trajectory_df contains extra measurements that aren't
# from passing_transform_ids. NOW all weighted_trajectory_df.index are passing_transform_ids
# passing_transform_indexer = np.isin(weighted_trajectory_df.index, passing_transform_ids)
# weighted_transform_ids = weighted_trajectory_df.index[passing_transform_indexer].tolist()
# # Check if there are degenerate transformation_hashes due to loss of resolution with the transform hasher
# if model_transform_hasher.translation_bin_width > sum(translation_perturb_steps) or \
# model_transform_hasher.rotation_bin_width > sum(rotation_steps):
# logger.info(f"Can't accurately describe each transformation due to resolution of the "
# f"{model_transform_hasher.__class__.__name__}")
# # Take the "set" of them
# ordered_indices = utils.remove_duplicates(ordered_indices)
# Reorder hits and metrics by the ordered_indices
filter_transforms_by_indices(ordered_indices)
poses_df = poses_df.loc[ordered_indices]
residues_df = residues_df.loc[ordered_indices]
logger.info(f'Optimization complete, with {number_of_transforms} final transformations')
# Set the passing transformation identifiers as the trajectory metrics index
# These should all be the same order as w/ or w/o optimize_found_transformations_by_metrics() the order of
# passing_transform_ids is fetched from the order of the selected_indices and each _df is sorted accordingly
passing_index = pd.Index([f'{identifier:d}' for identifier in passing_transform_ids],
name=sql.PoseMetrics.pose_id.name)
starting_transforms = len(passing_index)
# Deduplicate the indices by keeping the first instance
# The above sorting ensures that the first instance is the "best"
deduplicated_indices = ~passing_index.duplicated(keep='first')
# Filter data structures
passing_index = passing_index[deduplicated_indices]
poses_df = poses_df[deduplicated_indices]
residues_df = residues_df[deduplicated_indices]
filter_transforms_by_indices(np.flatnonzero(deduplicated_indices))
number_of_transforms = len(passing_index)
if starting_transforms != number_of_transforms:
logger.info(f'Removed {starting_transforms - number_of_transforms} due to transformation duplication')
# Finally, tabulate the ProteinMPNN metrics if they weren't already and are requested
if job.dock.proteinmpnn_score:
pass # ProteinMPNN metrics already collected
elif job.use_proteinmpnn: # Collect
logger.info(f'Measuring quality of docked interfaces with ProteinMPNN unconditional probabilities')
poses_df, residues_df = collect_dock_metrics(proteinmpnn_score=True)
poses_df.index = residues_df.index = passing_index
pose_names = poses_df.index.tolist()
def terminate(poses_df: pd.DataFrame, residues_df: pd.DataFrame) -> list[PoseJob]:
"""Finalize any remaining work and return to the caller"""
# Extract transformation parameters for output
def populate_pose_metadata():
"""Add all required PoseJob information to output the created Pose instances for persistent storage"""
# Save all pose transformation information
# From here out, the transforms used should be only those of interest for outputting/sequence design
# filter_transforms_by_indices() <- This is done above
# Format pose transformations for output
rotations1 = scipy.spatial.transform.Rotation.from_matrix(full_rotation1)
rotations2 = scipy.spatial.transform.Rotation.from_matrix(full_rotation2)
# Get all rotations in terms of the degree of rotation along the z-axis
# Using the x, y rotation to enforce the degeneracy matrix...
rotation_degrees_x1, rotation_degrees_y1, rotation_degrees_z1 = \
zip(*rotations1.as_rotvec(degrees=True).tolist())
rotation_degrees_x2, rotation_degrees_y2, rotation_degrees_z2 = \
zip(*rotations2.as_rotvec(degrees=True).tolist())
blank_parameter = list(repeat(None, number_of_transforms))
if sym_entry.is_internal_tx1:
nonlocal full_int_tx1
if len(full_int_tx1) > 1:
full_int_tx1 = full_int_tx1.squeeze()
z_heights1 = full_int_tx1[:, -1]
else:
z_heights1 = blank_parameter
if sym_entry.is_internal_tx2:
nonlocal full_int_tx2
if len(full_int_tx2) > 1:
full_int_tx2 = full_int_tx2.squeeze()
z_heights2 = full_int_tx2[:, -1]
else:
z_heights2 = blank_parameter
set_mat1_number, set_mat2_number, *_extra = sym_entry.setting_matrices_numbers
blank_parameters = list(repeat([None, None, None], number_of_transforms))
# if sym_entry.unit_cell:
# full_uc_dimensions = full_uc_dimensions[passing_symmetric_clash_indices_perturb]
# full_ext_tx1 = full_ext_tx1[:]
# full_ext_tx2 = full_ext_tx2[:]
# full_ext_tx_sum = full_ext_tx2 - full_ext_tx1
_full_ext_tx1 = blank_parameters if full_ext_tx1 is None else full_ext_tx1.squeeze()
_full_ext_tx2 = blank_parameters if full_ext_tx2 is None else full_ext_tx2.squeeze()
for idx, pose_job in enumerate(pose_jobs):
# Update the sql.EntityData with transformations
external_translation_x1, external_translation_y1, external_translation_z1 = _full_ext_tx1[idx]
external_translation_x2, external_translation_y2, external_translation_z2 = _full_ext_tx2[idx]
entity_transformations = [
dict(
rotation_x=rotation_degrees_x1[idx],
rotation_y=rotation_degrees_y1[idx],
rotation_z=rotation_degrees_z1[idx],
internal_translation_z=z_heights1[idx],
setting_matrix=set_mat1_number,
external_translation_x=external_translation_x1,
external_translation_y=external_translation_y1,
external_translation_z=external_translation_z1),
dict(
rotation_x=rotation_degrees_x2[idx],
rotation_y=rotation_degrees_y2[idx],
rotation_z=rotation_degrees_z2[idx],
internal_translation_z=z_heights2[idx],
setting_matrix=set_mat2_number,
external_translation_x=external_translation_x2,
external_translation_y=external_translation_y2,
external_translation_z=external_translation_z2)
]
# Update sql.EntityData, sql.EntityMetrics, sql.EntityTransform
# pose_id = pose_job.id
# entity_data = []
# Todo the number of entities and the number of transformations could be different
entity_transforms = []
for entity, transform in zip(pose.entities, entity_transformations):
transformation = sql.EntityTransform(**transform)
entity_transforms.append(transformation)
pose_job.entity_data.append(sql.EntityData(
meta=entity.metadata,
# metrics=entity.metrics,
transform=transformation)
)
# print('pose_job.entity_data', pose_job.entity_data)
# For whatever reason, this ^ print wouldn't print anything when above was written as:
# entity_data.append(sql.EntityData(pose=pose_job,
# And this warning occurred
# /home/kylemeador/symdesign/symdesign/protocols/fragdock.py:4394:
# SAWarning: Object of type <EntityData> not in session, add operation along 'EntityMetrics.entity'
# won't proceed
# It would print 4 objects, (2 of each EntityData) when this was written as:
# pose_job.entity_data.append(sql.EntityData(pose=pose_job,
session.add_all(entity_transforms) # + entity_data)
# # Need to generate the EntityData.id
# session.flush()
pose_jobs = [PoseJob.from_name(pose_name, project=project, protocol=protocol_name)
for pose_name in pose_names]
# Format output data, fix existing entries
# Populate the database with pose information. Has access to nonlocal session
populate_pose_metadata()
# Next, insert metadata information to database
pose_jobs = insert_pose_jobs(session, pose_jobs, project)
# For all new PoseJobs, insert them and their metrics into the database
remaining_pose_idx_name_pairs = []
for pose_job in pose_jobs:
name = pose_job.name
pose_name_index = pose_names.index(name)
if pose_name_index != -1:
remaining_pose_idx_name_pairs.append((pose_name_index, name))
# Finally, sort all the names to ensure that the indices from the first pass are accurate
# with the new set
remaining_indices, remaining_pose_names = zip(
*sorted(remaining_pose_idx_name_pairs, key=lambda name: name[0]))
# Select poses_df/residues_df by remaining remaining_pose_names
poses_df = poses_df.loc[remaining_pose_names, :]
residues_df = residues_df.loc[remaining_pose_names, :]
logger.debug(f'Reset the pose solutions with attributes:\n'
f'\tpose names={remaining_pose_names}\n'
f'\texisting transform indices={remaining_indices}\n')
# number_of_transforms = len(remaining_pose_names)
filter_transforms_by_indices(list(remaining_indices))
# trajectory = TrajectoryMetadata(poses=pose_jobs, protocol=protocol)
# session.add(trajectory)
if job.db:
pose_ids = [pose_job.id for pose_job in pose_jobs]
else:
pose_ids = remaining_pose_names
if job.use_proteinmpnn:
# Explicitly set false as scoring the wild-type sequence isn't desired now
reset_use_proteinmpnn = True
job.use_proteinmpnn = False
else:
reset_use_proteinmpnn = False
for idx, pose_job in enumerate(pose_jobs):
# Add the next set of coordinates
update_pose_coords(idx)
if job.output_fragments:
add_fragments_to_pose()
if job.output_trajectory:
available_chain_ids = chain_id_generator()
def _exhaust_n_chain_ids(n: int) -> str:
for _ in range(n - 1):
next(available_chain_ids)
return next(available_chain_ids)
with open(os.path.join(project_dir, 'trajectory_oligomeric_models.pdb'), 'a') as f_traj:
# pose.write(file_handle=f_traj, assembly=True)
f_traj.write('{:9s}{:>4d}\n'.format('MODEL', idx + 1))
model_specific_chain_id = next(available_chain_ids)
for entity in pose.entities:
starting_chain_id = entity.chain_id
entity.chain_id = model_specific_chain_id
entity.write(file_handle=f_traj, assembly=True)
entity.chain_id = starting_chain_id
model_specific_chain_id = _exhaust_n_chain_ids(entity.number_of_symmetry_mates)
f_traj.write(f'ENDMDL\n')
# Set the ASU, then write to a file
pose.set_contacting_asu(distance=cb_distance)
try: # Remove existing cryst_record
del pose._cryst_record
except AttributeError:
pass
# pose.uc_dimensions
# if sym_entry.unit_cell: # 2, 3 dimensions
# cryst_record = generate_cryst1_record(full_uc_dimensions[idx], sym_entry.resulting_symmetry)
# else:
# cryst_record = None
pose_job.pose = pose
pose_job.calculate_pose_design_metrics(session)
putils.make_path(pose_job.pose_directory)
pose_job.output_pose()
pose_job.source_path = pose_job.pose_path
pose_job.pose = None
logger.info(f'OUTPUT POSE: {pose_job.pose_directory}')
# Add metrics to the PoseJob then reset for next pose
for entity, data in zip(pose.entities, pose_job.entity_data):
data.metrics = entity.metrics
entity.clear_metrics()
# Output acquired metrics
if job.db:
# Update the poses_df and residues_df index to reflect the new pose_ids
poses_df.index = pd.Index(pose_ids, name=sql.PoseMetrics.pose_id.name)
# Write dataframes to the sql database
sql.write_dataframe(session, poses=poses_df)
output_residues = False
if output_residues: # Todo job.metrics.residues
residues_df.index = pd.Index(pose_ids, name=sql.PoseResidueMetrics.pose_id.name)
sql.write_dataframe(session, pose_residues=residues_df)
else: # Write to disk
residues_df.sort_index(level=0, axis=1, inplace=True, sort_remaining=False) # ascending=False
putils.make_path(job.all_scores)
residue_metrics_csv = os.path.join(job.all_scores, f'{building_blocks}_docked_poses_Residues.csv')
residues_df.to_csv(residue_metrics_csv)
logger.info(f'Wrote residue metrics to {residue_metrics_csv}')
trajectory_metrics_csv = \
os.path.join(job.all_scores, f'{building_blocks}_docked_poses_Trajectories.csv')
job.dataframe = trajectory_metrics_csv
poses_df = pd.concat([poses_df], keys=[('dock', 'pose')], axis=1)
poses_df.columns = poses_df.columns.swaplevel(0, 1)
poses_df.sort_index(level=2, axis=1, inplace=True, sort_remaining=False)
poses_df.sort_index(level=1, axis=1, inplace=True, sort_remaining=False)
poses_df.sort_index(level=0, axis=1, inplace=True, sort_remaining=False)
poses_df.to_csv(trajectory_metrics_csv)
logger.info(f'Wrote trajectory metrics to {trajectory_metrics_csv}')
# After pose output loop
# Explicitly set false as scoring the wildtype sequence isn't desired now
if reset_use_proteinmpnn:
job.use_proteinmpnn = True
# # Todo 2 modernize with the new SQL database and 6D transform aspirations
# # Cluster by perturbation if perturb_dof:
# if number_perturbations_applied > 1:
# perturbation_identifier = '-p_'
# cluster_type_str = 'ByPerturbation'
# seed_transforms = utils.remove_duplicates(
# [pose_name.split(perturbation_identifier)[0] for pose_name in remaining_pose_names])
# cluster_map = {seed_transform: remaining_pose_names[idx * number_perturbations_applied:
# (idx + 1) * number_perturbations_applied]
# for idx, seed_transform in enumerate(seed_transforms)}
# # for pose_name in remaining_pose_names:
# # seed_transform, *perturbation = pose_name.split(perturbation_identifier)
# # clustered_transformations[seed_transform].append(pose_name)
#
# # Set the number of poses to cluster equal to the sqrt of the search area
# job.cluster.number = math.sqrt(number_perturbations_applied)
# else:
# cluster_type_str = 'ByTransformation'
# cluster_map = cluster.cluster_by_transformations(*create_transformation_group(),
# values=project_pose_names)
# # Output clustering results
# job.cluster.map = utils.pickle_object(cluster_map,
# name=putils.default_clustered_pose_file.format('', cluster_type_str),
# out_path=project_dir)
# logger.info(f'Found {len(cluster_map)} unique clusters from {len(remaining_pose_names)} pose inputs. '
# f'Wrote cluster map to {job.cluster.map}')
return pose_jobs
# Clean up, save data/output results
with job.db.session(expire_on_commit=False) as session:
pose_jobs = terminate(poses_df, residues_df)
session.commit()
metrics_stmt = select(PoseJob).where(PoseJob.id.in_([pose_job.id for pose_job in pose_jobs])) \
.execution_options(populate_existing=True) \
.options(selectinload(PoseJob.metrics))
pose_jobs = session.scalars(metrics_stmt).all()
logger.info(f'Total {building_blocks} dock trajectory took {time.time() - frag_dock_time_start:.2f}s')
return pose_jobs
|