"""EXPLORATORY (not pre-registered; labelled as such in the report). 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. 1. PCA and correlations on the eight retained libraries (the pre-registered nine-library PCA is dominated by NZMF_3). 2. Method B with a mean-dependent prior (limma-trend-like): explains why the global-prior method B finds few DEGs. Usage: python3 exploratory.py """ import json, math, os, sys import numpy as np from scipy import special, stats sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import rederive as R def pca8(cm): s8 = R.GROUPS["GMF45"] + R.GROUPS["GMF50"] + R.GROUPS["NZMF"] K = cm[s8].values.astype(float); K = K[K.sum(axis=1) >= 6] s = R.size_factors(K); y = np.log2(K / s + 1) top = np.argsort(y.var(axis=1))[::-1][:500] Y = y[top].T; Y = Y - Y.mean(axis=0) U, S, Vt = np.linalg.svd(Y, full_matrices=False) pcs = U[:, :3] * S[:3]; ex = (S ** 2 / (S ** 2).sum())[:3] corr = np.corrcoef(y.T) return {"pcs": {k: [round(float(v), 2) for v in pcs[i]] for i, k in enumerate(s8)}, "var_explained": [round(float(x), 3) for x in ex], "mean_corr_within_GMF45": float(np.mean([corr[i, j] for i in range(3) for j in range(3) if i < j])), "mean_corr_within_GMF50": float(np.mean([corr[i, j] for i in range(3, 6) for j in range(3, 6) if i < j])), "mean_corr_GMF45_vs_GMF50": float(np.mean([corr[i, j] for i in range(3) for j in range(3, 6)])), "corr_matrix": {a: {b: round(float(corr[i, j]), 4) for j, b in enumerate(s8)} for i, a in enumerate(s8)}} def trigamma_inverse(x): return R.trigamma_inverse(x) def method_b_trend(K, s, labels, num, ref, nbins=20): y = np.log2(K + 0.5) - np.log2(s)[None, :] i1 = [j for j, l in enumerate(labels) if l == num]; i2 = [j for j, l in enumerate(labels) if l == ref] a, b = y[:, i1], y[:, i2]; n1, n2 = len(i1), len(i2); d = n1 + n2 - 2 diff = a.mean(1) - b.mean(1) s2 = (((a - a.mean(1, keepdims=True)) ** 2).sum(1) + ((b - b.mean(1, keepdims=True)) ** 2).sum(1)) / d A = y.mean(1); pos = s2 > 0 e = np.full(len(s2), np.nan); e[pos] = np.log(s2[pos]) - special.digamma(d / 2) + math.log(d / 2) order = np.argsort(A[pos]); bins = np.array_split(order, nbins) xs = np.array([np.median(A[pos][bb]) for bb in bins]); ys = np.array([np.mean(e[pos][bb]) for bb in bins]) tr = np.interp(A, xs, ys) res = e[pos] - tr[pos] evar = np.mean(res ** 2) * len(res) / (len(res) - 1) - special.polygamma(1, d / 2) d0 = 2 * trigamma_inverse(evar) if evar > 0 else float("inf") s0 = np.exp(tr + special.digamma(d0 / 2) - math.log(d0 / 2)) if np.isfinite(d0) else np.exp(tr) st2 = (d0 * s0 + d * s2) / (d0 + d) if np.isfinite(d0) else s0 t = diff / np.sqrt(st2 * (1 / n1 + 1 / n2)) p = 2 * stats.t.sf(np.abs(t), d + d0) if np.isfinite(d0) else 2 * stats.norm.sf(np.abs(t)) return diff, p, float(d0) def main(): folder, out_path = sys.argv[1], sys.argv[2] cm, _ = R.load_counts(folder) out = {"NOTE": "exploratory, not pre-registered", "pca_8": pca8(cm), "methodB_trend": {}} s8 = R.GROUPS["GMF45"] + R.GROUPS["GMF50"] + R.GROUPS["NZMF"] lab8 = ["GMF45"] * 3 + ["GMF50"] * 3 + ["NZMF"] * 2 K = cm[s8].values.astype(float); keep = K.sum(1) >= 6; K = K[keep]; s = R.size_factors(K) for num, ref in R.COMPARISONS: diff, p, d0 = method_b_trend(K, s, lab8, num, ref) sel = (R.bh(p) < 0.05) & (np.abs(diff) > 0.25) out["methodB_trend"][num + "_vs_" + ref] = {"DEGs": int(sel.sum()), "d0": d0} # permutation for the 5 uT comparison with the trend prior import itertools smp = R.GROUPS["GMF50"] + R.GROUPS["GMF45"] Kc = cm[smp].values.astype(float); kc = Kc.sum(1) >= 6; Kc = Kc[kc]; sc = R.size_factors(Kc) perm = [] for combo in itertools.combinations(smp, 3): if smp[0] not in combo: continue labels = ["X" if x in combo else "Y" for x in smp] diff, p, d0 = method_b_trend(Kc, sc, labels, "X", "Y") perm.append({"group": list(combo), "real": sorted(combo) == sorted(R.GROUPS["GMF50"]), "DEGs": int(((R.bh(p) < 0.05) & (np.abs(diff) > 0.25)).sum())}) out["methodB_trend_permutation_GMF50_vs_GMF45"] = perm json.dump(out, open(out_path, "w", encoding="utf-8", newline="\n"), indent=1) print(json.dumps({k: v for k, v in out["pca_8"].items() if k != "corr_matrix"}, indent=1)) print(json.dumps(out["methodB_trend"], indent=1)) for r in perm: print(r) if __name__ == "__main__": main()