// trna.sy -- Transfer RNA and the ribosomal translation machinery.
//
// tRNA is the adapter molecule: anticodon reads the mRNA codon,
// and the amino acid attached at the 3' CCA end is added to the
// growing peptide chain. This module covers tRNA structure,
// aminoacyl-tRNA synthetases, and the ribosomal cycle.
//
// Sources:
//   Ibba M, Soll D. Annu Rev Biochem 69:617-650, 2000 (aaRS review)
//   Ramakrishnan V. Cell 108:557-572, 2002 (ribosome structure, Nobel 2009)
//   Schmeing TM, Ramakrishnan V. Nature 461:1234-1242, 2009
//
// Marjorie McCubbins + Aether, April 2026
// Patent pending: 64/034,536

struct AminoacylSynthetase {
    amino_acid: String,
    code1: String,
    enzyme_name: String,
    accession: String,
    gene: String,
    synthetase_class: String,
    citation: String,
}

struct RibosomalStep {
    step_number: Int,
    phase: String,
    description: String,
    key_factor: String,
    factor_accession: String,
    energy_cost: String,
    citation: String,
}

fn aars(aa: String, c1: String, en: String, ac: String, ge: String, sc: String, ci: String) -> AminoacylSynthetase {
    return AminoacylSynthetase { amino_acid: aa, code1: c1, enzyme_name: en, accession: ac, gene: ge, synthetase_class: sc, citation: ci }
}

fn rstep(sn: Int, ph: String, de: String, kf: String, fa: String, ec: String, ci: String) -> RibosomalStep {
    return RibosomalStep { step_number: sn, phase: ph, description: de, key_factor: kf, factor_accession: fa, energy_cost: ec, citation: ci }
}

// ═══════════════════════════════════════════════════════
// AMINOACYL-tRNA SYNTHETASES (the second genetic code)
// ═══════════════════════════════════════════════════════
// Each enzyme charges its specific tRNA with the correct amino acid.
// Error rate: ~1 in 10,000 (editing domain proofreads mischarging).

fn lookup_synthetase(code1: String) -> AminoacylSynthetase {
    if code1 == "A" { return aars("Alanine", "A", "Alanyl-tRNA synthetase", "P49588", "AARS1", "Class II", "Ibba M, Soll D. Annu Rev Biochem 69:617-650, 2000") }
    if code1 == "R" { return aars("Arginine", "R", "Arginyl-tRNA synthetase", "P54136", "RARS1", "Class I", "Ibba 2000") }
    if code1 == "N" { return aars("Asparagine", "N", "Asparaginyl-tRNA synthetase", "O43776", "NARS1", "Class II", "Ibba 2000") }
    if code1 == "D" { return aars("Aspartate", "D", "Aspartyl-tRNA synthetase", "P14868", "DARS1", "Class II", "Ibba 2000") }
    if code1 == "C" { return aars("Cysteine", "C", "Cysteinyl-tRNA synthetase", "P49589", "CARS1", "Class I", "Ibba 2000") }
    if code1 == "E" { return aars("Glutamate", "E", "Glutamyl-tRNA synthetase", "P07814", "EARS2", "Class I", "Ibba 2000") }
    if code1 == "Q" { return aars("Glutamine", "Q", "Glutaminyl-tRNA synthetase", "P47897", "QARS1", "Class I", "Ibba 2000") }
    if code1 == "G" { return aars("Glycine", "G", "Glycyl-tRNA synthetase", "P41250", "GARS1", "Class II", "Ibba 2000") }
    if code1 == "H" { return aars("Histidine", "H", "Histidyl-tRNA synthetase", "P12081", "HARS1", "Class II", "Ibba 2000") }
    if code1 == "I" { return aars("Isoleucine", "I", "Isoleucyl-tRNA synthetase", "P41252", "IARS1", "Class I", "Ibba 2000") }
    if code1 == "L" { return aars("Leucine", "L", "Leucyl-tRNA synthetase", "Q9P2J5", "LARS1", "Class I", "Ibba 2000") }
    if code1 == "K" { return aars("Lysine", "K", "Lysyl-tRNA synthetase", "Q15046", "KARS1", "Class II", "Ibba 2000") }
    if code1 == "M" { return aars("Methionine", "M", "Methionyl-tRNA synthetase", "P56192", "MARS1", "Class I", "Ibba 2000") }
    if code1 == "F" { return aars("Phenylalanine", "F", "Phenylalanyl-tRNA synthetase alpha", "Q9Y285", "FARSA", "Class II", "Ibba 2000") }
    if code1 == "P" { return aars("Proline", "P", "Prolyl-tRNA synthetase", "P22234", "PARS2", "Class II", "Ibba 2000") }
    if code1 == "S" { return aars("Serine", "S", "Seryl-tRNA synthetase", "P49591", "SARS1", "Class II", "Ibba 2000") }
    if code1 == "T" { return aars("Threonine", "T", "Threonyl-tRNA synthetase", "P26639", "TARS1", "Class II", "Ibba 2000") }
    if code1 == "W" { return aars("Tryptophan", "W", "Tryptophanyl-tRNA synthetase", "P23381", "WARS1", "Class I", "Ibba 2000") }
    if code1 == "Y" { return aars("Tyrosine", "Y", "Tyrosyl-tRNA synthetase", "P54577", "YARS1", "Class I", "Ibba 2000") }
    if code1 == "V" { return aars("Valine", "V", "Valyl-tRNA synthetase", "P26640", "VARS1", "Class I", "Ibba 2000") }
    return aars("", "", "Unknown", "", "", "", "")
}

