# 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. Python 3.13, NumPy, pandas, SciPy.
import numpy as np, pandas as pd
from scipy import stats
d = pd.read_csv("luciferase.csv")                    # one row per well: transfection, condition, well, firefly, renilla (background-subtracted)
d["ratio"] = d.firefly / d.renilla
m = d.groupby(["transfection", "condition"])[["firefly", "renilla", "ratio"]].mean().unstack()   # mean of the 3 wells
for t, r in m.iterrows():
    print(f"Transfection {t}: firefly / Renilla control {r[('ratio', 'control')]:.2f}, treated {r[('ratio', 'treated')]:.2f} "
          f"(fold {r[('ratio', 'treated')] / r[('ratio', 'control')]:.2f}); Renilla treated / control {r[('renilla', 'treated')] / r[('renilla', 'control')]:.2f}")
def paired(x, label):
    lr = np.log2(x["treated"] / x["control"]); t = stats.ttest_1samp(lr, 0); ci = t.confidence_interval()
    print(f"{label} (paired, n = {len(lr)}): treated / control {2 ** lr.mean():.2f} (95% CI {2 ** ci.low:.2f} to {2 ** ci.high:.2f}), p = {t.pvalue:.2g}")
paired(m["firefly"], "A. Firefly alone, log2 ratio per transfection")
a, b = d.ratio[d.condition == "treated"], d.ratio[d.condition == "control"]; t = stats.ttest_ind(a, b, equal_var=False)
print(f"B. Firefly / Renilla, wells as n ({len(a)} vs {len(b)}), Welch t-test: treated / control {a.mean() / b.mean():.2f}, p = {t.pvalue:.2g}")
paired(m["ratio"], "C. Firefly / Renilla, log2 ratio per transfection")
paired(m["renilla"], "D. Check: Renilla alone, log2 ratio per transfection")
