scripts/verify_iip0035_d1b_auto_and_portfolio.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/verify_iip0035_d1b_auto_and_portfolio.py · SHA-256: fedb990aeaedd116…
elif geno_ok is not None and not isinstance(geno_ok, bool):
invalid_or_oob_keys.append("d1_01_4digit_hla_abc_kir_genotyping_verified")
geno_ok = None
elif geno_ok is False:
reasons.append("FAIL_D1_01_4DIGIT_HLA_ABC_KIR_GENOTYPING_MISSING")
d1_01_exclusion_detected_from_raw = False
if parsed_hlac_alleles is not None:
norm_a = parsed_hlac_alleles
d1_01_allele_derivation_mode = "DERIVED_FROM_SUBMITTED_4DIGIT_HLA_C_ALLELES"
both_nonfunc = all(a in _SP3C4C_NONFUNC_HLAC_ALLELES for a in norm_a)
any_confirmed_vl9 = any(a in _SP1C2C_VL9_COMPETENT_HLAC_ALLELES for a in norm_a)
if both_nonfunc:
d1_01_exclusion_detected_from_raw = True
is_homo = (norm_a[0] == norm_a[1])
derived_cls = (
"EXCLUDED_SP3C4C_HLAC07_17_HOMOZYGOTE_ROUTE_TO_ALLO_Y_OR_D1A"
if (is_homo and norm_a[0] != "HLA-C*18:01")
else "EXCLUDED_SP3C4C_HLAC07_17_1801_NONFUNCTIONAL_HOMO_OR_COMPOUND_HET_ROUTE_TO_ALLO_Y_OR_D1A"
)
matching_excl = (
{"EXCLUDED_SP3C4C_HLAC07_17_HOMOZYGOTE_ROUTE_TO_ALLO_Y_OR_D1A", "EXCLUDED_SP3C4C_HLAC07_17_1801_NONFUNCTIONAL_HOMO_OR_COMPOUND_HET_ROUTE_TO_ALLO_Y_OR_D1A"}
if (is_homo and norm_a[0] != "HLA-C*18:01")
else {"EXCLUDED_SP3C4C_HLAC07_17_1801_NONFUNCTIONAL_HOMO_OR_COMPOUND_HET_ROUTE_TO_ALLO_Y_OR_D1A"}
)
# Raw SP-3C/SP-4C nonfunctional pair ALWAYS enforces exclusion FAIL (even if hlac_cls is None or "UNAVAILABLE")
reasons.append("FAIL_D1_01_EXCLUDED_AH8P1_SP3C4C_NONFUNCTIONAL_HLAC_ROUTE_TO_ALLO_Y_OR_D1A")
if not hlac_cls_present:
hlac_cls = derived_cls
elif hlac_cls is None or (isinstance(hlac_cls, str) and hlac_cls == "UNAVAILABLE"):
d1_01_allele_derivation_mode = "RAW_EXCLUSION_FAIL_WITH_EXPLICIT_NULL_OR_UNAVAILABLE_CLASS"
elif isinstance(hlac_cls, str) and hlac_cls not in matching_excl:
reasons.append("FAIL_D1_01_ALLELE_CLASS_MISMATCH_WITH_RAW_HLA_C_GENOTYPE")
elif any_confirmed_vl9:
derived_cls = "ELIGIBLE_VL9_COMPETENT_HLAC"
if not hlac_cls_present:
hlac_cls = derived_cls
elif isinstance(hlac_cls, str) and hlac_cls != "UNAVAILABLE" and hlac_cls != derived_cls:
reasons.append("FAIL_D1_01_ALLELE_CLASS_MISMATCH_WITH_RAW_HLA_C_GENOTYPE")
else:
d1_01_allele_derivation_mode = "UNCLASSIFIED_HLA_C_ALLELE_OUTSIDE_LIN2023_TABLE1_HOLD"
reasons.append("HOLD_D1_01_UNCLASSIFIED_HLA_C_SIGNAL_PEPTIDE_ALLELE_REQUIRES_IPD_IMGT_VERIFICATION")
if isinstance(hlac_cls, str) and hlac_cls in EXCLUDED_HLAC_SP3C4C_CLASSES:
reasons.append("FAIL_D1_01_ALLELE_CLASS_MISMATCH_WITH_RAW_HLA_C_GENOTYPE")
d1_01_unknown_cls = False
if isinstance(hlac_cls, str) and hlac_cls == "UNAVAILABLE":
explicitly_unavailable.append("d1_auto_hlac_sp3c4c_genotype_class")
hlac_cls = None
elif hlac_cls is not None and not isinstance(hlac_cls, str):
invalid_or_oob_keys.append("d1_auto_hlac_sp3c4c_genotype_class")
hlac_cls = None
elif hlac_cls is not None and hlac_cls not in ALLOWED_HLAC_SP3C4C_CLASSES:
d1_01_unknown_cls = True
reasons.append("HOLD_D1_01_UNKNOWN_HLAC_SP3C4C_GENOTYPE_CLASS")
elif hlac_cls in EXCLUDED_HLAC_SP3C4C_CLASSES:
reasons.append("FAIL_D1_01_EXCLUDED_AH8P1_SP3C4C_NONFUNCTIONAL_HLAC_ROUTE_TO_ALLO_Y_OR_D1A")
if geno_ok is None or hlac_cls is None or d1_01_unknown_cls:
reasons.append(cell_mdd.get("D1-01", "HOLD_D1_01_MISSING_GENOTYPING_OR_UNKNOWN_HLAC_CLASS"))
if parsed_hlac_alleles is None and hlac_cls is None and d1_01_raw_hla_c_input_status != "MALFORMED":
d1_01_allele_derivation_mode = "MISSING_OR_UNAVAILABLE_GENOTYPE_CLASS_HOLD"
elif (
parsed_hlac_alleles is not None
and hlac_cls is None
and not d1_01_exclusion_detected_from_raw
and d1_01_allele_derivation_mode == "DERIVED_FROM_SUBMITTED_4DIGIT_HLA_C_ALLELES"
):
d1_01_allele_derivation_mode = "EXPLICIT_NULL_OR_UNAVAILABLE_BLOCKED_DERIVATION_HOLD"
# D1-02 (COMPOUND_ALL_PASS: requires BOTH caller-attested phased WGS == True AND submitted min mismatches in 3..25 plus all 3 numeric WGS readouts)
wgs_ok = observations.get("d1_02_n1_phased_wgs_offgrid_guard_011_cleared")
min_mm_hlac_e = _strict_int("obs_d1_02_min_mismatches_to_patient_hlac_e")
_min_mm_max = int(thr.get("min_mm_max", 25))
if min_mm_hlac_e is not None and min_mm_hlac_e > _min_mm_max:
invalid_or_oob_keys.append("obs_d1_02_min_mismatches_to_patient_hlac_e")
min_mm_hlac_e = None
if isinstance(wgs_ok, str) and wgs_ok == "UNAVAILABLE":
explicitly_unavailable.append("d1_02_n1_phased_wgs_offgrid_guard_011_cleared")
wgs_ok = None
elif wgs_ok is not None and not isinstance(wgs_ok, bool):
invalid_or_oob_keys.append("d1_02_n1_phased_wgs_offgrid_guard_011_cleared")
wgs_ok = None
elif wgs_ok is False:
reasons.append("FAIL_D1_02_N1_PHASED_WGS_OFFGRID_GUARD_011_HLAC_E_MISMATCH_CONFLICT")
if min_mm_hlac_e is not None and min_mm_hlac_e <= thr.get("min_mm_reject_le", 2):
reasons.append("FAIL_D1_02_N1_PHASED_WGS_OFFGRID_GUARD_011_HLAC_E_MISMATCH_CONFLICT")
wt_hlac = _dec("obs_d1_02_wt_hlac_allele_retention_pct")
bystander = _dec("obs_d1_02_hlac_bystander_ct_pct")
chr6_del = _dec("obs_d1_02_chr6p21_interlocus_del_pct")
if wt_hlac is not None and wt_hlac < thr.get("wt_hlac_min", Decimal("99.0")):
reasons.append("FAIL_D1_02_WT_HLAC_ALLELE_RETENTION_BELOW_99PCT")
if bystander is not None and bystander >= thr.get("bystander_lt", Decimal("0.5")):
reasons.append("FAIL_D1_02_HLAC_BYSTANDER_CT_DEAMINATION_GE_0P5PCT")
if chr6_del is not None and chr6_del > thr.get("chr6_del_eq", Decimal("0")):
reasons.append("FAIL_D1_02_CHR6P21_INTERLOCUS_DELETION_DETECTED")
wgs_ok_effective = (
wgs_ok is True
and min_mm_hlac_e is not None
and min_mm_hlac_e > thr.get("min_mm_reject_le", 2)
)
d1_02_offgrid_guard_evidence_mode = (
"CALLER_ATTESTED_PHASED_WGS_AND_SUBMITTED_MISMATCH_COUNT_COMPOUND_ALL_PASS"
if wgs_ok_effective
else (
"FAILED_D1_02_PHASED_WGS_OR_MIN_MISMATCH"
if (wgs_ok is False or (min_mm_hlac_e is not None and min_mm_hlac_e <= thr.get("min_mm_reject_le", 2)))
else "INCOMPLETE_D1_02_PHASED_WGS_OR_MIN_MISMATCH_HOLD"
)
)
if any(x is None for x in (wgs_ok, min_mm_hlac_e, wt_hlac, bystander, chr6_del)):
reasons.append(cell_mdd.get("D1-02", "HOLD_D1_02_INCOMPLETE_PHASED_WGS_QC"))
# D1-03 (requires head-to-head ADSC vs blood CiPSC comparator attestation matching Playbook §4.1.20)
cipsc_src = observations.get("d1_auto_cipsc_source_arm")
h2h_ok = observations.get("d1_03_head_to_head_adsc_vs_blood_comparator_attested")
bcor_ok = observations.get("d1_03_adsc_vs_blood_bcor_tp53_ppm1d_wgs_clean")
d1_03_unknown_src = False
if isinstance(cipsc_src, str) and cipsc_src == "UNAVAILABLE":
explicitly_unavailable.append("d1_auto_cipsc_source_arm")
cipsc_src = None
elif cipsc_src is not None and not isinstance(cipsc_src, str):
invalid_or_oob_keys.append("d1_auto_cipsc_source_arm")
cipsc_src = None
elif cipsc_src is not None and cipsc_src not in ALLOWED_CIPSC_SOURCES:
d1_03_unknown_src = True
reasons.append("HOLD_D1_03_UNKNOWN_CIPSC_SOURCE_ARM")
elif cipsc_src == "SUN_EXPOSED_DERMAL_FIBROBLAST_DISCOURAGED":
reasons.append("FAIL_D1_03_SUN_EXPOSED_FIBROBLAST_UV_BURDEN_BARRED")
if isinstance(h2h_ok, str) and h2h_ok == "UNAVAILABLE":
explicitly_unavailable.append("d1_03_head_to_head_adsc_vs_blood_comparator_attested")
h2h_ok = None
elif h2h_ok is not None and not isinstance(h2h_ok, bool):
invalid_or_oob_keys.append("d1_03_head_to_head_adsc_vs_blood_comparator_attested")
h2h_ok = None
elif h2h_ok is False:
reasons.append("FAIL_D1_03_HEAD_TO_HEAD_ADSC_VS_BLOOD_COMPARATOR_NOT_MET")
if h2h_ok is None:
reasons.append("HOLD_D1_03_HEAD_TO_HEAD_ADSC_VS_BLOOD_COMPARATOR_REQUIRED")
if isinstance(bcor_ok, str) and bcor_ok == "UNAVAILABLE":
explicitly_unavailable.append("d1_03_adsc_vs_blood_bcor_tp53_ppm1d_wgs_clean")
bcor_ok = None
elif bcor_ok is not None and not isinstance(bcor_ok, bool):
invalid_or_oob_keys.append("d1_03_adsc_vs_blood_bcor_tp53_ppm1d_wgs_clean")
bcor_ok = None
elif bcor_ok is False:
reasons.append("FAIL_D1_03_BCOR_TP53_PPM1D_OR_20Q11_1Q32_CLONAL_HIT_DETECTED")
chrm_depth = _dec("obs_d1_03_chrm_numt_amplicon_depth_x")
chrm_het = _dec("obs_d1_03_chrm_novel_nonsyn_het_pct")
if chrm_depth is not None and chrm_depth < thr.get("chrm_depth_min", Decimal("1000.0")):
reasons.append("FAIL_D1_03_CHRM_NUMT_AMPLICON_DEPTH_BELOW_1000X")
if chrm_het is not None and chrm_het >= thr.get("chrm_het_lt", Decimal("1.0")):
reasons.append("FAIL_D1_03_CHRM_NOVEL_NONSYN_HETEROPLASMY_GE_1PCT")
if any(x is None for x in (cipsc_src, h2h_ok, bcor_ok, chrm_depth, chrm_het)) or d1_03_unknown_src:
reasons.append(cell_mdd.get("D1-03", "HOLD_D1_03_INCOMPLETE_WGS_OR_CHRM_QC"))
# D1-04
chr13_ok = observations.get("d1_04_chr13_clybl_tandem_404bp_independent_from_chr19_verified")
if isinstance(chr13_ok, str) and chr13_ok == "UNAVAILABLE":
explicitly_unavailable.append("d1_04_chr13_clybl_tandem_404bp_independent_from_chr19_verified")
chr13_ok = None
elif chr13_ok is not None and not isinstance(chr13_ok, bool):
invalid_or_oob_keys.append("d1_04_chr13_clybl_tandem_404bp_independent_from_chr19_verified")
chr13_ok = None
elif chr13_ok is False:
reasons.append("FAIL_D1_04_CHR13_CLYBL_TANDEM_INDEPENDENCE_FROM_CHR19_UNVERIFIED")
azt_chr19_loss_ok = observations.get("d1_04_azt_kill_measured_after_forced_chr19_loss")
if isinstance(azt_chr19_loss_ok, str) and azt_chr19_loss_ok == "UNAVAILABLE":
explicitly_unavailable.append("d1_04_azt_kill_measured_after_forced_chr19_loss")
azt_chr19_loss_ok = None
elif azt_chr19_loss_ok is not None and not isinstance(azt_chr19_loss_ok, bool):
invalid_or_oob_keys.append("d1_04_azt_kill_measured_after_forced_chr19_loss")
azt_chr19_loss_ok = None
elif azt_chr19_loss_ok is False:
reasons.append("FAIL_D1_04_AZT_KILL_NOT_MEASURED_AFTER_FORCED_CHR19_LOSS")
if azt_chr19_loss_ok is None:
reasons.append("HOLD_D1_04_AZT_KILL_FORCED_CHR19_LOSS_CONDITION_REQUIRED")
htmpk_leak = _dec("obs_d1_04_g0_beta_htmpk_leak_pct")
elispot = _dec("obs_d1_04_hlac07_ryafsgvay_elispot_above_bg_pct")
ap_surv = _dec("obs_d1_04_dec053_ap1903_icasp9_survivor_frac")
ap_k = _strict_int("obs_d1_04_ap1903_survivor_count_k")
ap_n_raw_key = _strict_int("obs_d1_04_ap1903_raw_trials_n")
ap_n_eff_key = _strict_int("obs_d1_04_ap1903_effective_trials_n")
if ap_n_raw_key is not None and ap_n_eff_key is not None and ap_n_raw_key != ap_n_eff_key:
reasons.append("FAIL_D1_04_CONFLICTING_RAW_AND_EFFECTIVE_TRIALS_N")
ap_n = ap_n_raw_key if ap_n_raw_key is not None else ap_n_eff_key
azt_kill = _dec("obs_d1_04_dec053_azt_dividing_log10_kill")
ap_u95_cap = thr.get("ap1903_u95_max", Decimal("1e-8"))
if htmpk_leak is not None and htmpk_leak > thr.get("htmpk_leak_max", Decimal("0.1")):
reasons.append("FAIL_D1_04_G0_BETA_HTMPK_F105Y_LEAK_EXCEEDS_0P1PCT")
if elispot is not None and elispot > thr.get("elispot_max", Decimal("0.1")):
reasons.append("FAIL_D1_04_HLAC07_RYAFSGVAY_ELISPOT_EXCEEDS_0P1PCT_G0_GATE")
if ap_surv is not None and ap_surv > ap_u95_cap:
reasons.append("FAIL_D1_04_DEC053_AP1903_ICASP9_SURVIVOR_FRAC_EXCEEDS_1E_8")
_ap_cal_kw = _cal_kwargs("obs_d1_04_ap1903")
ap_cp: Dict[str, Any] | None = None
if ap_k is None or ap_n is None or ap_n < 1 or ap_k > ap_n:
if ap_k is not None and ap_k >= 1:
reasons.append("FAIL_HARD_STOP_D1_04_AP1903_SURVIVOR_DETECTED")
else:
reasons.append("HOLD_D1_04_AP1903_GY018_INTEGER_KN_REQUIRED")
else:
ap_cp = evaluate_sound_decimal_clopper_pearson_zero_event(
gate_id="D1_04_AP1903_ICASP9",
k=ap_k,
n_raw=ap_n,
max_u95_fraction=ap_u95_cap,
**_ap_cal_kw,
)
if ap_cp["status"] == "FAIL_HARD_STOP":
reasons.append("FAIL_HARD_STOP_D1_04_AP1903_SURVIVOR_DETECTED")
elif ap_cp["status"] != "PASS":
reasons.append(
"HOLD_D1_04_AP1903_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
if ap_cp["status"] == "HOLD_INCONCLUSIVE_INSUFFICIENT_INDEPENDENT_DENOMINATOR"
else f"HOLD_D1_04_AP1903_{ap_cp['status']}"
)
if ap_surv is not None:
with _clean_dec_ctx(40):
ap_expected = Decimal(ap_k) / Decimal(ap_n)
ap_tol = max(Decimal("0.05") * ap_expected, Decimal("1e-15"))
if (
(ap_k == 0 and ap_surv != Decimal(0))
or (ap_k > 0 and ap_surv <= Decimal(0))
or (ap_k > 0 and abs(ap_surv - ap_expected) > ap_tol)
):
reasons.append("FAIL_D1_04_AP1903_SURVIVOR_FRAC_INCONSISTENT_WITH_KN")
if azt_kill is not None and azt_kill < thr.get("azt_kill_min", Decimal("4.0")):
reasons.append("FAIL_D1_04_DEC053_AZT_DIVIDING_KILL_BELOW_4LOG_AFTER_CHR19_LOSS")
if any(x is None for x in (chr13_ok, azt_chr19_loss_ok, htmpk_leak, elispot, azt_kill, ap_k, ap_n)):
reasons.append(cell_mdd.get("D1-04", "HOLD_D1_04_MISSING_SWITCH_OR_GY018_INTEGER_KN"))
# D1-05 (MANDATORY per-locus L3 AND L4 Stage-7 Day-28 retention; context-isolated exact min comparison!)
l3_ret = _dec("obs_d1_05_l3_day28_retention_pct")
l4_ret = _dec("obs_d1_05_l4_day28_retention_pct")
day28_ret = _dec("obs_d1_05_l3_l4_day28_retention_pct")
fsp1_surv = _dec("obs_d1_05_fsp1_ferroptosis_survival_pct")
if l3_ret is not None and l3_ret < thr.get("l3_ret_min", Decimal("80.0")):
reasons.append("FAIL_D1_05_L3_STAGE7_DAY28_RETENTION_BELOW_80PCT")
if l4_ret is not None and l4_ret < thr.get("l4_ret_min", Decimal("80.0")):
reasons.append("FAIL_D1_05_L4_STAGE7_DAY28_RETENTION_BELOW_80PCT")
if l3_ret is None or l4_ret is None or fsp1_surv is None:
reasons.append(cell_mdd.get("D1-05", "HOLD_D1_05_MISSING_PER_LOCUS_DURABILITY_OR_FSP1"))
if l3_ret is not None and l4_ret is not None:
actual_min_l3_l4 = min(l3_ret, l4_ret)
if actual_min_l3_l4 < thr.get("min_l3_l4_ret_min", Decimal("80.0")):
reasons.append("FAIL_D1_05_L3_L4_STAGE7_DAY28_RETENTION_BELOW_80PCT")
if day28_ret is not None:
_min_diff_exceeds = _abs_diff_exceeds_tol(day28_ret, actual_min_l3_l4, Decimal("1e-9"))
if _min_diff_exceeds is True:
reasons.append("FAIL_D1_05_INCONSISTENT_REPORTED_MIN_L3_L4_RETENTION")
elif _min_diff_exceeds is None: