// supercoiling.sy -- DNA topology and transcriptional access as native Scyla.
//
// DNA supercoiling determines whether a gene is physically accessible for
// transcription. Negative supercoiling unwinds the double helix, exposing
// bases for RNA polymerase binding. Positive supercoiling overwounds and
// locks the gene shut. Topoisomerases regulate the coiling state.
//
// This module sits between epigenetics.sy (histone marks) and codons.sy
// (transcription/translation). The full stack is:
//
//   hormone state
//     → epigenetic marks (histone methylation/acetylation)
//     → chromatin compaction (nucleosome positioning)
//     → supercoiling state (this module)
//     → gene accessible or locked
//     → transcription (codons.sy transcribe_dna)
//     → translation (codons.sy translate)
//     → protein (human_proteome.sy)
//
// Sources:
//   Wang JC. Nat Rev Mol Cell Biol 3:430-440, 2002 (topoisomerase review)
//   Kouzine F et al. Nat Struct Mol Biol 20:396-403, 2013 (transcription-driven supercoiling)
//   Naughton C et al. Nat Struct Mol Biol 20:387-395, 2013 (supercoiling domains genome-wide)
//   Pommier Y et al. Nat Rev Cancer 6:789-802, 2006 (topoisomerases as drug targets)
//   Ma J, Wang MD. Nat Rev Mol Cell Biol 17:783-790, 2016 (supercoiling and gene regulation)
//
// Marjorie McCubbins + Aether, April 2026
// Patent pending: 64/034,536

// ═══════════════════════════════════════════════════════
// STRUCTS
// ═══════════════════════════════════════════════════════

struct SupercoilState {
    coil_state: String,
    linking_number_change: String,
    effect_on_transcription: String,
    description: String,
}

struct Topoisomerase {
    name: String,
    accession: String,
    gene: String,
    topo_type: String,
    mechanism: String,
    drug_target: String,
    clinical_use: String,
    citation: String,
}

struct SupercoilRegulation {
    trigger: String,
    enzyme_accession: String,
    enzyme_gene: String,
    coiling_change: String,
    transcription_effect: String,
    target_genes: String,
    citation: String,
}

fn sc_state(s: String, ln: String, e: String, d: String) -> SupercoilState {
    return SupercoilState { coil_state: s, linking_number_change: ln, effect_on_transcription: e, description: d }
}

fn topo(n: String, a: String, g: String, t: String, m: String, d: String, c: String, ci: String) -> Topoisomerase {
    return Topoisomerase { name: n, accession: a, gene: g, topo_type: t, mechanism: m, drug_target: d, clinical_use: c, citation: ci }
}

fn sc_reg(tr: String, ea: String, eg: String, cc: String, te: String, tg: String, ci: String) -> SupercoilRegulation {
    return SupercoilRegulation { trigger: tr, enzyme_accession: ea, enzyme_gene: eg, coiling_change: cc, transcription_effect: te, target_genes: tg, citation: ci }
}

// ═══════════════════════════════════════════════════════
// SUPERCOILING STATES
// ═══════════════════════════════════════════════════════

fn lookup_supercoil_state(name: String) -> SupercoilState {
    if name == "negative" { return sc_state("negative", "Lk < Lk0", "promotes transcription", "DNA is underwound. The reduced twist makes strand separation easier, facilitating RNA polymerase binding and transcription initiation. Most transcriptionally active regions are negatively supercoiled.") }
    if name == "positive" { return sc_state("positive", "Lk > Lk0", "inhibits transcription", "DNA is overwound. The increased twist resists strand separation, blocking RNA polymerase access. Positive supercoiling accumulates ahead of the replication fork and transcription bubble.") }
    if name == "relaxed" { return sc_state("relaxed", "Lk = Lk0", "neutral", "DNA is in its equilibrium topology. Neither promoting nor inhibiting transcription. B-form DNA with ~10.5 bp per helical turn.") }
    return sc_state("unknown", "", "", "")
}

// ═══════════════════════════════════════════════════════
// TOPOISOMERASES — the enzymes that control coiling
// ═══════════════════════════════════════════════════════

fn lookup_topoisomerase(name: String) -> Topoisomerase {
    // Type I — single-strand break, relax negative supercoils
    if name == "TOP1" { return topo("Topoisomerase I", "P11387", "TOP1", "Type IB", "Cuts one DNA strand, allows rotation to relax negative supercoils, then reseals. Does not require ATP.", "Camptothecin, irinotecan, topotecan", "Colorectal cancer (irinotecan), ovarian/lung cancer (topotecan)", "Pommier Y et al. Nat Rev Cancer 6:789-802, 2006") }
    if name == "TOP3A" { return topo("Topoisomerase IIIa", "Q13472", "TOP3A", "Type IA", "Resolves recombination intermediates and converging replication forks. Works with BLM helicase.", "none clinically", "Bloom syndrome when BLM is mutated", "Wang JC. Nat Rev Mol Cell Biol 3:430-440, 2002") }
    if name == "TOP3B" { return topo("Topoisomerase IIIb", "O95985", "TOP3B", "Type IA", "Acts on both DNA and RNA. Resolves R-loops. Linked to schizophrenia and autism when deleted.", "none clinically", "Neurodevelopmental disorders", "Ahmad M et al. Nat Commun 8:233, 2017") }

    // Type II — double-strand break, introduce or remove supercoils, require ATP
    if name == "TOP2A" { return topo("Topoisomerase IIa", "P11388", "TOP2A", "Type IIA", "Cuts both DNA strands, passes another duplex through the break, then reseals. Requires ATP. Essential for chromosome condensation and decatenation in mitosis.", "Doxorubicin, etoposide, mitoxantrone", "Leukemia (etoposide), breast cancer (doxorubicin), lymphoma", "Nitiss JL. Nat Rev Cancer 9:338-350, 2009") }
    if name == "TOP2B" { return topo("Topoisomerase IIb", "Q02880", "TOP2B", "Type IIA", "Resolves supercoiling at transcription sites. Required for gene activation by nuclear hormone receptors including glucocorticoid receptor. Creates transient DSBs at promoters.", "Same drugs as TOP2A (cardiotoxicity source)", "Doxorubicin cardiotoxicity is TOP2B-mediated", "Lyu YL et al. Science 312:1653-1657, 2006") }

    return topo("Unknown", "", "", "", "", "", "", "")
}

