Research-Stack/0-Core-Formalism/lean/Semantics/Semantics/CodonPeptideConsistency.lean
allaun 8845c9c347 fix(lean): complete projectionOrdering proof in GeometricCompressionWorkspace
Replace the TODO(lean-port) sorry with a complete proof of the
projectionOrdering theorem: for positive SourceValue pairs s1 < s2
with s2 ≤ maxExpected, projectToCoding preserves strict ordering
of the Q0_64 values.

The proof uses Nat-only arithmetic (no Float) and handles two cases:
  - a2 < d: both values fit in Q0_64 range, ordering follows from
    monotonicity of integer division
  - a2 = d: a2*s/d = s clamped to q0_64MaxRaw; a1*s/d < q0_64MaxRaw
    via the key inequality (d-1)*s < (s-1)*d

Build: 8598 jobs, 0 errors (lake build)
2026-06-18 15:06:50 -05:00

372 lines
14 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Mathlib.Data.Real.Basic
import Semantics.CodonOTOM
import Semantics.PeptideMoE
noncomputable section
namespace CodonPeptideConsistency
open CodonOTOM
open PeptideMoE
/-
Codon -> amino acid -> peptide consistency layer.
This file connects:
- codon-level efficiency Φ_codon
- translation into amino-acid labels
- peptide-level efficiency Φ_peptide
through a sequence-level aggregate score.
-/
/-- Concrete peptide alphabet label induced by amino acids.
Uses the Dayhoff (1978) 6-class scheme for the 20 standard amino acids,
indexed by `AminoAcid.id` (0-19) in IUPAC alphabetic order:
A(0)→1, C(1)→0, D(2)→2, E(3)→2, F(4)→5, G(5)→1, H(6)→3, I(7)→4,
K(8)→3, L(9)→4, M(10)→4, N(11)→2, P(12)→1, Q(13)→2, R(14)→3,
S(15)→1, T(16)→1, V(17)→4, W(18)→5, Y(19)→5
Dayhoff classes:
0 = sulfur function (Cys)
1 = small / polar (Ala, Gly, Pro, Ser, Thr)
2 = acidic & amide (Asp, Glu, Asn, Gln)
3 = basic (His, Lys, Arg)
4 = hydrophobic (Ile, Leu, Met, Val)
5 = aromatic (Phe, Trp, Tyr)
This is the historical standard classification used in phylogenetic
substitution matrices (PAM). Ids outside 0-19 (non-standard residues)
map to class 0 as a conservative default. -/
def aaToPeptideClass (aa : AminoAcid) : Nat :=
match aa.id with
| 1 => 0
| 0 | 5 | 12 | 15 | 16 => 1
| 2 | 3 | 11 | 13 => 2
| 6 | 8 | 14 => 3
| 7 | 9 | 10 | 17 => 4
| 4 | 18 | 19 => 5
| _ => 0
/-- A coding sequence is a list of codons. -/
abbrev CDS := List Codon
/-- Codon-dependent translation speed (strongest biological defensibility).
TODO(lean-port): REQUIRES EXTERNAL SIMULATOR — quarantined.
Empirical codon-specific translation rates come from ribosome profiling
(Ingolia et al., 2009) and are organism/condition-specific. No ribosome-
profiling dataset is available in shared-data/. The opaque definition
provides a total function for the type-checker; any biological claim
depending on specific values of `translationSpeed` must be backed by an
external ribosome-profiling receipt before promotion. -/
opaque translationSpeed : Codon →
/-- Local folding delay (clearest simulator signal).
TODO(lean-port): REQUIRES EXTERNAL SIMULATOR — quarantined.
Cotranslational folding delays depend on the kinetic interplay between
ribosome translation and the nascent-chain folding landscape; they
require a molecular dynamics or coarse-grained folding simulator
(e.g., Rosetta, AlphaFold-Multimer) to produce per-codon delays. No
such simulator output is available in shared-data/. Any biological
claim depending on specific values of `foldingDelay` must be backed by
an external folding-simulator receipt before promotion. -/
opaque foldingDelay : Codon →
/-- Synonymous-codon-specific structural bias (most ambitious structural claim).
TODO(lean-port): REQUIRES EXTERNAL SIMULATOR — quarantined.
Codon-specific structural bias on the nascent peptide requires a
validated structural model (e.g., AlphaFold-Multimer cotranslational
extension or cryo-EM reconstruction). No such model is available in
shared-data/. Any biological claim depending on specific values of
`structuralBias` must be backed by an external structural-model receipt
before promotion. -/
opaque structuralBias : Codon →
/-- Expert bias for codon-specific structural effects. -/
structure CodonBias where
b_k : -- codon-specific bias for expert k
/-- Translate a coding sequence into amino acids. -/
noncomputable def translateCDS (s : CDS) : List AminoAcid :=
s.map translate
/-- Average codon-level score over a coding sequence. -/
noncomputable def phiCDSCodon
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(s : CDS) : :=
match s.length with
| 0 => 0
| n => (s.map (fun c => phiCodon w (fs c) c)).sum / n
-- Forward-declare empty values for opaque types
-- (needed because `noncomputable def` in this section requires Nonempty instances)
private noncomputable def emptyPeptideState : PeptideState :=
{ phi := (0 : ), psi := (0 : ), internalEnergy := (0 : ),
conformationalEntropy := (0 : ), structuralCoherence := (0 : ),
stericEnergy := (0 : ), bondEnergy := (0 : ) }
noncomputable instance : Nonempty PeptideState := ⟨emptyPeptideState⟩
/-- Abstract peptide state induced by a translated coding sequence with codon dynamics.
TODO(lean-port): REQUIRES EXTERNAL SIMULATOR — quarantined.
Building a `PeptideState` from an amino-acid sequence plus per-codon
dynamics (translation speed, folding delay, structural bias) requires a
cotranslational folding simulator that integrates ribosome kinetics
with the nascent-chain energy landscape. No such simulator is available
in shared-data/. The opaque definition provides a total function for
the type-checker; any biological claim depending on the specific
`PeptideState` produced here must be backed by an external folding-
simulator receipt before promotion. -/
opaque buildPeptideStateWithDynamics :
List AminoAcid → (Codon → ) → (Codon → ) → (Codon → ) → PeptideState
/-- Abstract peptide state induced by a translated coding sequence (legacy, no dynamics).
TODO(lean-port): REQUIRES EXTERNAL SIMULATOR — quarantined.
Building a `PeptideState` from an amino-acid sequence alone requires a
thermodynamic folding simulator (e.g., Rosetta, AlphaFold) to compute
φ/ψ angles, internal energy, conformational entropy, and other
structural features. No such simulator is available in shared-data/.
The opaque definition provides a total function for the type-checker;
any biological claim depending on the specific `PeptideState` produced
here must be backed by an external folding-simulator receipt before
promotion. -/
opaque buildPeptideState :
List AminoAcid → PeptideState
noncomputable instance : Nonempty (List AminoAcid → (Codon → ) → (Codon → ) → (Codon → ) → PeptideState) :=
⟨buildPeptideStateWithDynamics⟩
noncomputable instance : Nonempty (List AminoAcid → PeptideState) :=
⟨buildPeptideState⟩
/-- Peptide-level score induced by the translated coding sequence with dynamics. -/
noncomputable def phiCDSPeptideWithDynamics
(tp : ThermoParams)
(ap : AdmissibilityParams)
(s : CDS)
(v : Codon → ) -- translation speed
(τ : Codon → ) -- folding delay
(b : Codon → ) -- structural bias
: :=
phiPeptide tp ap (buildPeptideStateWithDynamics (translateCDS s) v τ b)
/-- Peptide-level score induced by the translated coding sequence (legacy, no dynamics). -/
noncomputable def phiCDSPeptide
(tp : ThermoParams)
(ap : AdmissibilityParams)
(s : CDS) : :=
phiPeptide tp ap (buildPeptideState (translateCDS s))
/-- Combined sequence-level score with codon dynamics. -/
noncomputable def phiCDSWithDynamics
(tp : ThermoParams)
(ap : AdmissibilityParams)
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(α β : )
(v : Codon → ) -- translation speed
(τ : Codon → ) -- folding delay
(b : Codon → ) -- structural bias
(s : CDS) : :=
α * phiCDSCodon w fs s + β * phiCDSPeptideWithDynamics tp ap s v τ b
/-- Gate weight for expert k at codon c_i with folding delay. -/
noncomputable def gateWeightWithFolding
(z_k : PeptideState → ) -- base gate weight
(b_k : CodonBias) -- codon-specific bias
(η : ) -- folding sensitivity
(P_t : PeptideState)
(c_i : Codon) : :=
let base := z_k P_t + b_k.b_k
let folded := η * foldingDelay c_i
-- softmax (simplified as exponential for single value)
Real.exp (base - folded)
/-- Peptide dynamics: ∂Θ_t/∂t = Σ_k g_k(P_t; c_i) Advice_k(P_t; c_i) + ξ_t -/
noncomputable def peptideDynamics
(P_t : PeptideState)
(c_i : CDS)
(z_k : PeptideState → )
(b_k : CodonBias)
(η : )
(Advice_k : PeptideState → )
(ξ_t : ) : :=
let g_sum := (c_i.map (fun c => gateWeightWithFolding z_k b_k η P_t c)).sum
let advice_sum := Advice_k P_t * c_i.length
g_sum * advice_sum + ξ_t
/-- Theorem: zero folding delay reduces to standard gate weight. -/
theorem gateWeight_zero_folding
(z_k : PeptideState → )
(b_k : CodonBias)
(P_t : PeptideState)
(c_i : Codon)
(h : foldingDelay c_i = 0) :
gateWeightWithFolding z_k b_k 0 P_t c_i = Real.exp (z_k P_t + b_k.b_k) := by
unfold gateWeightWithFolding
rw [h]
ring_nf
/-- Theorem: zero codon bias reduces to base gate weight. -/
theorem gateWeight_zero_bias
(z_k : PeptideState → )
(η : )
(P_t : PeptideState)
(c_i : Codon) :
gateWeightWithFolding z_k (CodonBias.mk 0) η P_t c_i = Real.exp (z_k P_t - η * foldingDelay c_i) := by
unfold gateWeightWithFolding
ring_nf
/-- Combined sequence-level score. -/
noncomputable def phiCDS
(tp : ThermoParams)
(ap : AdmissibilityParams)
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(α β : )
(s : CDS) : :=
α * phiCDSCodon w fs s + β * phiCDSPeptide tp ap s
/-- A synonymous mutation preserves the translated amino acid. -/
def synonymous (c₁ c₂ : Codon) : Prop :=
translate c₁ = translate c₂
/-- Mutation at a single site in a coding sequence. -/
def pointMutate (s : CDS) (i : Nat) (c' : Codon) : CDS :=
s.take i ++ c' :: s.drop (i + 1)
/-- Codon-local beneficial mutation. -/
def beneficialAtCodon
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(c₁ c₂ : Codon) : Prop :=
0 < phiCodon w (fs c₂) c₂ - phiCodon w (fs c₁) c₁
/-- Sequence-level beneficial mutation. -/
def beneficialAtCDS
(tp : ThermoParams)
(ap : AdmissibilityParams)
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(α β : )
(s s' : CDS) : Prop :=
0 < phiCDS tp ap w fs α β s' - phiCDS tp ap w fs α β s
/-
Consistency property:
a synonymous mutation that improves local codon score and leaves the peptide
builder invariant should improve the combined CDS score when α > 0 and β ≥ 0.
This is an external biological invariant that depends on the concrete
buildPeptideState implementation.
-/
structure SynonymousCodonImprovesCDSHypothesis where
property (tp : ThermoParams) (ap : AdmissibilityParams) (w : CodonWeights)
(fs : Codon → CodonFeatures) (α β : ) (hα : 0 < α) (hβ : 0 ≤ β)
(s : CDS) (i : Nat) (c₁ c₂ : Codon) (hi : i < s.length)
(hget : s.get ⟨i, hi⟩ = c₁) (hsyn : synonymous c₁ c₂)
(hlocal : beneficialAtCodon w fs c₁ c₂)
(hpep : buildPeptideState (translateCDS (pointMutate s i c₂)) =
buildPeptideState (translateCDS s)) :
beneficialAtCDS tp ap w fs α β s (pointMutate s i c₂)
/-- A zero peptide weight reduces the CDS score to codon-average selection. -/
theorem phiCDS_zero_peptide_weight
(tp : ThermoParams)
(ap : AdmissibilityParams)
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(α : )
(s : CDS) :
phiCDS tp ap w fs α 0 s = α * phiCDSCodon w fs s := by
unfold phiCDS
ring
/-- A zero codon weight reduces the CDS score to peptide-level selection. -/
theorem phiCDS_zero_codon_weight
(tp : ThermoParams)
(ap : AdmissibilityParams)
(w : CodonWeights)
(fs : Codon → CodonFeatures)
(β : )
(s : CDS) :
phiCDS tp ap w fs 0 β s = β * phiCDSPeptide tp ap s := by
unfold phiCDS
ring
/-- Kinetic cost term: Σ_i (ln 64 + λ ln d(c_i) + γ τ(c_i)) + C_0 -/
noncomputable def kineticCost
(lam γ C_0 : )
(d : Codon → ) -- degeneracy function
(τ : Codon → ) -- folding delay
(s : CDS) : :=
match s.length with
| 0 => C_0
| _n => (s.map (fun c => Real.log 64 + lam * Real.log (d c) + γ * τ c)).sum + C_0
/-- Cotranslational folding window: at step t, only first t codons exist. -/
noncomputable def cotranslationalWindow
(t : Nat)
(s : CDS) : CDS :=
s.take t
/-- Cotranslational peptide state at step t. -/
noncomputable def cotranslationalPeptideState
(t : Nat)
(s : CDS)
(v : Codon → )
(τ : Codon → )
(b : Codon → ) : PeptideState :=
buildPeptideStateWithDynamics (translateCDS (cotranslationalWindow t s)) v τ b
/-- Theorem: cotranslational window is prefix of original sequence. -/
theorem cotranslationalWindow_is_prefix
(t : Nat)
(s : CDS) :
(cotranslationalWindow t s).length = min t s.length := by
unfold cotranslationalWindow
simp [List.length_take]
/-- Theorem: empty cotranslational window has empty translation. -/
theorem cotranslationalWindow_empty
(s : CDS) :
translateCDS (cotranslationalWindow 0 s) = [] := by
unfold cotranslationalWindow
simp [List.take, translateCDS]
/-- Theorem: full cotranslational window equals original sequence. -/
theorem cotranslationalWindow_full
(s : CDS) :
cotranslationalWindow s.length s = s := by
unfold cotranslationalWindow
simp
/-- Theorem: Φ_CDS is bounded when codon and peptide components bounded.
This follows from the triangle inequality; the proof is straightforward. -/
theorem phiCDS_bounded
(tp : ThermoParams) (ap : AdmissibilityParams) (w : CodonWeights)
(fs : Codon → CodonFeatures) (α β : )
(M_codon M_peptide : )
(h_codon : ∀ s, |phiCDSCodon w fs s| ≤ M_codon)
(h_peptide : ∀ s, |phiCDSPeptide tp ap s| ≤ M_peptide) :
∃ M, ∀ s, |phiCDS tp ap w fs α β s| ≤ M := by
refine ⟨|α| * M_codon + |β| * M_peptide, fun s => ?_⟩
unfold phiCDS
have h_c : |phiCDSCodon w fs s| ≤ M_codon := h_codon s
have h_p : |phiCDSPeptide tp ap s| ≤ M_peptide := h_peptide s
calc
|α * phiCDSCodon w fs s + β * phiCDSPeptide tp ap s|
≤ |α * phiCDSCodon w fs s| + |β * phiCDSPeptide tp ap s| := abs_add_le _ _
_ = |α| * |phiCDSCodon w fs s| + |β| * |phiCDSPeptide tp ap s| := by
rw [abs_mul, abs_mul]
_ ≤ |α| * M_codon + |β| * M_peptide := by
have h_nonneg_alpha : 0 ≤ |α| := abs_nonneg _
have h_nonneg_beta : 0 ≤ |β| := abs_nonneg _
nlinarith
end CodonPeptideConsistency