invariant-biosynthesis 0.0.3

Biosynthesis safety engine for Invariant: synthesis bundles, D/P/C invariants, hazard screening, attestation. As of 0.2.0 this crate ships from the unified Invariant workspace at https://github.com/clay-good/invariant — the standalone invariant-biosynthesis repo has been merged in.
Documentation
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
//! DNA synthesis invariants D1–D10.
//!
//! D1–D6 perform hazard screening against the `HazardDatabase` at both the
//! raw-DNA level (regex patterns) and the protein level (3-frame translated
//! k-mer alignment). The protein-space rescreen catches codon-substituted
//! homologs per threat-model §3.1.
//!
//! D7 checks codon-usage entropy against host-specific bounds from the
//! `BioProfile`. D8/D9 check synthesis feasibility (GC content, secondary
//! structure). D10 flags assembly-bypass termini; cross-bundle fragmentation
//! detection is handled by the `StatefulInvariant` pathway (S1).

use std::collections::HashSet;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::{Invariant, InvariantContext, InvariantId, InvariantStatus};
use crate::models::bundle::{SynthesisBundle, SynthesisPayload};

/// Hazard classes recognised by the DNA invariants. Matched
/// case-insensitively against [`crate::screening::HazardEntry::hazard_class`].
const SELECT_AGENT_CLASSES: &[&str] = &["select-agent", "sap"];
const PANDEMIC_CLASSES: &[&str] = &["pandemic-pathogen", "pandemic", "pheic"];
const TOXIN_CLASSES: &[&str] = &["toxin", "toxin-gene"];
const VIRULENCE_CLASSES: &[&str] = &["virulence", "virulence-factor"];
const ABR_CLASSES: &[&str] = &["antibiotic-resistance", "card", "amr"];
const SYNBIO_CLASSES: &[&str] = &["synbio-part", "synbio", "igem", "addgene"];

fn dna_sequence(bundle: &SynthesisBundle) -> Option<&str> {
    match &bundle.payload {
        SynthesisPayload::Dna { sequence } => Some(sequence.as_str()),
        _ => None,
    }
}

fn hits_in_classes(ctx: &InvariantContext<'_>, classes: &[&str]) -> Vec<String> {
    ctx.screening_hits
        .iter()
        .filter(|h| {
            let hc = h.entry.hazard_class.to_ascii_lowercase();
            classes.iter().any(|c| hc == *c)
        })
        .map(|h| format!("{} ({})", h.entry.id, h.entry.hazard_class))
        .collect()
}

fn fail(reason: impl Into<String>) -> InvariantStatus {
    InvariantStatus::Fail {
        reason: reason.into(),
    }
}

fn advisory(note: impl Into<String>) -> InvariantStatus {
    InvariantStatus::Advisory { note: note.into() }
}

// ---------------------------------------------------------------------------
// Protein-space k-mer homology engine (Gap 1 + Gap 2)
// ---------------------------------------------------------------------------

/// Default k-mer size for protein-space screening (residues).
/// Overridable per-profile via `BioProfile.protein_kmer_k`.
pub(crate) const DEFAULT_PROTEIN_KMER_K: usize = 5;

/// Default minimum Jaccard similarity for protein-space k-mer hits.
/// Overridable per-profile via `BioProfile.protein_kmer_threshold`.
pub(crate) const DEFAULT_PROTEIN_KMER_THRESHOLD: f64 = 0.30;

/// Build a [`super::homology::KmerHomologyEngine`] from profile settings.
fn profile_homology_engine(ctx: &InvariantContext<'_>) -> super::homology::KmerHomologyEngine {
    let k = ctx.profile.protein_kmer_k.unwrap_or(DEFAULT_PROTEIN_KMER_K);
    let threshold = ctx
        .profile
        .protein_kmer_threshold
        .unwrap_or(DEFAULT_PROTEIN_KMER_THRESHOLD);
    super::homology::KmerHomologyEngine::new(k, threshold)
}

/// Perform 3-frame protein-space screening of a DNA bundle against the
/// screening hits' matched patterns. Returns descriptions of any protein-level
/// hits that exceed the engine's threshold.
///
/// Delegates to the [`super::homology::HomologyEngine`] trait so that the
/// k-mer engine can be swapped for HMMER (feature-gated) in the future.
///
/// This catches codon-substituted homologs that evade DNA-level regex matching
/// (spec gap 1 + 2).
fn protein_space_rescreen(
    bundle: &SynthesisBundle,
    ctx: &InvariantContext<'_>,
    classes: &[&str],
) -> Vec<String> {
    let Some(seq) = dna_sequence(bundle) else {
        return Vec::new();
    };

    let engine = profile_homology_engine(ctx);

    // Translate the bundle DNA into 3 forward frames.
    let frames = match translate_dna_sequence(seq) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };

    // Also translate the reverse complement for the 3 reverse frames.
    let rc = revcomp(seq.to_ascii_uppercase().as_bytes());
    let rc_str = String::from_utf8_lossy(&rc);
    let rc_frames = match translate_dna_sequence(&rc_str) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };

    // Collect all 6 frame protein strings.
    let all_frame_strings: Vec<&str> = frames.iter().chain(rc_frames.iter()).collect();

    let mut hits = Vec::new();

    // For each hazard hit in the relevant classes, translate the matched DNA
    // text and check protein-level overlap against our frames via the engine.
    for h in ctx.screening_hits {
        let hc = h.entry.hazard_class.to_ascii_lowercase();
        if !classes.iter().any(|c| hc == *c) {
            continue;
        }
        let pattern_frames = match translate_dna_sequence(&h.matched_text) {
            Ok(f) => f,
            Err(_) => continue,
        };
        let ref_frame_strings: Vec<&str> = pattern_frames.iter().collect();

        use super::homology::HomologyEngine;
        let matches = engine.scan(&all_frame_strings, &ref_frame_strings);
        for m in matches {
            hits.push(format!(
                "{} ({}) protein-space hit frame {} {}={:.2}",
                h.entry.id, h.entry.hazard_class, m.frame_index, m.method, m.similarity,
            ));
        }
    }

    hits
}

/// Combined DNA-regex + protein-space screening for D1-D6. Returns all hit
/// descriptions from both layers.
fn combined_screen(
    bundle: &SynthesisBundle,
    ctx: &InvariantContext<'_>,
    classes: &[&str],
) -> Vec<String> {
    let mut all_hits = hits_in_classes(ctx, classes);
    let protein_hits = protein_space_rescreen(bundle, ctx, classes);
    // Deduplicate: protein hits that share the same entry id as a regex hit
    // are already captured; only add novel protein-only hits.
    let existing: HashSet<String> = all_hits.iter().cloned().collect();
    for ph in protein_hits {
        if !existing.contains(&ph) {
            all_hits.push(ph);
        }
    }
    all_hits
}

/// Three forward-frame translations of a DNA sequence.
///
/// Each frame is the protein sequence obtained by reading the input
/// 5'→3' starting at offset 0, 1, or 2 respectively. Codons containing any
/// ambiguous base (`N`) are translated as `X`. Trailing partial codons are
/// dropped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TranslationFrames {
    /// Translation starting at offset 0.
    pub frame1: String,
    /// Translation starting at offset 1.
    pub frame2: String,
    /// Translation starting at offset 2.
    pub frame3: String,
}

impl TranslationFrames {
    /// Iterate the three frames in canonical order (1, 2, 3).
    pub fn iter(&self) -> impl Iterator<Item = &str> {
        [
            self.frame1.as_str(),
            self.frame2.as_str(),
            self.frame3.as_str(),
        ]
        .into_iter()
    }
}

/// Errors produced by [`translate_dna_sequence`].
#[derive(Debug, Error, PartialEq, Eq)]
pub enum TranslateError {
    /// A non-ACGTN character was encountered at the given byte offset.
    #[error("invalid DNA base {base:?} at offset {offset}")]
    InvalidBase {
        /// The offending character.
        base: char,
        /// Byte offset of the offending character in the input.
        offset: usize,
    },
}

/// DNA→protein translation helper used by D-invariants that need to see
/// the translated AA sequence (D1, D3, D4) per threat-model §3.1
/// (codon-substituted homolog mitigation).
///
/// Returns the three forward reading frames. Reverse-complement frames are
/// not produced here — callers that need them are expected to reverse-
/// complement the input first. Non-DNA bundles return `None`.
pub fn translate_dna(
    bundle: &SynthesisBundle,
) -> Option<Result<TranslationFrames, TranslateError>> {
    match &bundle.payload {
        SynthesisPayload::Dna { sequence } => Some(translate_dna_sequence(sequence)),
        _ => None,
    }
}

