File size: 1,635 Bytes
6f32cc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Species overlap between splits (S__ tags of the kept windows): how many of a split's species also occur in train.

  python species_overlap.py [--root .] [--splits heldout valid] [--ref train]
"""

from __future__ import annotations

import argparse
import json
import re
from pathlib import Path


def species(root: Path, split: str) -> dict[str, int]:
    """species -> bases in the split's selected windows."""
    sel_path = root / split / "selected.json"
    sel = json.loads(sel_path.read_text())["shards"] if sel_path.exists() else None
    out: dict[str, int] = {}
    for jl in sorted((root / split).glob("*/*.jsonl")):
        rows = [json.loads(line) for line in jl.read_text().splitlines()]
        ids = sel.get(str(jl.with_suffix("").relative_to(root)), []) if sel is not None else range(len(rows))
        for i in ids:
            m = re.search(r"S__([^;|]*)", rows[i]["tag"])
            if m:
                out[m.group(1)] = out.get(m.group(1), 0) + rows[i]["len"]
    return out


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--root", type=Path, default=Path("."))
    ap.add_argument("--splits", nargs="+", default=["heldout", "valid"])
    ap.add_argument("--ref", default="train")
    a = ap.parse_args()
    ref = species(a.root, a.ref)
    for split in a.splits:
        sp = species(a.root, split)
        shared = set(sp) & set(ref)
        tagged = sum(sp.values())
        print(f"{split}: {len(sp)} species, {len(shared)} also in {a.ref} "
              f"({sum(sp[s] for s in shared) / max(tagged, 1):.1%} of its tagged bases)")


if __name__ == "__main__":
    main()