// ═══════════════════════════════════════════════════════
// STRESS-INDUCED SUPERCOILING CHANGES
// ═══════════════════════════════════════════════════════
//
// Cortisol activates GR which requires TOP2B to create a transient
// double-strand break at target gene promoters for transcription
// activation. This is the supercoiling step between "GR binds DNA"
// and "gene gets transcribed."

fn stress_supercoiling(trigger: String) -> SupercoilRegulation {
    if trigger == "cortisol_activation" { return sc_reg("cortisol-GR complex binds GRE", "Q02880", "TOP2B", "transient DSB creates local negative supercoiling", "promotes transcription of GR target genes", "FKBP5|SGK1|GILZ|PER1", "Haffner MC et al. Nat Genet 42:668-675, 2010") }
    if trigger == "transcription_elongation" { return sc_reg("RNA polymerase II elongation", "P11387", "TOP1", "positive ahead, negative behind (twin-domain model)", "must be resolved or transcription stalls", "all actively transcribed genes", "Liu LF, Wang JC. Proc Natl Acad Sci 84:7024-7027, 1987") }
    if trigger == "replication_stress" { return sc_reg("stalled replication fork", "P11388", "TOP2A", "positive supercoiling ahead of fork", "fork collapse if unresolved, triggers DNA damage response", "replication origins genome-wide", "Kouzine F et al. Nat Struct Mol Biol 20:396-403, 2013") }
    return sc_reg("", "", "", "", "", "", "")
}

// ═══════════════════════════════════════════════════════
// DRUG TARGETS — topoisomerase poisons
// ═══════════════════════════════════════════════════════
//
// Many chemotherapy drugs work by poisoning topoisomerases:
// they trap the enzyme on DNA after it cuts, creating permanent
// breaks that kill the cell. This is why supercoiling matters
// for cancer treatment.

fn topo_drug(drug_name: String) -> String {
    if drug_name == "camptothecin" { return "TOP1 poison. Traps TOP1-DNA cleavage complex. Prevents religation. Replication fork collision creates lethal DSBs." }
    if drug_name == "irinotecan" { return "TOP1 poison (prodrug, converted to SN-38). First-line colorectal cancer. Cited: Pommier 2006." }
    if drug_name == "topotecan" { return "TOP1 poison. Ovarian cancer, small cell lung cancer. Cited: Pommier 2006." }
    if drug_name == "etoposide" { return "TOP2 poison. Traps TOP2A cleavage complex. First-line for testicular cancer, lung cancer, leukemia. Cited: Nitiss 2009." }
    if drug_name == "doxorubicin" { return "TOP2 poison + DNA intercalator. Breast cancer, lymphoma. Cardiotoxicity via TOP2B in cardiomyocytes. Cited: Lyu 2006." }
    if drug_name == "mitoxantrone" { return "TOP2 poison. Acute leukemia, prostate cancer, multiple sclerosis. Cited: Nitiss 2009." }
    return "Unknown drug"
}

// ═══════════════════════════════════════════════════════
// LINKING NUMBER MATH
// ═══════════════════════════════════════════════════════
//
// Lk = Tw + Wr (linking number = twist + writhe)
// For relaxed B-DNA: Tw0 = N/10.5 (N = number of base pairs)
// Superhelical density: sigma = (Lk - Lk0) / Lk0
// Typical in vivo: sigma = -0.06 (6% underwound)

fn relaxed_twist(base_pairs: Int) -> Float {
    return base_pairs / 10.5
}

fn superhelical_density(lk: Float, lk0: Float) -> Float {
    return (lk - lk0) / lk0
}

fn typical_in_vivo_sigma() -> Float {
    // E. coli and human cells maintain ~6% negative supercoiling
    // Cited: Bliska JB, Cozzarelli NR. J Mol Biol 194:205-218, 1987
    return -0.06
}

// ═══════════════════════════════════════════════════════
// QUICK ACCESS
// ═══════════════════════════════════════════════════════

fn topoisomerase_count() -> Int {
    return 5
}

fn is_gene_accessible(sc: String) -> Bool {
    if sc == "negative" { return true }
    if sc == "relaxed" { return true }
    return false
}
