row_18["planned_doses_n"] = 1 # (b2) 4 recipients in recipient_outcomes vs 3 in recipient_ids on L01 + earlier malformed split-lot on L00 r18_cmc_4v3_rids_shape = copy.deepcopy(r18_base_cmc_4) for row_18 in r18_cmc_4v3_rids_shape: if row_18.get("lot_id") == "L00": row_18["planned_doses_n"] = 1 elif row_18.get("lot_id") == "L01": row_18["recipient_ids"] = [f"R18-01-{rep_i}" for rep_i in range(3)] # (c) Consistent 3-recipient control on L01 (all three binding fields present and exactly matching) r18_recips_ctrl3 = [row_x for row_x in r18_recips_4 if row_x["recipient_id"] != "R18-01-3"] r18_cmc_ctrl3 = _derive_reconciling_cmc_lot_ledger_from_recipients( r18_recips_ctrl3, r18_mcb, lot_to_wcb_manifest=r18_wcb ) for row_18 in r18_cmc_ctrl3: if row_18.get("lot_id") == "L01": row_18["planned_doses_n"] = 4 row_18["recipient_records"] = [ {"recipient_id": f"R18-01-{rep_i}", "c2b_responder": True} for rep_i in range(3) ] r18_compare_fields = ( "status", "promotion_authorized", "external_clinical_store_verified", "lot_bank_hierarchy_valid", "lot_to_mcb_manifest_verified", "lot_to_wcb_manifest_verified", "parallel_cmc_ledger_verified", "icc_sensitivity_report_attached", "icc_sensitivity_report", "k", "n", "deff", "n_eff", "k_eff", "failure_reasons", ) for r18_tag, r18_clin_rows, r18_cmc_rows, expect_promote, expect_shape_l00 in ( ("4V3-RECS", r18_recips_4, r18_cmc_4v3_recs, False, False), ("4V3-RECS-SHAPE", r18_recips_4, r18_cmc_4v3_recs_shape, False, True), ("4V3-RIDS-SHAPE", r18_recips_4, r18_cmc_4v3_rids_shape, False, True), ("CTRL-3-MATCH", r18_recips_ctrl3, r18_cmc_ctrl3, True, False), ): r18_views = [] for rev_rec in (False, True): for rev_cmc in (False, True): p_r18, _ = build_valid_gate28a_reference_bundle( r13_store, list(reversed(r18_clin_rows)) if rev_rec else r18_clin_rows, cohort_id=f"COHORT-R18-B1-{r18_tag}-{int(rev_rec)}-{int(rev_cmc)}", lot_to_mcb_manifest=r18_mcb, lot_to_wcb_manifest=r18_wcb, parallel_cmc_lot_ledger=list(reversed(r18_cmc_rows)) if rev_cmc else r18_cmc_rows, ) res_r18 = evaluate_gate28a_h1_promotion(p_r18, trusted_clinical_store=r13_store) r18_views.append({fk: res_r18.get(fk) for fk in r18_compare_fields}) if any(rv != r18_views[0] for rv in r18_views[1:]): raise ValueError(f"R18 B1 primary recipient binding order dependence for {r18_tag}: {r18_views}") v18 = r18_views[0] if expect_promote: if not ( v18["status"] == "PROMOTE_H1_88_TO_90" and v18["promotion_authorized"] is True and v18["external_clinical_store_verified"] is True and v18["lot_bank_hierarchy_valid"] is True and v18["lot_to_mcb_manifest_verified"] is True and v18["lot_to_wcb_manifest_verified"] is True and v18["parallel_cmc_ledger_verified"] is True and v18["icc_sensitivity_report_attached"] is True and v18["icc_sensitivity_report"] is not None and v18["k"] == 119 and v18["n"] == 119 and v18["deff"] is not None and v18["n_eff"] is not None and v18["k_eff"] is not None and v18["failure_reasons"] == [] ): raise ValueError(f"R18 B1 consistent 3-recipient control failed: {v18}") else: if not ( v18["status"] == "HOLD_H1_AT_88PCT" and v18["promotion_authorized"] is False and v18["external_clinical_store_verified"] is False and v18["lot_bank_hierarchy_valid"] is False and v18["lot_to_mcb_manifest_verified"] is False and v18["lot_to_wcb_manifest_verified"] is False and v18["parallel_cmc_ledger_verified"] is False and v18["icc_sensitivity_report_attached"] is False and v18["icc_sensitivity_report"] is None and v18["deff"] is None and v18["n_eff"] is None and v18["k_eff"] is None and any("CMC_PRIMARY_RECIPIENT_BINDING_CONFLICT" in fr and "'L01'" in fr for fr in v18["failure_reasons"]) and ( (not expect_shape_l00) or any("Lot 'L00'" in fr or "SPLIT_LOT 'L00'" in fr for fr in v18["failure_reasons"]) ) ): raise ValueError(f"R18 B1 contradictory primary recipient binding nullification failed ({r18_tag}): {v18}") cases.append({"case": "b1_r18_gate28a_contradictory_primary_recipient_bindings_full_nullification_all_4_orders", "passed": True}) return {"total_cases": len(cases), "all_passed": True, "cases": cases} # ============================================================================= # PART B — MODE-B CONSTRUCT ASSEMBLY & FULL 4-POINT PROTEIN GATE (P1-P4) # ============================================================================= def load_swissprot() -> tuple[dict[str, str], dict[int, set[str]], str]: gz_sha = sha256_file(SWISSPROT_GZ) if not (gz_sha == PINNED_SWISSPROT_SHA): raise ValueError(f"Swiss-Prot SHA mismatch: {gz_sha}") prots: dict[str, str] = {} cid, buf = None, [] with gzip.open(SWISSPROT_GZ, "rt", encoding="utf-8") as fh: for ln in fh: ln = ln.strip() if not ln: continue if ln.startswith(">"): if cid is not None: prots[cid] = "".join(buf) parts = ln[1:].split()[0].split("|") cid = parts[1] if len(parts) >= 3 else ln[1:].split()[0] buf = [] else: buf.append(ln) if cid is not None: prots[cid] = "".join(buf) ksets = {k: set() for k in (8, 9, 10, 11, 15)} for s in prots.values(): n = len(s) for k in (8, 9, 10, 11, 15): sk = ksets[k] for i in range(n - k + 1): sk.add(s[i : i + k]) return prots, ksets, gz_sha def find_ir_ge(seq: str, k: int = 16) -> list[tuple[int, int, str]]: s = seq.upper() rc_map = str.maketrans("ACGT", "TGCA") n = len(s) idx: dict[str, list[int]] = {} hits: list[tuple[int, int, str]] = [] for i in range(n - k + 1): sub = s[i : i + k] rc = sub.translate(rc_map)[::-1] if rc in idx: for p in idx[rc]: if i - p >= k: hits.append((p, i, sub)) if len(hits) >= 20: return hits idx.setdefault(sub, []).append(i) return hits def find_dr_ge(seq: str, k: int = 20, disjoint: bool = True) -> list[tuple[int, int, str]]: s = seq.upper() n = len(s) idx: dict[str, int] = {} hits: list[tuple[int, int, str]] = [] for i in range(n - k + 1): sub = s[i : i + k] if sub in idx: p = idx[sub] if (not disjoint) or (i - p >= k): hits.append((p, i, sub)) if len(hits) >= 30: return hits else: idx[sub] = i return hits def max_repeat_outside_canonical(seq: str, canonical_exempt_spans: list[tuple[int, int]]) -> dict[str, Any]: s = seq.upper() n = len(s) full_dr, full_motif = l2._max_disjoint_dr(s) full_ir, full_ir_motif = l2._max_disjoint_ir(s) best_out = (0, -1, -1, "") for k in range(50, 10, -1): seen: dict[str, list[int]] = {} found = None for i in range(n - k + 1): sub = s[i : i + k] if sub in seen: for p in seen[sub]: if i - p >= k: same_exempt = any( (s0 <= p and p + k <= e0 and s0 <= i and i + k <= e0) for s0, e0 in canonical_exempt_spans ) if not same_exempt: found = (k, p + 1, i + 1, sub) break if found: break seen.setdefault(sub, []).append(i) if found: best_out = found break return { "max_disjoint_ir_bp": int(full_ir), "max_ir_motif": str(full_ir_motif), "max_disjoint_dr_full_bp": int(full_dr), "max_dr_full_motif": str(full_motif), "max_disjoint_dr_outside_canonical_bp": best_out[0], "max_dr_outside_positions_1based": [best_out[1], best_out[2]] if best_out[0] else [], "max_dr_outside_motif": best_out[3], } def harden_polya_atg_and_acceptors(pa_seq: str) -> str: s = list(pa_seq.upper()) n = len(s) prot = [False] * n for mot in ("AATAAA", "ATTAAA"): st = 0 while True: idx = "".join(s).find(mot, st) if idx == -1: break for j in range(idx, idx + 6): prot[j] = True st = idx + 1 for _ in range(12): idx = "".join(s).find("ATG") if idx == -1: break if not prot[idx + 1]: s[idx + 1] = "C" elif not prot[idx]: s[idx] = "C" else: s[idx + 2] = "C" for _ in range(30): cur = "".join(s) sp = l2.scan_donors_and_acceptors_both_strands(cur, cargo_bounds_1based=(1, n), min_donor_cv=70.0, min_acc_idx=68.0) plus_a = [a for a in sp["acceptors"] if a["strand_relative_to_input"] == "+"] plus_d = [d for d in sp["donors"] if d["strand_relative_to_input"] == "+"] if not plus_a and not plus_d: break changed = False for a in plus_a: g_pos = a["exon_start_1based_on_input_plus"] - 2 a_pos = g_pos - 1 if 0 <= g_pos < n and not prot[g_pos]: s[g_pos] = "C" changed = True break elif 0 <= a_pos < n and not prot[a_pos]: s[a_pos] = "C" changed = True break if changed: continue for d in plus_d: