File size: 24,586 Bytes
ee5b44c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
from __future__ import annotations

import hashlib
from dataclasses import dataclass, field
from pathlib import Path

import numpy as np
import pandas as pd
from pandas.io.common import infer_compression

from .assets import asset_path
from .config import MAX_MSPRIME_RATE, Config
from .utils import loguniform


@dataclass
class PiecewiseRateMap:
    chrom: str
    position: np.ndarray
    rate: np.ndarray
    rate_unit: str = "per_bp_per_generation"
    rate_unit_before: str = "per_bp"
    rate_unit_after: str = "per_bp_per_generation"
    mean_rate_before_scaling: float = 0.0
    mean_rate_after_scaling: float = 0.0
    scaling_method: str = "identity"
    source_format: str = "unknown"

    @property
    def length(self) -> float:
        return float(self.position[-1]) if len(self.position) else 0.0


def _validate_piecewise_rate_map(rate_map: PiecewiseRateMap) -> None:
    position = np.asarray(rate_map.position)
    rate = np.asarray(rate_map.rate)
    if position.ndim != 1 or rate.ndim != 1:
        raise ValueError(f"Empirical map on {rate_map.chrom} must use one-dimensional arrays")
    if len(position) != len(rate) + 1 or len(rate) == 0:
        raise ValueError(
            f"Empirical map on {rate_map.chrom} must have one more position than rate"
        )
    if not np.all(np.isfinite(position)) or not np.all(np.isfinite(rate)):
        raise ValueError(f"Empirical map on {rate_map.chrom} contains non-finite values")
    if np.any(np.diff(position) <= 0):
        raise ValueError(f"Empirical map positions on {rate_map.chrom} must be strictly increasing")


@dataclass
class EmpiricalMapStore:
    rec_maps: dict[str, PiecewiseRateMap] = field(default_factory=dict)
    mut_maps: dict[str, PiecewiseRateMap] = field(default_factory=dict)
    metadata: dict[str, dict] = field(default_factory=dict)
    _common_span_cache: dict[str, tuple[float, float]] = field(
        default_factory=dict,
        repr=False,
    )
    _usable_chrom_cache: dict[float, tuple[str, ...]] = field(
        default_factory=dict,
        repr=False,
    )

    def common_chroms(self, min_len: int) -> list[str]:
        cache_key = float(min_len)
        cached = self._usable_chrom_cache.get(cache_key)
        if cached is not None:
            return list(cached)

        chroms = sorted(set(self.rec_maps) & set(self.mut_maps), key=_chrom_sort_key)
        usable: list[str] = []
        for chrom in chroms:
            try:
                self.common_slice_start_bounds(chrom, min_len)
            except ValueError:
                continue
            usable.append(chrom)
        self._usable_chrom_cache[cache_key] = tuple(usable)
        return usable

    def common_slice_start_bounds(self, chrom: str, length: int) -> tuple[float, float]:
        """Return the inclusive start-coordinate range usable by both maps."""
        if chrom not in self.rec_maps or chrom not in self.mut_maps:
            raise ValueError(f"Chromosome {chrom!r} is not present in both empirical maps")
        if length <= 0:
            raise ValueError("Slice length must be positive")

        cached_span = self._common_span_cache.get(chrom)
        if cached_span is None:
            rec_map = self.rec_maps[chrom]
            mut_map = self.mut_maps[chrom]
            _validate_piecewise_rate_map(rec_map)
            _validate_piecewise_rate_map(mut_map)
            cached_span = (
                max(float(rec_map.position[0]), float(mut_map.position[0])),
                min(float(rec_map.position[-1]), float(mut_map.position[-1])),
            )
            self._common_span_cache[chrom] = cached_span
        common_left, common_right = cached_span
        latest_start = common_right - float(length)
        if latest_start < common_left:
            raise ValueError(
                f"Chromosome {chrom!r} has only {common_right - common_left:g} bp "
                f"shared between empirical maps, shorter than requested slice {length:g} bp"
            )
        return common_left, latest_start

    def is_available(self, min_len: int) -> bool:
        return bool(self.common_chroms(min_len))


def _chrom_sort_key(chrom: str) -> tuple[int, str]:
    clean = chrom.lower().removeprefix("chr")
    return (int(clean), clean) if clean.isdigit() else (10_000, clean)


