← Back to Causal-Atlas Overview
This document details the clean-room mathematical, statistical, and geometric algorithms implemented in ca-core and ca-app/src/svg_export.rs.
ca_core::stats::fishers_exact_right_tail)Given gene universe size $N$, target set size $M$, active dataset DEG count $n$, and intersection $k = |T \cap D|$:
$$p = \sum_{j = k}^{\min(M, n)} \frac{\binom{M}{j}\binom{N - M}{n - j}}{\binom{N}{n}}$$
Log-factorials $\ln(m!)$ are tabulated exactly for $m \le 100{,}000$ and evaluated via Stirling's series above $100{,}000$, using log-sum-exp rescaling (max_log) to prevent floating-point underflow or overflow.
benjamini_hochberg_with_total)In genome-wide pathway (canonical.rs), regulator (ura.rs), and phenotype (dea.rs) sweeps, candidate sets with insufficient overlap (e.g., overlap == 0 in Canonical Pathways or overlap_count < 2 in URA/DEA) represent tested null hypotheses whose enrichment cannot pass significance (p = 1.0 for zero overlap).
Rather than applying Benjamini-Hochberg solely to the filtered subset of $K$ reported hits, Causal Atlas adjusts across the full count of tested hypotheses $m \ge K$ (all non-empty pathways with pw_size > 0 in Canonical Pathways; all molecular regulators with regulon_size >= 2 in URA; all phenotype terms with gene_map.len() >= 2 in DEA):
$$q_{(r)} = \min_{j \ge r} \min!\left(1,\; \frac{m}{j}\, p_{(j)}\right), \qquad r = 1, \dots, K$$
Because the $m - K$ omitted null hypotheses satisfy $p = 1.0$, their BH adjusted values equal $1.0$ and act solely through the global multiple-testing burden $m$.
kramer_activation_z)For a regulator or pathway with signed interactions $(w_i, s^{\text{edge}}_i, s^{\text{data}}_i)$ where $w_i > 0$ is finite, $s^{\text{edge}}_i \in {-1, +1}$, and $s^{\text{data}}_i = \operatorname{sgn}(\log_2 \mathrm{FC}_i) \in {-1, +1}$:
$$x_i = s^{\text{edge}}i \, s^{\text{data}}i \in {-1, +1}, \qquad Z = \frac{\sum{i \in S} w_i x_i}{\sqrt{\sum{i \in S} w_i^2}}$$
EdgeSign::Ambiguous (s_edge == 0), expected_dir == 0, or zero fold change (s_data == 0) are strictly excluded from $S$, so unsigned binding links neither dilute the denominator $\sqrt{\sum w_i^2}$ nor distort $\mu_1$ (mean_pw_sign / regulon mean sign).skew_flagged = true), the bias-corrected Z-score is:PredictedState::from_z rounds z to two decimal places (round_z_2dp), classifying Activated ($\ge +2.00$), Inhibited ($\le -2.00$), or Neutral (Unresolved), matching displayed {:+.2} labels identically.z.signum() (ura.rs)For every measured target in an upstream regulator's target_evidences, concordance (consistent) is evaluated relative to the regulator's inferred activation direction $\operatorname{sgn}(Z)$ rather than assuming every regulator is activated:
$$\text{consistent}_i \iff (Z \ne 0) \;\wedge\; (s^{\text{edge}}_i \ne 0) \;\wedge\; (s^{\text{data}}_i \ne 0) \;\wedge\; \left(s^{\text{edge}}_i \, s^{\text{data}}_i \, \operatorname{sgn}(Z) > 0\right)$$
Consequently, when an upstream regulator is predicted Inhibited ($Z \le -2.0$), downregulated targets ($s^{\text{data}}_i = -1$) linked via activating edges ($s^{\text{edge}}_i = +1$)—as well as upregulated targets linked via repressing edges ($s^{\text{edge}}_i = -1$)—correctly score consistent = true. Downstream Regulator Effects (dea.rs) directly consumes t.consistent for all activated and inhibited regulators.
stats.rs)ca_core::stats::erfc_approx(x) provides two-regime evaluation for z_to_two_sided_p(Z) = erfc(|Z| / sqrt(2)):
erfc(|x|) is evaluated via the incomplete-gamma continued fraction $\operatorname{erfc}(z) = \Gamma(\tfrac{1}{2}, z^2) / \sqrt{\pi}$:TINY = 1e-300, EPS = 1e-15, up to 128 steps). For $z > 5$ ($z^2 > 25$), convergence completes in a small number of iterations to near IEEE-754 f64 machine precision down through erfc(20) ≈ 5.40e-176.perturb.rs)run_perturbation_match guards against NaN / ±Inf fold-changes by filtering to finite (symbol, log2_fc) measurements, deduplicating by strongest magnitude, and sorting descending by log2_fc into ranks $r \in {1, \dots, N}$.
For a signature gene set $T$ of size $t = |T|$ with sorted unique 1-based ranks $V(1) < V(2) < \dots < V(t)$, the unweighted two-sample Kolmogorov-Smirnov / Subramanian running-sum enrichment score $ES(T) \in [-1, +1]$ is evaluated in $O(t \log t)$ time:
$$ES^+ = \max_{j=1..t}!\left(\frac{j}{t} - \frac{V(j) - j}{N - t}\right), \qquad ES^- = \min_{j=1..t}!\left(\frac{j - 1}{t} - \frac{V(j) - j}{N - t}\right)$$
choosing $ES^+$ if $ES^+ > |ES^-|$, $ES^-$ if $|ES^-| > ES^+$, and $0$ on an exact tie (or flat query profile).
Given $ES_{\text{up}}$ and $ES_{\text{down}}$:
Rank-based scoring guarantees strict invariance to extreme fold-change outliers and NaN rows.
cna.rs)Multi-Hop Causal Network Analysis finds shortest directed paths of depth $K \in {1, 2, 3}$ through signaling edges ($E_a$) terminating in a transcriptional edge ($E_t$), collapses consistent virtual edges $(r, v)$, prunes nested depth hypotheses (overlap_cnt > prev_overlap_cnt and p_val < prev_best_p * 0.95), and computes degree-corrected network-bias p-values stratified by $\lfloor \log_2(\text{in_degree}_t(g)) \rfloor$.
Within each in-degree bin pool of size $P$ requiring $d < P$ draws without replacement, Causal Atlas performs partial Fisher-Yates sampling in-place over a reusable per-bin vector using a deterministic SplitMix64 PRNG:
for i in 0..d:
j = i + rng.gen_index(P - i)
work_pool.swap(i, j)
Bins with $d \ge P$ are pre-accumulated outside the permutation loop. This eliminates BTreeSet heap allocations and coupon-collector slowdown while guaranteeing exact uniform $\binom{P}{d}^{-1}$ sampling across all cna_permutations iterations.
canonical.rs::simulate_map): Propagates observed expression anchors (fc / 3.0).tanh() and pinned user/drug clamps [-1.0, +1.0] along signed pathway edges over map_iterations steps with map_damping.ca-app/src/svg_export.rs): Each node renders as a 74x30 rounded <rect> centered at (cx, cy) ([cx ± 37, cy ± 15]). Prior to emitting <path d="M sx sy Q mx my ex ey">, both endpoints are clipped via ray-box intersection along the Quadratic Bezier endpoint tangents (mx - cx, my - cy) against [cx ± (37 + 1.2), cy ± (15 + 1.2)] so #arrow-act and #arrow-inh markers render cleanly outside node borders, accompanied by a <linearGradient> MAP activity colorbar legend.