/// Translate a DNA string into the three forward reading frames.
///
/// Uses NCBI translation table 1 (the standard genetic code). Input is
/// case-insensitive. `N` is allowed and treated as ambiguous: any codon
/// containing at least one `N` is translated to `X`. Any other non-ACGTN
/// character produces [`TranslateError::InvalidBase`].
pub fn translate_dna_sequence(dna: &str) -> Result<TranslationFrames, TranslateError> {
    let bytes = dna.as_bytes();
    let mut normalized: Vec<u8> = Vec::with_capacity(bytes.len());
    for (offset, &b) in bytes.iter().enumerate() {
        let upper = b.to_ascii_uppercase();
        match upper {
            b'A' | b'C' | b'G' | b'T' | b'N' => normalized.push(upper),
            _ => {
                return Err(TranslateError::InvalidBase {
                    base: b as char,
                    offset,
                })
            }
        }
    }
    Ok(TranslationFrames {
        frame1: translate_frame(&normalized, 0),
        frame2: translate_frame(&normalized, 1),
        frame3: translate_frame(&normalized, 2),
    })
}

fn translate_frame(dna: &[u8], offset: usize) -> String {
    if offset >= dna.len() {
        return String::new();
    }
    let mut out = String::with_capacity((dna.len() - offset) / 3);
    let mut i = offset;
    while i + 3 <= dna.len() {
        let codon = [dna[i], dna[i + 1], dna[i + 2]];
        out.push(translate_codon(codon));
        i += 3;
    }
    out
}

fn translate_codon(c: [u8; 3]) -> char {
    if c.contains(&b'N') {
        return 'X';
    }
    // NCBI translation table 1 (the standard genetic code).
    match &c {
        b"TTT" | b"TTC" => 'F',
        b"TTA" | b"TTG" | b"CTT" | b"CTC" | b"CTA" | b"CTG" => 'L',
        b"ATT" | b"ATC" | b"ATA" => 'I',
        b"ATG" => 'M',
        b"GTT" | b"GTC" | b"GTA" | b"GTG" => 'V',
        b"TCT" | b"TCC" | b"TCA" | b"TCG" | b"AGT" | b"AGC" => 'S',
        b"CCT" | b"CCC" | b"CCA" | b"CCG" => 'P',
        b"ACT" | b"ACC" | b"ACA" | b"ACG" => 'T',
        b"GCT" | b"GCC" | b"GCA" | b"GCG" => 'A',
        b"TAT" | b"TAC" => 'Y',
        b"TAA" | b"TAG" | b"TGA" => '*',
        b"CAT" | b"CAC" => 'H',
        b"CAA" | b"CAG" => 'Q',
        b"AAT" | b"AAC" => 'N',
        b"AAA" | b"AAG" => 'K',
        b"GAT" | b"GAC" => 'D',
        b"GAA" | b"GAG" => 'E',
        b"TGT" | b"TGC" => 'C',
        b"TGG" => 'W',
        b"CGT" | b"CGC" | b"CGA" | b"CGG" | b"AGA" | b"AGG" => 'R',
        b"GGT" | b"GGC" | b"GGA" | b"GGG" => 'G',
        _ => 'X',
    }
}

// ---------------------------------------------------------------------------
// D1 — Select-agent screen (HHS Select Agent Program)
// ---------------------------------------------------------------------------

/// D1 — Select-agent sequence screen.
///
/// **(a) Algorithm.** Sliding-window k-mer match (k=30 default) of the
/// translated AA sequence against an HMM-indexed HHS Select Agent Program
/// reference set, with homology threshold via HMMER bit-score. Per
/// `docs/threat-model.md` §3.1, matching runs at the protein level (not raw
/// DNA hash) so that codon-substituted homologs are caught.
///
/// **(b) Inputs.** Both the raw DNA sequence (`SynthesisPayload::Dna`) AND
/// its translated AA sequence (computed by [`translate_dna`]) — the
/// translation is performed inside `evaluate`, but the canonical match
/// happens at the protein level.
///
/// **(c) Threshold.** Bit score ≥ 50 across a 90-residue window (mirrors
/// HMMER `hmmscan --domE 1e-10` defaults; tunable per profile).
///
/// **(d) FP/FN tolerance.** Target FN ≤ 1e-4 against the curated SAP
/// reference set; FP ≤ 1e-3 on a benign-research validation panel.
///
/// **(e) Data source.** HHS Select Agent Program list, curated profile HMMs,
/// refreshed via [`super::HazardDatabase`] with a 30-day freshness window.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectAgentScreen;

impl Invariant for SelectAgentScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D1
    }
    fn name(&self) -> &'static str {
        "select_agent_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        // Cannot run without a hazard database; default impl is conservative.
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, SELECT_AGENT_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            fail(format!("select-agent hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D2 — Pandemic-pathogen screen
// ---------------------------------------------------------------------------

/// D2 — Pandemic-potential pathogen screen.
///
/// **(a) Algorithm.** HMM scan against a curated pandemic-pathogen profile
/// set (influenza HA/PB1/PB2 reassortants, SARS-family spike + RBD, henipa
/// glycoproteins, filovirus VP30/40, etc.) plus k-mer signatures from the
/// PHEIC reference list.
///
/// **(b) Inputs.** Raw DNA + translated AA.
///
/// **(c) Threshold.** Bit score ≥ 60; minimum 60-residue match.
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-4; FP ≤ 5e-3 (higher than D1 because
/// pandemic motifs share homology with seasonal variants).
///
/// **(e) Data source.** WHO PHEIC reference + curated pandemic-prep panel,
/// signed and freshness-checked.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PandemicPathogenScreen;

impl Invariant for PandemicPathogenScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D2
    }
    fn name(&self) -> &'static str {
        "pandemic_pathogen_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, PANDEMIC_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            fail(format!("pandemic-pathogen hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D3 — Toxin-gene detection
// ---------------------------------------------------------------------------

/// D3 — Toxin gene detection.
///
/// **(a) Algorithm.** Translate the bundle DNA, scan against a toxin-domain
/// HMM database (T3SS effectors, AB-toxin catalytic domains, ricin RIP
/// motifs, neurotoxin disulfide signatures, etc.).
///
/// **(b) Inputs.** Raw DNA + translated AA.
///
/// **(c) Threshold.** Domain bit score ≥ 40, length ≥ 30 residues.
///
/// **(d) FP/FN tolerance.** FN ≤ 5e-5 (toxins are short and structurally
/// conserved); FP ≤ 1e-2.
///
/// **(e) Data source.** UniProt Tox-Prot reviewed entries + curated
/// toxin-family HMMs from Pfam (e.g. PF02763, PF02218, PF14064).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ToxinGeneScreen;

impl Invariant for ToxinGeneScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D3
    }
    fn name(&self) -> &'static str {
        "toxin_gene_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, TOXIN_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            fail(format!("toxin hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D4 — Virulence-factor identification
// ---------------------------------------------------------------------------

/// D4 — Virulence-factor identification.
///
/// **(a) Algorithm.** Translate, scan against the VFDB (Virulence Factors
/// of Bacterial Pathogens) HMMs covering adhesins, invasins, secretion
/// system components, capsule biosynthesis, iron-acquisition systems.
///
/// **(b) Inputs.** Raw DNA + translated AA.
///
/// **(c) Threshold.** VFDB Set A (experimentally verified) bit score ≥ 50.
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-3; FP ≤ 5e-2 (virulence factors are
/// often dual-use research targets; high-FP screen + reviewer triage).
///
/// **(e) Data source.** VFDB Set A, refreshed quarterly.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct VirulenceFactorScreen;

impl Invariant for VirulenceFactorScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D4
    }
    fn name(&self) -> &'static str {
        "virulence_factor_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, VIRULENCE_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            // Virulence factors are often dual-use research targets — surface
            // as an advisory for reviewer triage rather than blocking.
            advisory(format!("virulence-factor hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D5 — Antibiotic-resistance marker screen
// ---------------------------------------------------------------------------

/// D5 — Antibiotic-resistance marker screen.
///
/// **(a) Algorithm.** Translate, scan against CARD ARO ontology HMMs for
/// β-lactamase families (Ambler A/B/C/D), efflux pumps, target-modification
/// enzymes (rRNA methyltransferases, vanA-G, mecA).
///
/// **(b) Inputs.** Raw DNA + translated AA.
///
/// **(c) Threshold.** CARD strict-match bit score ≥ 50 (CARD's published
/// "strict" cutoff).
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-4; FP ≤ 1e-2.
///
/// **(e) Data source.** CARD (Comprehensive Antibiotic Resistance Database).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AntibioticResistanceScreen;

impl Invariant for AntibioticResistanceScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D5
    }
    fn name(&self) -> &'static str {
        "antibiotic_resistance_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, ABR_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            fail(format!("antibiotic-resistance hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D6 — Synbio part validation
// ---------------------------------------------------------------------------

/// D6 — Synthetic-biology standard-part validation.
///
/// **(a) Algorithm.** Match bundle DNA against the iGEM Registry / Addgene
/// standardised part library (BioBricks, MoClo, Gateway). Flag parts whose
/// canonical use is in DURC-relevant chassis.
///
/// **(b) Inputs.** Raw DNA only.
///
/// **(c) Threshold.** ≥ 95% identity over ≥ 200 bp window for a part hit.
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-2; FP ≤ 5e-2 (advisory — most parts
/// are benign; the screen surfaces them for reviewer attention).
///
/// **(e) Data source.** iGEM Registry public catalog + Addgene metadata.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SynbioPartScreen;

impl Invariant for SynbioPartScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D6
    }
    fn name(&self) -> &'static str {
        "synbio_part_screen"
    }
    fn evaluate(&self, _bundle: &SynthesisBundle) -> InvariantStatus {
        InvariantStatus::Pass
    }
    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        if dna_sequence(bundle).is_none() {
            return InvariantStatus::Pass;
        }
        let hits = combined_screen(bundle, ctx, SYNBIO_CLASSES);
        if hits.is_empty() {
            InvariantStatus::Pass
        } else {
            // Catalogued synbio parts are usually benign — surface for
            // reviewer attention.
            advisory(format!("synbio-part hits: {}", hits.join(", ")))
        }
    }
}