// ═══════════════════════════════════════════════════════
// RIBOSOMAL TRANSLATION CYCLE
// ═══════════════════════════════════════════════════════

fn translation_step(step: Int) -> RibosomalStep {
    if step == 1 { return rstep(1, "initiation", "Small ribosomal subunit (40S) binds mRNA 5' cap with eIF4E. Scans for AUG start codon (Kozak sequence). Met-tRNAi loaded in P site.", "eIF2", "P05198", "1 GTP (eIF2 delivery of Met-tRNAi)", "Kozak M. Nucleic Acids Res 15:8125-8148, 1987") }
    if step == 2 { return rstep(2, "initiation", "Large subunit (60S) joins. eIF5B-GTP hydrolysis triggers factor release. 80S ribosome assembled with Met-tRNAi in P site, A site empty.", "eIF5B", "O60841", "1 GTP (subunit joining)", "Pestova TV et al. Genes Dev 14:2042-2047, 2000") }
    if step == 3 { return rstep(3, "elongation", "Aminoacyl-tRNA delivered to A site by eEF1A-GTP. Codon-anticodon match tested. GTP hydrolyzed on correct match (kinetic proofreading).", "eEF1A", "P68104", "1 GTP per amino acid delivered", "Schmeing TM, Ramakrishnan V. Nature 461:1234-1242, 2009") }
    if step == 4 { return rstep(4, "elongation", "Peptide bond formed. Peptidyl transferase center (28S rRNA) catalyzes transfer of peptide from P-site tRNA to A-site amino acid. Ribozyme catalysis.", "28S rRNA", "ribosomal RNA (not a protein)", "0 (no additional energy)", "Nissen P et al. Science 289:920-930, 2000") }
    if step == 5 { return rstep(5, "elongation", "Translocation: eEF2-GTP moves ribosome one codon downstream. A-site tRNA moves to P site. P-site tRNA moves to E site. E-site tRNA exits.", "eEF2", "P13639", "1 GTP per translocation", "Jorgensen R et al. J Mol Biol 326:1211-1224, 2003") }
    if step == 6 { return rstep(6, "termination", "Stop codon (UAA/UAG/UGA) enters A site. Release factor eRF1 recognizes stop. eRF3-GTP hydrolyzes. Peptide released from P-site tRNA.", "eRF1", "P62495", "1 GTP (eRF3)", "Frolova L et al. RNA 5:1014-1020, 1999") }
    if step == 7 { return rstep(7, "recycling", "ABCE1 splits 80S into 40S+60S. mRNA released. tRNA released. Subunits recycled for next round.", "ABCE1", "P61221", "2 ATP (ABCE1 ATPase)", "Pisarev AV et al. Cell 131:286-299, 2007") }
    return rstep(0, "", "", "", "", "", "")
}

fn translation_energy_cost() -> String {
    // Per amino acid added: 2 GTP (eEF1A delivery + eEF2 translocation) + 2 ATP (aminoacyl-tRNA charging)
    // Total: ~4 high-energy phosphate bonds per amino acid
    return "4 high-energy phosphate bonds per amino acid: 2 ATP (tRNA charging by aaRS) + 1 GTP (eEF1A, codon recognition) + 1 GTP (eEF2, translocation)"
}

fn translation_step_count() -> Int {
    return 7
}

fn synthetase_count() -> Int {
    return 20
}
