File size: 8,061 Bytes
43b3e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# MIT License, Bryan Cheng, 2026. Part of PANGEA/READOUT.
"""Add the MPRA stratum to the fragment library.

The plan's sampling design includes "the MPRA library sequences themselves, so every
downstream task has exact coverage".  MPRA elements are 170 to 249 bp while the solver
fragment is 61 bp, so each selected element is tiled with overlapping 61 bp windows and
the element identity and window offset are recorded.  That bookkeeping is what lets
fragment-level physics channels be reassembled into an element-level feature for the
CEILING arm; without it the solver output could not be matched back to an activity
measurement.

Elements are selected stratified by activity decile rather than at random, so the
covered set spans the full range of measured activity instead of concentrating in the
dense middle of the distribution. Tiling every element is not affordable: the lentiMPRA
K562 library alone would need about 1.5 million solves.

Adapters are stripped before tiling where the construct is documented, because the
adapter is identical across elements and would otherwise dominate the flanking context
of the terminal tiles.
"""
from __future__ import annotations

import argparse
import csv
import json
import os
import random
import sys
from typing import Dict, Iterable, List, Optional, Tuple

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from data.fasta import gc_fraction  # noqa: E402
from data.sample_fragments import FIELDS, frag_id  # noqa: E402

FRAGMENT_BP = 61

# Documented constructs. Adapter lengths are stripped from each end before tiling.
ADAPTERS: Dict[str, Tuple[int, int]] = {
    "lentimpra": (15, 15),      # 15 nt 5' adaptor + 200 nt element + 15 nt 3' adaptor
    "deepstarr": (0, 0),
    "jores_plant": (0, 0),
}


def stratified_sample(rows: List[dict], n: int, rng: random.Random,
                      n_bins: int = 10) -> List[dict]:
    """Sample n rows spread evenly across activity deciles."""
    if n >= len(rows):
        return list(rows)
    with_target = [r for r in rows if r.get("_target") is not None]
    if not with_target:
        return rng.sample(rows, n)
    with_target.sort(key=lambda r: r["_target"])
    per_bin = max(1, n // n_bins)
    out: List[dict] = []
    size = len(with_target)
    for b in range(n_bins):
        lo = b * size // n_bins
        hi = (b + 1) * size // n_bins
        chunk = with_target[lo:hi]
        if not chunk:
            continue
        out += rng.sample(chunk, min(per_bin, len(chunk)))
    # Top up to n if rounding left room.
    if len(out) < n:
        remaining = [r for r in with_target if r not in out]
        out += rng.sample(remaining, min(n - len(out), len(remaining)))
    return out[:n]


def tile(seq: str, length: int = FRAGMENT_BP, step: int = 40) -> List[Tuple[int, str]]:
    """Overlapping windows, with the last window flush to the 3' end."""
    if len(seq) < length:
        return []
    starts = list(range(0, len(seq) - length + 1, step))
    if starts[-1] != len(seq) - length:
        starts.append(len(seq) - length)
    return [(s, seq[s:s + length]) for s in starts]


def load_activity(path: str) -> List[dict]:
    rows: List[dict] = []
    with open(path) as f:
        for rec in csv.DictReader(f, delimiter="\t"):
            try:
                target = float(rec["target"])
            except (TypeError, ValueError, KeyError):
                target = None
            rows.append({**rec, "_target": target})
    return rows


def build(processed_dir: str, datasets: Dict[str, int], step: int,
          rng: random.Random) -> Tuple[List[dict], dict]:
    out: List[dict] = []
    report: Dict[str, object] = {}
    for name, n_elements in datasets.items():
        path = os.path.join(processed_dir, f"activity_{name}.tsv")
        if not os.path.exists(path):
            report[name] = {"status": "missing", "path": path}
            continue
        rows = load_activity(path)
        # One entry per unique element sequence; lentiMPRA stores two orientations.
        seen: Dict[str, dict] = {}
        for r in rows:
            seen.setdefault(r["sequence"], r)
        unique = list(seen.values())
        chosen = stratified_sample(unique, n_elements, rng)
        lo, hi = ADAPTERS.get(name, (0, 0))
        n_frag = 0
        skipped_short = 0
        for rec in chosen:
            seq = rec["sequence"].upper()
            core = seq[lo:len(seq) - hi] if (lo or hi) else seq
            if any(b not in "ACGT" for b in core):
                continue
            tiles = tile(core, FRAGMENT_BP, step)
            if not tiles:
                skipped_short += 1
                continue
            for offset, frag in tiles:
                out.append({
                    "stratum": "mpra",
                    "sequence": frag,
                    "source": f"{name}:{rec.get('subset', '')}",
                    "contig": rec.get("element_id", "") or "",
                    "start": offset,
                    "strand": "+",
                    "clade": rec.get("species", ""),
                })
                n_frag += 1
        report[name] = {
            "status": "ok",
            "unique_elements_available": len(unique),
            "elements_selected": len(chosen),
            "element_length_after_adapter_strip":
                len(chosen[0]["sequence"]) - lo - hi if chosen else None,
            "adapters_stripped": [lo, hi],
            "fragments_added": n_frag,
            "elements_skipped_too_short": skipped_short,
            "tile_step": step,
        }
    return out, report


def main() -> int:
    ap = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("--processed", required=True, help="dir with activity_*.tsv")
    ap.add_argument("--library", required=True, help="existing library TSV to extend")
    ap.add_argument("--out", required=True)
    ap.add_argument("--seed", type=int, default=20261119)
    ap.add_argument("--step", type=int, default=40)
    ap.add_argument("--lentimpra", type=int, default=4000)
    ap.add_argument("--deepstarr", type=int, default=4000)
    ap.add_argument("--jores", type=int, default=2000)
    a = ap.parse_args()

    rng = random.Random(a.seed)
    new_rows, report = build(a.processed, {
        "lentimpra": a.lentimpra,
        "deepstarr": a.deepstarr,
        "jores_plant": a.jores,
    }, a.step, rng)

    finalised = []
    for r in new_rows:
        rec = {k: r.get(k, "") for k in FIELDS}
        rec["sequence"] = r["sequence"]
        rec["length"] = len(r["sequence"])
        rec["gc"] = round(gc_fraction(r["sequence"]), 6)
        rec["frag_id"] = frag_id(r["sequence"], "mpra")
        finalised.append(rec)

    with open(a.library) as f:
        existing = list(csv.DictReader(f, delimiter="\t"))
    have = {r["frag_id"] for r in existing}
    deduped, dup = [], 0
    for r in finalised:
        if r["frag_id"] in have:
            dup += 1
            continue
        have.add(r["frag_id"])
        deduped.append(r)

    os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
    with open(a.out, "w", newline="") as f:
        w = csv.DictWriter(f, fieldnames=FIELDS, delimiter="\t", extrasaction="ignore")
        w.writeheader()
        w.writerows(existing)
        w.writerows(deduped)

    from collections import Counter

    strata = Counter(r["stratum"] for r in existing)
    strata["mpra"] = len(deduped)
    meta = {
        "source_library": a.library,
        "seed": a.seed,
        "tile_step": a.step,
        "mpra_fragments_added": len(deduped),
        "duplicates_dropped": dup,
        "total_fragments": len(existing) + len(deduped),
        "by_stratum": dict(strata),
        "datasets": report,
    }
    with open(a.out.replace(".tsv", "_meta.json"), "w") as f:
        json.dump(meta, f, indent=2)
    print(json.dumps(meta, indent=2))
    return 0


if __name__ == "__main__":
    sys.exit(main())