def _canonical_chrom(value) -> str:
    text = str(value).strip()
    if text.lower().startswith("chr"):
        text = text[3:]
    return f"chr{text}"


def _read_table_maybe_gzip(
    path: str,
    *,
    compression: str | None = None,
) -> pd.DataFrame:
    kwargs = dict(sep=r"\s+", comment="#", header=None, dtype=str)
    if compression is None:
        if path.endswith(".gz"):
            kwargs["compression"] = "gzip"
    elif not isinstance(compression, str) or not compression:
        raise ValueError(f"Unsupported empirical-map compression: {compression}")
    else:
        kwargs["compression"] = None if compression == "none" else compression
    return pd.read_csv(path, **kwargs)


def _inferred_map_compression(path: Path) -> str:
    """Match the legacy filename-based pandas decompression semantics."""

    path_text = str(path)
    if path_text.endswith(".gz"):
        return "gzip"
    inferred = infer_compression(path_text, "infer")
    return str(inferred) if inferred is not None else "none"


ROULETTE_RAW_TO_PER_GEN = 1.015e-7
RECOMB_CM_PER_MB_TO_PER_BP = 1.0e-8
# A published map has a resolution of its own, coarser than the truth.
OBSERVED_CELL_BP = (1_000.0, 20_000.0)
RECOMB_MAP_UNITS = {"per_bp", "cM_per_Mb"}
MUT_MAP_UNITS = {"per_bp", "relative", "roulette_raw"}


def _finite_mean(rates: np.ndarray) -> float:
    finite = np.asarray(rates, dtype=np.float64)
    finite = finite[np.isfinite(finite)]
    return float(np.mean(finite)) if len(finite) else 0.0


def _scale_rates(
    rates: np.ndarray,
    baseline_rate: float | None,
    map_kind: str,
    input_unit: str,
    scale_to_baseline: bool,
) -> tuple[np.ndarray, dict]:
    if map_kind == "recombination" and input_unit not in RECOMB_MAP_UNITS:
        raise ValueError(f"Unsupported recombination map unit: {input_unit}")
    if map_kind == "mutation" and input_unit not in MUT_MAP_UNITS:
        raise ValueError(f"Unsupported mutation map unit: {input_unit}")

    before_mean = _finite_mean(rates)
    fill = baseline_rate if baseline_rate is not None else 1e-8
    rates = np.nan_to_num(rates.astype(np.float64, copy=False), nan=fill, posinf=fill, neginf=0.0)

    method = "identity"
    if map_kind == "recombination":
        if input_unit == "cM_per_Mb":
            rates = rates * RECOMB_CM_PER_MB_TO_PER_BP
            method = "cM_per_Mb_to_per_bp_per_generation"
    elif input_unit == "roulette_raw":
        rates = rates * ROULETTE_RAW_TO_PER_GEN
        method = "roulette_raw_to_per_bp_per_generation"
        if scale_to_baseline and baseline_rate is not None:
            mean_after_conversion = _finite_mean(rates)
            if mean_after_conversion > 0:
                rates = rates * (float(baseline_rate) / mean_after_conversion)
                method += "+mean_to_baseline"
    elif input_unit == "relative":
        if baseline_rate is None:
            raise ValueError("relative mutation maps require a baseline mutation rate")
        positive_mean = _finite_mean(rates[rates > 0])
        if scale_to_baseline:
            if positive_mean <= 0:
                raise ValueError("relative mutation map has no positive rates to normalize")
            rates = rates * (float(baseline_rate) / positive_mean)
            method = "relative_mean_to_baseline"
        else:
            rates = rates * float(baseline_rate)
            method = "relative_multiplier_to_baseline"

    rates = np.clip(rates, 0.0, None)
    if len(rates) and np.nanmax(rates) > MAX_MSPRIME_RATE:
        raise ValueError(
            f"{map_kind} map rates look too large for msprime per-bp per-generation rates; "
            "set --recomb-map-unit/--mut-map-unit explicitly so rates can be converted"
        )
    after_mean = _finite_mean(rates)
    info = {
        "rate_unit_before": input_unit,
        "rate_unit_after": "per_bp_per_generation",
        "mean_rate_before_scaling": before_mean,
        "mean_rate_after_scaling": after_mean,
        "scaling_method": method,
    }
    return rates, info


def _piecewise_from_points(
    chrom: str,
    pos: np.ndarray,
    rate_points: np.ndarray,
    baseline_rate: float | None,
    map_kind: str,
    input_unit: str,
    scale_to_baseline: bool,
) -> PiecewiseRateMap:
    pos = np.asarray(pos)
    rate_points = np.asarray(rate_points)
    if pos.ndim != 1 or rate_points.ndim != 1 or len(pos) != len(rate_points):
        raise ValueError(f"Point map on {chrom} has incompatible position/rate arrays")
    if not np.all(np.isfinite(pos)) or not np.all(np.isfinite(rate_points)):
        raise ValueError(f"Point map on {chrom} contains non-finite values")
    order = np.argsort(pos)
    pos = pos[order].astype(np.float64, copy=False)
    rate_points = rate_points[order].astype(np.float64, copy=False)
    keep = np.concatenate([[True], np.diff(pos) > 0])
    pos = pos[keep]
    rate_points = rate_points[keep]
    if len(pos) == 0:
        raise ValueError(f"No numeric positions for empirical map chromosome {chrom}")
    if len(pos) > 1:
        left_step = pos[1] - pos[0]
        right_step = pos[-1] - pos[-2]
        boundaries = np.concatenate(
            [
                [max(0.0, pos[0] - left_step / 2.0)],
                (pos[:-1] + pos[1:]) / 2.0,
                [pos[-1] + max(1.0, right_step / 2.0)],
            ]
        )
    else:
        step = 1_000.0
        boundaries = np.array([max(0.0, pos[0] - step / 2.0), pos[0] + step / 2.0], dtype=np.float64)
    rates, scaling = _scale_rates(rate_points, baseline_rate, map_kind, input_unit, scale_to_baseline)
    return PiecewiseRateMap(
        chrom=chrom,
        position=boundaries.astype(np.float64),
        rate=rates.astype(np.float64),
        rate_unit=scaling["rate_unit_after"],
        **scaling,
        source_format="point_centers",
    )


def _piecewise_from_intervals(
    chrom: str,
    starts: np.ndarray,
    ends: np.ndarray,
    rates: np.ndarray,
    baseline_rate: float | None,
    map_kind: str,
    input_unit: str,
    scale_to_baseline: bool,
) -> PiecewiseRateMap:
    starts = np.asarray(starts)
    ends = np.asarray(ends)
    rates = np.asarray(rates)
    if (
        starts.ndim != 1
        or ends.ndim != 1
        or rates.ndim != 1
        or len(starts) != len(ends)
        or len(starts) != len(rates)
    ):
        raise ValueError(f"Interval map on {chrom} has incompatible start/end/rate arrays")
    if (
        not np.all(np.isfinite(starts))
        or not np.all(np.isfinite(ends))
        or not np.all(np.isfinite(rates))
    ):
        raise ValueError(f"Interval map on {chrom} contains non-finite values")
    order = np.argsort(starts)
    starts = starts[order].astype(np.float64, copy=False)
    ends = ends[order].astype(np.float64, copy=False)
    rates = rates[order].astype(np.float64, copy=False)
    keep = ends > starts
    starts = starts[keep]
    ends = ends[keep]
    rates = rates[keep]
    if len(starts) == 0:
        raise ValueError(f"No valid intervals for empirical map chromosome {chrom}")

    rates, scaling = _scale_rates(rates, baseline_rate, map_kind, input_unit, scale_to_baseline)
    filler = baseline_rate if baseline_rate is not None else float(np.nanmedian(rates))
    positions = [0.0]
    out_rates: list[float] = []
    current = 0.0
    for start, end, rate in zip(starts, ends, rates):
        start = float(start)
        end = float(end)
        if end <= current:
            continue
        if start > current:
            out_rates.append(float(filler))
            positions.append(start)
            current = start
        if end > current:
            out_rates.append(float(rate))
            positions.append(end)
            current = end

    positions_arr = np.asarray(positions, dtype=np.float64)
    rates_arr = np.asarray(out_rates, dtype=np.float64)
    keep = np.concatenate([[True], np.diff(positions_arr) > 0])
    positions_arr = positions_arr[keep]
    if len(rates_arr) != len(positions_arr) - 1:
        rates_arr = rates_arr[: len(positions_arr) - 1]
    return PiecewiseRateMap(
        chrom=chrom,
        position=positions_arr,
        rate=rates_arr.astype(np.float64),
        rate_unit=scaling["rate_unit_after"],
        **scaling,
        source_format="bed_intervals",
    )


def load_empirical_rate_table(
    path: str,
    baseline_rate: float | None = None,
    map_kind: str = "generic",
    rate_unit: str = "per_bp",
    scale_to_baseline: bool = True,
    *,
    compression: str | None = None,
) -> dict[str, PiecewiseRateMap]:
    """Load BED-like or position-rate maps, preserving chromosome boundaries.

    Canonical input is a BED-like interval table with columns:
    chrom, start, end, rate. Coordinates are 0-based half-open intervals and
    rate is per bp per generation unless rate_unit explicitly says otherwise.

    Legacy point-center maps are accepted and converted to interval boundaries
    with midpoint cuts. Missing interval gaps are filled with baseline_rate so
    the returned map can be passed to msprime after slicing.
    """
    df = _read_table_maybe_gzip(path, compression=compression)
    maps: dict[str, PiecewiseRateMap] = {}

    if df.shape[1] >= 4:
        chrom_col = df.iloc[:, 0].map(_canonical_chrom)
        starts = pd.to_numeric(df.iloc[:, 1], errors="coerce")
        ends = pd.to_numeric(df.iloc[:, 2], errors="coerce")
        rates = pd.to_numeric(df.iloc[:, 3], errors="coerce")
        parsed = pd.DataFrame({"chrom": chrom_col, "start": starts, "end": ends, "rate": rates}).dropna()
        for chrom, sub in parsed.groupby("chrom", sort=False):
            maps[str(chrom)] = _piecewise_from_intervals(
                str(chrom),
                sub["start"].to_numpy(),
                sub["end"].to_numpy(),
                sub["rate"].to_numpy(),
                baseline_rate,
                map_kind,
                rate_unit,
                scale_to_baseline,
            )
    elif df.shape[1] == 3:
        chrom_col = df.iloc[:, 0].map(_canonical_chrom)
        pos = pd.to_numeric(df.iloc[:, 1], errors="coerce")
        rates = pd.to_numeric(df.iloc[:, 2], errors="coerce")
        parsed = pd.DataFrame({"chrom": chrom_col, "pos": pos, "rate": rates}).dropna()
        for chrom, sub in parsed.groupby("chrom", sort=False):
            maps[str(chrom)] = _piecewise_from_points(
                str(chrom),
                sub["pos"].to_numpy(),
                sub["rate"].to_numpy(),
                baseline_rate,
                map_kind,
                rate_unit,
                scale_to_baseline,
            )
    elif df.shape[1] == 2:
        pos = pd.to_numeric(df.iloc[:, 0], errors="coerce")
        rates = pd.to_numeric(df.iloc[:, 1], errors="coerce")
        parsed = pd.DataFrame({"pos": pos, "rate": rates}).dropna()
        maps["__single__"] = _piecewise_from_points(
            "__single__",
            parsed["pos"].to_numpy(),
            parsed["rate"].to_numpy(),
            baseline_rate,
            map_kind,
            rate_unit,
            scale_to_baseline,
        )
    else:
        raise ValueError(f"Cannot parse empirical map: {path}")

    if not maps:
        raise ValueError(f"Empirical map has no usable numeric rows: {path}")
    return maps


def _load_empirical_rate_table_cached(
    path: str | Path,
    baseline_rate: float | None,
    map_kind: str,
    rate_unit: str,
    scale_to_baseline: bool,
) -> dict[str, PiecewiseRateMap]:
    from . import config as config_module
    from .map_cache import load_or_build_rate_maps

    parser_path = Path(path).expanduser()
    if not parser_path.is_absolute():
        parser_path = Path.cwd() / parser_path
    resolved_source = parser_path.resolve(strict=True)
    compression = _inferred_map_compression(parser_path)
    parse_parameters = {
        "parser": "load_empirical_rate_table",
        "compression": compression,
        "baseline_rate": baseline_rate,
        "map_kind": map_kind,
        "rate_unit": rate_unit,
        "scale_to_baseline": scale_to_baseline,
    }
    return load_or_build_rate_maps(
        source_path=resolved_source,
        parse_parameters=parse_parameters,
        related_code_paths={
            "empirical_maps_parser": Path(__file__),
            "rate_validation_config": Path(config_module.__file__),
        },
        builder=lambda snapshot: load_empirical_rate_table(
            str(snapshot),
            baseline_rate,
            map_kind=map_kind,
            rate_unit=rate_unit,
            scale_to_baseline=scale_to_baseline,
            compression=compression,
        ),
        map_class=PiecewiseRateMap,
    )


def _source_file_provenance(path: str | Path) -> dict[str, int | str]:
    source = Path(path).expanduser().resolve(strict=True)
    before = source.stat()
    digest = hashlib.sha256()
    with source.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    after = source.stat()
    identity_before = (
        before.st_dev,
        before.st_ino,
        before.st_size,
        before.st_mtime_ns,
    )
    identity_after = (
        after.st_dev,
        after.st_ino,
        after.st_size,
        after.st_mtime_ns,
    )
    if identity_before != identity_after:
        raise ValueError(f"Empirical map changed while recording provenance: {source}")
    return {
        "source_size_bytes": int(after.st_size),
        "source_sha256": digest.hexdigest(),
    }


def _summarize_maps(path: str, maps: dict[str, PiecewiseRateMap], requested_unit: str) -> dict:
    methods = sorted({m.scaling_method for m in maps.values()})
    formats = sorted({m.source_format for m in maps.values()})
    before = [m.mean_rate_before_scaling for m in maps.values()]
    after = [m.mean_rate_after_scaling for m in maps.values()]
    return {
        "path": path,
        **_source_file_provenance(path),
        "requested_rate_unit": requested_unit,
        "rate_unit_before": requested_unit,
        "rate_unit_after": "per_bp_per_generation",
        "mean_rate_before_scaling": float(np.mean(before)) if before else 0.0,
        "mean_rate_after_scaling": float(np.mean(after)) if after else 0.0,
        "scaling_method": "+".join(methods) if methods else "unknown",
        "source_formats": formats,
        "n_chromosomes": len(maps),
        "chromosomes": sorted(maps, key=_chrom_sort_key),
    }


def _resolve_map_path(path: str) -> Path | None:
    if not path:
        return None
    resolved = asset_path(path)
    return resolved if resolved.exists() else None


def load_empirical_maps(cfg: Config) -> EmpiricalMapStore:
    store = EmpiricalMapStore()
    recomb_path = _resolve_map_path(cfg.recomb_map)
    mut_path = _resolve_map_path(cfg.mut_map)
    if recomb_path is not None:
        print(f"[maps] loading empirical recombination map: {recomb_path}")
        store.rec_maps = _load_empirical_rate_table_cached(
            recomb_path,
            cfg.baseline_rec,
            map_kind="recombination",
            rate_unit=cfg.recomb_map_unit,
            scale_to_baseline=False,
        )
        store.metadata["recombination"] = _summarize_maps(str(recomb_path), store.rec_maps, cfg.recomb_map_unit)
    if mut_path is not None:
        print(f"[maps] loading empirical mutation map: {mut_path}")
        store.mut_maps = _load_empirical_rate_table_cached(
            mut_path,
            cfg.baseline_mu,
            map_kind="mutation",
            rate_unit=cfg.mut_map_unit,
            scale_to_baseline=cfg.mut_map_scale_to_baseline,
        )
        store.metadata["mutation"] = _summarize_maps(str(mut_path), store.mut_maps, cfg.mut_map_unit)
    return store


