#!/usr/bin/env python3 """E2 sample sets: the real-data sets with one fill individual replaced (approved 2026-11-02, option (a)). NA18498 (YRI, fill rank 54 of sets A and B) is not in the phase 3 call set. It is replaced, in the YRI fill only, by NA18520, the next unused YRI individual in the frozen selection order (SELECTION.json order index 96). The 32 view individuals of every set are unchanged; every other population's file is copied byte for byte. The same sets are used for both call sets. Writes /real/e2_samples/{.tsv x 25, UNION_SAMPLES.txt, SWAP.json} (read-only); refuses to overwrite. /usr/bin/python3 -B e2_make_samples.py """ import csv, hashlib, io, json, os, shutil, sys, time B = "$WORK/bench_h2h_20260915" SRC = f"{B}/real/autosomes_v1/samples" OUT = f"{B}/real/e2_samples" OLD, NEW, POP, RANK = "NA18498", "NA18520", "YRI", "54" def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest() def main(): if os.path.exists(OUT): sys.exit(f"refused: {OUT} exists") sel = json.load(open(f"{SRC}/SELECTION.json")) order = sel["populations"][POP]["order"] used = set(sel["populations"][POP]["sets"]["A"]) | set(sel["populations"][POP]["sets"]["B"]) nxt = [s for s in order if s not in used][0] assert nxt == NEW and order.index(NEW) == 96, (nxt, order.index(NEW)) os.makedirs(OUT) pops = sorted(f[:-4] for f in os.listdir(SRC) if f.endswith(".tsv")) assert len(pops) == 26 or len(pops) == 25, pops files = {} for pop in pops: src, dst = f"{SRC}/{pop}.tsv", f"{OUT}/{pop}.tsv" if pop != POP: shutil.copyfile(src, dst) else: text = open(src, encoding="utf-8").read() rows = list(csv.reader(io.StringIO(text), delimiter="\t")) hdr = rows[0] i_set, i_rank, i_role, i_id, i_ord = (hdr.index(k) for k in ("set", "rank", "role", "sample_id", "order_index")) n = 0 for r in rows[1:]: if r[i_id] == OLD: assert r[i_rank] == RANK and r[i_role] == "fill" and r[i_set] in ("A", "B"), r r[i_id], r[i_ord] = NEW, str(order.index(NEW)) n += 1 assert n == 2, n assert sum(r[i_id] == NEW for r in rows[1:]) == 2 buf = io.StringIO() csv.writer(buf, delimiter="\t", lineterminator="\n").writerows(rows) open(dst, "w", encoding="utf-8").write(buf.getvalue()) files[pop] = {"src_sha256": sha(src), "sha256": sha(dst), "changed": pop == POP} old_union = open(f"{SRC}/UNION_SAMPLES.txt").read().split() union = sorted({r["sample_id"] for pop in pops for r in csv.DictReader(open(f"{OUT}/{pop}.tsv"), delimiter="\t")}) assert union == sorted((set(old_union) - {OLD}) | {NEW}), "union differs from old union - NA18498 + NA18520" open(f"{OUT}/UNION_SAMPLES.txt", "w").write("\n".join(union) + "\n") rec = {"schema": "paper_exp_20261102.e2.samples/1", "utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "approval": "coordinator 2026-11-02: option (a) approved, NA18498 -> NA18520 in the YRI fill only, both call sets", "reason": "NA18498 is not in the phase 3 GRCh38 call set (2,548 samples); every view individual is present", "swap": {"population": POP, "sets": ["A", "B"], "rank": int(RANK), "role": "fill", "removed": OLD, "added": NEW, "added_order_index": order.index(NEW), "rule": "next unused individual in the frozen SELECTION.json order"}, "views_unchanged": True, "source_dir": SRC, "files": files, "union": {"n": len(union), "sha256": sha(f"{OUT}/UNION_SAMPLES.txt")}} json.dump(rec, open(f"{OUT}/SWAP.json", "w"), indent=1) for f in os.listdir(OUT): os.chmod(f"{OUT}/{f}", 0o444) print(json.dumps({"out": OUT, "n_union": len(union), "yri_sha256": files[POP]["sha256"]})) if __name__ == "__main__": main()