File size: 121,083 Bytes
83a9309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
import os
import sys
import json
import yaml
import argparse
import logging
import math # For JS Div log (if enabled later)
import numpy as np
from typing import List, Dict, Optional, Tuple
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
import h5py  # for final outputs
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from torch_geometric.nn import ChebConv
from torch_cluster import knn_graph
from sklearn.model_selection import train_test_split

#################################################################
# Argument Parsing & Config Loading
#################################################################
parser = argparse.ArgumentParser(description="Protein Reconstruction with Pretrained HNO & Two-Step Decoders + Optional Dihedral Losses + Diffusion Override")
parser.add_argument("--config", type=str, required=True, help="Path to YAML configuration file.")
parser.add_argument("--debug", action="store_true", help="Enable debug logging.")

# --- ADDED: Diffusion Override Arguments ---
parser.add_argument("--use_diffusion", action="store_true",
                    help="If set, attempt to override the internal pooling with diffused embeddings at final export.")
parser.add_argument("--diffused_backbone_h5", type=str, default=None,
                    help="Path to HDF5 with diffused backbone embeddings (e.g., dataset='generated_diffusion').")
parser.add_argument("--diffused_sidechain_h5", type=str, default=None,
                    help="Path to HDF5 with diffused sidechain embeddings (e.g., dataset='generated_diffusion').")
# --- END ADDED ---

args = parser.parse_args()

try:
    with open(args.config, "r") as f:
        config = yaml.safe_load(f)
except FileNotFoundError:
    print(f"ERROR: Configuration file not found at {args.config}")
    sys.exit(1)
except yaml.YAMLError as e:
    print(f"ERROR: Could not parse configuration file {args.config}: {e}")
    sys.exit(1)

#################################################################
# Logging Setup
#################################################################
use_debug = config.get("use_debug_logs", False) or args.debug
log_file = config.get("log_file", "logfile.log")

# Setup logger instance
logger = logging.getLogger("ProteinReconstruction")
logger.setLevel(logging.DEBUG if use_debug else logging.INFO)

# Prevent duplicate handlers if logger is accessed multiple times
if not logger.handlers:
    # File Handler
    try:
        fh = logging.FileHandler(log_file, mode="w")
        fh.setLevel(logging.DEBUG if use_debug else logging.INFO)
        formatter = logging.Formatter("[%(levelname)s] %(asctime)s - %(name)s - %(message)s")
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    except IOError as e:
        print(f"Warning: Could not write to log file {log_file}: {e}. Logging to console only.")

    # Console Handler
    ch = logging.StreamHandler(sys.stdout)
    ch.setLevel(logging.DEBUG if use_debug else logging.INFO)
    # Use a simpler format for console if desired
    # console_formatter = logging.Formatter("[%(levelname)s] %(message)s")
    # ch.setFormatter(console_formatter)
    if 'formatter' not in locals(): # Define formatter if file handler failed
         formatter = logging.Formatter("[%(levelname)s] %(asctime)s - %(name)s - %(message)s")
    ch.setFormatter(formatter)
    logger.addHandler(ch)

logger.info("Logger initialized.")
if use_debug:
    logger.debug("Debug mode is ON.")
else:
    logger.info("Debug mode is OFF.")

#################################################################
# Device Setup
#################################################################
force_cpu = config.get("force_cpu", False)
if force_cpu:
     device_name = "cpu"
elif torch.cuda.is_available():
     # Allow specifying CUDA device index in config, e.g., cuda_device: 0
     cuda_device_index = config.get("cuda_device", 0)
     device_name = f"cuda:{cuda_device_index}"
     try:
         # Test if the specified device is valid
         torch.cuda.get_device_name(cuda_device_index)
     except (AssertionError, RuntimeError) as e:
         logger.warning(f"Specified CUDA device {cuda_device_index} not available or invalid: {e}. Falling back to CPU.")
         device_name = "cpu"
else:
     device_name = "cpu"
device = torch.device(device_name)
logger.info(f"Using device: {device}")

#################################################################
# Utility: Checkpoint Save/Load
#################################################################
def save_checkpoint(state: Dict, filename: str, logger: logging.Logger):
    """Saves model and optimizer state dict."""
    try:
        torch.save(state, filename)
        logger.debug(f"Checkpoint saved to {filename}")
    except IOError as e:
        logger.error(f"Error saving checkpoint to {filename}: {e}")
    sys.stdout.flush()

def load_checkpoint(model: nn.Module,
                    optimizer: Optional[torch.optim.Optimizer],
                    filename: str,
                    device: torch.device) -> Tuple[nn.Module, Optional[torch.optim.Optimizer], int]:
    """Loads model and optimizer state dict. Returns model, optimizer, start_epoch."""
    start_epoch = 0
    if os.path.isfile(filename):
        logger.info(f"Loading checkpoint from '{filename}'")
        try:
            # Load checkpoint onto the specified device directly
            checkpoint = torch.load(filename, map_location=device)
            start_epoch = checkpoint.get("epoch", 0) # Use .get for safety

            # Load model state
            try:
                 model.load_state_dict(checkpoint["model_state_dict"])
            except RuntimeError as e:
                 logger.warning(f"Could not load model state dict strictly: {e}. Trying non-strict loading.")
                 # Try loading with strict=False if keys don't match exactly
                 model.load_state_dict(checkpoint["model_state_dict"], strict=False)

            # Load optimizer state if optimizer is provided and state exists
            if optimizer is not None and "optimizer_state_dict" in checkpoint:
                try:
                     optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
                     logger.info("Optimizer state loaded successfully.")
                     # Move optimizer state to the correct device (important if device changed)
                     for state in optimizer.state.values():
                         for k, v in state.items():
                             if isinstance(v, torch.Tensor):
                                 state[k] = v.to(device)
                except Exception as e:
                     logger.warning(f"Could not load optimizer state: {e}. Optimizer state ignored.")
            elif optimizer is not None:
                 logger.warning("Optimizer state dict not found in checkpoint. Optimizer state ignored.")

            # Ensure model is on the correct device *after* loading state dict
            model.to(device)
            logger.info(f"Loaded checkpoint. Resuming from epoch {start_epoch + 1}")

        except Exception as e:
            logger.error(f"Error loading checkpoint from '{filename}': {e}", exc_info=True)
            # Reset start_epoch if loading failed
            start_epoch = 0
            logger.warning("Training from scratch due to checkpoint loading error.")
        sys.stdout.flush()
    else:
        logger.info(f"No checkpoint found at '{filename}'. Training from scratch.")
        # Ensure model is on the correct device even when training from scratch
        model.to(device)
        sys.stdout.flush()
    return model, optimizer, start_epoch


#################################################################
# (A) PDB Parsing & Backbone/Sidechain Extraction
#################################################################
def parse_pdb(filename: str, logger: logging.Logger) -> Tuple[Dict, List]:
    """Parses ATOM records from a PDB file, handling alternate locations."""
    backbone_atoms = {"N", "CA", "C", "O", "OXT"}
    atoms_in_order = [] # List of (orig_res_id, orig_atom_index, category)
    atom_counter = 0
    processed_atom_indices = set() # To track unique atom serial numbers processed

    try:
        with open(filename, 'r') as pdb_file:
            for line in pdb_file:
                if not line.startswith("ATOM  ") and not line.startswith("HETATM"): # Process both for robustness? Assume ATOM only for now
                    continue

                atom_counter += 1
                record_type = line[0:6].strip()
                try:
                    # PDB format indices (1-based)
                    atom_serial = int(line[6:11])
                    atom_name = line[12:16].strip()
                    alt_loc = line[16].strip() # Alternate location indicator
                    res_name = line[17:20].strip()
                    chain_id = line[21].strip()
                    res_seq = int(line[22:26])
                    #icode = line[26].strip() # Insertion code
                except ValueError as e:
                    logger.warning(f"Skipping malformed {record_type} line {atom_counter} (parsing error: {e}): {line.strip()}")
                    continue

                # Handle alternate locations: Keep only blank or 'A'
                if alt_loc != '' and alt_loc != 'A':
                    continue

                # Handle duplicate atom serial numbers (often from alt locs missed by above filter)
                # Keep only the first occurrence encountered
                if atom_serial in processed_atom_indices:
                    continue
                processed_atom_indices.add(atom_serial)

                # Create a unique original residue identifier
                orig_res_id = f"{chain_id}:{res_name}:{res_seq}"

                # Classify atom
                category = "backbone" if atom_name in backbone_atoms else "sidechain"

                # Store info needed for renumbering
                atoms_in_order.append((orig_res_id, atom_serial, category))

    except FileNotFoundError:
        logger.error(f"PDB file not found: {filename}")
        return {}, []
    except Exception as e:
        logger.error(f"Error reading PDB file {filename}: {e}", exc_info=True)
        return {}, []

    if not atoms_in_order:
        logger.error(f"No valid ATOM records (with altLoc='' or 'A') found in PDB file: {filename}")
    else:
        logger.info(f"Parsed {len(atoms_in_order)} unique ATOM records from {filename}.")

    return {}, atoms_in_order # We don't need original_dict, just the ordered list

def renumber_atoms_and_residues(atoms_in_order: List[Tuple[str, int, str]], logger: logging.Logger) -> Tuple[Dict, Dict]:
    """Renumbers residues and atoms consecutively starting from 0."""
    new_res_dict = {} # Maps new_res_id -> {"backbone": [new_atom_indices], "sidechain": [new_atom_indices]}
    orig_atom_to_new_atom_map = {} # Maps orig_atom_serial -> new_atom_index
    orig_res_to_new_res_map = {} # Maps orig_res_id -> new_res_id
    next_new_res_id = 0
    next_new_atom_index = 0

    # Determine residue appearance order based on the input list
    seen_res_ids_order = {}
    res_order_counter = 0
    for orig_res_id, _, _ in atoms_in_order:
        if orig_res_id not in seen_res_ids_order:
            seen_res_ids_order[orig_res_id] = res_order_counter
            res_order_counter += 1

    # Create tuples for stable sorting: (residue_appearance_order, original_atom_serial, orig_res_id, category)
    sortable_atoms = [
        (seen_res_ids_order[orig_res_id], orig_atom_serial, orig_res_id, category)
        for orig_res_id, orig_atom_serial, category in atoms_in_order
    ]
    # Sort primarily by residue appearance, secondarily by original atom serial number
    sortable_atoms.sort()

    # Perform renumbering based on sorted order
    for _, orig_atom_serial, orig_res_id, category in sortable_atoms:
        # Assign new residue ID if first time seeing this original residue ID
        if orig_res_id not in orig_res_to_new_res_map:
            orig_res_to_new_res_map[orig_res_id] = next_new_res_id
            new_res_dict[next_new_res_id] = {"backbone": [], "sidechain": []}
            next_new_res_id += 1

        # Get new residue ID and add new atom index
        new_res_id = orig_res_to_new_res_map[orig_res_id]
        new_res_dict[new_res_id][category].append(next_new_atom_index)
        orig_atom_to_new_atom_map[orig_atom_serial] = next_new_atom_index
        next_new_atom_index += 1

    logger.info(f"Renumbered {next_new_res_id} residues and {next_new_atom_index} atoms consecutively.")
    return new_res_dict, orig_atom_to_new_atom_map # Return mapping if needed later

def get_global_indices(renumbered_dict: Dict) -> Tuple[List[int], List[int]]:
    """Extracts sorted global lists of backbone and sidechain atom indices."""
    backbone_indices, sidechain_indices = [], []
    # Sort by the new residue ID to ensure consistent global ordering
    for res_id in sorted(renumbered_dict.keys()):
        # Indices within each residue should already be sorted by atom appearance
        backbone_indices.extend(renumbered_dict[res_id]["backbone"])
        sidechain_indices.extend(renumbered_dict[res_id]["sidechain"])
    # The combined lists should be globally sorted because of the renumbering process
    return backbone_indices, sidechain_indices


#################################################################
# (B) Load JSON heavy-atom coordinates
#################################################################
def load_heavy_atom_coords_from_json(json_file: str, logger: logging.Logger) -> Tuple[List[torch.Tensor], int]:
    """Loads coordinates from JSON, assuming keys are '0', '1', ... (new residue IDs)."""
    logger.info(f"Loading heavy atom coordinates from JSON: {json_file}")
    try:
        with open(json_file, "r") as f:
            data = json.load(f)
    except FileNotFoundError:
        logger.error(f"JSON file not found: {json_file}")
        return [], -1
    except json.JSONDecodeError as e:
        logger.error(f"Error decoding JSON file {json_file}: {e}")
        return [], -1

    # Expect keys to be NEW residue IDs (0-based integers as strings)
    try:
        residue_keys_sorted_int = sorted([int(k) for k in data.keys()])
        residue_keys_sorted_str = [str(k) for k in residue_keys_sorted_int] # Keep string keys for dict access
        logger.info(f"Found data for {len(residue_keys_sorted_str)} residues in JSON.")
    except ValueError:
        logger.error(f"Residue keys in {json_file} must be sortable integers ('0', '1', ...). Check JSON format.")
        return [], -1

    if not residue_keys_sorted_str:
        logger.error(f"No residue data found in {json_file}.")
        return [], -1

    # Determine number of frames and structure from the first residue
    first_res_key = residue_keys_sorted_str[0]
    try:
        frame_data = data[first_res_key].get("heavy_atom_coords_per_frame")
        if not isinstance(frame_data, list):
             raise TypeError("'heavy_atom_coords_per_frame' is not a list.")
        num_frames = len(frame_data)
        if num_frames == 0:
             raise ValueError("First residue has 0 frames.")
        # Check coordinate structure of the first atom in the first frame
        first_coords = np.array(frame_data[0][0])
        if first_coords.shape != (3,):
             raise ValueError(f"Expected coordinate shape (3,), but got {first_coords.shape}")
    except (KeyError, IndexError, TypeError, ValueError) as e:
        logger.error(f"Invalid structure or data for first residue ('{first_res_key}') in {json_file}. Cannot determine frames/coords. Error: {e}")
        return [], -1

    logger.info(f"Number of frames found in JSON: {num_frames}")
    coords_per_frame_list = [] # List to store tensors for each frame
    total_atoms_check = -1 # For consistency check

    # Iterate through frames
    for frame_idx in range(num_frames):
        frame_coords_list_np = [] # List of numpy arrays for this frame
        current_frame_atoms = 0
        # Iterate through residues in sorted order
        for res_key in residue_keys_sorted_str:
             try:
                 coords_this_res_raw = data[res_key]["heavy_atom_coords_per_frame"][frame_idx]
                 coords_this_res = np.array(coords_this_res_raw, dtype=np.float32)
                 # Validate shape: [num_atoms_in_res, 3]
                 if coords_this_res.ndim != 2 or coords_this_res.shape[1] != 3:
                      raise ValueError(f"Invalid coordinate shape {coords_this_res.shape}, expected [N, 3].")
                 frame_coords_list_np.append(coords_this_res)
                 current_frame_atoms += coords_this_res.shape[0]
             except (KeyError, IndexError, ValueError, TypeError) as e:
                 logger.error(f"Error processing residue {res_key} frame {frame_idx} in {json_file}: {e}")
                 return [], -1 # Abort on error

        # Check atom count consistency after processing all residues for the frame
        if frame_idx == 0:
            total_atoms_check = current_frame_atoms
            logger.info(f"Total atoms found in first frame from JSON: {total_atoms_check}")
        elif current_frame_atoms != total_atoms_check:
            logger.error(f"Inconsistent atom count in frame {frame_idx} ({current_frame_atoms}) vs first frame ({total_atoms_check}). Cannot proceed.")
            return [], -1

        # Concatenate numpy arrays for the frame -> [total_atoms, 3]
        try:
            frame_coords_np = np.concatenate(frame_coords_list_np, axis=0)
            # Convert to PyTorch tensor and add to list
            coords_per_frame_list.append(torch.tensor(frame_coords_np, dtype=torch.float32))
        except ValueError as e:
             logger.error(f"Error concatenating coordinates for frame {frame_idx}: {e}. Check atom counts within residues.")
             return [], -1

    if not coords_per_frame_list:
         logger.error("Failed to load any coordinate frames from JSON.")
         return [], -1

    return coords_per_frame_list, total_atoms_check

