failures.append("FAIL_ENSC_CURRENT_ASSAY_SCOPE_AUTHORITY_CONTRADICTION") else: cur_clinical_ok = True cur_bank_ok = True elif scope_cur == "BANK_QUALIFICATION_ONLY": if dec_cur != "STAGE_LIMITED_BANK_QUALIFIED" or bank_cur is not True or promo_cur is not False: failures.append("FAIL_ENSC_CURRENT_ASSAY_SCOPE_AUTHORITY_CONTRADICTION") else: cur_bank_ok = True # Reconcile ALL governing EnSC release measurements strictly from verified GATE-35-ENSC archive # via the common study-level alias validators shared identically with GP2 (Audit R6-03 / R9-05 / R12-05)! arch_internal_conflict = False arch_incomplete = False caller_vs_arch_conflict = bool( caller_roster_or_id_conflict or caller_wk_res["hard_fail"] or caller_dose_res["hard_fail"] or caller_n_res["hard_fail"] or caller_ev_res["hard_fail"] or caller_host_res["hard_fail"] ) arch_ev_res = _audit_study_level_int_aliases( arch_cur, role="ARCHIVE", gate_tag="ENSC", field_tag="TERATOMA_EVENTS", alias_keys=STUDY_TERATOMA_EVENTS_ALIASES, min_val=0, allow_zero=True, nonzero_event_fail_code="FAIL_ENSC_CURRENT_ASSAY_TERATOMA_DETECTED", below_min_fail_code="FAIL_ENSC_CURRENT_ASSAY_INVALID_TERATOMA_EVENTS", missing_hold_code="HOLD_ENSC_CURRENT_ASSAY_MISSING_TERATOMA_EVENTS", invalid_fail_code="FAIL_ENSC_CURRENT_ASSAY_INVALID_TERATOMA_EVENTS", holds=holds, failures=failures, ) ec_int, ec_inc, ec_cal = _reconcile_study_level_int_archive_and_caller( arch_res=arch_ev_res, caller_res=caller_ev_res, gate_tag="ENSC", field_tag="TERATOMA_EVENTS", mismatch_fail_code="FAIL_ENSC_CURRENT_ASSAY_TERATOMA_EVENTS_MISMATCH", holds=holds, failures=failures, ) arch_internal_conflict = arch_internal_conflict or ec_int arch_incomplete = arch_incomplete or ec_inc caller_vs_arch_conflict = caller_vs_arch_conflict or ec_cal ac_ev = arch_ev_res["resolved_val"] arch_n_res = _audit_study_level_int_aliases( arch_cur, role="ARCHIVE", gate_tag="ENSC", field_tag="TERATOMA_N", alias_keys=STUDY_TERATOMA_N_ALIASES, min_val=20, allow_zero=False, below_min_fail_code="FAIL_ENSC_CURRENT_ASSAY_INVALID_TERATOMA_N", missing_hold_code="HOLD_ENSC_CURRENT_ASSAY_MISSING_TERATOMA_N", invalid_fail_code="FAIL_ENSC_CURRENT_ASSAY_INVALID_TERATOMA_N", holds=holds, failures=failures, ) nc_int, nc_inc, nc_cal = _reconcile_study_level_int_archive_and_caller( arch_res=arch_n_res, caller_res=caller_n_res, gate_tag="ENSC", field_tag="TERATOMA_N", mismatch_fail_code="FAIL_ENSC_CURRENT_ASSAY_TERATOMA_N_MISMATCH", holds=holds, failures=failures, ) arch_internal_conflict = arch_internal_conflict or nc_int arch_incomplete = arch_incomplete or nc_inc caller_vs_arch_conflict = caller_vs_arch_conflict or nc_cal ac_n = arch_n_res["resolved_val"] if _is_strict_int(ac_n) and ac_n > 0 and _is_strict_int(ac_ev) and ac_ev > ac_n: failures.append("FAIL_ENSC_CURRENT_ASSAY_IMPOSSIBLE_TERATOMA_EVENTS_GT_N") arch_internal_conflict = True # Common study-level follow-up alias validator (Audit R12-05; shared identically with GP2!) arch_wk_res = _audit_study_level_followup_aliases( arch_cur, role="ARCHIVE", gate_tag="ENSC", min_weeks=26.0, alias_keys=STUDY_TERATOMA_FOLLOWUP_ALIASES, holds=holds, failures=failures, ) wc_int, wc_inc, wc_cal = _reconcile_study_level_followup_archive_and_caller( arch_res=arch_wk_res, caller_res=caller_wk_res, gate_tag="ENSC", holds=holds, failures=failures, ) arch_internal_conflict = arch_internal_conflict or wc_int arch_incomplete = arch_incomplete or wc_inc caller_vs_arch_conflict = caller_vs_arch_conflict or wc_cal arch_host_res = _audit_study_level_host_aliases( arch_cur, role="ARCHIVE", gate_tag="ENSC", required_host="SCID-Beige", alias_keys=STUDY_TERATOMA_HOST_ALIASES, wrong_host_fail_code="FAIL_ENSC_CURRENT_ASSAY_WRONG_HOST_STRAIN", invalid_host_fail_code="FAIL_ENSC_CURRENT_ASSAY_WRONG_HOST_STRAIN", missing_hold_code="HOLD_ENSC_CURRENT_ASSAY_MISSING_TUMOR_HOST_STRAIN", holds=holds, failures=failures, ) hc_int, hc_inc, hc_cal = _reconcile_study_level_host_archive_and_caller( arch_res=arch_host_res, caller_res=caller_host_res, gate_tag="ENSC", mismatch_fail_code="FAIL_ENSC_CURRENT_ASSAY_HOST_STRAIN_MISMATCH", holds=holds, failures=failures, ) arch_internal_conflict = arch_internal_conflict or hc_int arch_incomplete = arch_incomplete or hc_inc caller_vs_arch_conflict = caller_vs_arch_conflict or hc_cal arch_dose_res = _audit_study_level_int_aliases( arch_cur, role="ARCHIVE", gate_tag="ENSC", field_tag="DOSE", alias_keys=STUDY_TERATOMA_DOSE_ALIASES, min_val=10_000_000, allow_zero=False, below_min_fail_code="FAIL_ENSC_CURRENT_ASSAY_CHALLENGE_DOSE_LT_1E7", missing_hold_code="HOLD_ENSC_CURRENT_ASSAY_MISSING_TUMOR_CHALLENGE_DOSE_CELLS", invalid_fail_code="FAIL_ENSC_CURRENT_ASSAY_INVALID_TUMOR_CHALLENGE_DOSE_CELLS", holds=holds, failures=failures, ) dc_int, dc_inc, dc_cal = _reconcile_study_level_int_archive_and_caller( arch_res=arch_dose_res, caller_res=caller_dose_res, gate_tag="ENSC", field_tag="DOSE", mismatch_fail_code="FAIL_ENSC_CURRENT_ASSAY_CHALLENGE_DOSE_MISMATCH", holds=holds, failures=failures, ) arch_internal_conflict = arch_internal_conflict or dc_int arch_incomplete = arch_incomplete or dc_inc caller_vs_arch_conflict = caller_vs_arch_conflict or dc_cal if "teratoma_lot_id" in arch_cur: if arch_cur["teratoma_lot_id"] is None: holds.append("HOLD_ENSC_CURRENT_ASSAY_MISSING_TERATOMA_LOT_ID") arch_incomplete = True elif not _is_nonempty_str(arch_cur["teratoma_lot_id"]) or (valid_lot is not None and arch_cur["teratoma_lot_id"] != valid_lot): failures.append("FAIL_ENSC_CURRENT_ASSAY_TERATOMA_LOT_ID_MISMATCH") arch_internal_conflict = True if t_lot is None or not _is_nonempty_str(t_lot) or (valid_lot is not None and t_lot != valid_lot): caller_vs_arch_conflict = True # Mandatory archived per-animal teratoma roster validation strictly on its own (zero caller data!), # followed by separate caller reconciliation and strict 5-state U95 derivation/labeling (Audit R7-03 / R8-07 / R8-08 / R9-05 / R12-05)! arch_rosters: list[tuple[str, int, int, int, bool, dict[str, dict[str, Any]], set[str], dict[str, str], bool]] = [] arch_id_lists: list[tuple[str, int, set[str], dict[str, str]]] = [] for rk in _ensc_roster_keys: if rk in arch_cur: if arch_cur[rk] is None: arch_incomplete = True if "HOLD_INCOMPLETE_ARCHIVED_PER_ANIMAL_ROSTER_ALIAS_NULL" not in holds: holds.append("HOLD_INCOMPLETE_ARCHIVED_PER_ANIMAL_ROSTER_ALIAS_NULL") continue ad_n, ad_ev, ad_unk, ad_miss, ad_recs, ad_ids, ad_raw, ad_int_fail = _audit_ensc_teratoma_roster( arch_cur[rk], expected_n=ac_n, expected_ev=ac_ev, expected_lot=valid_lot, prefix="ARCHIVE_", ) arch_rosters.append((rk, ad_n, ad_ev, ad_unk, ad_miss, ad_recs, ad_ids, ad_raw, ad_int_fail)) if ad_int_fail: arch_internal_conflict = True if ad_miss or ad_unk > 0: arch_incomplete = True for ik in _ensc_id_list_keys: if ik in arch_cur: if arch_cur[ik] is None: arch_incomplete = True if "HOLD_INCOMPLETE_ARCHIVED_ANIMAL_ID_LIST_ALIAS_NULL" not in holds: holds.append("HOLD_INCOMPLETE_ARCHIVED_ANIMAL_ID_LIST_ALIAS_NULL") continue aid_fails: list[str] = [] ai_n, ai_ids, ai_raw = _audit_canonical_id_list( arch_cur[ik], min_distinct=20, expected_count=ac_n, label_prefix="ENSC_ARCHIVE_ANIMAL", failures=aid_fails, ) for af in aid_fails: if af == "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" and af in failures: continue failures.append(af) if aid_fails: arch_internal_conflict = True if ai_ids is not None: arch_id_lists.append((ik, ai_n, ai_ids, ai_raw)) # Reconcile study-level follow-up against archive per-animal follow-up floor (Audit R12-05) if _reconcile_study_followup_against_animal_floor( study_res=arch_wk_res, animal_rosters=[r[5] for r in arch_rosters], role="ARCHIVE", gate_tag="ENSC", failures=failures, ): arch_internal_conflict = True # Reconcile archive ID-list aliases against one another strictly on archive data if len(arch_id_lists) >= 2: ref_ai_ids, ref_ai_raw = arch_id_lists[0][2], arch_id_lists[0][3] for il in arch_id_lists[1:]: if il[2] != ref_ai_ids: failures.append("FAIL_ENSC_ARCHIVE_ROSTER_ID_ALIAS_CONFLICT") arch_internal_conflict = True elif any(il[3].get(cid) != ref_ai_raw.get(cid) for cid in ref_ai_ids): if "FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID" not in failures: failures.append("FAIL_NONUNIQUE_CANONICAL_EQUIVALENCE_ID") failures.append("FAIL_ENSC_ARCHIVE_ROSTER_ID_ALIAS_CONFLICT") arch_internal_conflict = True # Compute provisional aggregate-only U95 estimate (strictly isolated ONLY in Case 2 HOLD when archived roster is missing/incomplete) agg_n_cands = [x for x in (t_n, ac_n) if _is_strict_int(x) and 0 < x <= MAX_CLONOGENIC_CELLS] agg_ev_cands = [x for x in (t_ev, ac_ev) if _is_strict_int(x) and x >= 0] if agg_n_cands and agg_ev_cands: eff_agg_n = min(agg_n_cands) eff_agg_ev = max(agg_ev_cands) if 0 < eff_agg_n <= MAX_CLONOGENIC_CELLS and 0 <= eff_agg_ev <= eff_agg_n: provisional_agg_u95_pct = _safe_round_finite(clopper_pearson_u95(eff_agg_ev, eff_agg_n) * 100.0, 4) else: provisional_agg_u95_pct = None if not arch_rosters: holds.append("HOLD_ENSC_MISSING_ARCHIVED_PER_ANIMAL_TERATOMA_ROSTER") u95_pct = None ensc_distinct_animals = None ensc_derived_events = None if arch_internal_conflict: provisional_agg_u95_pct = None u95_derivation_source = "WITHHELD_INTERNAL_ARCHIVE_CONFLICT"