nx_peptide_formula_test.nx source
↩ module page · 124 lines · 8000 B
1// nx_peptide_formula_test.nx -- THE LIAR-KILLER GATE. Triangulates the peptide
2// mass stack against external ground truth by two independent computation
3// paths plus published molecular formulas. If nx_peptide's residue table were
4// wrong, these tests would catch it -- because the elemental path does not
5// share that table, and the formulas are hand-checkable integers.
6// expect_exit: 0 license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_peptide.nx"
9import "nx_peptide_formula.nx"
10
11func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
12func t_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 }
13
14func main() -> i64 {
15 var pass: i64 = 0
16 var total: i64 = 0
17
18 let ang: *u8 = "DRVYIHPF" as *u8
19 let bk: *u8 = "RPPGFSPFR" as *u8
20
21 // --- T1 ANGIOTENSIN II: derived formula == published C50H71N13O12 ---
22 // The sharpest external check -- hand-verifiable integers, not a mass.
23 total = total + 1
24 let f: *NxFormula = pfm_formula(ang)
25 t_puts("T1 angiotensin II derived formula = C" as *u8); t_putn(f.c); t_puts("H" as *u8); t_putn(f.h); t_puts("N" as *u8); t_putn(f.n); t_puts("O" as *u8); t_putn(f.o); t_puts("S" as *u8); t_putn(f.s); t_puts(" vs published C50H71N13O12: " as *u8)
26 if pfm_formula_matches(ang, 50, 71, 13, 12, 0) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
27
28 // --- T2 BRADYKININ: derived formula == published C50H73N15O11 ---
29 total = total + 1
30 let fb: *NxFormula = pfm_formula(bk)
31 t_puts("T2 bradykinin derived formula = C" as *u8); t_putn(fb.c); t_puts("H" as *u8); t_putn(fb.h); t_puts("N" as *u8); t_putn(fb.n); t_puts("O" as *u8); t_putn(fb.o); t_puts(" vs published C50H73N15O11: " as *u8)
32 if pfm_formula_matches(bk, 50, 73, 15, 11, 0) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
33
34 // --- T3 TWO INDEPENDENT METHODS AGREE: elemental path vs residue-sum ---
35 // The core liar-kill: a different primitive, same answer.
36 total = total + 1
37 let m_elem: i64 = pfm_mass_q4(ang)
38 let m_resi: i64 = pep_mass_mono_q4(ang)
39 var d3: i64 = m_elem - m_resi
40 if d3 < 0 { d3 = 0 - d3 }
41 t_puts("T3 angiotensin: elemental=" as *u8); t_putn(m_elem); t_puts(" residue-sum=" as *u8); t_putn(m_resi); t_puts(" delta=" as *u8); t_putn(d3); t_puts(" Q4 (<=3, tight): " as *u8)
42 if pfm_methods_agree(ang, 3) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
43
44 // --- T4 BOTH methods land on the PUBLISHED mass 1045.5345 ---
45 total = total + 1
46 var de: i64 = m_elem - 10455345
47 if de < 0 { de = 0 - de }
48 let resi_ok: i64 = pep_mass_agrees(m_resi, 10455345, 5)
49 t_puts("T4 elemental vs lit 10455345: delta=" as *u8); t_putn(de); t_puts(" residue-vs-lit ok=" as *u8); t_putn(resi_ok); t_puts(": " as *u8)
50 if de <= 20 { if resi_ok == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
51
52 // --- T5 the two methods agree across a BATTERY of sequences ---
53 // If any residue's two representations disagreed, one of these breaks.
54 total = total + 1
55 // Tight tolerance (<=3 Q4 = 0.0003 Da) across every case: the two methods
56 // now genuinely agree, so a loose tolerance would only hide a future liar.
57 var ok5: i64 = 1
58 if pfm_methods_agree("ACDEFGHIKLMNPQRSTVWY" as *u8, 3) != 1 { ok5 = 0 } // all 20
59 if pfm_methods_agree("GGGGGG" as *u8, 3) != 1 { ok5 = 0 }
60 if pfm_methods_agree("WWWWWW" as *u8, 3) != 1 { ok5 = 0 }
61 if pfm_methods_agree("CCCCCC" as *u8, 3) != 1 { ok5 = 0 }
62 if pfm_methods_agree("RPPGFSPFR" as *u8, 3) != 1 { ok5 = 0 }
63 if pfm_methods_agree("DRVYIHPF" as *u8, 3) != 1 { ok5 = 0 }
64 t_puts("T5 two methods agree (tol<=3 Q4) across all-20 + homopolymers + real peptides: " as *u8)
65 if ok5 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
66
67 // --- T6 NEGATIVE CONTROL: a deliberately WRONG published formula is
68 // REJECTED. Proves the check can actually fail (not vacuous). ---
69 total = total + 1
70 let wrong1: i64 = pfm_formula_matches(ang, 50, 71, 14, 12, 0) // N14 not N13
71 let wrong2: i64 = pfm_formula_matches(ang, 51, 71, 13, 12, 0) // C51 not C50
72 let right: i64 = pfm_formula_matches(ang, 50, 71, 13, 12, 0)
73 t_puts("T6 wrong-N rejected=" as *u8); t_putn(wrong1); t_puts(" wrong-C rejected=" as *u8); t_putn(wrong2); t_puts(" right accepted=" as *u8); t_putn(right); t_puts(" want 0/0/1: " as *u8)
74 var ok6: i64 = 1
75 if wrong1 != 0 { ok6 = 0 }
76 if wrong2 != 0 { ok6 = 0 }
77 if right != 1 { ok6 = 0 }
78 if ok6 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
79
80 // --- T7 NEGATIVE CONTROL: the methods must DISAGREE on tampered input.
81 // Compare angiotensin's elemental mass to bradykinin's residue
82 // mass -- different molecules, must NOT pass the agree check. ---
83 total = total + 1
84 let m_ang_elem: i64 = pfm_mass_q4(ang)
85 let m_bk_resi: i64 = pep_mass_mono_q4(bk)
86 var cross: i64 = m_ang_elem - m_bk_resi
87 if cross < 0 { cross = 0 - cross }
88 t_puts("T7 cross-molecule delta (angiotensin elem vs bradykinin residue) = " as *u8); t_putn(cross); t_puts(" Q4, must be large (>1000): " as *u8)
89 if cross > 1000 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
90
91 // --- T8 a single residue triangulates: Trp elemental == residue == lit ---
92 // Trp monoisotopic residue = 186.07931 -> 1860793 Q4 (published).
93 total = total + 1
94 let w_elem: i64 = pfm_mass_q4("W" as *u8) - PFM_WATER_Q4 // strip the added water to get the residue
95 let w_resi: i64 = pep_residue_mono_q4(PEP_W)
96 var dw: i64 = w_elem - w_resi
97 if dw < 0 { dw = 0 - dw }
98 t_puts("T8 Trp residue: elemental=" as *u8); t_putn(w_elem); t_puts(" table=" as *u8); t_putn(w_resi); t_puts(" delta=" as *u8); t_putn(dw); t_puts(" Q4 (<=3): " as *u8)
99 if dw <= 3 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
100
101 // --- T9 fail-closed: a non-standard residue refuses in BOTH the formula
102 // and the mass paths ---
103 total = total + 1
104 let bad_f: *NxFormula = pfm_formula("DRVYIHPZ" as *u8)
105 let bad_m: i64 = pfm_mass_q4("DRVYIHPZ" as *u8)
106 t_puts("T9 bad residue: formula-valid=" as *u8); t_putn(bad_f.valid); t_puts(" mass=" as *u8); t_putn(bad_m); t_puts(" want 0/-1: " as *u8)
107 if bad_f.valid == 0 { if bad_m == PEP_INVALID { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) } } else { t_puts("FAIL\n" as *u8) }
108
109 // --- T10 the atom-count table is internally sound: sum of all residue
110 // atoms for the 20-mer equals the formula's fields (no packing
111 // overflow, all fields < 100 as encoded) ---
112 total = total + 1
113 let f20: *NxFormula = pfm_formula("ACDEFGHIKLMNPQRSTVWY" as *u8)
114 t_puts("T10 20-mer formula C" as *u8); t_putn(f20.c); t_puts("H" as *u8); t_putn(f20.h); t_puts("N" as *u8); t_putn(f20.n); t_puts("O" as *u8); t_putn(f20.o); t_puts("S" as *u8); t_putn(f20.s); t_puts(" (S=2 from C+M): " as *u8)
115 var ok10: i64 = 1
116 if f20.valid != 1 { ok10 = 0 }
117 if f20.s != 2 { ok10 = 0 }
118 if f20.c < 100 { ok10 = 0 }
119 if ok10 == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
120
121 t_puts("PEPTIDE-FORMULA-LIARKILL-GATE passed " as *u8); t_putn(pass); t_puts("/" as *u8); t_putn(total)
122 if pass == total { t_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
123 t_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1
124}