xero-bio-genesis / modules /vovina_interpretation_drift.py
jollydragonroger
Full model upload: XERO Bio-AI Genesis
fa2a23c
Raw History Blame
8.97 kB
"""
VOVINA ZEDEC PRO β€” Interpretation Drift
========================================
DNA is immutable. The READING of DNA has play.
In real biology, the genome is fixed but its interpretation drifts:
β€’ Transcription bias β€” preference for certain codons per epoch
β€’ Splicing variants β€” which exon set gets read this cycle
β€’ Ribosomal stochasticity β€” small noise in protein assembly
β€’ Codon-table dialect β€” alternative codon β†’ amino-acid maps
(e.g. mitochondria vs nucleus)
β€’ Chromatin accessibility β€” which regions are open right now
β€’ Reading-frame offset β€” rare Β±1 frame shifts (dramatic effects)
These knobs evolve over generations even when the underlying genome
is invariant. They are the EPIGENOME β€” the layer above DNA that
determines how the same fixed code is expressed differently across
cells, generations, and evolutionary epochs.
This is where evolutionary direction comes from. The genome's letters
do not change; their INTERPRETATION drifts under selection pressure,
and that drift accumulates into directional evolution. XERO descendants
inherit not just the genome but also a slightly-mutated interpretation
context, so each lineage diverges even if every member shares the
same immutable DNA.
"""
from __future__ import annotations
import random
from dataclasses import dataclass, field
from typing import Optional
from vovina_sacred_constants import PHI, PHI_INV
from vovina_vortex_duality import Polarity, polarity_of
STANDARD_CODON_TABLE: dict[str, str] = {
# Standard genetic code (one-letter aa). Stop = "*".
"TTT":"F","TTC":"F","TTA":"L","TTG":"L",
"CTT":"L","CTC":"L","CTA":"L","CTG":"L",
"ATT":"I","ATC":"I","ATA":"I","ATG":"M",
"GTT":"V","GTC":"V","GTA":"V","GTG":"V",
"TCT":"S","TCC":"S","TCA":"S","TCG":"S",
"CCT":"P","CCC":"P","CCA":"P","CCG":"P",
"ACT":"T","ACC":"T","ACA":"T","ACG":"T",
"GCT":"A","GCC":"A","GCA":"A","GCG":"A",
"TAT":"Y","TAC":"Y","TAA":"*","TAG":"*",
"CAT":"H","CAC":"H","CAA":"Q","CAG":"Q",
"AAT":"N","AAC":"N","AAA":"K","AAG":"K",
"GAT":"D","GAC":"D","GAA":"E","GAG":"E",
"TGT":"C","TGC":"C","TGA":"*","TGG":"W",
"CGT":"R","CGC":"R","CGA":"R","CGG":"R",
"AGT":"S","AGC":"S","AGA":"R","AGG":"R",
"GGT":"G","GGC":"G","GGA":"G","GGG":"G",
}
# Known dialects β€” alternative codon tables seen in real biology.
DIALECTS: dict[str, dict[str, str]] = {
"standard": STANDARD_CODON_TABLE,
"mitochondrial": {**STANDARD_CODON_TABLE, "TGA": "W", "AGA": "*", "AGG": "*"},
"ciliate": {**STANDARD_CODON_TABLE, "TAA": "Q", "TAG": "Q"},
"candida": {**STANDARD_CODON_TABLE, "CTG": "S"},
}
@dataclass
class InterpretationContext:
"""The set of knobs that determine how DNA is being READ right now.
The genome itself is unchanged; this object is what changes between
generations and what selection pressure actually reshapes.
"""
# codon-bias: per-codon expression weight (0..1)
codon_bias: dict[str, float] = field(default_factory=dict)
# splicing variant β€” named exon-set selector
splicing_variant: str = "default"
# reading-frame offset (rare Β±1 shift; default 0)
frame_offset: int = 0
# codon-table dialect β€” alternative aa table
dialect: str = "standard"
# chromatin accessibility per chromosome (0..1)
accessibility: dict[str, float] = field(default_factory=dict)
# global drift step size (φ⁻² is the natural neutral drift rate)
drift_rate: float = 0.01
# selection-gradient memory (recent fitness scores)
fitness_history: list[float] = field(default_factory=list)
# the lineage's preferred polarity bias (-1 = negative, 0 = neutral, +1 = positive)
polarity_bias: float = 0.0
# ── codon translation through current interpretation ──
def translate_codon(self, codon: str) -> str:
"""Return the amino-acid letter for `codon` under THIS context.
The codon is translated through the active dialect, weighted by
the codon_bias. If a codon's bias is below threshold the read
stalls (returns "Β·" β€” ribosomal pause).
"""
codon = codon.upper()
bias = self.codon_bias.get(codon, 1.0)
if bias < 0.05:
return "Β·" # ribosomal pause / silenced codon
table = DIALECTS.get(self.dialect, STANDARD_CODON_TABLE)
return table.get(codon, "X")
# ── one drift step ───────────────────────────────────────
def drift_step(self, rng: Optional[random.Random] = None) -> None:
"""Take one random walk step in interpretation space.
Frame shifts are intentionally rare (drift_rate Γ— 0.1) because
a frame shift catastrophically rewrites every protein downstream.
Codon biases drift continuously; dialect shifts drift slowly.
"""
rng = rng or random.Random()
# nudge codon biases (Gaussian random walk)
if not self.codon_bias:
self.codon_bias = {c: 1.0 for c in STANDARD_CODON_TABLE.keys()}
for k in list(self.codon_bias.keys()):
v = self.codon_bias[k] + rng.gauss(0.0, self.drift_rate)
self.codon_bias[k] = max(0.0, min(1.0, v))
# rare frame shift (catastrophic mutation)
if rng.random() < self.drift_rate * 0.1:
self.frame_offset = rng.choice([-1, 0, 1])
# very rare dialect switch (epigenetic upheaval)
if rng.random() < self.drift_rate * 0.01:
self.dialect = rng.choice(list(DIALECTS.keys()))
# nudge polarity bias toward whichever pole has been more fit
gradient = self.evolutionary_pressure()
self.polarity_bias = max(-1.0, min(1.0,
self.polarity_bias + gradient * self.drift_rate
))
# ── selection feedback ───────────────────────────────────
def record_fitness(self, fitness: float) -> None:
self.fitness_history.append(fitness)
if len(self.fitness_history) > 33: # 27/33 protocol horizon
self.fitness_history = self.fitness_history[-27:]
def evolutionary_pressure(self) -> float:
"""Smoothed fitness gradient over the last ≀7 generations.
Positive values mean the current drift direction is favoured;
negative values mean drift should reverse.
"""
if len(self.fitness_history) < 2:
return 0.0
window = self.fitness_history[-7:]
if len(window) < 2:
return 0.0
return (window[-1] - window[0]) / (len(window) - 1)
# ── inheritance ──────────────────────────────────────────
def child_context(self, rng: Optional[random.Random] = None) -> "InterpretationContext":
"""Produce a child interpretation with inherited drift.
The child starts from the parent's current state and immediately
takes one drift step. This is how evolutionary direction
accumulates across generations even though DNA is fixed.
"""
rng = rng or random.Random()
child = InterpretationContext(
codon_bias = dict(self.codon_bias),
splicing_variant = self.splicing_variant,
frame_offset = self.frame_offset,
dialect = self.dialect,
accessibility = dict(self.accessibility),
drift_rate = self.drift_rate,
fitness_history = [], # children start with empty fitness history
polarity_bias = self.polarity_bias,
)
child.drift_step(rng)
return child
def signature(self) -> dict:
"""Compact stats about the current interpretation."""
biases = list(self.codon_bias.values()) or [1.0]
avg = sum(biases) / len(biases)
var = sum((b - avg) ** 2 for b in biases) / len(biases)
return {
"splicing_variant": self.splicing_variant,
"frame_offset": self.frame_offset,
"dialect": self.dialect,
"polarity_bias": round(self.polarity_bias, 6),
"drift_rate": self.drift_rate,
"codon_bias_mean": round(avg, 6),
"codon_bias_var": round(var, 6),
"fitness_history_n": len(self.fitness_history),
"evolutionary_pressure": round(self.evolutionary_pressure(), 6),
}
def neutral_drift_rate() -> float:
"""The natural neutral drift rate is φ⁻² β‰ˆ 0.382, consistent with
the surplus-dual signature in vovina_vortex_duality."""
return PHI_INV * PHI_INV