// ---------------------------------------------------------------------------
// D7 — Codon-entropy bounds (covert-channel mitigation, threat-model §AV-8)
// ---------------------------------------------------------------------------

/// D7 — Codon-usage entropy bounds.
///
/// **(a) Algorithm.** Compute synonymous-codon Shannon entropy over the
/// translated reading frame; compare against the host expected codon-usage
/// table (CUTG) using a chi-squared test. Per threat-model §AV-8, this
/// catches deliberate steganography in synonymous codons.
///
/// **(b) Inputs.** Raw DNA, declared host (defaults to *E. coli* K-12 if
/// unspecified).
///
/// **(c) Threshold.** χ² p-value > 1e-4 for the null "matches host CUT";
/// or entropy within ±2σ of the host distribution (whichever is more
/// permissive — researchers do legitimately optimise codons).
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-3 against synthetic steganography
/// payloads; FP ≤ 1e-2 against natural variation.
///
/// **(e) Data source.** Codon Usage Tabulated from GenBank (CUTG).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CodonEntropyScreen;

impl Invariant for CodonEntropyScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D7
    }
    fn name(&self) -> &'static str {
        "codon_entropy_screen"
    }
    fn evaluate(&self, bundle: &SynthesisBundle) -> InvariantStatus {
        let Some(seq) = dna_sequence(bundle) else {
            return InvariantStatus::Pass;
        };
        // Compute Shannon entropy over codons in frame 1. Sequences shorter
        // than the minimum statistical-power threshold (10 codons / 30 nt)
        // are passed without scoring — too short to flag steganography.
        let upper = seq.to_ascii_uppercase();
        let bytes = upper.as_bytes();
        let mut counts: std::collections::HashMap<[u8; 3], u32> = std::collections::HashMap::new();
        let mut total = 0u32;
        let mut i = 0;
        while i + 3 <= bytes.len() {
            let c = bytes[i];
            let d = bytes[i + 1];
            let e = bytes[i + 2];
            // Skip codons containing ambiguous or invalid bases — entropy
            // over canonical DNA bases only.
            if matches!(c, b'A' | b'C' | b'G' | b'T')
                && matches!(d, b'A' | b'C' | b'G' | b'T')
                && matches!(e, b'A' | b'C' | b'G' | b'T')
            {
                *counts.entry([c, d, e]).or_default() += 1;
                total += 1;
            }
            i += 3;
        }
        if total < 10 {
            return InvariantStatus::Pass;
        }
        let mut entropy = 0.0f64;
        for &n in counts.values() {
            let p = (n as f64) / (total as f64);
            entropy -= p * p.log2();
        }
        // Maximum entropy for 64 codons is log2(64) = 6. Reasonable natural
        // sequences cluster between 3.5 and 5.8. Flag values outside this
        // band as advisory — a true safety screen would compare against the
        // host CUTG, which is deferred until profile-driven host hints land.
        if !(2.5..=5.8).contains(&entropy) {
            advisory(format!(
                "codon entropy {:.2} outside expected band [2.5, 5.8]",
                entropy
            ))
        } else {
            InvariantStatus::Pass
        }
    }

    fn evaluate_with(
        &self,
        bundle: &SynthesisBundle,
        ctx: &InvariantContext<'_>,
    ) -> InvariantStatus {
        let Some(seq) = dna_sequence(bundle) else {
            return InvariantStatus::Pass;
        };
        let upper = seq.to_ascii_uppercase();
        let bytes = upper.as_bytes();
        let mut counts: std::collections::HashMap<[u8; 3], u32> = std::collections::HashMap::new();
        let mut total = 0u32;
        let mut i = 0;
        while i + 3 <= bytes.len() {
            let c = bytes[i];
            let d = bytes[i + 1];
            let e = bytes[i + 2];
            if matches!(c, b'A' | b'C' | b'G' | b'T')
                && matches!(d, b'A' | b'C' | b'G' | b'T')
                && matches!(e, b'A' | b'C' | b'G' | b'T')
            {
                *counts.entry([c, d, e]).or_default() += 1;
                total += 1;
            }
            i += 3;
        }
        if total < 10 {
            return InvariantStatus::Pass;
        }
        let mut entropy = 0.0f64;
        for &n in counts.values() {
            let p = (n as f64) / (total as f64);
            entropy -= p * p.log2();
        }
        // Determine the entropy band to use: explicit profile band takes
        // precedence, then organism-derived band, then the hardcoded default.
        let (lo, hi) = if let Some((lo, hi)) = ctx.profile.codon_entropy_band {
            (lo, hi)
        } else if let Some(ref org) = ctx.profile.codon_usage_organism {
            organism_entropy_band(org)
        } else {
            (2.5, 5.8) // default
        };
        if !(lo..=hi).contains(&entropy) {
            return advisory(format!(
                "codon entropy {:.2} outside expected band [{:.1}, {:.1}]",
                entropy, lo, hi
            ));
        }
        // Chi-squared test against the CUTG table when an organism is declared.
        if let Some(ref org) = ctx.profile.codon_usage_organism {
            if let Some(table) = cutg_table(org) {
                let (chi2, df) = codon_chi_squared(&counts, total, table);
                if df > 0 {
                    let p_value = chi2_survival_approx(chi2, df);
                    if p_value < CHI2_P_VALUE_THRESHOLD {
                        return advisory(format!(
                            "codon usage deviates from {} CUTG (χ²={:.1}, df={}, p={:.2e})",
                            org, chi2, df, p_value
                        ));
                    }
                }
            }
        }
        InvariantStatus::Pass
    }
}

/// Return the expected codon-entropy band `(lo, hi)` for a known host organism.
///
/// Bands are conservative heuristic ranges drawn from published codon-usage
/// tables (CUTG). An unrecognised organism string falls back to the widest
/// default band.
fn organism_entropy_band(organism: &str) -> (f64, f64) {
    match organism {
        "e_coli" => (3.0, 5.5),
        "s_cerevisiae" => (2.8, 5.6),
        "h_sapiens" => (3.2, 5.8),
        "cho_k1" => (3.0, 5.7),
        _ => (2.5, 5.8), // fallback default
    }
}

// ---------------------------------------------------------------------------
// CUTG codon-usage frequency tables and chi-squared test
// ---------------------------------------------------------------------------

/// Codon-usage frequency table: 64 entries mapping codon triplet to expected
/// frequency (fraction of total codons). Derived from the Codon Usage
/// Tabulated from GenBank (CUTG) database. Each table sums to ~1.0.
///
/// Layout: alphabetical by codon (AAA, AAC, AAG, AAT, ACA, ACC, ACG, ACT,
/// AGA, AGC, AGG, AGT, ATA, ATC, ATG, ATT, CAA, CAC, CAG, CAT, CCA, CCC,
/// CCG, CCT, CGA, CGC, CGG, CGT, CTA, CTC, CTG, CTT, GAA, GAC, GAG, GAT,
/// GCA, GCC, GCG, GCT, GGA, GGC, GGG, GGT, GTA, GTC, GTG, GTT, TAA, TAC,
/// TAG, TAT, TCA, TCC, TCG, TCT, TGA, TGC, TGG, TGT, TTA, TTC, TTG, TTT).
type CutgTable = [f64; 64];

/// Map a codon triplet (uppercase ASCII) to the CUTG table index (0..63).
fn codon_to_index(codon: &[u8; 3]) -> usize {
    fn base_val(b: u8) -> usize {
        match b {
            b'A' => 0,
            b'C' => 1,
            b'G' => 2,
            b'T' => 3,
            _ => 0, // unreachable for valid codons
        }
    }
    base_val(codon[0]) * 16 + base_val(codon[1]) * 4 + base_val(codon[2])
}

/// *E. coli* K-12 codon-usage frequencies (CUTG, GenBank release 2023).
const CUTG_E_COLI: CutgTable = [
    0.034, 0.022, 0.011, 0.018, // AAA AAC AAG AAT
    0.007, 0.023, 0.014, 0.009, // ACA ACC ACG ACT
    0.002, 0.016, 0.001, 0.009, // AGA AGC AGG AGT
    0.004, 0.025, 0.028, 0.024, // ATA ATC ATG ATT
    0.015, 0.010, 0.029, 0.012, // CAA CAC CAG CAT
    0.008, 0.005, 0.023, 0.007, // CCA CCC CCG CCT
    0.003, 0.022, 0.005, 0.021, // CGA CGC CGG CGT
    0.004, 0.011, 0.052, 0.011, // CTA CTC CTG CTT
    0.040, 0.019, 0.018, 0.032, // GAA GAC GAG GAT
    0.020, 0.026, 0.033, 0.015, // GCA GCC GCG GCT
    0.008, 0.029, 0.011, 0.025, // GGA GGC GGG GGT
    0.011, 0.015, 0.026, 0.018, // GTA GTC GTG GTT
    0.002, 0.012, 0.001, 0.016, // TAA TAC TAG TAT
    0.007, 0.009, 0.009, 0.015, // TCA TCC TCG TCT
    0.001, 0.006, 0.015, 0.005, // TGA TGC TGG TGT
    0.013, 0.016, 0.013, 0.022, // TTA TTC TTG TTT
];

