File size: 20,076 Bytes
e918a5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""
Differential expression (PyDESeq2) and over-representation analysis (ORA).

Counts matrices use **samples × genes** layout for PyDESeq2 ≥ 0.5.
"""

from __future__ import annotations

import io
import math
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple

import numpy as np
import pandas as pd
from scipy.stats import false_discovery_control, fisher_exact

try:
    from pydeseq2.dds import DeseqDataSet
    from pydeseq2.ds import DeseqStats

    _PYDESQ2_AVAILABLE = True
except ImportError:  # pragma: no cover - optional heavy dep
    DeseqDataSet = None  # type: ignore[misc, assignment]
    DeseqStats = None  # type: ignore[misc, assignment]
    _PYDESQ2_AVAILABLE = False

try:
    import gseapy as gp

    _GSEAPY_AVAILABLE = True
except ImportError:  # pragma: no cover - optional extra dep
    gp = None  # type: ignore[assignment]
    _GSEAPY_AVAILABLE = False


def pydeseq2_available() -> bool:
    return _PYDESQ2_AVAILABLE


def gseapy_available() -> bool:
    return _GSEAPY_AVAILABLE


def default_analysis_options() -> Dict[str, Any]:
    """Defaults aligned with common RNA-seq practice (DESeq2 prefilter, directional ORA)."""
    return {
        "min_total_count": 10,
        "padj_alpha": 0.05,
        "ora_min_pathway_genes": 3,
        # Use "up" for treated-vs-control activation screens; "both" is the safe default.
        "de_query_direction": "both",
        "min_abs_log2fc": 0.0,
    }


def merge_analysis_options(case: Dict[str, Any]) -> Dict[str, Any]:
    out = default_analysis_options()
    raw = case.get("analysis_options")
    if not isinstance(raw, dict):
        return out
    for k, v in raw.items():
        if k not in out or v is None:
            continue
        if k in ("min_total_count", "ora_min_pathway_genes"):
            out[k] = int(v)
        elif k in ("padj_alpha", "min_abs_log2fc"):
            out[k] = float(v)
        elif k == "de_query_direction":
            out[k] = str(v).lower().strip()
        else:
            out[k] = v
    return out


def filter_counts_by_minimum_total(
    counts_df: pd.DataFrame,
    min_total: int,
) -> Tuple[pd.DataFrame, int, int]:
    """
    Remove genes with summed counts below ``min_total`` (DESeq2-style prefilter).

    Returns:
        (filtered_df, n_before, n_after)
    """
    if min_total <= 0:
        return counts_df, counts_df.shape[1], counts_df.shape[1]
    totals = counts_df.sum(axis=0)
    keep = totals >= min_total
    n_before = int(counts_df.shape[1])
    filtered = counts_df.loc[:, keep]
    n_after = int(filtered.shape[1])
    return filtered, n_before, n_after


def normalize_gene_ids(raw: Sequence[str]) -> List[str]:
    """
    Normalize gene identifiers for downstream gene set matching.

    GEO count tables often use a combined key like ``ENSG...__TP53``.
    We keep the symbol suffix when present.
    """

    out: List[str] = []
    for g in raw:
        s = str(g)
        if "__" in s:
            s = s.split("__", 1)[1]
        out.append(s)
    return out


def load_counts_csv_as_samples_by_genes(
    path: str | Path,
    *,
    sample_ids: Optional[Sequence[str]] = None,
) -> pd.DataFrame:
    """
    Load a counts table from CSV/CSV.GZ and return **samples × genes** DataFrame.

    Expected file layout:
      - rows: genes
      - columns: sample IDs
      - first column: gene identifier (may be unnamed)
    """

    p = Path(path)
    df = pd.read_csv(p, index_col=0)
    if df.empty:
        raise ValueError(f"Counts file is empty: {p}")

    df.index = normalize_gene_ids(df.index.tolist())
    df = df.apply(pd.to_numeric, errors="coerce").fillna(0).astype(int)

    # Aggregate duplicate symbols (common when collapsing Ensembl->symbol).
    if df.index.has_duplicates:
        df = df.groupby(df.index).sum()

    # genes × samples -> samples × genes
    counts_df = df.T

    if sample_ids is not None:
        missing = [s for s in sample_ids if s not in counts_df.index]
        if missing:
            raise ValueError(
                f"Counts file missing sample columns/rows for: {missing[:10]}"
                + (" ..." if len(missing) > 10 else "")
            )
        counts_df = counts_df.loc[list(sample_ids)]

    return counts_df


def counts_dict_to_samples_by_genes(
    counts: Dict[str, Sequence[int]],
    sample_ids: Sequence[str],
) -> pd.DataFrame:
    """Build a samples × genes count matrix from gene → per-sample counts."""
    sid_to_i = {sid: i for i, sid in enumerate(sample_ids)}
    rows = []
    for sid in sample_ids:
        j = sid_to_i[sid]
        rows.append([int(counts[g][j]) for g in counts])
    return pd.DataFrame(rows, index=list(sample_ids), columns=list(counts.keys()))


def build_sample_metadata(
    sample_ids: Sequence[str],
    condition_by_sample: Dict[str, str],
) -> pd.DataFrame:
    missing = [s for s in sample_ids if s not in condition_by_sample]
    if missing:
        raise ValueError(
            f"sample_metadata missing entries for sample_ids: {missing[:10]}"
            + (" ..." if len(missing) > 10 else "")
        )
    conds = [condition_by_sample[s] for s in sample_ids]
    return pd.DataFrame({"condition": conds}, index=list(sample_ids))


def validate_counts_case(case: Dict[str, Any]) -> Optional[str]:
    """Return an error message if pipeline case JSON is inconsistent, else None."""
    counts = case.get("counts")
    sample_ids = case.get("sample_ids")
    if not isinstance(counts, dict) or not sample_ids:
        return None
    n = len(sample_ids)
    for gene, vals in counts.items():
        if len(vals) != n:
            return (
                f"Gene {gene!r} has {len(vals)} count values but "
                f"sample_ids has length {n}."
            )
    return None


def load_author_de_table_csv(
    path: str | Path,
    *,
    gene_column: str | None = None,
    log2fc_column: str = "log2FoldChange",
    pvalue_column: str = "pvalue",
    padj_column: str = "padj",
) -> List[Dict[str, Any]]:
    """
    Load a precomputed differential expression (DE) table (author-provided).

    Supports the common GEO supplement format used in GSE227102:
      - semicolon-delimited
      - decimal comma in numeric columns (e.g. ``0,12``) and scientific like ``1,47E-18``
      - gene symbol in a column like ``Gene,name`` and/or an Ensembl ``ID``

    Returns:
        DE rows in the same schema as ``run_deseq2_contrast`` output, sorted by ascending padj.
    """

    p = Path(path)
    if not p.is_file():
        raise ValueError(f"DE table file not found: {p}")

    df = pd.read_csv(p, sep=";")
    if df.empty:
        raise ValueError(f"DE table is empty: {p}")

    # Pick gene column.
    if gene_column is None:
        for cand in ("Gene,name", "gene", "symbol", "Gene", "gene_name"):
            if cand in df.columns:
                gene_column = cand
                break
    if gene_column is None or gene_column not in df.columns:
        raise ValueError(
            "Could not infer gene column. Available columns: "
            + ", ".join(map(str, df.columns.tolist()))
        )

    # Normalize numeric strings (decimal commas).
    for c in (log2fc_column, pvalue_column, padj_column):
        if c not in df.columns:
            raise ValueError(f"Missing required column {c!r} in DE table: {p}")
        df[c] = df[c].astype(str).str.replace(",", ".", regex=False)
        df[c] = pd.to_numeric(df[c], errors="coerce")

    df[gene_column] = df[gene_column].astype(str)

    rows: List[Dict[str, Any]] = []
    for _, r in df.iterrows():
        gene = str(r.get(gene_column, "")).strip()
        if not gene or gene.lower() in ("nan", "none"):
            continue
        padj = float(r.get(padj_column)) if pd.notna(r.get(padj_column)) else 1.0
        rows.append(
            {
                "gene": gene,
                "baseMean": float("nan"),  # unknown for author tables; kept for schema compat
                "log2FoldChange": float(r.get(log2fc_column, 0.0))
                if pd.notna(r.get(log2fc_column))
                else 0.0,
                "lfcSE": None,
                "pvalue": float(r.get(pvalue_column, 1.0))
                if pd.notna(r.get(pvalue_column))
                else 1.0,
                "padj": padj,
                "significant": False,  # filled by caller using chosen alpha
            }
        )

    rows.sort(
        key=lambda x: (
            _safe_padj_value(x.get("padj")),
            -abs(float(x.get("log2FoldChange") or 0.0)),
        )
    )
    return rows


def run_deseq2_contrast(
    counts_df: pd.DataFrame,
    metadata_df: pd.DataFrame,
    alt_level: str,
    ref_level: str,
    *,
    padj_alpha: float = 0.05,
    min_replicates: int = 2,
) -> Tuple[List[Dict[str, Any]], Optional[str]]:
    """
    Run PyDESeq2 Wald test for ``alt_level`` vs ``ref_level`` on column ``condition``.

    Returns:
        (de_rows, error_message). ``de_rows`` are sorted by ascending adjusted p-value.
    """
    if not _PYDESQ2_AVAILABLE:
        return [], "PyDESeq2 is not installed."

    levels = set(metadata_df["condition"].tolist())
    if ref_level not in levels or alt_level not in levels:
        return [], (
            f"Contrast invalid: need both reference {ref_level!r} and "
            f"alternate {alt_level!r} in sample metadata; got {sorted(levels)}."
        )

    try:
        dds = DeseqDataSet(
            counts=counts_df,
            metadata=metadata_df,
            design="~condition",
            refit_cooks=True,
            min_replicates=min_replicates,
            quiet=True,
        )
        buf_out, buf_err = io.StringIO(), io.StringIO()
        with redirect_stdout(buf_out), redirect_stderr(buf_err):
            dds.deseq2()
            stat_res = DeseqStats(dds, contrast=["condition", alt_level, ref_level])
            stat_res.summary()
        res = stat_res.results_df
    except Exception as exc:  # pragma: no cover - fitting failures
        return [], f"DESeq2 failed: {exc}"

    de_rows: List[Dict[str, Any]] = []
    for gene, row in res.iterrows():
        padj = float(row["padj"]) if pd.notna(row["padj"]) else 1.0
        de_rows.append(
            {
                "gene": str(gene),
                "baseMean": float(row.get("baseMean", 0.0)),
                "log2FoldChange": float(row.get("log2FoldChange", 0.0)),
                "lfcSE": float(row.get("lfcSE", 0.0))
                if pd.notna(row.get("lfcSE"))
                else None,
                "pvalue": float(row.get("pvalue", 1.0))
                if pd.notna(row.get("pvalue"))
                else 1.0,
                "padj": padj,
                "significant": padj <= padj_alpha,
            }
        )
    de_rows.sort(key=lambda r: (r["padj"], -abs(r["log2FoldChange"])))
    return de_rows, None


def benjamini_hochberg(p_values: Sequence[float]) -> List[float]:
    """Benjamini–Hochberg FDR; returns q-values in original order (fallback)."""
    m = len(p_values)
    if m == 0:
        return []
    p_arr = np.nan_to_num(np.asarray(p_values, dtype=float), nan=1.0)
    order = np.argsort(p_arr)
    sorted_p = p_arr[order]
    adj_sorted = np.empty(m)
    running = 1.0
    for i in range(m - 1, -1, -1):
        running = min(sorted_p[i] * m / (i + 1), running)
        adj_sorted[i] = running
    out = np.empty(m)
    out[order] = adj_sorted
    return np.clip(out, 0.0, 1.0).tolist()


def adjust_pvalues_bh(p_values: Sequence[float]) -> List[float]:
    """Benjamini–Hochberg adjusted p-values using SciPy (preferred)."""
    m = len(p_values)
    if m == 0:
        return []
    p_arr = np.clip(
        np.nan_to_num(np.asarray(p_values, dtype=float), nan=1.0), 1e-300, 1.0
    )
    try:
        adj = false_discovery_control(p_arr, method="bh")
        return np.clip(adj, 0.0, 1.0).tolist()
    except Exception:
        return benjamini_hochberg(p_values)


def ora_fisher(
    de_genes: Sequence[str],
    pathway_genes: Dict[str, Sequence[str]],
    universe_genes: Sequence[str],
    *,
    min_pathway_genes: int = 3,
) -> List[Dict[str, Any]]:
    """
    Over-representation analysis (one-sided Fisher exact, greater overlap).

    ``universe_genes`` should be the **same gene set** used for DESeq2 (prefiltered).

    Pathways smaller than ``min_pathway_genes`` in the universe are skipped (reduces
    noise from tiny sets).
    """
    u: Set[str] = set(universe_genes)
    de: Set[str] = {g for g in de_genes if g in u}
    results: List[Dict[str, Any]] = []
    de_n = len(de)

    for pname, pgenes in pathway_genes.items():
        pset = {g for g in pgenes if g in u}
        if len(pset) < min_pathway_genes:
            continue
        overlap = sorted(de & pset)
        a = len(overlap)
        b = len(de - pset)
        c = len(pset - de)
        d = len(u) - a - b - c
        if d < 0:
            d = 0
        oddsr, p_raw = fisher_exact([[a, b], [c, d]], alternative="greater")
        p_f = float(p_raw) if math.isfinite(float(p_raw)) else 1.0
        results.append(
            {
                "pathway": pname,
                "p_value": p_f,
                "odds_ratio": float(oddsr) if np.isfinite(oddsr) else None,
                "overlap_genes": overlap,
                "overlap_count": a,
                "pathway_size": len(pset),
                "de_in_universe": de_n,
                "gene_ratio": f"{a}/{len(pset)}",
            }
        )

    qvals = adjust_pvalues_bh([r["p_value"] for r in results])
    for r, q in zip(results, qvals):
        r["q_value"] = q
    results.sort(key=lambda x: (x["p_value"], -x["overlap_count"]))
    return results


def enrichr_ora(
    query_genes: Sequence[str],
    *,
    libraries: Sequence[str],
    background: Optional[Sequence[str]] = None,
    top_k: int = 50,
) -> tuple[List[Dict[str, Any]], Optional[str]]:
    """
    Enrichr-based ORA using gseapy (requires network for most libraries).

    Returns:
        (rows, error_message)
    """

    if not _GSEAPY_AVAILABLE:
        return [], "gseapy is not installed."
    q = [str(g) for g in query_genes if g]
    if not q:
        return [], "Empty query gene list."
    libs = [str(x) for x in libraries if x]
    if not libs:
        return [], "No Enrichr libraries configured."

    rows: List[Dict[str, Any]] = []
    try:
        for lib in libs:
            enr = gp.enrichr(  # type: ignore[union-attr]
                gene_list=q,
                gene_sets=lib,
                background=list(background) if background is not None else None,
                outdir=None,
                no_plot=True,
            )
            res = getattr(enr, "results", None)
            if res is None or res.empty:
                continue
            for _, r in res.head(top_k).iterrows():
                genes = []
                raw = r.get("Genes")
                if isinstance(raw, str):
                    genes = [g.strip() for g in raw.replace(";", ",").split(",") if g.strip()]
                rows.append(
                    {
                        "pathway": f"{lib}: {r.get('Term')}",
                        "p_value": float(r.get("P-value", 1.0)),
                        "q_value": float(r.get("Adjusted P-value", 1.0)),
                        "odds_ratio": float(r.get("Odds Ratio"))
                        if pd.notna(r.get("Odds Ratio"))
                        else None,
                        "overlap_genes": genes,
                        "overlap_count": int(r.get("Overlap", "0/0").split("/")[0])
                        if isinstance(r.get("Overlap"), str)
                        else None,
                        "pathway_size": int(r.get("Overlap", "0/0").split("/")[1])
                        if isinstance(r.get("Overlap"), str)
                        else None,
                    }
                )
    except Exception as exc:  # pragma: no cover
        return [], f"Enrichr failed: {exc}"

    rows.sort(key=lambda x: (x.get("q_value", 1.0), x.get("p_value", 1.0)))
    return rows, None


def _safe_padj_value(v: Any) -> float:
    try:
        x = float(v)
    except (TypeError, ValueError):
        return 1.0
    return 1.0 if math.isnan(x) else x


def pick_de_query_genes(
    de_rows: Sequence[Dict[str, Any]],
    *,
    padj_alpha: float = 0.05,
    max_genes: int = 200,
    direction: str = "both",
    min_abs_log2fc: float = 0.0,
) -> List[str]:
    """
    Genes for ORA query: significant by ``padj`` and optional **direction** (activation).

    ``direction``: ``\"up\"`` (alt > ref), ``\"down\"`` (alt < ref), or ``\"both\"``.
    """
    dir_norm = direction.lower().strip()
    if dir_norm not in ("up", "down", "both"):
        dir_norm = "both"

    def lfc_ok(r: Dict[str, Any]) -> bool:
        try:
            lfc = float(r.get("log2FoldChange", 0.0))
        except (TypeError, ValueError):
            return False
        if math.isnan(lfc):
            return False
        if dir_norm == "both":
            return abs(lfc) >= min_abs_log2fc
        if dir_norm == "up":
            return lfc >= min_abs_log2fc
        return lfc <= -min_abs_log2fc

    sig: List[str] = []
    for r in de_rows:
        if _safe_padj_value(r.get("padj", 1.0)) > padj_alpha:
            continue
        if not lfc_ok(r):
            continue
        sig.append(r["gene"])

    if not sig:
        for r in de_rows[:max_genes]:
            if lfc_ok(r):
                sig.append(r["gene"])
    if not sig:
        sig = [r["gene"] for r in de_rows[:max_genes]]
    return sig[:max_genes]


def compare_pathways_detail(
    pathway_a: str,
    pathway_b: str,
    pathway_genes: Dict[str, Sequence[str]],
    de_genes: Sequence[str],
) -> Dict[str, Any]:
    """Exclusive vs shared DE support between two pathways."""
    pa = set(pathway_genes.get(pathway_a, []))
    pb = set(pathway_genes.get(pathway_b, []))
    de = set(de_genes)
    only_a = sorted((pa - pb) & de)
    only_b = sorted((pb - pa) & de)
    shared = sorted((pa & pb) & de)
    return {
        "pathway_a": pathway_a,
        "pathway_b": pathway_b,
        "exclusive_to_a": only_a,
        "exclusive_to_b": only_b,
        "shared_de_support": shared,
        "pathway_a_size": len(pa),
        "pathway_b_size": len(pb),
        "overlap_pathway_genes": sorted(pa & pb),
    }


def overlap_genes_across_top_pathways(
    ora_rows: Sequence[Dict[str, Any]],
    top_k: int = 5,
) -> Dict[str, Any]:
    """DE genes that appear in more than one of the top-k pathways by p-value."""
    top = [r for r in ora_rows[:top_k] if r.get("overlap_genes")]
    gene_to_paths: Dict[str, List[str]] = {}
    for row in top:
        p = row["pathway"]
        for g in row.get("overlap_genes", []):
            gene_to_paths.setdefault(g, []).append(p)
    multi = {g: paths for g, paths in gene_to_paths.items() if len(paths) > 1}
    return {
        "genes_supporting_multiple_top_pathways": sorted(multi.keys()),
        "gene_to_pathways": {g: multi[g] for g in sorted(multi)},
    }


def top_hits_statistically_close(
    ora_rows: Sequence[Dict[str, Any]],
    *,
    ratio_threshold: float = 10.0,
    top_k: int = 3,
) -> Dict[str, Any]:
    """Flag when the top two enriched pathways have similar p-values (ratio bound)."""
    if len(ora_rows) < 2:
        return {
            "close_top_hits": False,
            "p_ratio": None,
            "note": "fewer than 2 pathways",
        }
    p1 = ora_rows[0]["p_value"]
    p2 = ora_rows[1]["p_value"]
    if p1 <= 0 or p2 <= 0:
        ratio = None
        close = False
    else:
        ratio = max(p1, p2) / min(p1, p2)
        close = ratio <= ratio_threshold
    return {
        "close_top_hits": close,
        "p_ratio": ratio,
        "p_top1": p1,
        "p_top2": p2,
    }