def slice_piecewise_map(
    rng: np.random.Generator,
    rate_map: PiecewiseRateMap,
    length: int,
    obs_noise_sigma: float,
    baseline_rate: float,
    start: float | None = None,
    *,
    validate_input: bool = True,
) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, dict]:
    """Slice a rate map, optionally at an explicit source-map coordinate.

    Omitting ``start`` preserves the original random-slice API. Passing it lets
    callers use the exact same genomic interval for multiple empirical maps.
    """
    if validate_input:
        _validate_piecewise_rate_map(rate_map)
    global_pos = rate_map.position
    global_rate = rate_map.rate
    if length <= 0:
        raise ValueError("Slice length must be positive")

    min_start = float(global_pos[0])
    max_start = float(global_pos[-1]) - float(length)
    if max_start < min_start:
        raise ValueError(
            f"Empirical map on {rate_map.chrom} spans only "
            f"{float(global_pos[-1]) - min_start:g} bp, shorter than requested slice {length:g} bp"
        )
    if start is None:
        start = min_start if max_start == min_start else float(rng.uniform(min_start, max_start))
    else:
        start = float(start)
        if not np.isfinite(start):
            raise ValueError("Slice start must be finite")
        if start < min_start or start > max_start:
            raise ValueError(
                f"Slice start {start:g} is outside the valid range "
                f"[{min_start:g}, {max_start:g}] for {rate_map.chrom}"
            )

    end = start + length

    internal_boundaries = global_pos[(global_pos > start) & (global_pos < end)]
    absolute_interval_starts = np.concatenate(
        [np.array([start], dtype=np.float64), internal_boundaries.astype(np.float64, copy=False)]
    )
    rate_indices = np.searchsorted(global_pos, absolute_interval_starts, side="right") - 1
    rate_indices = np.clip(rate_indices, 0, len(global_rate) - 1)
    rate = global_rate[rate_indices].copy()
    pos = np.concatenate(
        [
            np.array([0.0], dtype=np.float64),
            internal_boundaries.astype(np.float64, copy=False) - start,
            np.array([float(length)], dtype=np.float64),
        ]
    )

    true_rate = np.clip(rate, baseline_rate * 1e-4, baseline_rate * 500)

    # The observed map is what the model is handed and is expected to trust.
    # It is re-binned onto a grid of its own before the noise is applied, for
    # two reasons.  Returning it on the true breakpoints handed over every
    # boundary of the true map exactly, and noised only the intensities, when
    # a published map is uncertain about where the hotspots are as much as
    # about how strong they are.  And a real map reports the average rate in
    # its own bins, which also smooths a hotspot it did not resolve.
    obs_cell = float(loguniform(rng, *OBSERVED_CELL_BP))
    obs_pos = np.arange(0.0, float(length) + obs_cell, obs_cell)
    if len(obs_pos) < 2:
        obs_pos = np.array([0.0, float(length)], dtype=np.float64)
    obs_pos[-1] = float(length)
    cumulative = np.concatenate(([0.0], np.cumsum(true_rate * np.diff(pos))))
    obs_rate = np.diff(np.interp(obs_pos, pos, cumulative)) / np.diff(obs_pos)

    # Centred so the multiplier has mean one.  With a zero location a lognormal
    # has mean exp(sigma^2 / 2), so the observed map integral sat above the
    # truth in every row, and nw_log10 divides the segregating count by exactly
    # that integral.
    from .maps import observed_noise_multiplier

    obs_rate = obs_rate * observed_noise_multiplier(
        rng, obs_rate.shape[0], obs_cell, obs_noise_sigma, true_rates=obs_rate
    )
    obs_rate = np.clip(obs_rate, baseline_rate * 1e-5, baseline_rate * 1000)
    meta = {
        "chrom": rate_map.chrom,
        "slice_start_bp": start,
        "slice_end_bp": end,
        "rate_unit": rate_map.rate_unit,
        "rate_unit_before": rate_map.rate_unit_before,
        "rate_unit_after": rate_map.rate_unit_after,
        "mean_rate_before_scaling": rate_map.mean_rate_before_scaling,
        "mean_rate_after_scaling": rate_map.mean_rate_after_scaling,
        "scaling_method": rate_map.scaling_method,
        "source_format": rate_map.source_format,
        "observed_cell_bp": obs_cell,
        "observed_noise_sigma": float(obs_noise_sigma),
    }
    return (
        pos.astype(np.float64),
        true_rate.astype(np.float64),
        obs_pos.astype(np.float64),
        obs_rate.astype(np.float64),
        meta,
    )