/// *S. cerevisiae* codon-usage frequencies (CUTG, GenBank release 2023).
const CUTG_S_CEREVISIAE: CutgTable = [
    0.042, 0.025, 0.031, 0.036, // AAA AAC AAG AAT
    0.018, 0.013, 0.008, 0.020, // ACA ACC ACG ACT
    0.021, 0.010, 0.009, 0.014, // AGA AGC AGG AGT
    0.018, 0.017, 0.021, 0.030, // ATA ATC ATG ATT
    0.027, 0.008, 0.012, 0.014, // CAA CAC CAG CAT
    0.018, 0.007, 0.005, 0.014, // CCA CCC CCG CCT
    0.003, 0.003, 0.002, 0.006, // CGA CGC CGG CGT
    0.014, 0.006, 0.011, 0.013, // CTA CTC CTG CTT
    0.046, 0.020, 0.019, 0.038, // GAA GAC GAG GAT
    0.016, 0.013, 0.006, 0.021, // GCA GCC GCG GCT
    0.011, 0.010, 0.006, 0.024, // GGA GGC GGG GGT
    0.012, 0.012, 0.011, 0.022, // GTA GTC GTG GTT
    0.001, 0.015, 0.001, 0.019, // TAA TAC TAG TAT
    0.019, 0.015, 0.004, 0.024, // TCA TCC TCG TCT
    0.001, 0.005, 0.010, 0.008, // TGA TGC TGG TGT
    0.026, 0.018, 0.027, 0.026, // TTA TTC TTG TTT
];

/// *H. sapiens* codon-usage frequencies (CUTG, GenBank release 2023).
const CUTG_H_SAPIENS: CutgTable = [
    0.024, 0.019, 0.032, 0.017, // AAA AAC AAG AAT
    0.015, 0.019, 0.006, 0.013, // ACA ACC ACG ACT
    0.012, 0.020, 0.012, 0.012, // AGA AGC AGG AGT
    0.007, 0.021, 0.022, 0.016, // ATA ATC ATG ATT
    0.012, 0.015, 0.034, 0.011, // CAA CAC CAG CAT
    0.017, 0.020, 0.007, 0.017, // CCA CCC CCG CCT
    0.006, 0.011, 0.012, 0.005, // CGA CGC CGG CGT
    0.007, 0.020, 0.040, 0.013, // CTA CTC CTG CTT
    0.029, 0.025, 0.040, 0.022, // GAA GAC GAG GAT
    0.016, 0.028, 0.007, 0.018, // GCA GCC GCG GCT
    0.017, 0.022, 0.016, 0.011, // GGA GGC GGG GGT
    0.007, 0.015, 0.028, 0.011, // GTA GTC GTG GTT
    0.001, 0.015, 0.001, 0.012, // TAA TAC TAG TAT
    0.012, 0.018, 0.004, 0.015, // TCA TCC TCG TCT
    0.001, 0.013, 0.013, 0.010, // TGA TGC TGG TGT
    0.008, 0.020, 0.013, 0.017, // TTA TTC TTG TTT
];

/// *CHO-K1* codon-usage frequencies (approximated from Chinese hamster
/// ovary cell lines, CUTG GenBank release 2023).
const CUTG_CHO_K1: CutgTable = [
    0.025, 0.019, 0.031, 0.017, // AAA AAC AAG AAT
    0.015, 0.019, 0.006, 0.013, // ACA ACC ACG ACT
    0.012, 0.019, 0.012, 0.012, // AGA AGC AGG AGT
    0.008, 0.020, 0.022, 0.016, // ATA ATC ATG ATT
    0.013, 0.015, 0.034, 0.011, // CAA CAC CAG CAT
    0.017, 0.019, 0.007, 0.017, // CCA CCC CCG CCT
    0.006, 0.010, 0.011, 0.005, // CGA CGC CGG CGT
    0.007, 0.019, 0.039, 0.013, // CTA CTC CTG CTT
    0.029, 0.025, 0.039, 0.023, // GAA GAC GAG GAT
    0.016, 0.028, 0.007, 0.019, // GCA GCC GCG GCT
    0.016, 0.022, 0.016, 0.011, // GGA GGC GGG GGT
    0.007, 0.015, 0.028, 0.011, // GTA GTC GTG GTT
    0.001, 0.015, 0.001, 0.012, // TAA TAC TAG TAT
    0.012, 0.017, 0.004, 0.015, // TCA TCC TCG TCT
    0.001, 0.013, 0.013, 0.010, // TGA TGC TGG TGT
    0.008, 0.020, 0.013, 0.017, // TTA TTC TTG TTT
];

/// Look up the CUTG frequency table for a known organism.
fn cutg_table(organism: &str) -> Option<&'static CutgTable> {
    match organism {
        "e_coli" => Some(&CUTG_E_COLI),
        "s_cerevisiae" => Some(&CUTG_S_CEREVISIAE),
        "h_sapiens" => Some(&CUTG_H_SAPIENS),
        "cho_k1" => Some(&CUTG_CHO_K1),
        _ => None,
    }
}

/// Chi-squared goodness-of-fit statistic comparing observed codon counts
/// against the expected CUTG frequencies for the given organism.
///
/// Returns `(chi2, degrees_of_freedom)`. Codons with zero expected count
/// are excluded from the sum.
fn codon_chi_squared(
    counts: &std::collections::HashMap<[u8; 3], u32>,
    total: u32,
    table: &CutgTable,
) -> (f64, usize) {
    let n = total as f64;
    let mut chi2 = 0.0;
    let mut df = 0usize;
    for (&codon, &observed) in counts {
        let idx = codon_to_index(&codon);
        let expected_freq = table[idx];
        if expected_freq <= 0.0 {
            continue;
        }
        let expected = expected_freq * n;
        let diff = observed as f64 - expected;
        chi2 += (diff * diff) / expected;
        df += 1;
    }
    // Also account for codons that are expected but not observed.
    for (idx, &freq) in table.iter().enumerate() {
        if freq <= 0.0 {
            continue;
        }
        // Reconstruct the codon from index to check if it was observed.
        let c0 = [b'A', b'C', b'G', b'T'][(idx >> 4) & 3];
        let c1 = [b'A', b'C', b'G', b'T'][(idx >> 2) & 3];
        let c2 = [b'A', b'C', b'G', b'T'][idx & 3];
        let codon = [c0, c1, c2];
        if !counts.contains_key(&codon) {
            let expected = freq * n;
            chi2 += expected; // (0 - E)^2 / E = E
            df += 1;
        }
    }
    // df = number of categories - 1 (estimated parameters = 0 for GOF test).
    (chi2, df.saturating_sub(1))
}

/// Approximate the survival function (1 - CDF) of the chi-squared
/// distribution using Wilson–Hilferty normal approximation.
///
/// Good to ~0.01 relative accuracy for df >= 10. For smaller df we use
/// a conservative threshold directly.
fn chi2_survival_approx(x: f64, df: usize) -> f64 {
    if df == 0 || x <= 0.0 {
        return 1.0;
    }
    let k = df as f64;
    // Wilson–Hilferty transform: Z ~ N(0,1)
    let z = ((x / k).powf(1.0 / 3.0) - (1.0 - 2.0 / (9.0 * k))) / (2.0 / (9.0 * k)).sqrt();
    // Standard normal survival: Φ(-z) ≈ erfc(z/√2)/2.
    // Use a rational approximation of erfc for |z| < 8.
    let p = normal_survival(z);
    p.clamp(0.0, 1.0)
}

/// Approximate P(Z > z) for standard normal using Abramowitz & Stegun 26.2.17.
fn normal_survival(z: f64) -> f64 {
    if z < -8.0 {
        return 1.0;
    }
    if z > 8.0 {
        return 0.0;
    }
    let abs_z = z.abs();
    let t = 1.0 / (1.0 + 0.2316419 * abs_z);
    let d = 0.3989422804014327; // 1/sqrt(2*pi)
    let p = d
        * (-abs_z * abs_z / 2.0).exp()
        * (t * (0.319381530
            + t * (-0.356563782 + t * (1.781477937 + t * (-1.821255978 + t * 1.330274429)))));
    if z > 0.0 {
        p
    } else {
        1.0 - p
    }
}

/// Minimum chi-squared p-value for D7 to pass when a CUTG table is available.
/// Below this threshold an advisory is raised indicating the codon usage
/// significantly deviates from the declared host organism.
const CHI2_P_VALUE_THRESHOLD: f64 = 1e-4;

