// ThermoCas9 — methylation-sensitive Cas9 from Geobacillus thermodenitrificans T12
// Based on: Roth, Shu, Zhao, Trasanidou, Hoffman et al.
// "Molecular basis for methylation-sensitive editing by Cas9"
// Nature (2026) — DOI: 10.1038/s41586-026-10384-z (open access, CC BY-NC-ND 4.0)
// Numerical efficiencies are illustrative ranges drawn from the paper's figures.

const CELL_LINES = {
  MCF7:    { name: "MCF-7",    long: "Luminal/ER+ breast adenocarcinoma",        type: "Cancer",    tissue: "Breast" },
  MCF10A:  { name: "MCF-10A",  long: "Non-tumorigenic mammary epithelium",       type: "Normal",    tissue: "Breast" },
  HCT116:  { name: "HCT116",   long: "Colorectal adenocarcinoma",                type: "Cancer",    tissue: "Colon"  },
  HEK293T: { name: "HEK293T",  long: "Human embryonic kidney (reference)",       type: "Reference", tissue: "Kidney" },
};

// Cell-line pair scenarios mirroring the paper's experimental design.
const SCENARIOS = {
  BREAST: {
    name: "Luminal Breast Cancer",
    short: "ER+ BRCA",
    cancer_line: "MCF7",
    normal_line: "MCF10A",
    blurb: "Luminal/ER+ breast cancers exhibit consistent hypomethylation at ESR1 and GATA3 — both luminal-signature transcription factors. ThermoCas9 selectively edits the hypomethylated cancer alleles while the methylated normal alleles remain inaccessible (Roth et al., 2026, Fig. 5).",
    targets: [
      {
        gene: "GATA3", site: "T18", coords: "Chr 10:8,045,463",
        pam: "NNNNCGA", beta_cancer: 0.02, beta_normal: 0.31,
        edit_cancer: 0.78, edit_normal: 0.0, role: "Oncogenic luminal TF",
        note: "Truncating GATA3 mutations cause dominant-negative loss of luminal differentiation in ~50% of luminal/ER+ tumors. CE-ThermoCas9 RNP achieved up to 78% editing in MCF-7 (Fig. 5e).",
      },
      {
        gene: "ESR1", site: "T17", coords: "Chr 6:151,690,043",
        pam: "NNNNCGA", beta_cancer: 0.07, beta_normal: 0.94,
        edit_cancer: 0.13, edit_normal: 0.0, role: "Oestrogen receptor 1",
        note: "Sharp methylation differential (β 0.07 vs 0.94) makes ESR1 a textbook example of methylation-locked targeting. Ligand-independent ESR1 mutations drive endocrine-therapy resistance.",
      },
      {
        gene: "EGFLAM", site: "T11", coords: "Chr 5:38,258,782",
        pam: "NNNNCGA", beta_cancer: 0.01, beta_normal: 0.49,
        edit_cancer: 0.10, edit_normal: 0.0, role: "Control locus",
        note: "Intermediate methylation in MCF-10A (β=0.49) confirms the dose-response: even partial methylation suppresses editing.",
      },
    ],
  },
  COLORECTAL: {
    name: "Colorectal Carcinoma · proof-of-concept",
    short: "CRC PoC",
    cancer_line: "HCT116",
    normal_line: "HEK293T",
    blurb: "EMX1 is differentially methylated between HCT116 (cancer-derived, hypermethylated PAM) and HEK293T (reference, unmethylated PAM). ThermoCas9 cleanly edits EMX1 in HEK293T but yields null editing in HCT116 — a binary on/off demonstration of methylation gating (Fig. 5a).",
    targets: [
      {
        gene: "EMX1", site: "T4", coords: "EMX1 locus, target 4",
        pam: "NNNNCGA", beta_cancer: 0.95, beta_normal: 0.00,
        edit_cancer: 0.00, edit_normal: 0.16, role: "CRISPR validation locus",
        note: "Reverses the cancer/normal pattern: in this pair the 'cancer' line carries the methylation. Demonstrates the on/off behavior cleanly.",
      },
      {
        gene: "VEGFA", site: "T9", coords: "VEGFA locus, target 9",
        pam: "NNNNCGA", beta_cancer: 0.00, beta_normal: 0.00,
        edit_cancer: 0.16, edit_normal: 0.33, role: "Positive control",
        note: "Unmethylated in both lines → both edit (33% in HEK293T, 16% in HCT116). Confirms the enzyme works when not gated by methylation.",
      },
      {
        gene: "VEGFA", site: "T3", coords: "VEGFA locus, target 3",
        pam: "NNNNCGA", beta_cancer: 0.95, beta_normal: 0.95,
        edit_cancer: 0.00, edit_normal: 0.00, role: "Negative control",
        note: "Methylated in both lines → null editing in both. Methylation, not chromatin or expression, is the gating step.",
      },
      {
        gene: "PRDX4", site: "T5", coords: "PRDX4 locus, target 5",
        pam: "NNNNCGA", beta_cancer: 0.95, beta_normal: 0.00,
        edit_cancer: 0.00, edit_normal: 0.18, role: "Differentially methylated",
        note: "Hypermethylated in HCT116, unmethylated in HEK293T. Editing tracks methylation, not cell-line identity.",
      },
    ],
  },
};

// ThermoCas9 enzyme variants
const VARIANTS = {
  WT: {
    name: "Wild-type ThermoCas9",
    abbr: "WT",
    desc: "Native Type II-C Cas9 from Geobacillus thermodenitrificans T12 (1082 aa). Recognizes 5'-NNNNCGA-3' or 5'-NNNNCCA-3' PAM. Cleavage blocked when the PAM cytosine is 5mC.",
    activity_scale: 0.55,
    methylation_sensitive: true,
  },
  CE: {
    name: "Catalytically Enhanced ThermoCas9",
    abbr: "CE",
    desc: "Engineered double mutant Glu655Gly + Asn696Ile from directed evolution of the HNH–RuvC linker (Supplementary Fig. 18). Higher cleavage rate, methylation sensitivity preserved.",
    activity_scale: 1.0,
    methylation_sensitive: true,
  },
  D1017A: {
    name: "ThermoCas9-D1017A",
    abbr: "D1017A",
    desc: "Asp1017→Ala. Asp1017 contacts C5* of the PAM through its major groove. Mutation virtually abolishes cleavage activity (Fig. 4c).",
    activity_scale: 0.05,
    methylation_sensitive: true,
  },
  S1019A: {
    name: "ThermoCas9-S1019A",
    abbr: "S1019A",
    desc: "Ser1019→Ala. Loses methylation discrimination but retains cleavage activity — separates PAM recognition from methylation sensing (Fig. 4c).",
    activity_scale: 0.50,
    methylation_sensitive: false,
  },
};

// Delivery formats validated in the paper
const DELIVERY = {
  RNP: {
    name: "RNP nucleofection",
    short: "RNP",
    desc: "Pre-assembled ThermoCas9-sgRNA ribonucleoprotein delivered by Lonza nucleofector. Highest efficiency observed: 78% at GATA3 in MCF-7.",
    efficiency: 1.0,
  },
  mRNA: {
    name: "ThermoCas9-mRNA + sgRNA",
    short: "mRNA",
    desc: "Codon-optimized ThermoCas9-NLS mRNA + chemically modified sgRNA via Lipofectamine. LNP-compatible for in vivo work.",
    efficiency: 0.7,
  },
  PLASMID: {
    name: "Plasmid (DNA)",
    short: "Plasmid",
    desc: "Co-transfection of ThermoCas9 + sgRNA + EGFP plasmids; FACS-sortable. Reference / lowest efficiency.",
    efficiency: 0.45,
  },
};

// Comparison nucleases
const NUCLEASES = {
  thermocas9:    { name: "ThermoCas9 (WT)",                         short: "TC9",     color: "var(--accent)",  organism: "G. thermodenitrificans T12" },
  ce_thermocas9: { name: "Catalytically Enhanced ThermoCas9",       short: "CE-TC9",  color: "var(--good)",    organism: "Engineered (Roth 2026)"     },
  spcas9:        { name: "SpCas9 (S. pyogenes)",                    short: "SpCas9",  color: "var(--fg-dim)",  organism: "S. pyogenes",  methylation_insensitive: true },
  acecas9:       { name: "AceCas9 (Acidothermus cellulolyticus)",   short: "AceCas9", color: "var(--purple)",  organism: "A. cellulolyticus", inhibition_step: "activation" },
};

// Activity vs PAM-cytosine methylation (β value 0–1).
// Calibrated to Roth et al. Fig. 1c (target-strand 5mCpG curve) and Fig. 5a/e indel rates.
const activityVsMethylation = (nuclease, beta) => {
  const b = Math.max(0, Math.min(1, beta));
  switch (nuclease) {
    case "spcas9":
      return 0.85;                                   // methylation-insensitive
    case "thermocas9":
      return 0.85 * Math.pow(1 - b, 3.5) + 0.02;     // strong inhibition at binding step
    case "ce_thermocas9":
      return 0.95 * Math.pow(1 - b, 3.0) + 0.03;     // higher peak, similar profile
    case "acecas9":
      return 0.80 * Math.pow(1 - b, 2.2) + 0.05;     // sensitive but inhibited at activation step
    default:
      return 0;
  }
};

const PAM_OPTIONS = ["NNNNCGA", "NNNNCCA"];

const TC9_FACTS = {
  organism: "Geobacillus thermodenitrificans T12",
  type: "Type II-C Cas9",
  size_aa: 1082,
  optimal_temp_C: [37, 55],
  pam_options: PAM_OPTIONS,
  pam_critical_residue: "Asp1017",
  methylation_sensing_residue: "Ser1019",
  activation_residues: ["Lys711", "Asp723"],
  hnh_rotation_deg: 180,
  pdb_pre_cleavage: "9AR4",
  pdb_post_cleavage: "9AR7",
  resolution_pre_A: 2.8,
  resolution_post_A: 2.2,
  paper: {
    title: "Molecular basis for methylation-sensitive editing by Cas9",
    authors: "Roth MO, Shu Y, Zhao Y, Trasanidou D, Hoffman RD, et al.",
    journal: "Nature",
    year: 2026,
    doi: "10.1038/s41586-026-10384-z",
    url: "https://www.nature.com/articles/s41586-026-10384-z",
    doi_url: "https://doi.org/10.1038/s41586-026-10384-z",
  },
};

const DEFAULT_TARGET_INDEX = { BREAST: 0, COLORECTAL: 0 };

function hashStr(s) { let h = 2166136261; for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 16777619); } return h >>> 0; }
function mulberry32(a) { return () => { a = (a + 0x6D2B79F5) >>> 0; let t = a; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; }

Object.assign(window, {
  CELL_LINES, SCENARIOS, VARIANTS, DELIVERY, NUCLEASES,
  activityVsMethylation, PAM_OPTIONS, TC9_FACTS, DEFAULT_TARGET_INDEX,
  hashStr, mulberry32,
});