#################################################################
# (C) Kabsch Alignment
#################################################################
def compute_centroid(X: torch.Tensor) -> torch.Tensor:
    """Computes centroid by averaging over the atom dimension (assumed to be -2)."""
    return X.mean(dim=-2)

def kabsch_algorithm(P: torch.Tensor, Q: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
    """Aligns Q onto P using Kabsch algorithm. Handles batches."""
    P = P.float()
    Q = Q.float()
    is_batched = P.ndim == 3
    if not is_batched:
        P = P.unsqueeze(0)
        Q = Q.unsqueeze(0)
    B, N, _3 = P.shape # Batch size, Num atoms, Coords

    # Center coordinates
    centroid_P = compute_centroid(P) # [B, 3]
    centroid_Q = compute_centroid(Q) # [B, 3]
    P_centered = P - centroid_P.unsqueeze(1) # [B, N, 3]
    Q_centered = Q - centroid_Q.unsqueeze(1) # [B, N, 3]

    # Covariance matrix H = Q_centered^T * P_centered
    C = torch.bmm(Q_centered.transpose(1, 2), P_centered) # [B, 3, 3]

    # SVD
    try:
        # Use torch.linalg.svd for robustness
        V, S, Wt = torch.linalg.svd(C) # V=[B,3,3], S=[B,3], Wt=[B,3,3]
    except torch._C._LinAlgError as e:
        logger.error(f"SVD failed during Kabsch: {e}. Returning identity alignment.")
        identity_U = torch.eye(3, device=P.device).unsqueeze(0).expand(B, -1, -1)
        Q_aligned_fallback = Q - centroid_Q.unsqueeze(1) + centroid_P.unsqueeze(1)
        if not is_batched: return identity_U.squeeze(0), Q_aligned_fallback.squeeze(0)
        return identity_U, Q_aligned_fallback

    # Ensure proper rotation (determinant +1)
    det_VWt = torch.det(torch.bmm(V, Wt)) # [B]
    D = torch.eye(3, device=P.device).unsqueeze(0).repeat(B, 1, 1) # [B, 3, 3]
    D[:, 2, 2] = torch.sign(det_VWt)
    U = torch.bmm(torch.bmm(V, D), Wt) # Rotation matrix [B, 3, 3]

    # Apply rotation and translate back
    Q_aligned_centered = torch.bmm(Q_centered, U)
    Q_aligned = Q_aligned_centered + centroid_P.unsqueeze(1)

    if not is_batched:
        U = U.squeeze(0)
        Q_aligned = Q_aligned.squeeze(0)

    return U, Q_aligned

def align_frames_to_first(coords_list: List[torch.Tensor], logger: logging.Logger, device: torch.device) -> List[torch.Tensor]:
    """Aligns all coordinate frames to the first frame using Kabsch."""
    logger.info("Aligning coordinate frames to the first frame...")
    if not coords_list:
        logger.warning("Coordinate list is empty, cannot align.")
        return []
    # Reference frame on target device
    reference = coords_list[0].float().to(device)
    # Store aligned frames on CPU
    aligned_coords_list = [coords_list[0].cpu()] # Keep first frame as is on CPU
    logger.debug(f"Reference frame shape: {reference.shape} on {reference.device}")

    num_frames_to_align = len(coords_list) - 1
    for i, coords in enumerate(coords_list[1:], start=1):
        coords_on_device = coords.float().to(device)
        _, coords_aligned_device = kabsch_algorithm(reference, coords_on_device)
        # Store aligned result on CPU
        aligned_coords_list.append(coords_aligned_device.cpu())

        if i % 500 == 0 or i == num_frames_to_align:
             logger.info(f"Aligned {i}/{num_frames_to_align} frames...")

    logger.info("Finished aligning frames.")
    return aligned_coords_list

#################################################################
# (D) Build PyG Graph Dataset
#################################################################
def build_graph_dataset(coords_list: List[torch.Tensor],
                        knn_neighbors: int = 4,
                        logger: Optional[logging.Logger] = None,
                        device: torch.device = torch.device('cpu')) -> List[Data]:
    """Builds PyTorch Geometric dataset with k-NN graphs."""
    if logger:
        logger.info(f"Building PyG dataset using k-NN graph (k={knn_neighbors}) on device '{device}'...")
    dataset = []
    num_frames = len(coords_list)
    for i, coords in enumerate(coords_list):
        # Calculate k-NN graph on the specified device
        coords_device = coords.to(device)
        # batch=None explicitly for single graph processing
        edge_index = knn_graph(coords_device, k=knn_neighbors, loop=False, batch=None)

        # Create Data object on CPU
        # Store original aligned coordinates as both input features 'x' and target 'y'
        data = Data(x=coords.cpu(), edge_index=edge_index.cpu(), y=coords.cpu())
        dataset.append(data)

        if logger and ((i + 1) % 500 == 0 or (i + 1) == num_frames):
            logger.info(f"Built graph for {i+1}/{num_frames} frames...")
    if logger:
        logger.info("Finished building PyG dataset.")
    return dataset

#################################################################
# (E) Blind Pooling Module (2D)
#################################################################
class BlindPooling2D(nn.Module):
    """Pools [B, N, E] input to [B, H*W] using AdaptiveAvgPool2d over N*E grid."""
    def __init__(self, H: int, W: int):
        super().__init__()
        self.pool2d = nn.AdaptiveAvgPool2d((H, W))
        self.output_dim = H * W

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # Input shape: [B, N, E]
        if x.shape[1] == 0: # Handle empty sequence dimension N
             return torch.zeros(x.shape[0], self.output_dim, device=x.device, dtype=x.dtype)
        B, N, E = x.shape
        # Add channel dim: [B, 1, N, E] (Treat N as Height, E as Width)
        x_4d = x.unsqueeze(1)
        pooled = self.pool2d(x_4d)  # -> [B, 1, H, W]
        pooled_flat = pooled.view(B, self.output_dim) # -> [B, H*W]
        return pooled_flat

#################################################################
# (F) HNO Model (ChebConv based)
#################################################################
class HNO(nn.Module):
    """Graph Neural Network Encoder using ChebConv layers."""
    def __init__(self, hidden_dim: int, K: int):
        super().__init__()
        self._debug_logged_fwd = False
        self._debug_logged_rep = False
        logger.debug(f"Initializing HNO with hidden_dim={hidden_dim}, K={K}")

        self.conv1 = ChebConv(3, hidden_dim, K=K) # Input features = 3 (coords)
        self.conv2 = ChebConv(hidden_dim, hidden_dim, K=K)
        self.conv3 = ChebConv(hidden_dim, hidden_dim, K=K)
        self.conv4 = ChebConv(hidden_dim, hidden_dim, K=K)
        self.bano1 = nn.BatchNorm1d(hidden_dim)
        self.bano2 = nn.BatchNorm1d(hidden_dim)
        self.bano3 = nn.BatchNorm1d(hidden_dim)
        # Final layer predicts coordinates (reconstruction) or representation
        self.mlpRep = nn.Linear(hidden_dim, 3) # Predicts 3D coordinates

    def _log_shape(self, name: str, tensor: torch.Tensor, log_debug: bool, flag_attr: str):
         """Helper for conditional debug logging of tensor shapes."""
         if log_debug and not getattr(self, flag_attr, False):
              logger.debug(f"[HNO {name}] Shape: {tensor.shape}")

    def forward(self, x: torch.Tensor, edge_index: torch.Tensor, log_debug: bool = False) -> torch.Tensor:
        """Forward pass for coordinate reconstruction."""
        log_now = log_debug and not self._debug_logged_fwd
        self._log_shape("Input", x, log_now, "_debug_logged_fwd")
        x = x.float()

        x = self.conv1(x, edge_index)
        x = self.bano1(F.leaky_relu(x))
        self._log_shape("After conv1+bano1", x, log_now, "_debug_logged_fwd")

        x = self.conv2(x, edge_index)
        x = self.bano2(F.leaky_relu(x))
        self._log_shape("After conv2+bano2", x, log_now, "_debug_logged_fwd")

        x = self.conv3(x, edge_index)
        x = self.bano3(F.relu(x))
        self._log_shape("After conv3+bano3", x, log_now, "_debug_logged_fwd")

        x = self.conv4(x, edge_index)
        self._log_shape("After conv4", x, log_now, "_debug_logged_fwd")

        x = F.normalize(x, p=2.0, dim=1) # Use p=2.0 (float)
        self._log_shape("After normalize", x, log_now, "_debug_logged_fwd")

        x = self.mlpRep(x)
        self._log_shape("Output (mlpRep)", x, log_now, "_debug_logged_fwd")

        if log_now: self._debug_logged_fwd = True
        return x

    def forward_representation(self, x: torch.Tensor, edge_index: torch.Tensor, log_debug: bool = False) -> torch.Tensor:
        """Forward pass to get latent representation [N, hidden_dim]."""
        log_now = log_debug and not self._debug_logged_rep
        self._log_shape("Rep Input", x, log_now, "_debug_logged_rep")
        x = x.float()

        x = self.conv1(x, edge_index)
        x = self.bano1(F.leaky_relu(x))
        self._log_shape("Rep After conv1+bano1", x, log_now, "_debug_logged_rep")

        x = self.conv2(x, edge_index)
        x = self.bano2(F.leaky_relu(x))
        self._log_shape("Rep After conv2+bano2", x, log_now, "_debug_logged_rep")

        x = self.conv3(x, edge_index)
        x = self.bano3(F.relu(x))
        self._log_shape("Rep After conv3+bano3", x, log_now, "_debug_logged_rep")

        x = self.conv4(x, edge_index)
        self._log_shape("Rep After conv4", x, log_now, "_debug_logged_rep")

        x = F.normalize(x, p=2.0, dim=1) # Use p=2.0 (float)
        self._log_shape("Rep Output", x, log_now, "_debug_logged_rep")

        if log_now: self._debug_logged_rep = True
        return x

#################################################################
# (G) Helper: Build simple MLP
#################################################################
def build_mlp(input_dim: int, output_dim: int, hidden_dim: int = 128, num_layers: int = 2,
              use_layernorm: bool = True, final_activation: Optional[nn.Module] = None) -> nn.Sequential:
    """Builds a multi-layer perceptron."""
    layers = []
    in_dim = input_dim
    if num_layers <= 0:
         raise ValueError("Number of MLP layers must be at least 1.")
    elif num_layers == 1:
         layers.append(nn.Linear(in_dim, output_dim))
    else:
         # First hidden layer
         layers.append(nn.Linear(in_dim, hidden_dim))
         layers.append(nn.ReLU())
         if use_layernorm: layers.append(nn.LayerNorm(hidden_dim))
         in_dim = hidden_dim
         # Intermediate hidden layers
         for _ in range(num_layers - 2):
              layers.append(nn.Linear(in_dim, hidden_dim))
              layers.append(nn.ReLU())
              if use_layernorm: layers.append(nn.LayerNorm(hidden_dim))
         # Final output layer
         layers.append(nn.Linear(in_dim, output_dim))

    if final_activation is not None:
         layers.append(final_activation)

    return nn.Sequential(*layers)

#################################################################
# (H) Backbone Decoder
#################################################################
class BackboneDecoder(nn.Module):
    """Decodes backbone coordinates from HNO latent space and reference."""
    def __init__(self, num_total_atoms: int, backbone_indices: torch.Tensor, emb_dim: int,
                 pooling_dim: Tuple[int, int] = (20, 4), mlp_depth: int = 2, mlp_hidden_dim: int = 128):
        super().__init__()
        self._debug_logged = False
        self.num_total_atoms = num_total_atoms
        self.register_buffer("backbone_indices", backbone_indices.cpu(), persistent=False)
        self.backbone_count = len(self.backbone_indices)
        if self.backbone_count == 0: logger.warning("BackboneDecoder initialized with 0 backbone atoms.")

        self.pool_backbone = BlindPooling2D(*pooling_dim)
        self.pool_output_dim = self.pool_backbone.output_dim # Use Script #1's naming

        self.mlp_input_dim = self.backbone_count * (emb_dim + self.pool_output_dim) if self.backbone_count > 0 else 0
        self.mlp_output_dim = self.backbone_count * 3

        self.mlp_flat = nn.Identity()
        if self.backbone_count > 0:
            self.mlp_flat = build_mlp(
                input_dim=self.mlp_input_dim, output_dim=self.mlp_output_dim,
                hidden_dim=mlp_hidden_dim, num_layers=mlp_depth, use_layernorm=True)

        self.last_pooled_backbone = None

    # --- MODIFIED: Added override_pooled_backbone ---
    def forward(self, hno_latent: torch.Tensor, z_ref: torch.Tensor, log_debug: bool = False,
                override_pooled_backbone: Optional[torch.Tensor] = None) -> torch.Tensor:

        if hno_latent.ndim == 3:
            B, N, E = hno_latent.shape
            x = hno_latent
            if N != self.num_total_atoms:
                logger.warning(f"BackboneDecoder Warning: Input N ({N}) != expected num_total_atoms ({self.num_total_atoms}).")
        elif hno_latent.ndim == 2:
            B_times_N, E = hno_latent.shape
            if B_times_N == 0:
                 return torch.empty(0, self.backbone_count, 3, device=hno_latent.device, dtype=hno_latent.dtype)
            if self.num_total_atoms == 0: raise ValueError("BackboneDecoder: num_total_atoms is 0.")
            if B_times_N % self.num_total_atoms != 0:
                 raise ValueError(f"BackboneDecoder: Input B*N ({B_times_N}) not divisible by num_total_atoms ({self.num_total_atoms}).")
            B = B_times_N // self.num_total_atoms
            N = self.num_total_atoms
            x = hno_latent.view(B, N, E)
        else:
            raise ValueError(f"BackboneDecoder: Unsupported hno_latent input ndim: {hno_latent.ndim}. Expected 2 or 3.")

        if self.backbone_count == 0:
             return torch.empty(B, 0, 3, device=hno_latent.device, dtype=hno_latent.dtype)

        z_ref = z_ref.to(hno_latent.device)
        should_log = (log_debug and not self._debug_logged)
        # x is now guaranteed to be [B, N, E]

        # --- Prepare MLP inputs ---
        backbone_emb = x[:, self.backbone_indices, :] # [B, bb_count, E]

        # --- Conditional Pooling ---
        if override_pooled_backbone is None:
            pooled_backbone = self.pool_backbone(backbone_emb) # [B, pool_out_dim]
            # Store internally generated pooled embedding only when not overriding
            self.last_pooled_backbone = pooled_backbone.detach().cpu()
        else:
            # Use the provided override
            if override_pooled_backbone.shape != (B, self.pool_output_dim):
                 raise ValueError(f"BackboneDecoder override_pooled_backbone has shape {override_pooled_backbone.shape}, expected ({B}, {self.pool_output_dim})")
            pooled_backbone = override_pooled_backbone.to(hno_latent.device) # Ensure device
            # Do not update self.last_pooled_backbone when overriding
        # --- End Conditional Pooling ---

        if z_ref is None: raise ValueError("[BackboneDecoder] z_ref is None.")
        z_ref_backbone_single = z_ref[self.backbone_indices, :] # [bb_count, E]
        z_ref_backbone = z_ref_backbone_single.unsqueeze(0).expand(B, -1, -1) # [B, bb_count, E]

        pooled_backbone_expanded = pooled_backbone.unsqueeze(1).expand(-1, self.backbone_count, -1) # [B, bb_count, pool_out_dim]
        combined = torch.cat([z_ref_backbone, pooled_backbone_expanded], dim=-1) # [B, bb_count, E + pool_out_dim]
        combined_flat = combined.view(B, -1) # [B, bb_count * (E + pool_out_dim)]

        # --- Predict ---
        pred_bb_flat = self.mlp_flat(combined_flat) # [B, bb_count * 3]
        pred_bb = pred_bb_flat.view(B, self.backbone_count, 3) # [B, bb_count, 3]

        if should_log:
            logger.debug(f"[BackboneDecoder] Input: hno_latent(viewed) {x.shape}, z_ref {z_ref.shape}")
            logger.debug(f"[BackboneDecoder] backbone_emb {backbone_emb.shape}, pooled_backbone {pooled_backbone.shape}")
            if override_pooled_backbone is not None:
                 logger.debug(f"[BackboneDecoder] Used override_pooled_backbone: {override_pooled_backbone.shape}")
            logger.debug(f"[BackboneDecoder] combined_flat {combined_flat.shape} -> mlp_flat -> pred_bb_flat {pred_bb_flat.shape}")
            logger.debug(f"[BackboneDecoder] Output: pred_bb {pred_bb.shape}")
            self._debug_logged = True

        return pred_bb
    # --- END MODIFIED ---

#################################################################
# (I) Sidechain Decoder
#################################################################
class SidechainDecoder(nn.Module):
    """Decodes full coordinates using backbone prediction and latent info."""
    def __init__(self, num_total_atoms: int, sidechain_indices: torch.Tensor, backbone_indices: torch.Tensor,
                 emb_dim: int, pooling_dim: Tuple[int, int] = (20, 4), mlp_depth: int = 2,
                 mlp_hidden_dim: int = 128, arch_type: int = 0):
        super().__init__()
        self._debug_logged = False
        self.num_total_atoms = num_total_atoms
        self.emb_dim = emb_dim
        self.arch_type = arch_type
        self.register_buffer("sidechain_indices", sidechain_indices.cpu(), persistent=False)
        self.register_buffer("backbone_indices", backbone_indices.cpu(), persistent=False)
        self.sidechain_count = len(self.sidechain_indices)
        self.backbone_count = len(self.backbone_indices)
        if self.sidechain_count == 0: logger.warning("SidechainDecoder initialized with 0 sidechain atoms.")

        self.pool_sidechain = BlindPooling2D(*pooling_dim)
        self.pool_output_dim = self.pool_sidechain.output_dim if self.sidechain_count > 0 else 0 # Use Script #1's naming

        # Optional reduction layers
        sc_zref_reduced_dim = 0
        self.sc_zref_reduce = None
        if arch_type >= 1 and self.sidechain_count > 0:
             sc_zref_input_dim = self.sidechain_count * emb_dim
             if sc_zref_input_dim > 0:
                  self.sc_zref_reduce = nn.Linear(sc_zref_input_dim, 128)
                  sc_zref_reduced_dim = 128

        bb_reduced_dim = 0
        self.bb_reduce = None
        if arch_type == 2 and self.backbone_count > 0:
             bb_input_dim = self.backbone_count * 3
             if bb_input_dim > 0:
                  self.bb_reduce = nn.Linear(bb_input_dim, 128)
                  bb_reduced_dim = 128

        # Determine MLP input dimension based on architecture
        final_in_dim = 0
        bb_term_dim = self.backbone_count * 3 if self.backbone_count > 0 else 0
        if arch_type == 0:   final_in_dim = bb_term_dim + self.pool_output_dim
        elif arch_type == 1: final_in_dim = bb_term_dim + self.pool_output_dim + sc_zref_reduced_dim
        elif arch_type == 2: final_in_dim = bb_reduced_dim + self.pool_output_dim + sc_zref_reduced_dim
        else: raise ValueError(f"Unsupported SidechainDecoder arch_type: {arch_type}")

        # Main MLP predicts sidechain coords
        self.mlp_sidechain = nn.Identity() # Default if no sidechains
        if self.sidechain_count > 0 and final_in_dim > 0 :
            self.mlp_sidechain = build_mlp(
                input_dim=final_in_dim, output_dim=self.sidechain_count * 3,
                hidden_dim=mlp_hidden_dim, num_layers=mlp_depth, use_layernorm=True)
        elif self.sidechain_count > 0 and final_in_dim == 0:
             logger.warning("Sidechain MLP input dimension is 0, check architecture/counts.")

        self.last_pooled_sidechain = None

    # --- MODIFIED: Added override_pooled_sidechain ---
    def forward(self, hno_latent: torch.Tensor, predicted_backbone: torch.Tensor,
                z_ref: torch.Tensor, log_debug: bool = False,
                override_pooled_sidechain: Optional[torch.Tensor] = None) -> torch.Tensor:

        if hno_latent.ndim == 3:
            B, N, E = hno_latent.shape
            x = hno_latent
            if N != self.num_total_atoms:
                 logger.warning(f"SidechainDecoder Warning: Input N ({N}) != expected num_total_atoms ({self.num_total_atoms}).")
        elif hno_latent.ndim == 2:
            B_times_N, E = hno_latent.shape
            if B_times_N == 0:
                 return torch.empty(0, self.num_total_atoms, 3, device=hno_latent.device, dtype=predicted_backbone.dtype)
            if self.num_total_atoms == 0: raise ValueError("SidechainDecoder: num_total_atoms is 0.")
            if B_times_N % self.num_total_atoms != 0:
                 raise ValueError(f"SidechainDecoder: Input B*N ({B_times_N}) not divisible by num_total_atoms ({self.num_total_atoms}).")
            B = B_times_N // self.num_total_atoms
            N = self.num_total_atoms
            x = hno_latent.view(B, N, E)
        else:
            raise ValueError(f"SidechainDecoder: Unsupported hno_latent input ndim: {hno_latent.ndim}. Expected 2 or 3.")

        should_log = (log_debug and not self._debug_logged)
        # x is now guaranteed to be [B, N, E]

        # --- Prepare MLP Inputs ---
        # 1. Pooled sidechain context (current frame or override)
        pooled_sidechain = torch.empty(B, 0, device=hno_latent.device) # Default if no sidechains
        if self.sidechain_count > 0:
            sidechain_emb = x[:, self.sidechain_indices, :]
            # --- Conditional Pooling ---
            if override_pooled_sidechain is None:
                pooled_sidechain = self.pool_sidechain(sidechain_emb) # [B, pool_out_dim]
                # Store internally generated pooled embedding only when not overriding
                self.last_pooled_sidechain = pooled_sidechain.detach().cpu()
            else:
                # Use the provided override
                if override_pooled_sidechain.shape != (B, self.pool_output_dim):
                     raise ValueError(f"SidechainDecoder override_pooled_sidechain has shape {override_pooled_sidechain.shape}, expected ({B}, {self.pool_output_dim})")
                pooled_sidechain = override_pooled_sidechain.to(hno_latent.device) # Ensure device
                # Do not update self.last_pooled_sidechain when overriding
            # --- End Conditional Pooling ---
        # If sidechain_count is 0, pooled_sidechain remains shape [B, 0]

        # 2. Backbone information (predicted coords)
        bb_flat = predicted_backbone.view(B, self.backbone_count * 3) # [B, bb_count * 3]
        bb_reduced = None
        if self.arch_type == 2 and self.bb_reduce:
             bb_reduced = self.bb_reduce(bb_flat) # [B, 128]

        # 3. Sidechain reference information (from z_ref)
        sc_zref_reduced = None
        if self.arch_type >= 1 and self.sc_zref_reduce:
            if z_ref is None: raise ValueError(f"Arch type {self.arch_type} requires z_ref.")
            if self.sidechain_count > 0:
                 sc_zref_single = z_ref[self.sidechain_indices, :]
                 sc_zref = sc_zref_single.unsqueeze(0).expand(B, -1, -1)
                 sc_zref_flat = sc_zref.view(B, self.sidechain_count * E)
                 sc_zref_reduced = self.sc_zref_reduce(sc_zref_flat) # [B, 128]
            else: # If arch >= 1 but no SC atoms, need zero tensor of expected dim
                 sc_zref_reduced = torch.zeros(B, self.sc_zref_reduce.out_features, device=hno_latent.device)

        # --- Concatenate inputs based on arch_type ---
        final_input_list = []
        if self.arch_type == 0:
             if self.backbone_count > 0: final_input_list.append(bb_flat)
             if self.pool_output_dim > 0: final_input_list.append(pooled_sidechain)
        elif self.arch_type == 1:
             if self.backbone_count > 0: final_input_list.append(bb_flat)
             if self.pool_output_dim > 0: final_input_list.append(pooled_sidechain)
             if sc_zref_reduced is not None: final_input_list.append(sc_zref_reduced)
        elif self.arch_type == 2:
             if bb_reduced is not None: final_input_list.append(bb_reduced)
             if self.pool_output_dim > 0: final_input_list.append(pooled_sidechain)
             if sc_zref_reduced is not None: final_input_list.append(sc_zref_reduced)

        if final_input_list:
             final_input = torch.cat(final_input_list, dim=-1)
        else: # Should only happen if backbone_count=0 and pool_output_dim=0
             final_input = torch.empty(B, 0, device=hno_latent.device)

        # --- Predict Sidechain Coordinates ---
        pred_sidechain_coords = torch.empty(B, 0, 3, device=hno_latent.device, dtype=predicted_backbone.dtype)
        if self.sidechain_count > 0 and isinstance(self.mlp_sidechain, nn.Sequential): # Check if MLP exists
            sidechain_coords_flat = self.mlp_sidechain(final_input) # [B, sc_count * 3]
            pred_sidechain_coords = sidechain_coords_flat.view(B, self.sidechain_count, 3)

        # --- Combine Backbone and Sidechain ---
        full_coords = torch.zeros(B, self.num_total_atoms, 3, device=hno_latent.device, dtype=predicted_backbone.dtype)
        if self.backbone_count > 0:
            full_coords[:, self.backbone_indices, :] = predicted_backbone
        if self.sidechain_count > 0:
            full_coords[:, self.sidechain_indices, :] = pred_sidechain_coords

        if should_log:
             logger.debug(f"[SidechainDecoder arch={self.arch_type}] Input: hno_latent(viewed) {x.shape}, pred_bb {predicted_backbone.shape}, z_ref {z_ref.shape}")
             if override_pooled_sidechain is not None:
                  logger.debug(f"[SidechainDecoder] Used override_pooled_sidechain: {override_pooled_sidechain.shape}")
             logger.debug(f"[SidechainDecoder] final_input {final_input.shape}")
             if self.sidechain_count > 0: logger.debug(f"[SidechainDecoder] pred_sc {pred_sidechain_coords.shape}")
             logger.debug(f"[SidechainDecoder] Output: full_coords {full_coords.shape}")
             self._debug_logged = True

        return full_coords
    # --- END MODIFIED ---

#################################################################
# (X) Dihedral Angle Utilities
#################################################################
@torch.jit.script
def compute_dihedral(a: torch.Tensor, b: torch.Tensor, c: torch.Tensor, d: torch.Tensor) -> torch.Tensor:
    """Computes dihedral angle(s). Input shapes [B, 3] or [B, N_angles, 3]."""
    b1 = b - a
    b2 = c - b
    b3 = d - c
    n1 = torch.cross(b1, b2, dim=-1)
    n2 = torch.cross(b2, b3, dim=-1)
    # Use p=2.0 (float) and add eps for stability
    n1_norm = F.normalize(n1, p=2.0, dim=-1, eps=1e-8)
    n2_norm = F.normalize(n2, p=2.0, dim=-1, eps=1e-8)
    b2_norm = F.normalize(b2, p=2.0, dim=-1, eps=1e-8)
    m1 = torch.cross(n1_norm, b2_norm, dim=-1)
    x = (n1_norm * n2_norm).sum(dim=-1)
    y = (m1 * n2_norm).sum(dim=-1)
    angle = torch.atan2(y, x)
    return angle

def compute_all_dihedrals_vectorized(coords: torch.Tensor,
                                     dihedral_info_precomputed: Dict[str, Dict],
                                     num_res: int) -> Dict[str, torch.Tensor]:
    """Computes all specified dihedrals (phi, psi, chi1-5) vectorially."""
    B = coords.shape[0]
    device = coords.device
    all_angles_out = {} # Dictionary to store angle tensors: name -> [B, num_res]

    for angle_name, info in dihedral_info_precomputed.items():
        indices = info.get('indices') # List of 4 index tensors
        res_idx_tensor = info.get('res_idx') # Tensor mapping calculation index to residue index

        # Initialize output tensor for this angle type with zeros
        angles_out_tensor = torch.zeros(B, num_res, device=device, dtype=coords.dtype)

        # Check if valid indices exist for this angle type
        if indices is not None and res_idx_tensor is not None and indices[0].numel() > 0:
            num_angles_of_this_type = indices[0].numel()
            # Gather coordinates using the specific indices
            try:
                a = coords[:, indices[0], :] # Shape [B, num_angles, 3]
                b = coords[:, indices[1], :]
                c = coords[:, indices[2], :]
                d = coords[:, indices[3], :]
            except IndexError as e:
                 logger.error(f"IndexError gathering coords for {angle_name}: {e}. Max index needed might exceed N={coords.shape[1]}.")
                 all_angles_out[angle_name] = angles_out_tensor # Store zeros and continue
                 continue

            # Compute all angles of this type simultaneously
            angle_values = compute_dihedral(a, b, c, d) # Shape [B, num_angles]

            # Place calculated values into the correct residue slots
            batch_indices = torch.arange(B, device=device).unsqueeze(1) # [B, 1]
            try:
                 # res_idx_tensor is [num_angles], need [1, num_angles] for broadcasting with batch_indices
                 angles_out_tensor[batch_indices, res_idx_tensor.unsqueeze(0)] = angle_values
            except IndexError as e:
                 logger.error(f"IndexError scattering angles for {angle_name}: {e}. Max res index needed={res_idx_tensor.max().item()}, num_res={num_res}")
                 # Output remains zeros

        all_angles_out[angle_name] = angles_out_tensor

    return all_angles_out

def compute_angle_kl_div(pred_flat_valid: torch.Tensor, true_flat_valid: torch.Tensor, nbins=36, angle_range=(-np.pi, np.pi)):
    min_angle, max_angle = angle_range
    pred_flat_valid_detached = pred_flat_valid.detach()
    true_flat_valid_detached = true_flat_valid.detach()
    if pred_flat_valid_detached.numel() == 0 or true_flat_valid_detached.numel() == 0: return torch.tensor(0.0, device=pred_flat_valid.device)
    edges = torch.linspace(min_angle, max_angle, nbins + 1, device=pred_flat_valid_detached.device)
    pred_hist = torch.histc(pred_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    true_hist = torch.histc(true_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    epsilon = 1e-10
    pred_dist = pred_hist / (pred_hist.sum() + epsilon)
    true_dist = true_hist / (true_hist.sum() + epsilon)
    pred_log_dist = torch.log(pred_dist + epsilon)
    kl_val = F.kl_div(pred_log_dist, true_dist, reduction='sum', log_target=False) # KL(True || Pred)
    return kl_val

def compute_angle_js_div(pred_flat_valid: torch.Tensor, true_flat_valid: torch.Tensor, nbins=36, angle_range=(-np.pi, np.pi)):
    min_angle, max_angle = angle_range
    pred_flat_valid_detached = pred_flat_valid.detach()
    true_flat_valid_detached = true_flat_valid.detach()
    if pred_flat_valid_detached.numel() == 0 or true_flat_valid_detached.numel() == 0: return torch.tensor(0.0, device=pred_flat_valid.device)
    edges = torch.linspace(min_angle, max_angle, nbins + 1, device=pred_flat_valid_detached.device)
    pred_hist = torch.histc(pred_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    true_hist = torch.histc(true_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    epsilon = 1e-10
    Q = pred_hist / (pred_hist.sum() + epsilon) # Predicted
    P = true_hist / (true_hist.sum() + epsilon) # True
    M = 0.5 * (P + Q)
    log_M = torch.log(M + epsilon)
    # KL(P || M) = sum(P * log(P/M)) => F.kl_div(input=logM, target=P)
    kl_pm = F.kl_div(log_M, P, reduction='sum', log_target=False)
    # KL(Q || M) = sum(Q * log(Q/M)) => F.kl_div(input=logM, target=Q)
    kl_qm = F.kl_div(log_M, Q, reduction='sum', log_target=False)
    jsd = 0.5 * (kl_pm + kl_qm)
    return jsd

def compute_angle_wasserstein(pred_flat_valid: torch.Tensor, true_flat_valid: torch.Tensor, nbins=36, angle_range=(-np.pi, np.pi)):
    min_angle, max_angle = angle_range
    pred_flat_valid_detached = pred_flat_valid.detach()
    true_flat_valid_detached = true_flat_valid.detach()
    if pred_flat_valid_detached.numel() == 0 or true_flat_valid_detached.numel() == 0: return torch.tensor(0.0, device=pred_flat_valid.device)
    edges = torch.linspace(min_angle, max_angle, nbins + 1, device=pred_flat_valid_detached.device)
    pred_hist = torch.histc(pred_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    true_hist = torch.histc(true_flat_valid_detached, bins=nbins, min=min_angle, max=max_angle)
    epsilon = 1e-10
    pred_dist = pred_hist / (pred_hist.sum() + epsilon)
    true_dist = true_hist / (true_hist.sum() + epsilon)
    pred_cdf = torch.cumsum(pred_dist, dim=0)
    true_cdf = torch.cumsum(true_dist, dim=0)
    # L1 distance between CDFs (approximation of Wasserstein-1)
    wasserstein_l1 = torch.sum(torch.abs(pred_cdf - true_cdf))
    return wasserstein_l1


#################################################################
# (J) Training Routines (Modified Backbone, Modified Sidechain)
#################################################################
def train_hno_model(model: nn.Module, # Type hint for HNO model
                    train_loader: DataLoader,
                    test_loader: DataLoader,
                    num_epochs: int,
                    learning_rate: float,
                    checkpoint_path: str,
                    save_interval: int = 10,
                    device: torch.device = torch.device('cpu') # Pass device
                   ) -> nn.Module:
    """
    Trains the HNO encoder model using coordinate reconstruction loss (MSE).
    """
    model = model.to(device) # Ensure model starts on the right device

    # Filter out parameters that don't require gradients
    try:
        trainable_params = filter(lambda p: p.requires_grad, model.parameters())
        optimizer = torch.optim.Adam(trainable_params, lr=learning_rate)
    except ValueError: # Happens if model has no trainable parameters
         logger.warning("HNO model has no trainable parameters. Skipping optimizer creation/training.")
         optimizer = None

    criterion = nn.MSELoss()

    # Load checkpoint if exists (moves model to device again, loads optimizer state)
    model, optimizer, start_epoch = load_checkpoint(model, optimizer, checkpoint_path, device)

    logger.info(f"Starting HNO training from epoch {start_epoch+1}, total epochs={num_epochs}, LR={learning_rate}")
    sys.stdout.flush()

    for epoch in range(start_epoch, num_epochs):
        model.train() # Set model to training mode
        train_loss_val = 0.0
        num_batches = len(train_loader)

        # Check if optimizer exists before training loop
        if optimizer is None:
             logger.warning(f"No optimizer found for HNO model. Cannot train epoch {epoch+1}. Skipping...")
             break # Exit training loop if no optimizer

        for batch_idx, data in enumerate(train_loader):
            # Data object contains: x (input coords), edge_index, y (target coords), batch
            data = data.to(device)
            optimizer.zero_grad(set_to_none=True)

            # Forward pass: Predict coordinates from input coordinates
            # Log debug info only for the very first batch of the first epoch if enabled
            log_flag = (epoch == start_epoch and batch_idx == 0 and use_debug)
            pred = model(data.x, data.edge_index, log_debug=log_flag)

            # Calculate loss against ground truth coordinates (data.y)
            loss = criterion(pred, data.y)

            # Backward pass and optimization
            loss.backward()
            # Optional: Gradient clipping
            # torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
            optimizer.step()

            train_loss_val += loss.item()
            # End Training Batch Loop

        # Calculate average training loss for the epoch
        avg_train_loss = train_loss_val / num_batches if num_batches > 0 else 0.0

        # --- Validation Phase ---
        model.eval() # Set model to evaluation mode
        test_loss_val = 0.0
        num_val_batches = len(test_loader)
        with torch.no_grad(): # Disable gradient calculations for validation
            for data in test_loader:
                data = data.to(device)
                pred = model(data.x, data.edge_index)
                loss = criterion(pred, data.y)
                test_loss_val += loss.item()
        # Calculate average validation loss
        avg_test_loss = test_loss_val / num_val_batches if num_val_batches > 0 else 0.0

        # Log epoch results
        logger.info(f"[HNO] Epoch {epoch+1}/{num_epochs} => TRAIN MSE={avg_train_loss:.6f}, TEST MSE={avg_test_loss:.6f}")
        sys.stdout.flush()

        # --- Save Checkpoint ---
        current_epoch_num = epoch + 1
        # Save checkpoint if interval is reached or it's the last epoch
        if optimizer is not None and (current_epoch_num % save_interval == 0 or current_epoch_num == num_epochs):
            checkpoint_state = {
                "epoch": current_epoch_num,
                "model_state_dict": model.state_dict(),
                "optimizer_state_dict": optimizer.state_dict(),
            }
            # Overwrite the main checkpoint file (or save epoch-specific ones)
            save_checkpoint(checkpoint_state, checkpoint_path, logger)
            logger.info(f"HNO checkpoint saved at epoch {current_epoch_num} -> {checkpoint_path}")
            sys.stdout.flush()
        # End Epoch Loop

    logger.info(f"Finished training HNO model. Final checkpoint at {checkpoint_path}")
    return model


def train_backbone_decoder(
    model: BackboneDecoder, train_loader: DataLoader, test_loader: DataLoader,
    device: torch.device, logger: logging.Logger, config: dict, checkpoint_path: str,
    z_ref: torch.Tensor,
    dihedral_info_precomputed: Optional[Dict[str, Dict]] = None,
    dihedral_mask_all: Optional[torch.Tensor] = None, # Shape [num_res, num_angle_types]
    num_res: Optional[int] = None
):
    """ Train Backbone Decoder. Uses Coord MSE + Optional Dihedral Loss (Div + MSE) for Phi/Psi."""
    fraction_dihedral = 0.1

    lr = config.get("learning_rate", 0.001)
    epochs = config.get("num_epochs", 50)
    trainable_params = filter(lambda p: p.requires_grad, model.parameters())
    optimizer = torch.optim.Adam(trainable_params, lr=lr) if list(model.parameters()) else None
    coord_criterion = nn.MSELoss()

    num_total_atoms = model.num_total_atoms
    start_epoch = 0
    model, optimizer, start_epoch = load_checkpoint(model, optimizer, checkpoint_path, device)
    model.to(device) # Ensure model is on device after loading
    z_ref = z_ref.to(device)

    # --- Dihedral Loss Setup ---
    use_dihedral_loss = config.get("use_dihedral", False)
    lambda_1 = config.get("lambda_1", 0.0) # Weight for TOTAL BB DIVERGENCE loss (Phi + Psi)
    lambda_2 = config.get("lambda_2", 0.0) # Weight for TOTAL BB Torsion MSE loss (Phi + Psi)
    divergence_type = config.get("divergence_type", "KL").upper()
    backbone_angle_types = ['phi', 'psi'] # Angles relevant for backbone loss
    phi_mask, psi_mask = None, None

    if use_dihedral_loss:
        if dihedral_info_precomputed is None or dihedral_mask_all is None or num_res is None:
            logger.warning("Backbone dihedral loss requested but precomputed info missing. Disabling.")
            use_dihedral_loss = False
        else:
            # Extract masks for phi and psi
            try:
                 angle_types_all = ['phi', 'psi', 'chi1', 'chi2', 'chi3', 'chi4', 'chi5']
                 phi_mask_idx = angle_types_all.index('phi')
                 psi_mask_idx = angle_types_all.index('psi')
                 phi_mask = dihedral_mask_all[:, phi_mask_idx].to(device) # [num_res]
                 psi_mask = dihedral_mask_all[:, psi_mask_idx].to(device) # [num_res]
                 logger.info(f"Backbone Decoder => use_dihedral_loss=True (Phi/Psi), Type={divergence_type}, lambda_Div={lambda_1:.4f}, lambda_MSE={lambda_2:.4f}")
            except (ValueError, IndexError) as e:
                 logger.error(f"Error extracting phi/psi masks from dihedral_mask_all: {e}. Disabling dihedral loss.")
                 use_dihedral_loss = False

    # Select divergence function
    compute_divergence = None
    if use_dihedral_loss:
        if divergence_type == "JS": compute_divergence = compute_angle_js_div
        elif divergence_type == "WASSERSTEIN": compute_divergence = compute_angle_wasserstein
        elif divergence_type == "KL": compute_divergence = compute_angle_kl_div
        else:
            logger.warning(f"Unknown backbone divergence_type '{divergence_type}'. Defaulting to KL.")
            divergence_type = "KL"
            compute_divergence = compute_angle_kl_div
    # --- End Dihedral Loss Setup ---

    logger.info(f"Starting Backbone Decoder training from epoch {start_epoch+1}, total epochs={epochs}, LR={lr}")
    best_train_loss = float("inf")
  
    for epoch in range(start_epoch, epochs):
        model.train()
        # Accumulators
        total_loss_bb_mse, total_loss_torsion_mse_phi, total_loss_torsion_mse_psi = 0.0, 0.0, 0.0
        total_loss_div_phi, total_loss_div_psi, total_loss_combined = 0.0, 0.0, 0.0

        num_batches = len(train_loader)
        for i, data in enumerate(train_loader):
            data = data.to(device)
            if optimizer: optimizer.zero_grad(set_to_none=True)
            log_flag = (i == 0 and epoch == start_epoch and use_debug) # Log first batch of first training epoch

            # Standard forward pass for training (no override)
            pred_bb = model(data.x, z_ref=z_ref, log_debug=log_flag)

            B_times_N, _ = data.y.shape
            B = B_times_N // num_total_atoms if num_total_atoms > 0 and B_times_N % num_total_atoms == 0 else 0
            if B == 0: continue
            coords_3d_gt = data.y.view(B, num_total_atoms, 3)
            gt_backbone = coords_3d_gt[:, model.backbone_indices, :]

            loss_bb_mse = coord_criterion(pred_bb, gt_backbone)
            current_loss = loss_bb_mse

            loss_torsion_mse_phi_batch, loss_torsion_mse_psi_batch = torch.tensor(0.0, device=device), torch.tensor(0.0, device=device)
            loss_div_phi_batch, loss_div_psi_batch = torch.tensor(0.0, device=device), torch.tensor(0.0, device=device)

            if use_dihedral_loss and compute_divergence is not None and random.random()<fraction_dihedral:
                # Reconstruct full structure approximately for angle calculation
                full_pred = torch.zeros_like(coords_3d_gt)
                full_pred[:, model.backbone_indices, :] = pred_bb

                predicted_angles_dict = compute_all_dihedrals_vectorized(full_pred, dihedral_info_precomputed, num_res)
                true_angles_dict = compute_all_dihedrals_vectorized(coords_3d_gt, dihedral_info_precomputed, num_res)

                # --- Phi Loss ---
                phi_pred = predicted_angles_dict.get('phi')
                phi_true = true_angles_dict.get('phi')
                if phi_pred is not None and phi_true is not None and phi_mask is not None and phi_mask.any():
                    phi_mask_expanded = phi_mask.view(1, -1).expand(B, -1)
                    phi_pred_valid_flat = phi_pred[phi_mask_expanded]
                    phi_true_valid_flat = phi_true[phi_mask_expanded]
                    if phi_pred_valid_flat.numel() > 0:
                         loss_torsion_mse_phi_batch = F.mse_loss(phi_pred_valid_flat, phi_true_valid_flat)
                         loss_div_phi_batch = compute_divergence(phi_pred_valid_flat, phi_true_valid_flat)

                # --- Psi Loss ---
                psi_pred = predicted_angles_dict.get('psi')
                psi_true = true_angles_dict.get('psi')
                if psi_pred is not None and psi_true is not None and psi_mask is not None and psi_mask.any():
                    psi_mask_expanded = psi_mask.view(1, -1).expand(B, -1)
                    psi_pred_valid_flat = psi_pred[psi_mask_expanded]
                    psi_true_valid_flat = psi_true[psi_mask_expanded]
                    if psi_pred_valid_flat.numel() > 0:
                         loss_torsion_mse_psi_batch = F.mse_loss(psi_pred_valid_flat, psi_true_valid_flat)
                         loss_div_psi_batch = compute_divergence(psi_pred_valid_flat, psi_true_valid_flat)

                # --- Combine and Add to Loss ---
                loss_div_total_batch = loss_div_phi_batch + loss_div_psi_batch
                loss_torsion_mse_total_batch = loss_torsion_mse_phi_batch + loss_torsion_mse_psi_batch
                current_loss = current_loss + lambda_1 * loss_div_total_batch + lambda_2 * loss_torsion_mse_total_batch

            # Backpropagation
            if optimizer and current_loss.requires_grad:
                current_loss.backward()
                optimizer.step()

            # Accumulate
            total_loss_bb_mse += loss_bb_mse.item()
            total_loss_torsion_mse_phi += loss_torsion_mse_phi_batch.item()
            total_loss_torsion_mse_psi += loss_torsion_mse_psi_batch.item()
            total_loss_div_phi += loss_div_phi_batch.item()
            total_loss_div_psi += loss_div_psi_batch.item()
            total_loss_combined += current_loss.item()
            # End Train Batch Loop

        # --- Averages and Logging ---
        if num_batches == 0: continue
        avg_loss_bb_mse = total_loss_bb_mse / num_batches
        avg_loss_torsion_mse_phi = total_loss_torsion_mse_phi / num_batches
        avg_loss_torsion_mse_psi = total_loss_torsion_mse_psi / num_batches
        avg_loss_div_phi = total_loss_div_phi / num_batches
        avg_loss_div_psi = total_loss_div_psi / num_batches
        avg_loss_combined = total_loss_combined / num_batches

        # --- Validation ---
        model.eval()
        val_total_loss_bb_mse, val_total_loss_torsion_mse_phi, val_total_loss_torsion_mse_psi = 0.0, 0.0, 0.0
        val_total_loss_div_phi, val_total_loss_div_psi, val_total_loss_combined = 0.0, 0.0, 0.0
        num_val_batches = len(test_loader)

        with torch.no_grad():
            for data in test_loader:
                data = data.to(device)
                # Standard forward pass for validation
                pred_bb = model(data.x, z_ref=z_ref, log_debug=False)
                B_times_N, _ = data.y.shape
                B = B_times_N // num_total_atoms if num_total_atoms > 0 and B_times_N % num_total_atoms == 0 else 0
                if B == 0: continue
                coords_3d_gt = data.y.view(B, num_total_atoms, 3)
                gt_backbone = coords_3d_gt[:, model.backbone_indices, :]
                loss_bb_mse = coord_criterion(pred_bb, gt_backbone)
                val_loss = loss_bb_mse
                loss_torsion_mse_phi_batch, loss_torsion_mse_psi_batch = torch.tensor(0.0, device=device), torch.tensor(0.0, device=device)
                loss_div_phi_batch, loss_div_psi_batch = torch.tensor(0.0, device=device), torch.tensor(0.0, device=device)

                if use_dihedral_loss and compute_divergence is not None:
                    full_pred = torch.zeros_like(coords_3d_gt)
                    full_pred[:, model.backbone_indices, :] = pred_bb
                    predicted_angles_dict = compute_all_dihedrals_vectorized(full_pred, dihedral_info_precomputed, num_res)
                    true_angles_dict = compute_all_dihedrals_vectorized(coords_3d_gt, dihedral_info_precomputed, num_res)

                    phi_pred = predicted_angles_dict.get('phi')
                    phi_true = true_angles_dict.get('phi')
                    if phi_pred is not None and phi_true is not None and phi_mask is not None and phi_mask.any():
                        phi_mask_expanded = phi_mask.view(1, -1).expand(B, -1)
                        phi_pred_valid_flat = phi_pred[phi_mask_expanded]
                        phi_true_valid_flat = phi_true[phi_mask_expanded]
                        if phi_pred_valid_flat.numel() > 0:
                             loss_torsion_mse_phi_batch = F.mse_loss(phi_pred_valid_flat, phi_true_valid_flat)
                             loss_div_phi_batch = compute_divergence(phi_pred_valid_flat, phi_true_valid_flat)

                    psi_pred = predicted_angles_dict.get('psi')
                    psi_true = true_angles_dict.get('psi')
                    if psi_pred is not None and psi_true is not None and psi_mask is not None and psi_mask.any():
                        psi_mask_expanded = psi_mask.view(1, -1).expand(B, -1)
                        psi_pred_valid_flat = psi_pred[psi_mask_expanded]
                        psi_true_valid_flat = psi_true[psi_mask_expanded]
                        if psi_pred_valid_flat.numel() > 0:
                             loss_torsion_mse_psi_batch = F.mse_loss(psi_pred_valid_flat, psi_true_valid_flat)
                             loss_div_psi_batch = compute_divergence(psi_pred_valid_flat, psi_true_valid_flat)

                    loss_div_total_batch = loss_div_phi_batch + loss_div_psi_batch
                    loss_torsion_mse_total_batch = loss_torsion_mse_phi_batch + loss_torsion_mse_psi_batch
                    val_loss = val_loss + lambda_1 * loss_div_total_batch + lambda_2 * loss_torsion_mse_total_batch

                # Accumulate validation losses
                val_total_loss_bb_mse += loss_bb_mse.item()
                val_total_loss_torsion_mse_phi += loss_torsion_mse_phi_batch.item()
                val_total_loss_torsion_mse_psi += loss_torsion_mse_psi_batch.item()
                val_total_loss_div_phi += loss_div_phi_batch.item()
                val_total_loss_div_psi += loss_div_psi_batch.item()
                val_total_loss_combined += val_loss.item()
                # End validation batch loop

        # Calculate average validation losses
        if num_val_batches == 0: continue
        avg_val_bb_mse = val_total_loss_bb_mse / num_val_batches
        avg_val_torsion_mse_phi = val_total_loss_torsion_mse_phi / num_val_batches
        avg_val_torsion_mse_psi = val_total_loss_torsion_mse_psi / num_val_batches
        avg_val_div_phi = val_total_loss_div_phi / num_val_batches
        avg_val_div_psi = val_total_loss_div_psi / num_val_batches
        avg_val_combined = val_total_loss_combined / num_val_batches

        # --- Log Epoch Results ---
        div_label = divergence_type.upper()
        log_msg = (
            f"[BackboneDecoder] Epoch {epoch+1}/{epochs} => \n"
            f"  TRAIN: BB_MSE={avg_loss_bb_mse:.4f} | "
            f"Phi(MSE={avg_loss_torsion_mse_phi:.4f}, {div_label}={avg_loss_div_phi:.4f}) | "
            f"Psi(MSE={avg_loss_torsion_mse_psi:.4f}, {div_label}={avg_loss_div_psi:.4f}) | "
            f"TOTAL_Loss={avg_loss_combined:.4f}\n"
            f"  TEST : BB_MSE={avg_val_bb_mse:.4f} | "
            f"Phi(MSE={avg_val_torsion_mse_phi:.4f}, {div_label}={avg_val_div_phi:.4f}) | "
            f"Psi(MSE={avg_val_torsion_mse_psi:.4f}, {div_label}={avg_val_div_psi:.4f}) | "
            f"TOTAL_Loss={avg_val_combined:.4f}"
        )
        logger.info(log_msg)
        sys.stdout.flush()

        # --- Save Checkpoint ---
        current_epoch_num = epoch + 1
        if avg_loss_combined < best_train_loss and optimizer:
             checkpoint_state = {
                 "epoch": current_epoch_num,
                 "model_state_dict": model.state_dict(),
                 "optimizer_state_dict": optimizer.state_dict()}
             save_checkpoint(checkpoint_state, checkpoint_path, logger)
             best_train_loss = avg_loss_combined
             logger.info(f"[Backbone] ↓ new best TRAIN loss {best_train_loss:.4f} – checkpoint saved to {checkpoint_path}")
             sys.stdout.flush()

        # End epoch loop

    logger.info(f"Finished training Backbone decoder. Final checkpoint at {checkpoint_path}")
    # ---- reload the best weights we just saved ----
    model, _, _ = load_checkpoint(model, None, checkpoint_path, device)
    model.eval()                          # switch to inference mode
    logger.info(f"Best backbone weights re‑loaded from {checkpoint_path}")
    return model



def train_sidechain_decoder(
    model: SidechainDecoder, train_loader: DataLoader, test_loader: DataLoader,
    backbone_decoder: BackboneDecoder, device: torch.device, logger: logging.Logger,
    config: dict, checkpoint_path: str, z_ref: torch.Tensor,
    dihedral_info_precomputed: Optional[Dict[str, Dict]] = None,
    dihedral_mask_all: Optional[torch.Tensor] = None, # Shape [num_res, num_angle_types]
    num_res: Optional[int] = None
):
    """ Train Sidechain Decoder. Uses Coord MSE + Optional Dihedral Loss (Div + MSE) for Chi1-5."""
    fraction_dihedral = 0.1
  
    lr = config.get("learning_rate", 0.001)
    epochs = config.get("num_epochs", 50)
    trainable_params = filter(lambda p: p.requires_grad, model.parameters())
    has_params = any(True for _ in model.parameters()) # Check if model actually has parameters
    optimizer = torch.optim.Adam(trainable_params, lr=lr) if has_params else None
    coord_criterion = nn.MSELoss()

    num_atoms = model.num_total_atoms
    start_epoch = 0
    model, optimizer, start_epoch = load_checkpoint(model, optimizer, checkpoint_path, device)
    model.to(device) # Ensure model on device
    backbone_decoder = backbone_decoder.to(device).eval() # Ensure BB decoder on device and eval
    z_ref = z_ref.to(device)

    # --- Sidechain Dihedral Loss Setup ---
    use_dihedral_sc = config.get("use_dihedral_sc", False)
    lambda_1_sc = config.get("lambda_1_sc", 0.0) # Weight for TOTAL SC DIVERGENCE loss
    lambda_2_sc = config.get("lambda_2_sc", 0.0) # Weight for TOTAL SC Torsion MSE loss
    divergence_type_sc = config.get("divergence_type_sc", "KL").upper()
    sidechain_angle_types = ['chi1', 'chi2', 'chi3', 'chi4', 'chi5']
    sc_angle_mask_indices = [] # Indices into columns of dihedral_mask_all

    if use_dihedral_sc:
        if dihedral_info_precomputed is None or dihedral_mask_all is None or num_res is None:
            logger.warning("Sidechain dihedral loss requested but precomputed info missing. Disabling.")
            use_dihedral_sc = False
        else:
            try:
                 angle_types_all = ['phi', 'psi', 'chi1', 'chi2', 'chi3', 'chi4', 'chi5']
                 # Get column indices for chi1-5 in the mask
                 sc_angle_mask_indices = [angle_types_all.index(name) for name in sidechain_angle_types]
                 # Ensure mask is on device
                 dihedral_mask_all = dihedral_mask_all.to(device)
                 logger.info(f"Sidechain Decoder => use_dihedral_sc=True (Chi1-5), Type={divergence_type_sc}, lambda_Div={lambda_1_sc:.4f}, lambda_MSE={lambda_2_sc:.4f}")
            except (ValueError, IndexError) as e:
                 logger.error(f"Error setting up sidechain masks from dihedral_mask_all: {e}. Disabling SC dihedral loss.")
                 use_dihedral_sc = False

    # Select divergence function
    compute_divergence_sc = None
    if use_dihedral_sc:
        if divergence_type_sc == "JS": compute_divergence_sc = compute_angle_js_div
        elif divergence_type_sc == "WASSERSTEIN": compute_divergence_sc = compute_angle_wasserstein
        elif divergence_type_sc == "KL": compute_divergence_sc = compute_angle_kl_div
        else:
            logger.warning(f"Unknown sidechain divergence_type '{divergence_type_sc}'. Defaulting to KL.")
            divergence_type_sc = "KL"
            compute_divergence_sc = compute_angle_kl_div
    # --- End Sidechain Dihedral Loss Setup ---

    logger.info(f"Starting Sidechain Decoder training from epoch {start_epoch+1}, total epochs={epochs}, LR={lr}")
    best_train_loss = float("inf")
    for epoch in range(start_epoch, epochs):
        model.train()
        backbone_decoder.eval()

        # Accumulators
        total_train_loss, total_train_bb_mse, total_train_sc_mse = 0.0, 0.0, 0.0
        total_train_torsion_mse_sc, total_train_div_sc = 0.0, 0.0

        num_batches = len(train_loader)
        for i, data in enumerate(train_loader):
            data = data.to(device)
            if optimizer: optimizer.zero_grad(set_to_none=True)
            log_flag = (i == 0 and epoch == start_epoch and use_debug)

            # Use standard forward pass for training
            with torch.no_grad():
                pred_bb = backbone_decoder(data.x, z_ref=z_ref, log_debug=False) # No override here
            full_pred = model(data.x, pred_bb, z_ref=z_ref, log_debug=log_flag) # No override here

            B_times_N, _ = data.y.shape
            B = B_times_N // num_atoms if num_atoms > 0 and B_times_N % num_atoms == 0 else 0
            if B == 0: continue
            coords_3d_gt = data.y.view(B, num_atoms, 3)

            # --- Base Coordinate Loss ---
            bb_mse = torch.tensor(0.0, device=device)
            if model.backbone_count > 0:
                 bb_pred_from_full = full_pred[:, model.backbone_indices, :]
                 bb_gt = coords_3d_gt[:, model.backbone_indices, :]
                 bb_mse = coord_criterion(bb_pred_from_full, bb_gt)
            sc_mse = torch.tensor(0.0, device=device)
            if model.sidechain_count > 0:
                sc_pred = full_pred[:, model.sidechain_indices, :]
                sc_gt = coords_3d_gt[:, model.sidechain_indices, :]
                sc_mse = coord_criterion(sc_pred, sc_gt)
            current_loss = bb_mse + sc_mse

            # --- Sidechain Dihedral Losses ---
            loss_torsion_mse_sc_batch = torch.tensor(0.0, device=device)
            loss_div_sc_batch = torch.tensor(0.0, device=device)
            if use_dihedral_sc and compute_divergence_sc is not None and random.random() < fraction_dihedral:
                predicted_angles_dict = compute_all_dihedrals_vectorized(full_pred, dihedral_info_precomputed, num_res)
                true_angles_dict = compute_all_dihedrals_vectorized(coords_3d_gt, dihedral_info_precomputed, num_res)

                # Iterate through chi angle types ['chi1', ..., 'chi5']
                for angle_idx, angle_name in enumerate(sidechain_angle_types):
                    mask_col_idx = sc_angle_mask_indices[angle_idx]
                    pred_angles = predicted_angles_dict.get(angle_name)
                    true_angles = true_angles_dict.get(angle_name)
                    # Extract the mask column for this chi angle type
                    mask = dihedral_mask_all[:, mask_col_idx] # [num_res] bool

                    if pred_angles is not None and true_angles is not None and mask.any():
                        mask_expanded = mask.view(1, -1).expand(B, -1)
                        pred_valid_flat = pred_angles[mask_expanded]
                        true_valid_flat = true_angles[mask_expanded]

                        if pred_valid_flat.numel() > 0:
                            # Add MSE for this chi type to the batch total
                            loss_torsion_mse_sc_batch += F.mse_loss(pred_valid_flat, true_valid_flat)
                            # Add Divergence for this chi type to the batch total
                            loss_div_sc_batch += compute_divergence_sc(pred_valid_flat, true_valid_flat)

                # Add weighted total SC dihedral losses to current loss
                current_loss = current_loss + lambda_1_sc * loss_div_sc_batch + lambda_2_sc * loss_torsion_mse_sc_batch
            # --- End Dihedral Loss ---

            # Backpropagation
            if optimizer and current_loss.requires_grad:
                current_loss.backward()
                optimizer.step()

            # Accumulate losses
            total_train_loss += current_loss.item()
            total_train_bb_mse += bb_mse.item()
            total_train_sc_mse += sc_mse.item()
            total_train_torsion_mse_sc += loss_torsion_mse_sc_batch.item()
            total_train_div_sc += loss_div_sc_batch.item()
            # End train batch loop

        # --- Averages and Logging ---
        if num_batches == 0: continue
        avg_train_loss = total_train_loss / num_batches
        avg_train_bb = total_train_bb_mse / num_batches
        avg_train_sc = total_train_sc_mse / num_batches
        avg_train_torsion_mse_sc = total_train_torsion_mse_sc / num_batches
        avg_train_div_sc = total_train_div_sc / num_batches

        # --- Validation ---
        model.eval()
        val_total_loss, val_total_bb_mse, val_total_sc_mse = 0.0, 0.0, 0.0
        val_total_torsion_mse_sc, val_total_div_sc = 0.0, 0.0
        num_val_batches = len(test_loader)

        with torch.no_grad():
            for data in test_loader:
                data = data.to(device)
                # Standard forward pass for validation
                pred_bb = backbone_decoder(data.x, z_ref=z_ref, log_debug=False)
                full_pred = model(data.x, pred_bb, z_ref=z_ref, log_debug=False)
                B_times_N, _ = data.y.shape
                B = B_times_N // num_atoms if num_atoms > 0 and B_times_N % num_atoms == 0 else 0
                if B == 0: continue
                coords_3d_gt = data.y.view(B, num_atoms, 3)

                # Base loss
                bb_mse = torch.tensor(0.0, device=device)
                if model.backbone_count > 0:
                     bb_pred_from_full = full_pred[:, model.backbone_indices, :]
                     bb_gt = coords_3d_gt[:, model.backbone_indices, :]
                     bb_mse = coord_criterion(bb_pred_from_full, bb_gt)
                sc_mse = torch.tensor(0.0, device=device)
                if model.sidechain_count > 0:
                     sc_pred = full_pred[:, model.sidechain_indices, :]
                     sc_gt = coords_3d_gt[:, model.sidechain_indices, :]
                     sc_mse = coord_criterion(sc_pred, sc_gt)
                val_loss = bb_mse + sc_mse

                # SC Dihedral loss
                loss_torsion_mse_sc_batch = torch.tensor(0.0, device=device)
                loss_div_sc_batch = torch.tensor(0.0, device=device)
                if use_dihedral_sc and compute_divergence_sc is not None:
                    predicted_angles_dict = compute_all_dihedrals_vectorized(full_pred, dihedral_info_precomputed, num_res)
                    true_angles_dict = compute_all_dihedrals_vectorized(coords_3d_gt, dihedral_info_precomputed, num_res)
                    for angle_idx, angle_name in enumerate(sidechain_angle_types):
                        mask_col_idx = sc_angle_mask_indices[angle_idx]
                        pred_angles = predicted_angles_dict.get(angle_name)
                        true_angles = true_angles_dict.get(angle_name)
                        mask = dihedral_mask_all[:, mask_col_idx]
                        if pred_angles is not None and true_angles is not None and mask.any():
                            mask_expanded = mask.view(1, -1).expand(B, -1)
                            pred_valid_flat = pred_angles[mask_expanded]
                            true_valid_flat = true_angles[mask_expanded]
                            if pred_valid_flat.numel() > 0:
                                 loss_torsion_mse_sc_batch += F.mse_loss(pred_valid_flat, true_valid_flat)
                                 loss_div_sc_batch += compute_divergence_sc(pred_valid_flat, true_valid_flat)
                    val_loss = val_loss + lambda_1_sc * loss_div_sc_batch + lambda_2_sc * loss_torsion_mse_sc_batch

                # Accumulate validation losses
                val_total_loss += val_loss.item()
                val_total_bb_mse += bb_mse.item()
                val_total_sc_mse += sc_mse.item()
                val_total_torsion_mse_sc += loss_torsion_mse_sc_batch.item()
                val_total_div_sc += loss_div_sc_batch.item()
                # End validation batch loop

        # --- Calculate Averages and Log ---
        if num_val_batches == 0: continue
        avg_test_loss = val_total_loss / num_val_batches
        avg_test_bb = val_total_bb_mse / num_val_batches
        avg_test_sc = val_total_sc_mse / num_val_batches
        avg_test_torsion_mse_sc = val_total_torsion_mse_sc / num_val_batches
        avg_test_div_sc = val_total_div_sc / num_val_batches

        div_label_sc = divergence_type_sc.upper()
        log_msg = (
            f"[SidechainDecoder] Epoch {epoch+1}/{epochs} => \n"
            f"  TRAIN: TotalLoss={avg_train_loss:.4f} | Coord(BB={avg_train_bb:.4f}, SC={avg_train_sc:.4f}) | "
            f"SC_Dihedral(MSE={avg_train_torsion_mse_sc:.4f}, {div_label_sc}={avg_train_div_sc:.4f})\n"
            f"  TEST : TotalLoss={avg_test_loss:.4f} | Coord(BB={avg_test_bb:.4f}, SC={avg_test_sc:.4f}) | "
            f"SC_Dihedral(MSE={avg_test_torsion_mse_sc:.4f}, {div_label_sc}={avg_test_div_sc:.4f})"
        )
        logger.info(log_msg)
        sys.stdout.flush()

        # --- Save Checkpoint ---
        current_epoch_num = epoch + 1
        if avg_train_loss < best_train_loss and optimizer:
            checkpoint_state = {
                 "epoch": current_epoch_num,
                 "model_state_dict": model.state_dict(),
                 "optimizer_state_dict": optimizer.state_dict()}
            save_checkpoint(checkpoint_state, checkpoint_path, logger)
            logger.info(f"Sidechain decoder checkpoint saved at epoch {current_epoch_num} -> {checkpoint_path}")
            best_train_loss = avg_train_loss
            logger.info(f"[Side‑chain] ↓ new best TRAIN loss {best_train_loss:.4f} – checkpoint saved to {checkpoint_path}")
            sys.stdout.flush()
        # End Epoch Loop

    logger.info(f"Finished training Sidechain decoder. Final checkpoint at {checkpoint_path}")
    model, _, _ = load_checkpoint(model, None, checkpoint_path, device)
    model.eval()
    logger.info(f"Best side‑chain weights re‑loaded from {checkpoint_path}")
    return model



#################################################################
# (K) Output Generation (Modified for Diffusion Override)
#################################################################


#################################################################
# (K) Output Generation (Modified for Diffusion Override & Debugging)
#################################################################
@torch.no_grad()
def export_final_outputs(
    raw_dataset: List[Data], dec_dataset: List[Data], hno_model: HNO,
    backbone_decoder: BackboneDecoder, sidechain_decoder: SidechainDecoder,
    z_ref: torch.Tensor, num_atoms: int, struct_dir: str, latent_dir: str,
    device: torch.device,
    use_diff: bool = False, diff_bb: Optional[torch.Tensor] = None, diff_sc: Optional[torch.Tensor] = None
):
    """
    Exports final predictions and intermediate results to HDF5 files.
    Includes standard export and optional diffusion override export with added debugging.
    """
    global logger # Assuming logger is defined globally

    logger.info("Exporting final outputs: Ground Truth, HNO, Backbone, Full Coords, Pooled Embeddings.")

    hno_model.eval().to(device)
    backbone_decoder.eval().to(device)
    sidechain_decoder.eval().to(device)
    z_ref = z_ref.to(device)

    # Get indices and counts from models
    # Ensure these are tensors on CPU for indexing numpy arrays later if needed, although models keep buffers on CPU anyway.
    backbone_indices = backbone_decoder.backbone_indices.cpu() # Get from buffer
    sidechain_indices = sidechain_decoder.sidechain_indices.cpu() # Get from buffer
    backbone_count = len(backbone_indices)
    sidechain_count = len(sidechain_indices)

    # Define STANDARD output file paths
    gt_path = os.path.join(struct_dir, "ground_truth_aligned.h5")
    hno_recon_path = os.path.join(struct_dir, "hno_reconstructions.h5")
    backbone_path = os.path.join(struct_dir, "backbone_coords.h5")
    full_path = os.path.join(struct_dir, "full_coords.h5")
    backbone_pooled_path = os.path.join(latent_dir, "backbone_pooled.h5")
    sidechain_pooled_path = os.path.join(latent_dir, "sidechain_pooled.h5")

    # Define potential diffusion output paths
    backbone_path_diff = os.path.join(struct_dir, "backbone_coords_diff.h5")
    full_path_diff = os.path.join(struct_dir, "full_coords_diff.h5")

    total_samples = len(raw_dataset)
    if len(dec_dataset) != total_samples:
        logger.warning(f"Dataset length mismatch: Raw ({total_samples}) vs Dec ({len(dec_dataset)}). Using minimum.")
        total_samples = min(total_samples, len(dec_dataset))
    logger.info(f"Exporting {total_samples} standard samples.")

    bb_pool_dim = backbone_decoder.pool_output_dim
    sc_pool_dim = sidechain_decoder.pool_output_dim if sidechain_count > 0 else 0

    # --- Standard Export Loop ---
    dset_gt, dset_hno, dset_bb, dset_full, dset_bbpool, dset_scpool = None, None, None, None, None, None
    try:
        logger.debug("DEBUG: Entering standard export 'try' block.")
        with h5py.File(gt_path, "w") as gt_h5, \
             h5py.File(hno_recon_path, "w") as hno_h5, \
             h5py.File(backbone_path, "w") as bb_h5, \
             h5py.File(full_path, "w") as full_h5, \
             h5py.File(backbone_pooled_path, "w") as bbp_h5, \
             h5py.File(sidechain_pooled_path, "w") as scp_h5:

            logger.debug("DEBUG: Opened standard HDF5 files.")
            # Create datasets (standard)
            dset_gt = gt_h5.create_dataset("ground_truth_coords", (total_samples, num_atoms, 3), dtype='float32') if num_atoms > 0 else None
            dset_hno = hno_h5.create_dataset("hno_coords", (total_samples, num_atoms, 3), dtype='float32') if num_atoms > 0 else None
            dset_bb = bb_h5.create_dataset("backbone_coords", (total_samples, backbone_count, 3), dtype='float32') if backbone_count > 0 else None
            dset_full = full_h5.create_dataset("full_coords", (total_samples, num_atoms, 3), dtype='float32') if num_atoms > 0 else None
            dset_bbpool = bbp_h5.create_dataset("backbone_pooled", (total_samples, bb_pool_dim), dtype='float32') if bb_pool_dim > 0 else None
            dset_scpool = scp_h5.create_dataset("sidechain_pooled", (total_samples, sc_pool_dim), dtype='float32') if sc_pool_dim > 0 else None
            logger.debug(f"DEBUG: Created standard datasets (GT: {dset_gt is not None}, HNO: {dset_hno is not None}, BB: {dset_bb is not None}, Full: {dset_full is not None}, BBPool: {dset_bbpool is not None}, SCPool: {dset_scpool is not None})")

            logger.debug("DEBUG: Starting standard export loop.")
            for idx in range(total_samples):
                raw_data = raw_dataset[idx].to(device)
                dec_data = dec_dataset[idx].to(device)
                hno_latent = dec_data.x # Shape [N, E]
                hno_latent_batch = hno_latent.unsqueeze(0) # Shape [1, N, E]

                # 1. Ground Truth
                if dset_gt is not None:
                    dset_gt[idx] = dec_data.y.cpu().numpy()

                # 2. HNO Reconstruction
                if dset_hno is not None:
                    hno_recon = hno_model(raw_data.x, raw_data.edge_index)
                    dset_hno[idx] = hno_recon.cpu().numpy()

                # --- Process using INTERNAL pooling ---
                pred_bb = torch.empty(1, 0, 3, device=device) # Default

                # 3. Backbone Prediction (Standard)
                if dset_bb is not None:
                    pred_bb = backbone_decoder(hno_latent_batch, z_ref=z_ref) # NO override
                    bb_np_array = pred_bb.squeeze(0).cpu().numpy()
                    dset_bb[idx] = bb_np_array
                    if dset_bbpool is not None and hasattr(backbone_decoder, "last_pooled_backbone") and backbone_decoder.last_pooled_backbone is not None:
                         dset_bbpool[idx] = backbone_decoder.last_pooled_backbone.numpy()

                # 4. Full Structure Prediction (Standard)
                if dset_full is not None:
                    full_pred = sidechain_decoder(hno_latent_batch, pred_bb, z_ref=z_ref) # NO override
                    full_np_array = full_pred.squeeze(0).cpu().numpy()
                    dset_full[idx] = full_np_array
                    if dset_scpool is not None and hasattr(sidechain_decoder, "last_pooled_sidechain") and sidechain_decoder.last_pooled_sidechain is not None:
                        dset_scpool[idx] = sidechain_decoder.last_pooled_sidechain.numpy()

                if (idx + 1) % 500 == 0 or (idx + 1) == total_samples:
                     logger.info(f"Exported standard results for {idx+1}/{total_samples} samples...")
            logger.debug("DEBUG: Finished standard export loop.")
        logger.debug("DEBUG: Exited standard export 'with h5py.File...' block.")

    except Exception as e:
        logger.error(f"Error during standard HDF5 export: {e}", exc_info=True)
        logger.debug(f"DEBUG standard export exception details:", exc_info=True)
        # Depending on severity, you might want to return here or allow diffusion export to proceed

    # --- Diffusion Override Export ---
    dset_bb_diff, dset_full_diff = None, None # For final logging status check
    logger.debug(f"DEBUG export_final_outputs: Checking diffusion export condition. Received use_diff = {use_diff}")
    if use_diff:
        logger.debug("DEBUG: use_diff is True, proceeding with diffusion checks.")
        if diff_bb is None or diff_sc is None:
             logger.error("use_diff=True but diff_bb or diff_sc is None in export_final_outputs. Cannot perform diffusion export.")
             logger.debug(f"DEBUG export_final_outputs: Skipping diffusion export because diff_bb is None: {diff_bb is None}, or diff_sc is None: {diff_sc is None}")
        else:
            logger.debug(f"DEBUG: diff_bb shape: {diff_bb.shape}, diff_sc shape: {diff_sc.shape}")
            N_data = len(dec_dataset)
            N_diff_bb = diff_bb.shape[0]
            N_diff_sc = diff_sc.shape[0]
            N_diff = min(N_data, N_diff_bb, N_diff_sc)
            logger.debug(f"DEBUG: Calculated N_data={N_data}, N_diff_bb={N_diff_bb}, N_diff_sc={N_diff_sc}, N_diff={N_diff}")

            # Corrected Logic: Export if N_diff > 0
            if N_diff == 0:
                 logger.warning("No diffused embeddings available or dataset empty (N_diff=0). Skipping diffusion export.")
                 logger.debug("DEBUG: Skipping diffusion export because N_diff is 0.")
            else: # N_diff > 0
                if N_diff < N_data:
                    logger.warning(f"Number of diffused embeddings ({N_diff_bb} BB, {N_diff_sc} SC) is less than dataset size ({N_data}). Exporting only {N_diff} diffused samples.")
                # This log should always appear if N_diff > 0
                logger.info(f"Exporting {N_diff} samples using diffused embeddings.")
                try:
                    logger.debug("DEBUG: Entered diffusion export 'try' block.")
                    with h5py.File(backbone_path_diff, "w") as bb_diff_h5, \
                         h5py.File(full_path_diff, "w") as full_diff_h5:

                        logger.debug(f"DEBUG: Attempting to open diffusion HDF5 files: {backbone_path_diff}, {full_path_diff}")
                        logger.debug("DEBUG: Successfully opened diffusion HDF5 files for writing.")

                        # Create diffusion datasets
                        dset_bb_diff = bb_diff_h5.create_dataset("backbone_coords_diff", (N_diff, backbone_count, 3), dtype='float32') if backbone_count > 0 else None
                        logger.debug(f"DEBUG: Created dset_bb_diff: {dset_bb_diff} (Type: {type(dset_bb_diff)})")

                        dset_full_diff = full_diff_h5.create_dataset("full_coords_diff", (N_diff, num_atoms, 3), dtype='float32') if num_atoms > 0 else None
                        logger.debug(f"DEBUG: Created dset_full_diff: {dset_full_diff} (Type: {type(dset_full_diff)})")

                        # Check if datasets were actually created before looping
                        skip_loop = dset_bb_diff is None and dset_full_diff is None
                        logger.debug(f"DEBUG: Checking loop skip condition: dset_bb_diff is None ({dset_bb_diff is None}) AND dset_full_diff is None ({dset_full_diff is None}) -> Skip = {skip_loop}")
                        if skip_loop:
                             logger.warning("Neither backbone nor full diff datasets seem to have been created (check counts or HDF5 permissions?). Skipping diff export loop.")
                        else:
                             logger.debug(f"DEBUG export_final_outputs: Proceeding with diffusion export loop for N_diff = {N_diff} samples.")
                             for idx in range(N_diff):
                                # --- Limit verbose logs to first few iterations ---
                                log_this_iter = idx < 3 # Log verbosely for idx 0, 1, 2

                                if log_this_iter:
                                    logger.debug(f"DEBUG Loop idx={idx}: Starting processing.")
                                try:
                                    dec_data = dec_dataset[idx].to(device)
                                    hno_latent = dec_data.x
                                    hno_latent_batch = hno_latent.unsqueeze(0)

                                    bb_over = diff_bb[idx:idx+1, :].to(device)
                                    sc_over = diff_sc[idx:idx+1, :].to(device)
                                    if log_this_iter:
                                        logger.debug(f"DEBUG Loop idx={idx}: bb_over shape {bb_over.shape}, sc_over shape {sc_over.shape}")

                                    pred_bb_diff = torch.empty(1, 0, 3, device=device)
                                    if dset_bb_diff is not None:
                                        if log_this_iter:
                                            logger.debug(f"DEBUG Loop idx={idx}: Calling backbone_decoder with override.")
                                        pred_bb_diff = backbone_decoder(hno_latent_batch, z_ref=z_ref, override_pooled_backbone=bb_over)

                                        if log_this_iter: # Log stats/shape only for first few
                                            logger.debug(f"DEBUG Loop idx={idx}: Shape of pred_bb_diff: {pred_bb_diff.shape}")
                                            if pred_bb_diff.numel() > 0: # Avoid errors on empty tensors
                                                logger.debug(f"DEBUG Loop idx={idx}: Stats pred_bb_diff: min={pred_bb_diff.min().item():.3f}, max={pred_bb_diff.max().item():.3f}, mean={pred_bb_diff.mean().item():.3f}, has_nan={torch.isnan(pred_bb_diff).any().item()}")
                                            else:
                                                 logger.debug(f"DEBUG Loop idx={idx}: pred_bb_diff is empty.")


                                        bb_np_array = pred_bb_diff.squeeze(0).cpu().numpy()
                                        if log_this_iter: # Log shape only for first few
                                            logger.debug(f"DEBUG Loop idx={idx}: Shape of bb_np_array for HDF5: {bb_np_array.shape}")
                                        dset_bb_diff[idx] = bb_np_array
                                        # if log_this_iter: # Log assignment only for first few if needed
                                        #     logger.debug(f"DEBUG Loop idx={idx}: Assigned to dset_bb_diff.")


                                    if dset_full_diff is not None:
                                        if log_this_iter:
                                            logger.debug(f"DEBUG Loop idx={idx}: Calling sidechain_decoder with override.")
                                        full_pred_diff = sidechain_decoder(hno_latent_batch, pred_bb_diff, z_ref=z_ref, override_pooled_sidechain=sc_over)

                                        if log_this_iter: # Log stats/shape only for first few
                                            logger.debug(f"DEBUG Loop idx={idx}: Shape of full_pred_diff: {full_pred_diff.shape}")
                                            if full_pred_diff.numel() > 0:
                                                logger.debug(f"DEBUG Loop idx={idx}: Stats full_pred_diff: min={full_pred_diff.min().item():.3f}, max={full_pred_diff.max().item():.3f}, mean={full_pred_diff.mean().item():.3f}, has_nan={torch.isnan(full_pred_diff).any().item()}")
                                            else:
                                                 logger.debug(f"DEBUG Loop idx={idx}: full_pred_diff is empty.")


                                        full_np_array = full_pred_diff.squeeze(0).cpu().numpy()
                                        if log_this_iter: # Log shape only for first few
                                            logger.debug(f"DEBUG Loop idx={idx}: Shape of full_np_array for HDF5: {full_np_array.shape}")
                                        dset_full_diff[idx] = full_np_array
                                        # if log_this_iter: # Log assignment only for first few if needed
                                        #     logger.debug(f"DEBUG Loop idx={idx}: Assigned to dset_full_diff.")

                                    # Keep the INFO progress log unconditional
                                    if (idx + 1) % 500 == 0 or (idx + 1) == N_diff:
                                        logger.info(f"Exported diffusion override results for {idx+1}/{N_diff} samples...")

                                except Exception as loop_e:
                                     logger.error(f"Error during diffusion export loop at index {idx}: {loop_e}", exc_info=True)
                                     logger.debug(f"DEBUG Loop idx={idx}: Exception details:", exc_info=True)

                             logger.debug("DEBUG: Finished diffusion export loop.") # Keep this unconditional


                    logger.debug("DEBUG: Exited diffusion 'with h5py.File...' block.")
                    logger.info(f"Saved diffusion-based coords to:\n  {backbone_path_diff}\n  {full_path_diff}")

                except Exception as e:
                     logger.error(f"Error during diffusion HDF5 export setup or file handling: {e}", exc_info=True)
                     logger.debug(f"DEBUG: Diffusion export setup exception details:", exc_info=True)
    else:
        # Log reason for skipping if use_diff was False initially or became False
        logger.debug(f"DEBUG export_final_outputs: Skipping diffusion export block because use_diff flag is {use_diff}.")
        # Assuming 'args' is accessible or passed if needed for this specific debug line
        # if 'args' in globals() and not args.use_diffusion:
        #     logger.debug("DEBUG export_final_outputs: --use_diffusion flag was likely not provided.")
        # else:
        #      logger.debug("DEBUG export_final_outputs: --use_diffusion flag WAS provided, but override was likely disabled due to loading/shape errors in main.")
        logger.info("Diffusion override not requested or embeddings not validated; skipping diff output.")




#################################################################
# (L) Main Orchestration Function
#################################################################
def main():
    logger.info(f"Script started. Using device: {device}")

    # --- Setup Directories ---
    out_dirs = config.get("output_directories", {})
    ckpt_dir = out_dirs.get("checkpoint_dir", "checkpoints")
    struct_dir = out_dirs.get("structure_dir", "structures")
    latent_dir = out_dirs.get("latent_dir", "latent_reps")
    try: os.makedirs(ckpt_dir, exist_ok=True); os.makedirs(struct_dir, exist_ok=True); os.makedirs(latent_dir, exist_ok=True)
    except OSError as e: logger.error(f"Error creating output directories: {e}"); sys.exit(1)

    # --- Load Config Parameters ---
    json_path = config.get("json_path")
    pdb_filename = config.get("pdb_filename")
    if not json_path or not pdb_filename: logger.error("Missing 'json_path' or 'pdb_filename' in config."); sys.exit(1)

    num_workers = config.get("num_workers", 0)
    pin_memory = (device.type == "cuda")

    # --- Data Loading and Preprocessing ---
    coords_per_frame, json_num_atoms = load_heavy_atom_coords_from_json(json_path, logger)
    if not coords_per_frame: logger.error("Failed to load coordinates from JSON."); sys.exit(1)

    logger.info(f"Parsing PDB: {pdb_filename}")
    _, atoms_in_order = parse_pdb(pdb_filename, logger)
    if not atoms_in_order: logger.error("Failed to parse PDB."); sys.exit(1)
    renumbered_dict, _ = renumber_atoms_and_residues(atoms_in_order, logger)
    bb_indices_list, sc_indices_list = get_global_indices(renumbered_dict)
    backbone_indices = torch.tensor(bb_indices_list, dtype=torch.long) # CPU tensor
    sidechain_indices = torch.tensor(sc_indices_list, dtype=torch.long) # CPU tensor
    num_atoms_pdb = len(backbone_indices) + len(sidechain_indices)

    if json_num_atoms != num_atoms_pdb: logger.error(f"Atom count mismatch! JSON ({json_num_atoms}) != PDB ({num_atoms_pdb})."); sys.exit(1)
    num_atoms = num_atoms_pdb
    logger.info(f"Found {len(backbone_indices)} backbone and {len(sidechain_indices)} sidechain atoms (Total: {num_atoms}).")

    coords_aligned = align_frames_to_first(coords_per_frame, logger, device)
    if not coords_aligned: logger.error("Failed to align coordinates."); sys.exit(1)

    knn_value = config.get("knn_value", 4)
    dataset = build_graph_dataset(coords_aligned, knn_neighbors=knn_value, logger=logger, device=device)
    if not dataset: logger.error("Failed to build graph dataset."); sys.exit(1)

    # --- 1) HNO Training/Loading ---
    hno_conf = config.get("hno_training", {})
    hno_batch_size = hno_conf.get("batch_size", 32)
    train_data_hno, test_data_hno = train_test_split(dataset, test_size=0.1, random_state=42)
    train_loader_hno = DataLoader(train_data_hno, batch_size=hno_batch_size, shuffle=True, num_workers=num_workers, pin_memory=pin_memory, drop_last=True)
    test_loader_hno = DataLoader(test_data_hno, batch_size=hno_batch_size, shuffle=False, num_workers=num_workers, pin_memory=pin_memory)

    cheb_order = config.get("cheb_order", 3)
    hidden_dim = config.get("hidden_dim", 128)
    hno_model = HNO(hidden_dim, K=cheb_order) # Initialized on CPU
    hno_ckpt = os.path.join(ckpt_dir, config.get("hno_ckpt", "hno_model.pth"))
    logger.info(f"Training/loading HNO => {hno_conf.get('num_epochs', 0)} epochs, LR={hno_conf.get('learning_rate', 0.001)}")
    hno_model = train_hno_model(
        model=hno_model, train_loader=train_loader_hno, test_loader=test_loader_hno,
        num_epochs=hno_conf.get("num_epochs", 0), learning_rate=hno_conf.get("learning_rate", 0.001),
        checkpoint_path=hno_ckpt, save_interval=hno_conf.get("save_interval", 10), device=device)
    hno_model.eval().to(device) # Ensure on correct device and eval mode

    # --- 2) Build Decoder Dataset ---
    logger.info("Building decoder dataset (latent embeddings)...")
    dec_dataset = []
    inference_batch_size = config.get("inference_batch_size", hno_batch_size * 2)
    inference_loader = DataLoader(dataset, batch_size=inference_batch_size, shuffle=False, num_workers=num_workers, pin_memory=pin_memory)
    with torch.no_grad():
        for data_batch in inference_loader:
            data_batch = data_batch.to(device)
            # Use forward_representation to get embeddings
            x_emb_batch = hno_model.forward_representation(data_batch.x, data_batch.edge_index)
            y_batch = data_batch.y # Ground truth coordinates
            num_graphs = data_batch.num_graphs
            node_slices = torch.cumsum(torch.bincount(data_batch.batch), 0)
            node_slices = torch.cat([torch.tensor([0], device=device), node_slices])
            # Split batch back into individual graphs
            for i in range(num_graphs):
                 start, end = node_slices[i], node_slices[i+1]
                 # Store embedding as 'x' and true coords as 'y' on CPU
                 dec_dataset.append(Data(x=x_emb_batch[start:end].cpu(), y=y_batch[start:end].cpu()))
    logger.info(f"Built decoder dataset with {len(dec_dataset)} samples.")

    # --- 3) Split Decoder Dataset ---
    dec_batch_size = config.get("decoder_batch_size", 16)
    train_data_dec, test_data_dec = train_test_split(dec_dataset, test_size=0.1, random_state=42)
    train_loader_dec = DataLoader(train_data_dec, batch_size=dec_batch_size, shuffle=True, num_workers=num_workers, pin_memory=pin_memory, drop_last=True)
    test_loader_dec = DataLoader(test_data_dec, batch_size=dec_batch_size, shuffle=False, num_workers=num_workers, pin_memory=pin_memory)

    # --- 4) Calculate z_ref ---
    logger.info("Computing z_ref...")
    with torch.no_grad():
        # Use the first sample from the original dataset (which has coords in 'x')
        first_frame_data = dataset[0].to(device)
        z_ref = hno_model.forward_representation(first_frame_data.x, first_frame_data.edge_index, log_debug=use_debug)
    logger.debug(f"z_ref calculated => shape {z_ref.shape}, device {z_ref.device}")

    # --- ADDED: Save X_ref and z_ref ---
    try:
        x_ref_path = os.path.join(struct_dir, "X_ref_coords.pt")
        z_ref_path = os.path.join(struct_dir, "z_ref_embedding.pt")
        # Ensure X_ref (first aligned coords) is on CPU before saving
        X_ref_cpu = coords_aligned[0].cpu() # coords_aligned is already on CPU
        torch.save(X_ref_cpu, x_ref_path)
        # Ensure z_ref is on CPU before saving
        z_ref_cpu = z_ref.cpu()
        torch.save(z_ref_cpu, z_ref_path)
        logger.info(f"Saved reference coordinates to: {x_ref_path}")
        logger.info(f"Saved reference embedding to: {z_ref_path}")
    except Exception as e:
        logger.error(f"Error saving X_ref or z_ref: {e}", exc_info=True)
    # --- END ADDED ---


    # --- 5) Precompute Dihedral Info ---
    torsion_json_path = config.get("torsion_info_path", "condensed_residues.json")
    dihedral_info_precomputed = {} # Dict: name -> {'indices': [...], 'res_idx': tensor}
    dihedral_mask_all = None     # Tensor: [num_res, 7] bool mask
    num_res = None
    angle_types_all = ['phi', 'psi', 'chi1', 'chi2', 'chi3', 'chi4', 'chi5']
    num_angle_types = len(angle_types_all)

    if os.path.isfile(torsion_json_path):
        try:
            with open(torsion_json_path, "r") as f: torsion_info = json.load(f)
            logger.info(f"Torsion info loaded from {torsion_json_path}")
            logger.info("Precomputing ALL dihedral angle indices and masks...")

            indices_lists = {name: [[], [], [], []] for name in angle_types_all}
            residue_indices_lists = {name: [] for name in angle_types_all}

            try:
                 torsion_keys_sorted = sorted([int(k) for k in torsion_info.keys()])
                 num_res = len(torsion_keys_sorted)
                 logger.info(f"Found torsion info for {num_res} residues.")
            except ValueError: logger.error("Invalid torsion JSON keys."); torsion_info = None

            if torsion_info and num_res is not None and num_res > 0:
                 valid_angle_mask_list = [[False] * num_angle_types for _ in range(num_res)]
                 # Loop through residues and angle types
                 for r_idx, res_id_int in enumerate(torsion_keys_sorted):
                      res_str = str(res_id_int)
                      res_data = torsion_info.get(res_str, {})
                      torsion_atoms = res_data.get("torsion_atoms", {})
                      chi_atoms = torsion_atoms.get("chi", {})
                      for type_idx, angle_name in enumerate(angle_types_all):
                           indices = None
                           if angle_name in ['phi', 'psi']: indices = torsion_atoms.get(angle_name, None)
                           elif angle_name.startswith('chi') and angle_name in chi_atoms: indices = chi_atoms.get(angle_name, None)

                           if isinstance(indices, list) and len(indices) == 4 and None not in indices:
                                if all(0 <= idx < num_atoms for idx in indices):
                                     for list_idx, atom_idx in enumerate(indices):
                                          indices_lists[angle_name][list_idx].append(atom_idx)
                                     residue_indices_lists[angle_name].append(r_idx)
                                     valid_angle_mask_list[r_idx][type_idx] = True

                 # Convert lists to tensors
                 try:
                      for angle_name in angle_types_all:
                           if residue_indices_lists[angle_name]:
                                dihedral_info_precomputed[angle_name] = {
                                     'indices': [torch.tensor(lst, dtype=torch.long, device=device) for lst in indices_lists[angle_name]],
                                     'res_idx': torch.tensor(residue_indices_lists[angle_name], dtype=torch.long, device=device) }
                           else: dihedral_info_precomputed[angle_name] = {'indices': None, 'res_idx': None}
                      dihedral_mask_all = torch.tensor(valid_angle_mask_list, dtype=torch.bool, device=device)
                      logger.info("Finished precomputing dihedral info.")
                      if use_debug and dihedral_mask_all is not None:
                          logger.debug(f"DEBUG [main]: dihedral_mask_all shape: {dihedral_mask_all.shape}")
                          # Add more debug prints for mask sums if needed

                 except Exception as e: logger.error(f"Error converting dihedral lists to tensors: {e}", exc_info=True); dihedral_info_precomputed={}; dihedral_mask_all=None; num_res=None
            else: logger.warning("Invalid or empty torsion info, dihedral loss disabled."); dihedral_info_precomputed={}; dihedral_mask_all=None; num_res=None
        except Exception as e: logger.error(f"Error loading/processing torsion file {torsion_json_path}: {e}", exc_info=True); dihedral_info_precomputed={}; dihedral_mask_all=None; num_res=None
    else: logger.warning(f"Torsion file not found: {torsion_json_path}. Dihedral loss disabled.")

    # --- 6) Backbone Decoder Training/Loading ---
    stepA_conf = config.get("decoderB_training", {})
    pooling_dim_backbone = tuple(config.get("pooling_dim_backbone", [20, 4]))
    backbone_decoder_ckpt = os.path.join(ckpt_dir, config.get("bb_decoder_ckpt", "decoder_backbone.pth"))
    backbone_decoder_model = BackboneDecoder(
        num_total_atoms=num_atoms, backbone_indices=backbone_indices, emb_dim=hidden_dim,
        pooling_dim=pooling_dim_backbone, mlp_depth=stepA_conf.get("decoder_depth", 2),
        mlp_hidden_dim=stepA_conf.get("mlp_hidden_dim", 128))
    logger.info("--- Training/loading Backbone Decoder ---")
    backbone_decoder_model = train_backbone_decoder(
        model=backbone_decoder_model, train_loader=train_loader_dec, test_loader=test_loader_dec,
        device=device, logger=logger, config=stepA_conf, checkpoint_path=backbone_decoder_ckpt,
        z_ref=z_ref, dihedral_info_precomputed=dihedral_info_precomputed,
        dihedral_mask_all=dihedral_mask_all, num_res=num_res)
    backbone_decoder_model.eval().to(device) # Ensure on device and eval mode

    # --- 7) Sidechain Decoder Training/Loading ---
    stepB_conf = config.get("decoderSC_training", {})
    pooling_dim_sidechain = tuple(config.get("pooling_dim_sidechain", [20, 4]))
    sidechain_decoder_ckpt = os.path.join(ckpt_dir, config.get("sc_decoder_ckpt","decoder_sidechain.pth"))
    sidechain_decoder_model = SidechainDecoder(
        num_total_atoms=num_atoms, sidechain_indices=sidechain_indices, backbone_indices=backbone_indices,
        emb_dim=hidden_dim, pooling_dim=pooling_dim_sidechain, mlp_depth=stepB_conf.get("decoder_depth", 2),
        mlp_hidden_dim=stepB_conf.get("mlp_hidden_dim", 128), arch_type=stepB_conf.get("arch_type", 0))
    logger.info("--- Training/loading Sidechain Decoder ---")
    sidechain_decoder_model = train_sidechain_decoder(
        model=sidechain_decoder_model, train_loader=train_loader_dec, test_loader=test_loader_dec,
        backbone_decoder=backbone_decoder_model, device=device, logger=logger,
        config=stepB_conf, checkpoint_path=sidechain_decoder_ckpt, z_ref=z_ref,
        dihedral_info_precomputed=dihedral_info_precomputed, dihedral_mask_all=dihedral_mask_all, num_res=num_res)
    sidechain_decoder_model.eval().to(device) # Ensure on device and eval mode

    logger.info("All training tasks completed successfully!")

    # --- ADDED: Step 7.5 - Load Optional Diffused Embeddings ---
    # --- MODIFIED: Step 7.5 - Load Optional Diffused Embeddings with Debugging ---
    diff_bb_torch = None
    diff_sc_torch = None
    use_diffusion_override = False # Flag to pass to export function
    bb_pool_dim_expected = backbone_decoder_model.pool_output_dim
    sc_pool_dim_expected = sidechain_decoder_model.pool_output_dim

    # Log expected dimensions regardless of whether override is used
    logger.debug(f"DEBUG: Expected BB pool dim: {bb_pool_dim_expected}")
    logger.debug(f"DEBUG: Expected SC pool dim: {sc_pool_dim_expected}")

    if args.use_diffusion:
        if not args.diffused_backbone_h5 or not os.path.isfile(args.diffused_backbone_h5):
            logger.warning(f"use_diffusion=True but diffused_backbone_h5 path invalid or not found: {args.diffused_backbone_h5}. Skipping diffusion override.")
        elif not args.diffused_sidechain_h5 or not os.path.isfile(args.diffused_sidechain_h5):
            logger.warning(f"use_diffusion=True but diffused_sidechain_h5 path invalid or not found: {args.diffused_sidechain_h5}. Skipping diffusion override.")
        else:
            logger.info("Loading diffused pooled embeddings to potentially override final structure generation.")
            try:
                # Load data
                with h5py.File(args.diffused_backbone_h5, "r") as f:
                    if "generated_diffusion" in f: diff_bb_np = f["generated_diffusion"][:]
                    elif "backbone_pooled" in f: diff_bb_np = f["backbone_pooled"][:]
                    else: raise KeyError(f"Cannot find expected dataset ('generated_diffusion' or 'backbone_pooled') in {args.diffused_backbone_h5}")

                with h5py.File(args.diffused_sidechain_h5, "r") as f:
                     if "generated_diffusion" in f: diff_sc_np = f["generated_diffusion"][:]
                     elif "sidechain_pooled" in f: diff_sc_np = f["sidechain_pooled"][:]
                     else: raise KeyError(f"Cannot find expected dataset ('generated_diffusion' or 'sidechain_pooled') in {args.diffused_sidechain_h5}")

                logger.debug(f"DEBUG: Loaded raw diffused shapes: BB={diff_bb_np.shape if diff_bb_np is not None else 'None'}, SC={diff_sc_np.shape if diff_sc_np is not None else 'None'}")

                # Reshape if needed
                if diff_bb_np is not None and diff_bb_np.ndim > 2:
                    N = diff_bb_np.shape[0]
                    expected_elements = N * bb_pool_dim_expected
                    if diff_bb_np.size == expected_elements:
                        diff_bb_np = diff_bb_np.reshape(N, bb_pool_dim_expected)
                        logger.debug(f"DEBUG: Reshaped BB diffused to {diff_bb_np.shape}")
                    else:
                        logger.warning(f"Cannot reshape BB diffused array {diff_bb_np.shape} to expected elements {expected_elements}. Shape mismatch.")

                if diff_sc_np is not None and diff_sc_np.ndim > 2:
                    N = diff_sc_np.shape[0]
                    expected_elements = N * sc_pool_dim_expected
                    if diff_sc_np.size == expected_elements:
                         diff_sc_np = diff_sc_np.reshape(N, sc_pool_dim_expected)
                         logger.debug(f"DEBUG: Reshaped SC diffused to {diff_sc_np.shape}")
                    else:
                         logger.warning(f"Cannot reshape SC diffused array {diff_sc_np.shape} to expected elements {expected_elements}. Shape mismatch.")

                # Log shapes *after* potential reshaping, before the check
                logger.debug(f"DEBUG: Shape of diff_bb_np before final check: {diff_bb_np.shape if diff_bb_np is not None else 'None'}")
                logger.debug(f"DEBUG: Shape of diff_sc_np before final check: {diff_sc_np.shape if diff_sc_np is not None else 'None'}")

                # Perform the final dimension check
                bb_check_ok = diff_bb_np is not None and diff_bb_np.ndim == 2 and diff_bb_np.shape[1] == bb_pool_dim_expected
                sc_check_ok = diff_sc_np is not None and diff_sc_np.ndim == 2 and diff_sc_np.shape[1] == sc_pool_dim_expected
                logger.debug(f"DEBUG: BB final shape check result: {bb_check_ok} (Actual Dim: {diff_bb_np.shape[1] if diff_bb_np is not None and diff_bb_np.ndim == 2 else 'N/A'}, Expected Dim: {bb_pool_dim_expected})")
                logger.debug(f"DEBUG: SC final shape check result: {sc_check_ok} (Actual Dim: {diff_sc_np.shape[1] if diff_sc_np is not None and diff_sc_np.ndim == 2 else 'N/A'}, Expected Dim: {sc_pool_dim_expected})")

                if bb_check_ok and sc_check_ok:
                    diff_bb_torch = torch.from_numpy(diff_bb_np).float()
                    diff_sc_torch = torch.from_numpy(diff_sc_np).float()
                    logger.info(f"Loaded and processed diffused embeddings: BB={diff_bb_torch.shape}, SC={diff_sc_torch.shape}")
                    use_diffusion_override = True
                    logger.debug(f"DEBUG: Setting use_diffusion_override = {use_diffusion_override} (Checks Passed)")
                else:
                     logger.error("Final shape check failed for diffused embeddings. Disabling override.")
                     use_diffusion_override = False
                     logger.debug(f"DEBUG: Setting use_diffusion_override = {use_diffusion_override} (Checks Failed)")

            except Exception as e:
                logger.error(f"Error loading or processing diffused embeddings: {e}", exc_info=True)
                logger.warning("Disabling diffusion override due to loading error.")
                use_diffusion_override = False
                diff_bb_torch = None
                diff_sc_torch = None
                logger.debug(f"DEBUG: Setting use_diffusion_override = {use_diffusion_override} (Exception during loading)")
    # --- END MODIFIED ---
    '''
    # --- 8) Export Final Outputs ---
    logger.info("--- Exporting Final Outputs ---")
    # Add debug log before calling export
    logger.debug(f"DEBUG: Calling export_final_outputs with use_diff = {use_diffusion_override}")
    export_final_outputs(
        raw_dataset=dataset, dec_dataset=dec_dataset, hno_model=hno_model,
        backbone_decoder=backbone_decoder_model, sidechain_decoder=sidechain_decoder_model,
        z_ref=z_ref, num_atoms=num_atoms, struct_dir=struct_dir, latent_dir=latent_dir, device=device,
        use_diff=use_diffusion_override, # Pass the determined flag
        diff_bb=diff_bb_torch,
        diff_sc=diff_sc_torch
        )
    '''
    sys.stdout.flush()
    logger.info("Script finished successfully.")

if __name__ == "__main__":
    main()