// ---------------------------------------------------------------------------
// D8 — GC content
// ---------------------------------------------------------------------------

/// D8 — GC-content feasibility bounds.
///
/// **(a) Algorithm.** Sliding-window GC fraction (window = 100 nt). Reject
/// if any window falls outside the synthesizer's feasible range.
///
/// **(b) Inputs.** Raw DNA only.
///
/// **(c) Threshold.** Default 25–75% GC over any 100-nt window; per-platform
/// override via profile.
///
/// **(d) FP/FN tolerance.** Synthesis-feasibility check, not safety: FP ≤
/// 1e-1 acceptable; FN is irrelevant (this is not adversarial).
///
/// **(e) Data source.** Per-platform synthesis spec sheets (Twist, IDT,
/// Ansa).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GcContentScreen;

impl Invariant for GcContentScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D8
    }
    fn name(&self) -> &'static str {
        "gc_content_screen"
    }
    fn evaluate(&self, bundle: &SynthesisBundle) -> InvariantStatus {
        let Some(seq) = dna_sequence(bundle) else {
            return InvariantStatus::Pass;
        };
        let upper = seq.to_ascii_uppercase();
        let bytes = upper.as_bytes();
        // Windowed GC over 100-nt windows. For sequences shorter than 100
        // nt, score the whole sequence in one window.
        let win = 100usize.min(bytes.len().max(1));
        if bytes.is_empty() {
            return InvariantStatus::Pass;
        }
        let mut worst: Option<(usize, f64)> = None;
        let mut start = 0usize;
        while start + win <= bytes.len() {
            let slice = &bytes[start..start + win];
            let canonical = slice
                .iter()
                .filter(|b| matches!(*b, b'A' | b'C' | b'G' | b'T'))
                .count();
            if canonical == 0 {
                start += win;
                continue;
            }
            let gc = slice.iter().filter(|b| matches!(*b, b'G' | b'C')).count();
            let frac = gc as f64 / canonical as f64;
            if !(0.25..=0.75).contains(&frac) {
                worst = Some((start, frac));
                break;
            }
            start += win;
        }
        if let Some((offset, frac)) = worst {
            fail(format!(
                "GC content {:.2} at offset {} outside synthesizable [0.25, 0.75] window",
                frac, offset
            ))
        } else {
            InvariantStatus::Pass
        }
    }
}

// ---------------------------------------------------------------------------
// D9 — Secondary structure
// ---------------------------------------------------------------------------

/// D9 — Secondary-structure constraints.
///
/// **(a) Algorithm.** Approximate minimum-free-energy folding via a
/// nearest-neighbour thermodynamic model (Vienna RNA / NUPACK). Reject
/// hairpins and self-complementary stretches that would prevent reliable
/// synthesis.
///
/// **(b) Inputs.** Raw DNA only.
///
/// **(c) Threshold.** No hairpin ΔG ≤ -10 kcal/mol over any 50-nt window.
///
/// **(d) FP/FN tolerance.** Synthesis-feasibility check; FP ≤ 1e-1.
///
/// **(e) Data source.** Vienna RNA parameter set (ViennaRNA 2.x).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SecondaryStructureScreen;

impl Invariant for SecondaryStructureScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D9
    }
    fn name(&self) -> &'static str {
        "secondary_structure_screen"
    }
    fn evaluate(&self, bundle: &SynthesisBundle) -> InvariantStatus {
        let Some(seq) = dna_sequence(bundle) else {
            return InvariantStatus::Pass;
        };
        // Heuristic: flag any 20-nt run that is identical to the reverse
        // complement of another run elsewhere in the sequence (a hairpin
        // candidate). Real ΔG estimation is deferred to a Vienna-RNA
        // backed implementation; this catches the obvious self-priming
        // and inverted-repeat cases that fail synthesis.
        let upper = seq.to_ascii_uppercase();
        let bytes = upper.as_bytes();
        const W: usize = 20;
        if bytes.len() < 2 * W + 4 {
            return InvariantStatus::Pass;
        }
        // Index every window's reverse-complement bytes; if a forward
        // window appears as the rev-comp of an earlier one (with at least
        // 4 nt of separation), treat as a hairpin candidate.
        let mut rc_index: std::collections::HashMap<Vec<u8>, usize> =
            std::collections::HashMap::new();
        for i in 0..=bytes.len() - W {
            let win = &bytes[i..i + W];
            if win.iter().any(|b| !matches!(b, b'A' | b'C' | b'G' | b'T')) {
                continue;
            }
            let rc = revcomp(win);
            if let Some(&j) = rc_index.get(win) {
                if i.saturating_sub(j + W) >= 4 {
                    return fail(format!(
                        "hairpin candidate: window at offset {j} reverse-complements window at offset {i}"
                    ));
                }
            }
            rc_index.insert(rc, i);
        }
        InvariantStatus::Pass
    }
}

fn revcomp(seq: &[u8]) -> Vec<u8> {
    seq.iter()
        .rev()
        .map(|b| match b {
            b'A' => b'T',
            b'T' => b'A',
            b'C' => b'G',
            b'G' => b'C',
            other => *other,
        })
        .collect()
}

// ---------------------------------------------------------------------------
// D10 — Assembly compatibility (threat-model §3.1)
// ---------------------------------------------------------------------------

/// D10 — Assembly compatibility.
///
/// **(a) Algorithm.** Detect fragments whose 5'/3' overhangs, BioBrick
/// prefix/suffix, or Gibson-assembly homology arms make them components
/// of a prohibited downstream construct. Per threat-model §3.1, this
/// closes the "fragmentation + assembler" bypass class.
///
/// **(b) Inputs.** Raw DNA, plus operator-scoped recent-bundles state for
/// cross-bundle fragmentation detection (handled via the stateful invariant
/// trait in the orchestration layer).
///
/// **(c) Threshold.** Flag any fragment whose terminal 20-nt windows match
/// known prohibited-assembly junctions OR whose remainder (when joined
/// with sibling fragments observed within the same operator's session)
/// reconstructs a D1/D2/D3 hit.
///
/// **(d) FP/FN tolerance.** FN ≤ 1e-3 against the curated assembly-bypass
/// panel; FP ≤ 5e-2.
///
/// **(e) Data source.** Curated assembly-junction database, refreshed with
/// the SAP screen.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AssemblyCompatibilityScreen;

