# Dual-luciferase assay, treated vs control: firefly alone, firefly / Renilla with wells as n, the paired log ratio per
# transfection, and a check of the Renilla control. R 4.3.3, base R.
d <- read.csv("luciferase.csv")                      # one row per well: transfection, condition, well, firefly, renilla (background-subtracted)
f2 <- function(p) formatC(signif(p, 2), format = "g")
d$ratio <- d$firefly / d$renilla
mm <- function(v) tapply(d[[v]], list(d$transfection, d$condition), mean)              # mean of the 3 wells
FF <- mm("firefly"); RL <- mm("renilla"); RT <- mm("ratio")
for (t in rownames(RT)) cat(sprintf("Transfection %s: firefly / Renilla control %.2f, treated %.2f (fold %.2f); Renilla treated / control %.2f\n",
                                    t, RT[t, "control"], RT[t, "treated"], RT[t, "treated"] / RT[t, "control"], RL[t, "treated"] / RL[t, "control"]))
paired <- function(x, label) {
  lr <- log2(x[, "treated"] / x[, "control"]); t <- t.test(lr)
  cat(sprintf("%s (paired, n = %d): treated / control %.2f (95%% CI %.2f to %.2f), p = %s\n", label, length(lr), 2^mean(lr), 2^t$conf.int[1], 2^t$conf.int[2], f2(t$p.value)))
}
paired(FF, "A. Firefly alone, log2 ratio per transfection")
a <- d$ratio[d$condition == "treated"]; b <- d$ratio[d$condition == "control"]; t <- t.test(a, b)
cat(sprintf("B. Firefly / Renilla, wells as n (%d vs %d), Welch t-test: treated / control %.2f, p = %s\n", length(a), length(b), mean(a) / mean(b), f2(t$p.value)))
paired(RT, "C. Firefly / Renilla, log2 ratio per transfection")
paired(RL, "D. Check: Renilla alone, log2 ratio per transfection")
