"""Overlap and direction tests fixed in RULES_overlap.txt. Data only. Fact-Checked Science ("Our analysis"), https://factchecked.science/planthopper-magnetic-genes/ Data: Xie, W., et al. (2026), Dryad, doi:10.5061/dryad.r4xgxd2vx (CC0); make the input folder with prepare_inputs.py, then pass /dataset (or /field for igrf_route.py) as the script's usage says. Usage: python3 overlap.py """ import itertools import json import os import sys import numpy as np from scipy import stats sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from xlsx_reader import read_xlsx # noqa: E402 FIG1 = "Raw_data_for_Figure1-Complete list of differentially expressed genes in all comparisons.xlsx" def load(wb, sh): d = {} for r in wb[sh]["rows"][1:]: if not r: continue padj = None if r[6] in (None, "NA") else float(r[6]) d[r[0]] = (float(r[2]), padj) return d def deg(v): return v[1] is not None and v[1] < 0.05 and abs(v[0]) > 0.25 def regrouping_null(folder): """EXPLORATORY, added after the fresh review of 20 November 2026 (not pre-registered). Both of the authors' comparisons against 45 uT share the same three 45 uT samples, so chance highs and lows in those samples push both fold changes the same way. To see what chance alone gives, split the six GMF samples into every pair of groups of three (10 splits, the real one included; 'pseudo-50' is the group with GMF50_1), fit our negative-binomial method A (one model of all eight retained samples, shared dispersion trend; rederive.py) and compare the DEG lists of pseudo-50 vs pseudo-45 with NZMF vs pseudo-45 (share with the same sign) and with NZMF vs pseudo-50 (share with opposite signs), and their overlap against the overlap expected if the lists were independent. DEG: BH < 0.05 and |log2FC| > 0.25.""" import rederive as R cm, _ = R.load_counts(folder) gmf = ["GMF50_1", "GMF50_2", "GMF50_3", "GMF45_1", "GMF45_2", "GMF45_3"] smp = gmf + ["NZMF_1", "NZMF_2"] K = cm[smp].values.astype(float) K = K[K.sum(axis=1) >= 6] s = R.size_factors(K) base = (K / s).mean(axis=1) n_genes = K.shape[0] rows = [] for combo in itertools.combinations(gmf, 3): if gmf[0] not in combo: continue lab = ["P50" if x in combo else ("P45" if x in gmf else "NZ") for x in smp] fits = {} for key, num, ref in (("a", "P50", "P45"), ("b", "NZ", "P45"), ("c", "NZ", "P50")): lfc, p, _ = R.method_a(K, s, lab, num, ref, base, "trend") sel, _, _ = R.call(lfc, p) fits[key] = (lfc, sel) def pair(k1, k2, opposite): (l1, d1), (l2, d2) = fits[k1], fits[k2] both = d1 & d2 k = int(both.sum()) exp = float(d1.sum()) * float(d2.sum()) / n_genes same = np.sign(l1[both]) == np.sign(l2[both]) share = float(np.mean(~same if opposite else same)) if k else None return {"degs_first": int(d1.sum()), "degs_second": int(d2.sum()), "shared": k, "expected_if_independent": round(exp, 1), "overlap_ratio": round(k / exp, 2) if exp else None, ("share_opposite_sign" if opposite else "share_same_sign"): round(share, 3) if share is not None else None} rows.append({"pseudo50": list(combo), "real": sorted(combo) == sorted(gmf[:3]), "P50vsP45_and_NZvsP45": pair("a", "b", False), "P50vsP45_and_NZvsP50": pair("a", "c", True)}) others = [r for r in rows if not r["real"]] def rng(key, field): v = [r[key][field] for r in others if r[key][field] is not None] return [min(v), max(v)] if v else None return {"NOTE": "exploratory, added after the fresh review of 20 November 2026; not pre-registered", "genes": n_genes, "splits": rows, "summary_other_splits": { "same_sign_share_range": rng("P50vsP45_and_NZvsP45", "share_same_sign"), "overlap_ratio_range_vs_NZvsP45": rng("P50vsP45_and_NZvsP45", "overlap_ratio"), "opposite_sign_share_range": rng("P50vsP45_and_NZvsP50", "share_opposite_sign"), "overlap_ratio_range_vs_NZvsP50": rng("P50vsP45_and_NZvsP50", "overlap_ratio")}} def main(): folder, out_path = sys.argv[1], sys.argv[2] wb = read_xlsx(os.path.join(folder, FIG1)) a = load(wb, "DEG_GMF50_vs_GMF45") b = load(wb, "DEG_NZMF_vs_GMF45") c = load(wb, "DEG_NZMF_vs_GMF50") out = {} for name, other in (("NZMF_vs_GMF45", b), ("NZMF_vs_GMF50", c)): pop = [g for g in a if a[g][1] is not None and other[g][1] is not None] A = [g for g in pop if deg(a[g])] B = [g for g in pop if deg(other[g])] both = sorted(set(A) & set(B)) N, K, n = len(pop), len(B), len(A) k = len(both) p_hyper = float(stats.hypergeom.sf(k - 1, N, K, n)) opp = sum(1 for g in both if np.sign(a[g][0]) != np.sign(other[g][0])) binom = stats.binomtest(opp, k, 0.5) if k else None lfa = np.array([a[g][0] for g in pop]) lfo = np.array([other[g][0] for g in pop]) rho = stats.spearmanr(lfa, lfo) rec = {"population_genes_with_padj_in_both": N, "DEG_5uT_in_pop": n, "DEG_other_in_pop": K, "O1_overlap": k, "O1_expected": n * K / N, "O1_hypergeom_p_above_chance": p_hyper, "O2_or_O4_opposite_sign": opp, "share_opposite": (opp / k) if k else None, "binom_p_two_sided": float(binom.pvalue) if binom else None, "O3_spearman_all_genes": float(rho.statistic), "O3_spearman_p": float(rho.pvalue)} if name == "NZMF_vs_GMF45": s = rec["share_opposite"] rec["O2_label"] = ("evidence of a graded (shared, monotonic) component" if (s is not None and s > 0.5 and rec["binom_p_two_sided"] < 0.05) else ("same-sign excess" if (s is not None and s < 0.5 and rec["binom_p_two_sided"] < 0.05) else "no clear direction")) out[name] = rec out["exploratory_regrouping_null"] = regrouping_null(folder) with open(out_path, "w", encoding="utf-8", newline="\n") as f: json.dump(out, f, indent=1) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()