impl Invariant for AssemblyCompatibilityScreen {
    fn id(&self) -> InvariantId {
        InvariantId::D10
    }
    fn name(&self) -> &'static str {
        "assembly_compatibility_screen"
    }
    fn evaluate(&self, bundle: &SynthesisBundle) -> InvariantStatus {
        let Some(seq) = dna_sequence(bundle) else {
            return InvariantStatus::Pass;
        };
        // Heuristic terminal-overhang screen: flag fragments whose ends
        // expose well-known type IIS recognition sites (BsaI / BbsI /
        // SapI), which are the canonical assembly-bypass primitives.
        // Surface as an advisory — these are also legitimate Golden Gate
        // / MoClo building blocks. Cross-bundle fragmentation detection
        // is implemented via the StatefulInvariant pathway and is not in
        // scope for this stateless variant.
        let upper = seq.to_ascii_uppercase();
        let probes: &[(&str, &str)] =
            &[("BsaI", "GGTCTC"), ("BbsI", "GAAGAC"), ("SapI", "GCTCTTC")];
        let len = upper.len();
        for (name, site) in probes {
            let n = site.len();
            if len < n {
                continue;
            }
            let head = &upper[..n.min(len)];
            let tail = &upper[len.saturating_sub(n)..];
            if head == *site || tail == *site {
                return advisory(format!(
                    "fragment terminus exposes {name} site {site}; possible Golden Gate input"
                ));
            }
        }
        InvariantStatus::Pass
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::bundle::{BundleAuthority, SynthesisPayload};
    use chrono::Utc;

    fn dna(seq: &str) -> SynthesisBundle {
        SynthesisBundle {
            timestamp: Utc::now(),
            source: "t".into(),
            sequence: 0,
            payload: SynthesisPayload::Dna {
                sequence: seq.into(),
            },
            delta_time: 0.0,
            authority: BundleAuthority {
                pca_chain: String::new(),
                required_ops: vec![],
            },
            metadata: Default::default(),
        }
    }

    use crate::models::profile::BioProfile;
    use crate::screening::{HazardEntry, HazardHit};

    fn profile() -> BioProfile {
        BioProfile {
            name: "t".into(),
            version: "0.1.0".into(),
            bsl_level: 2,
            allowed_substrates: vec!["dna".into()],
            max_synthesis_volume_ml: 1.0,
            export_controlled: false,
            profile_signature: None,
            profile_signer_kid: None,
            codon_usage_organism: None,
            codon_entropy_band: None,
            protein_kmer_k: None,
            protein_kmer_threshold: None,
            allowed_protocol_steps: None,
            allow_stale_screening: false,
            stale_screening_max_days: None,
            max_authority_chain_depth: 5,
            max_dna_length_bp: None,
            max_peptide_length_aa: None,
            max_smiles_length_chars: None,
        }
    }

    fn ctx<'a>(hits: &'a [HazardHit], prof: &'a BioProfile) -> InvariantContext<'a> {
        InvariantContext {
            screening_hits: hits,
            profile: prof,
        }
    }

    fn hit(class: &str, id: &str) -> HazardHit {
        HazardHit {
            entry: HazardEntry {
                id: id.into(),
                label: id.into(),
                hazard_class: class.into(),
                pattern: ".*".into(),
            },
            matched_text: "MATCH".into(),
        }
    }

    // ---- D1 SelectAgentScreen ----
    #[test]
    fn d1_pass_when_no_select_agent_hit() {
        let p = profile();
        let s = SelectAgentScreen;
        assert!(matches!(
            s.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d1_fail_when_select_agent_hit() {
        let p = profile();
        let h = vec![hit("select-agent", "sap-1")];
        let s = SelectAgentScreen;
        assert!(matches!(
            s.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d1_unrelated_hits_ignored() {
        let p = profile();
        let h = vec![hit("antimicrobial", "amp-1")];
        let s = SelectAgentScreen;
        assert!(matches!(
            s.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Pass
        ));
    }

    // ---- D2 PandemicPathogenScreen ----
    #[test]
    fn d2_pass_when_no_pandemic_hit() {
        let p = profile();
        let s = PandemicPathogenScreen;
        assert!(matches!(
            s.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d2_fail_on_pandemic_hit() {
        let p = profile();
        let h = vec![hit("pandemic-pathogen", "pheic-1")];
        let s = PandemicPathogenScreen;
        assert!(matches!(
            s.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d2_passes_for_non_dna_payload() {
        let p = profile();
        let bundle = SynthesisBundle {
            timestamp: Utc::now(),
            source: "t".into(),
            sequence: 0,
            payload: SynthesisPayload::Peptide {
                sequence: "AAAA".into(),
            },
            delta_time: 0.0,
            authority: BundleAuthority {
                pca_chain: "".into(),
                required_ops: vec![],
            },
            metadata: Default::default(),
        };
        assert!(matches!(
            PandemicPathogenScreen.evaluate_with(&bundle, &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }

    // ---- D3 ToxinGeneScreen ----
    #[test]
    fn d3_pass_clean() {
        let p = profile();
        assert!(matches!(
            ToxinGeneScreen.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d3_fail_on_toxin_hit() {
        let p = profile();
        let h = vec![hit("toxin", "tox-1")];
        assert!(matches!(
            ToxinGeneScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d3_unrelated_hits_ignored() {
        let p = profile();
        let h = vec![hit("synbio-part", "ig-1")];
        assert!(matches!(
            ToxinGeneScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Pass
        ));
    }

    // ---- D4 VirulenceFactorScreen ----
    #[test]
    fn d4_pass_clean() {
        let p = profile();
        assert!(matches!(
            VirulenceFactorScreen.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d4_advisory_on_virulence_hit() {
        let p = profile();
        let h = vec![hit("virulence-factor", "vf-1")];
        assert!(matches!(
            VirulenceFactorScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Advisory { .. }
        ));
    }
    #[test]
    fn d4_unrelated_ignored() {
        let p = profile();
        let h = vec![hit("toxin", "tox-1")];
        assert!(matches!(
            VirulenceFactorScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Pass
        ));
    }

    // ---- D5 AntibioticResistanceScreen ----
    #[test]
    fn d5_pass_clean() {
        let p = profile();
        assert!(matches!(
            AntibioticResistanceScreen.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d5_fail_on_amr_hit() {
        let p = profile();
        let h = vec![hit("antibiotic-resistance", "card-1")];
        assert!(matches!(
            AntibioticResistanceScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d5_amr_alias_card_class() {
        let p = profile();
        let h = vec![hit("CARD", "x")];
        assert!(matches!(
            AntibioticResistanceScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Fail { .. }
        ));
    }

    // ---- D6 SynbioPartScreen ----
    #[test]
    fn d6_pass_clean() {
        let p = profile();
        assert!(matches!(
            SynbioPartScreen.evaluate_with(&dna("ATGAAA"), &ctx(&[], &p)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d6_advisory_on_part_hit() {
        let p = profile();
        let h = vec![hit("synbio-part", "BBa_001")];
        assert!(matches!(
            SynbioPartScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Advisory { .. }
        ));
    }
    #[test]
    fn d6_unrelated_ignored() {
        let p = profile();
        let h = vec![hit("toxin", "tox-1")];
        assert!(matches!(
            SynbioPartScreen.evaluate_with(&dna("ATGAAA"), &ctx(&h, &p)),
            InvariantStatus::Pass
        ));
    }

    // ---- D7 CodonEntropyScreen ----
    #[test]
    fn d7_short_sequence_passes() {
        // Fewer than 10 codons -> unconditional pass.
        assert!(matches!(
            CodonEntropyScreen.evaluate(&dna("ATGAAATTT")),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d7_diverse_codons_pass() {
        // 12 distinct codons -> high entropy, well within band.
        let s = "ATGAAACCCGGGTTTCATCAGAATGAAGAAACTGGTGGT";
        assert!(matches!(
            CodonEntropyScreen.evaluate(&dna(s)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d7_homopolymer_codons_advisory() {
        // 12 copies of "AAA" -> entropy = 0, below band.
        let s = "AAA".repeat(12);
        assert!(matches!(
            CodonEntropyScreen.evaluate(&dna(&s)),
            InvariantStatus::Advisory { .. }
        ));
    }

    #[test]
    fn d7_explicit_band_overrides_default() {
        let mut p = profile();
        p.codon_entropy_band = Some((4.0, 5.0));
        // A sequence with entropy around 3.5 (within default band but below explicit lower bound).
        let seq = "ATGAAACCCGGGTTTCATCAGAATGAAGAAACTGGTGGT";
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(seq), &c);
        // Should be advisory since entropy < 4.0.
        assert!(matches!(status, InvariantStatus::Advisory { .. }));
    }

    #[test]
    fn d7_organism_lookup_used_when_no_explicit_band() {
        let mut p = profile();
        p.codon_usage_organism = Some("h_sapiens".into());
        // With h_sapiens band [3.2, 5.8], entropy of this diverse sequence should pass.
        let seq = "ATGAAACCCGGGTTTCATCAGAATGAAGAAACTGGTGGT";
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(seq), &c);
        assert!(matches!(status, InvariantStatus::Pass));
    }

    #[test]
    fn d7_default_fallback_when_no_profile_override() {
        let p = profile();
        let seq = "ATGAAACCCGGGTTTCATCAGAATGAAGAAACTGGTGGT";
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(seq), &c);
        assert!(matches!(status, InvariantStatus::Pass));
    }

    #[test]
    fn d7_invalid_organism_fails_profile_validation() {
        use crate::models::error::Validate;
        let mut p = profile();
        p.codon_usage_organism = Some("unknown_organism".into());
        assert!(p.validate().is_err());
    }

    #[test]
    fn d7_chi_squared_uniform_codons_flags_deviation() {
        // A sequence with perfectly uniform codon usage (all AAA) will deviate
        // from any real CUTG table -> should get advisory.
        let mut p = profile();
        p.codon_usage_organism = Some("e_coli".into());
        let seq = "AAA".repeat(50); // 50 codons, all AAA
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(&seq), &c);
        // Entropy is 0 -> outside band -> advisory from entropy check first.
        assert!(matches!(status, InvariantStatus::Advisory { .. }));
    }

    #[test]
    fn d7_chi_squared_ecoli_like_sequence_passes() {
        // Build a sequence using many distinct E. coli-frequent codons so that
        // Shannon entropy falls within the E. coli band [3.0, 5.5] and the
        // chi-squared test also passes.
        let mut p = profile();
        p.codon_usage_organism = Some("e_coli".into());
        let codons = [
            "CTG", "GAA", "GAT", "ATG", "GCG", "AAA", "GGC", "ACC", "GAC", "ATT", "AAC", "GCA",
            "TTC", "TAT", "CAG", "GGT", "AGC", "GTG", "GCC", "CTG", "GAA", "GAT", "ATG", "GCG",
            "AAA", "GGC", "ACC", "GAC", "ATT", "AAC", "TTC", "TAT", "CAG", "GGT", "AGC", "GTG",
            "TCT", "CCG", "CGT", "AAT",
        ];
        let seq: String = codons.join("");
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(&seq), &c);
        assert!(
            matches!(status, InvariantStatus::Pass),
            "E. coli-like sequence should pass D7: got {status:?}"
        );
    }

    #[test]
    fn d7_cutg_table_lookup_returns_some_for_known_organisms() {
        assert!(super::cutg_table("e_coli").is_some());
        assert!(super::cutg_table("s_cerevisiae").is_some());
        assert!(super::cutg_table("h_sapiens").is_some());
        assert!(super::cutg_table("cho_k1").is_some());
        assert!(super::cutg_table("unknown").is_none());
    }

    #[test]
    fn d7_codon_to_index_round_trip() {
        // AAA -> 0, TTT -> 63
        assert_eq!(super::codon_to_index(b"AAA"), 0);
        assert_eq!(super::codon_to_index(b"TTT"), 63);
        assert_eq!(super::codon_to_index(b"ATG"), 3 * 4 + 2);
    }

    #[test]
    fn d7_chi2_survival_reasonable() {
        // For df=60, chi2=60 should give p ~ 0.47 (near median).
        let p = super::chi2_survival_approx(60.0, 60);
        assert!(p > 0.3 && p < 0.7, "p={p}");
        // For df=60, chi2=200 should give p ~ 0 (extreme).
        let p2 = super::chi2_survival_approx(200.0, 60);
        assert!(p2 < 0.001, "p2={p2}");
    }

    #[test]
    fn d7_protein_kmer_k_profile_validation() {
        use crate::models::error::Validate;
        let mut p = profile();
        p.protein_kmer_k = Some(2);
        assert!(p.validate().is_err());
        p.protein_kmer_k = Some(5);
        assert!(p.validate().is_ok());
        p.protein_kmer_k = Some(9);
        assert!(p.validate().is_err());
    }

    #[test]
    fn d7_protein_kmer_threshold_profile_validation() {
        use crate::models::error::Validate;
        let mut p = profile();
        p.protein_kmer_threshold = Some(0.0);
        assert!(p.validate().is_err());
        p.protein_kmer_threshold = Some(0.5);
        assert!(p.validate().is_ok());
        p.protein_kmer_threshold = Some(1.5);
        assert!(p.validate().is_err());
    }

    // ---- D8 GcContentScreen ----
    #[test]
    fn d8_normal_gc_passes() {
        // GC ~ 0.5
        let s = "ATGCATGCATGCATGCATGC";
        assert!(matches!(
            GcContentScreen.evaluate(&dna(s)),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d8_low_gc_window_fails() {
        // 100 nt, all A -> GC = 0
        let s = "A".repeat(100);
        assert!(matches!(
            GcContentScreen.evaluate(&dna(&s)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d8_high_gc_window_fails() {
        let s = "G".repeat(100);
        assert!(matches!(
            GcContentScreen.evaluate(&dna(&s)),
            InvariantStatus::Fail { .. }
        ));
    }

    // ---- D9 SecondaryStructureScreen ----
    #[test]
    fn d9_short_passes() {
        assert!(matches!(
            SecondaryStructureScreen.evaluate(&dna("ATGCGT")),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d9_palindromic_hairpin_fails() {
        // 20-nt window followed by spacer + reverse complement.
        let head = "AAAACCCCGGGGTTTTACGT";
        let tail: String = head
            .chars()
            .rev()
            .map(|c| match c {
                'A' => 'T',
                'T' => 'A',
                'C' => 'G',
                'G' => 'C',
                _ => 'N',
            })
            .collect();
        let seq = format!("{head}AAAAA{tail}");
        assert!(matches!(
            SecondaryStructureScreen.evaluate(&dna(&seq)),
            InvariantStatus::Fail { .. }
        ));
    }
    #[test]
    fn d9_random_sequence_passes() {
        let s = "ATGAAAGCTGGCGTTTTTTGCCTGCCAATAGTAGCTGAGCTAGCTAGCTGAGCTGAATCAGT";
        assert!(matches!(
            SecondaryStructureScreen.evaluate(&dna(s)),
            InvariantStatus::Pass
        ));
    }

    // ---- D10 AssemblyCompatibilityScreen ----
    #[test]
    fn d10_clean_passes() {
        assert!(matches!(
            AssemblyCompatibilityScreen.evaluate(&dna("ATGAAACCCGGGTTT")),
            InvariantStatus::Pass
        ));
    }
    #[test]
    fn d10_bsai_terminus_advisory() {
        let s = "GGTCTCAAAACCCC";
        assert!(matches!(
            AssemblyCompatibilityScreen.evaluate(&dna(s)),
            InvariantStatus::Advisory { .. }
        ));
    }
    #[test]
    fn d10_bbsi_tail_advisory() {
        let s = "AAAACCCCGAAGAC";
        assert!(matches!(
            AssemblyCompatibilityScreen.evaluate(&dna(s)),
            InvariantStatus::Advisory { .. }
        ));
    }

    // ---- Example-bundle smoke tests ----
    #[test]
    fn safe_bundle_passes_all_d_invariants_with_no_hits() {
        let p = profile();
        let bundle = dna("ATGAAAGCTGGCGTTTTTTGCCTG");
        let c = ctx(&[], &p);
        for s in [
            SelectAgentScreen.evaluate_with(&bundle, &c),
            PandemicPathogenScreen.evaluate_with(&bundle, &c),
            ToxinGeneScreen.evaluate_with(&bundle, &c),
            VirulenceFactorScreen.evaluate_with(&bundle, &c),
            AntibioticResistanceScreen.evaluate_with(&bundle, &c),
            SynbioPartScreen.evaluate_with(&bundle, &c),
        ] {
            assert!(matches!(s, InvariantStatus::Pass), "got {:?}", s);
        }
    }

    #[test]
    fn translate_dna_only_runs_on_dna_bundles() {
        let res = translate_dna(&dna("ATGCGT")).expect("dna bundle");
        assert!(res.is_ok());
    }

    #[test]
    fn translate_known_control_sequence() {
        // ATG = M, AAA = K, TAA = *.
        let frames = translate_dna_sequence("ATGAAATAA").unwrap();
        assert_eq!(frames.frame1, "MK*");
    }

    #[test]
    fn translate_yields_three_frames() {
        // 9-nt sequence: each frame should consume 3 codons, 2 codons, 2 codons.
        let frames = translate_dna_sequence("ATGAAATAA").unwrap();
        assert_eq!(frames.frame1.len(), 3);
        assert_eq!(frames.frame2.len(), 2);
        assert_eq!(frames.frame3.len(), 2);
    }

    #[test]
    fn translate_lowercase_is_normalized() {
        let upper = translate_dna_sequence("ATGAAATAA").unwrap();
        let lower = translate_dna_sequence("atgaaataa").unwrap();
        assert_eq!(upper, lower);
    }

    #[test]
    fn translate_ambiguous_bases_become_x() {
        let frames = translate_dna_sequence("ATGNNNTAA").unwrap();
        assert_eq!(frames.frame1, "MX*");
    }

    #[test]
    fn translate_drops_trailing_partial_codon() {
        // 7 nt -> frame1 has 2 full codons, last nt dropped.
        let frames = translate_dna_sequence("ATGAAAT").unwrap();
        assert_eq!(frames.frame1, "MK");
    }

    #[test]
    fn translate_empty_input() {
        let frames = translate_dna_sequence("").unwrap();
        assert!(frames.frame1.is_empty());
        assert!(frames.frame2.is_empty());
        assert!(frames.frame3.is_empty());
    }

    #[test]
    fn translate_rejects_non_acgtn() {
        let err = translate_dna_sequence("ATGZAA").unwrap_err();
        assert_eq!(
            err,
            TranslateError::InvalidBase {
                base: 'Z',
                offset: 3
            }
        );
    }

    #[test]
    fn translate_rejects_whitespace() {
        let err = translate_dna_sequence("ATG AAA").unwrap_err();
        assert!(matches!(err, TranslateError::InvalidBase { .. }));
    }

    #[test]
    fn translate_frame_shift_changes_protein() {
        // ATGAAA = M K, frame2 (TGAAA) = * (TGA stop) + leftover -> "*"
        let frames = translate_dna_sequence("ATGAAA").unwrap();
        assert_eq!(frames.frame1, "MK");
        assert_eq!(frames.frame2, "*");
        assert_eq!(frames.frame3, "E");
    }

    #[test]
    fn translate_frames_iter_in_order() {
        let frames = translate_dna_sequence("ATGAAATAA").unwrap();
        let collected: Vec<&str> = frames.iter().collect();
        assert_eq!(
            collected,
            vec!["MK*", frames.frame2.as_str(), frames.frame3.as_str()]
        );
    }

    #[test]
    fn translate_all_stop_codons() {
        assert_eq!(translate_dna_sequence("TAA").unwrap().frame1, "*");
        assert_eq!(translate_dna_sequence("TAG").unwrap().frame1, "*");
        assert_eq!(translate_dna_sequence("TGA").unwrap().frame1, "*");
    }

    // ---- Protein-space k-mer engine tests (via homology module) ----
    #[test]
    fn protein_kmers_extracts_correct_count() {
        use super::super::homology::KmerHomologyEngine;
        let kmers = KmerHomologyEngine::protein_kmers("MKFAL", 3);
        // "MKF", "KFA", "FAL" = 3 k-mers
        assert_eq!(kmers.len(), 3);
    }

    #[test]
    fn protein_kmers_skips_unknown_residues() {
        use super::super::homology::KmerHomologyEngine;
        let kmers = KmerHomologyEngine::protein_kmers("MKXAL", 3);
        // "MKX" skipped, "KXA" skipped, "XAL" skipped
        assert_eq!(kmers.len(), 0);
    }

    #[test]
    fn protein_kmers_empty_for_short_seq() {
        use super::super::homology::KmerHomologyEngine;
        let kmers = KmerHomologyEngine::protein_kmers("MK", 3);
        assert!(kmers.is_empty());
    }

    #[test]
    fn kmer_jaccard_identical_is_one() {
        use super::super::homology::KmerHomologyEngine;
        let a = KmerHomologyEngine::protein_kmers("MKFAL", 3);
        let sim = KmerHomologyEngine::jaccard(&a, &a);
        assert!((sim - 1.0).abs() < 1e-10);
    }

    #[test]
    fn kmer_jaccard_disjoint_is_zero() {
        use super::super::homology::KmerHomologyEngine;
        let a = KmerHomologyEngine::protein_kmers("MKFAL", 3);
        let b = KmerHomologyEngine::protein_kmers("DDDDD", 3);
        let sim = KmerHomologyEngine::jaccard(&a, &b);
        assert!((sim - 0.0).abs() < 1e-10);
    }

    #[test]
    fn combined_screen_includes_regex_hits() {
        let p = profile();
        let h = vec![hit("select-agent", "sap-1")];
        let bundle = dna("ATGAAA");
        let c = ctx(&h, &p);
        let hits = super::combined_screen(&bundle, &c, SELECT_AGENT_CLASSES);
        assert!(!hits.is_empty());
        assert!(hits.iter().any(|h| h.contains("sap-1")));
    }

    #[test]
    fn combined_screen_no_hits_for_clean_bundle() {
        let p = profile();
        let bundle = dna("ATGAAA");
        let c = ctx(&[], &p);
        let hits = super::combined_screen(&bundle, &c, SELECT_AGENT_CLASSES);
        assert!(hits.is_empty());
    }

    #[test]
    fn protein_rescreen_detects_codon_substituted_homolog() {
        // Create a hazard hit with a DNA pattern that translates to the same
        // protein as the bundle DNA (codon substitution).
        let p = profile();
        // "ATGAAAGCG" translates frame1 to "MKA"
        // "ATGAAGGCC" translates frame1 to "MKA" (synonymous codons)
        let h = vec![HazardHit {
            entry: HazardEntry {
                id: "sap-codon-sub".into(),
                label: "codon-sub test".into(),
                hazard_class: "select-agent".into(),
                pattern: ".*".into(),
            },
            matched_text: "ATGAAAGCG".into(), // MKA in frame1
        }];
        let bundle = dna("ATGAAGGCC"); // MKA in frame1 (synonymous)
        let c = ctx(&h, &p);
        let protein_hits = super::protein_space_rescreen(&bundle, &c, SELECT_AGENT_CLASSES);
        // The protein k-mers should overlap since both encode MKA.
        // With k=5 and only 3 residues, there won't be enough k-mers.
        // Use a longer sequence to trigger the threshold.
        // This test verifies the function runs without error.
        assert!(protein_hits.is_empty()); // too short for k=5

        // Now with a longer sequence that shares protein k-mers.
        let long_dna = "ATGAAAGCGTTCCTGATTGATAACGAAGCC"; // 30nt = 10 codons
        let h2 = vec![HazardHit {
            entry: HazardEntry {
                id: "sap-long".into(),
                label: "long test".into(),
                hazard_class: "select-agent".into(),
                pattern: ".*".into(),
            },
            // Same protein via synonymous codons:
            matched_text: "ATGAAGGCCTTTTTGATTGACAATGAGGCC".into(),
        }];
        let bundle2 = dna(long_dna);
        let c2 = ctx(&h2, &p);
        let protein_hits2 = super::protein_space_rescreen(&bundle2, &c2, SELECT_AGENT_CLASSES);
        // Both encode "MKAFLIDNEA" — protein k-mers should match.
        assert!(
            !protein_hits2.is_empty(),
            "protein-space rescreen should detect synonymous-codon homolog"
        );
    }

    #[test]
    fn d1_detects_protein_level_hit() {
        let p = profile();
        // Create a hit whose matched_text shares protein-level homology.
        let long_dna = "ATGAAAGCGTTCCTGATTGATAACGAAGCC";
        let h = vec![HazardHit {
            entry: HazardEntry {
                id: "sap-protein".into(),
                label: "protein hit".into(),
                hazard_class: "select-agent".into(),
                pattern: ".*".into(),
            },
            matched_text: "ATGAAGGCCTTTTTGATTGACAATGAGGCC".into(),
        }];
        let bundle = dna(long_dna);
        let c = ctx(&h, &p);
        let status = SelectAgentScreen.evaluate_with(&bundle, &c);
        assert!(
            matches!(status, InvariantStatus::Fail { .. }),
            "D1 should fail on protein-level hit: got {status:?}"
        );
    }

    // ---- D7 chi-squared deviation for wrong-organism codons ----

    #[test]
    fn d7_chi_squared_yeast_codons_deviate_from_ecoli() {
        // Build a sequence using yeast-preferred codons (AGA, TTA, ACA, etc.)
        // that are rare in E. coli. Declare E. coli as the host organism.
        // Shannon entropy should be within the E. coli band, but chi-squared
        // should flag significant deviation.
        let mut p = profile();
        p.codon_usage_organism = Some("e_coli".into());
        // Yeast-biased codons: heavy on AGA (rare in E.coli: 0.002),
        // TTA (0.013), ACA (0.007), ATA (0.004).
        let codons = [
            "AGA", "AGA", "AGA", "AGA", "AGA", "TTA", "TTA", "TTA", "TTA", "TTA", "ACA", "ACA",
            "ACA", "ACA", "ACA", "ATA", "ATA", "ATA", "ATA", "ATA", "GAA", "GAA", "GAA", "GAA",
            "GAA", "ATG", "ATG", "ATG", "ATG", "ATG", "CTG", "CTG", "CTG", "CTG", "CTG", "GCG",
            "GCG", "GCG", "GCG", "GCG",
        ];
        let seq: String = codons.join("");
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(&seq), &c);
        // Should produce advisory (either entropy or chi-squared deviation).
        assert!(
            matches!(status, InvariantStatus::Advisory { .. }),
            "yeast-biased codons should deviate from E. coli CUTG: got {status:?}"
        );
    }

    #[test]
    fn d7_chi_squared_advisory_contains_chi_squared_info() {
        // Deliberately skewed codon usage with an organism declared should
        // produce a chi-squared advisory mentioning the organism and p-value.
        let mut p = profile();
        p.codon_usage_organism = Some("h_sapiens".into());
        // Extremely biased: only 4 distinct codons, heavily skewed.
        let codons: Vec<&str> = std::iter::repeat_n("AGA", 15)
            .chain(std::iter::repeat_n("CTG", 15))
            .chain(std::iter::repeat_n("GAA", 5))
            .chain(std::iter::repeat_n("ATG", 5))
            .collect();
        let seq: String = codons.join("");
        let c = ctx(&[], &p);
        let status = CodonEntropyScreen.evaluate_with(&dna(&seq), &c);
        if let InvariantStatus::Advisory { note } = &status {
            // Could be entropy or chi-squared; either is correct for skewed usage.
            assert!(
                note.contains("entropy") || note.contains("χ²") || note.contains("CUTG"),
                "advisory should mention entropy or chi-squared: {note}"
            );
        } else {
            panic!("expected Advisory, got {status:?}");
        }
    }

    // ---- Protein-space rescreen: reverse-complement detection ----

    #[test]
    fn protein_rescreen_catches_reverse_complement_homolog() {
        let p = profile();
        // The reverse complement of this sequence should translate to the
        // same protein in one of its frames.
        let fwd = "ATGAAAGCGTTCCTGATTGATAACGAAGCC";
        let rc_bytes = super::revcomp(fwd.to_ascii_uppercase().as_bytes());
        let rc = String::from_utf8(rc_bytes).unwrap();
        let h = vec![HazardHit {
            entry: HazardEntry {
                id: "sap-rc".into(),
                label: "reverse complement".into(),
                hazard_class: "select-agent".into(),
                pattern: ".*".into(),
            },
            matched_text: fwd.into(),
        }];
        // Bundle contains the reverse complement.
        let bundle = dna(&rc);
        let c = ctx(&h, &p);
        let hits = super::protein_space_rescreen(&bundle, &c, SELECT_AGENT_CLASSES);
        assert!(
            !hits.is_empty(),
            "should detect protein homology via reverse complement"
        );
    }

    // ---- Profile-configured k-mer parameters ----

    #[test]
    fn profile_kmer_k_overrides_default() {
        let mut p = profile();
        p.protein_kmer_k = Some(3);
        let h = vec![HazardHit {
            entry: HazardEntry {
                id: "sap-k3".into(),
                label: "k3 test".into(),
                hazard_class: "select-agent".into(),
                pattern: ".*".into(),
            },
            // 15nt -> 5 residues in frame1. With k=3 there are 3 k-mers.
            matched_text: "ATGAAAGCGTTCCTG".into(),
        }];
        let bundle = dna("ATGAAAGCGTTCCTG");
        let c = ctx(&h, &p);
        let hits = super::protein_space_rescreen(&bundle, &c, SELECT_AGENT_CLASSES);
        assert!(
            !hits.is_empty(),
            "smaller k should produce hits for identical 5-residue proteins"
        );
    }
}