), 'stage0_5p_3p_race_splice_junction_verified': (race_ok is True), 'hcmv_pos_nk_donor_panel_ge_6_with_zero_failing_donors': ( donor_n >= 6 and nk_fail_n == 0 ), 'ul40_reactive_ctl_donor_panel_ge_6_with_zero_failing_donors': ( ul40_resp_n >= 6 and ul40_fail_n == 0 ), 'sct_nkg2a_kd_positive_and_upper95_le_10uM': (0.001 <= vals['obs_hlae_sct_nkg2a_kd_uM'][0] and ka_u95 <= 10.0), 'sct_nkg2c_kd_positive': (vals['obs_hlae_sct_nkg2c_kd_uM'][0] >= 0.001), 'resting_hlae_hour0_lower95_clears_mode_floor': (hlae_h0_l95 >= req_h0_floor), 'resting_nkg2a_pos_nk_lysis_upper95_le_8pct': (_u95('obs_nkg2a_pos_resting_nk_lysis_pct') <= 8.0), 'ifng24h_nkg2a_pos_nk_lysis_upper95_le_8pct': (_u95('obs_nkg2a_pos_ifng24h_nk_lysis_pct') <= 8.0), 'resting_nkg2a_pos_nk_cd107a_upper95_le_10pct': (_u95('obs_nkg2a_pos_resting_cd107a_pct') <= 10.0), 'ifng24h_nkg2a_pos_nk_cd107a_upper95_le_10pct': (_u95('obs_nkg2a_pos_ifng24h_cd107a_pct') <= 10.0), 'resting_nkg2c_kir_pos_lysis_upper95_le_8pct': (_u95('obs_nkg2c_kir_pos_resting_lysis_pct') <= 8.0), 'ifng24h_nkg2c_kir_pos_lysis_upper95_le_8pct': (_u95('obs_nkg2c_kir_pos_ifng24h_lysis_pct') <= 8.0), 'resting_double_neg_nkg2c_pos_lysis_upper95_le_8pct': (_u95('obs_nkg2a_neg_kir2dl_neg_nkg2c_pos_resting_lysis_pct') <= 8.0), 'ifng24h_double_neg_nkg2c_pos_lysis_upper95_le_8pct': (_u95('obs_nkg2a_neg_kir2dl_neg_nkg2c_pos_ifng24h_lysis_pct') <= 8.0), 'resting_pooled_3subset_hcmv_nk_lysis_upper95_le_10pct': (_u95('obs_hcmv_pooled_3subset_resting_nk_lysis_pct') <= 10.0), 'ifng24h_pooled_3subset_hcmv_nk_lysis_upper95_le_10pct': (_u95('obs_hcmv_pooled_3subset_ifng24h_nk_lysis_pct') <= 10.0), 'nkg2c_cd107a_degranulation_fold_upper95_le_1p25x': (_u95('obs_nkg2c_cd107a_degranulation_fold_vs_b2m_ko') <= 1.25), 'cmv_ul40_hlae_cd8_ctl_lysis_upper95_le_5pct': (_u95('obs_cmv_ul40_hlae_cd8_lysis_pct') <= 5.0), } if floor_mode == 'LOT_RELEASE_25K': p10_pair = _read_pair('obs_hypoxic_72h_hlae_p10_single_cell_molecules', 'obs_hypoxic_72h_hlae_p10_single_cell_se') checks['release_mode_hypoxic_72h_hlae_p10_lower95_ge_15000'] = ( p10_pair is not None and (p10_pair[0] - z95 * p10_pair[1]) >= 15000.0 ) if meta['alpha3_allele'] == 'MUT_D227K_T228A': wt_ka = _read_pair('obs_wt_sct_nkg2a_kd_uM', 'obs_wt_sct_nkg2a_kd_se_uM') cd8aa = _read_pair('obs_hlae_cd8aa_kd_uM', 'obs_hlae_cd8aa_kd_se_uM') cd8ab = _read_pair('obs_hlae_cd8ab_kd_uM', 'obs_hlae_cd8ab_kd_se_uM') treg_ret = _read_pair('obs_cd8_treg_suppression_retention_pct_of_wt', 'obs_cd8_treg_suppression_retention_se') treg_del = _read_pair('obs_cd8_treg_suppression_delta_vs_wt_pp', 'obs_cd8_treg_suppression_delta_vs_wt_se') ctl_del_wt = _read_pair('obs_cmv_ul40_hlae_cd8_lysis_delta_vs_wt_pp', 'obs_cmv_ul40_hlae_cd8_lysis_delta_vs_wt_se') hla_a2_ctrl = obs.get('hla_a2_d227k_t228a_positive_control_spr_verified') checks['d227k_t228a_layer3_nkg2a_kd_upper95_le_2p5uM_and_le_1p25x_wt'] = ( wt_ka is not None and ka_u95 <= 2.5 and ka_u95 <= 1.25 * wt_ka[0] ) checks['d227k_t228a_layer3_cd8aa_and_cd8ab_kd_lower95_gt_1000uM'] = ( cd8aa is not None and cd8ab is not None and (cd8aa[0] - z95 * cd8aa[1]) > 1000.0 and (cd8ab[0] - z95 * cd8ab[1]) > 1000.0 ) checks['d227k_t228a_layer3_hla_a2_positive_control_spr_verified'] = (hla_a2_ctrl is True) checks['d227k_t228a_layer3_cd8_treg_retention_ge_80pct_and_delta_ge_minus10pp'] = ( treg_ret is not None and treg_del is not None and (treg_ret[0] - z95 * treg_ret[1]) >= 80.0 and (treg_del[0] - z95 * treg_del[1]) >= -10.0 ) checks['d227k_t228a_layer3_ul40_ctl_delta_vs_wt_upper95_le_2pp'] = ( ctl_del_wt is not None and (ctl_del_wt[0] + z95 * ctl_del_wt[1]) <= 2.0 ) if meta['requires_nonself_peptide_gate']: dq_si = _read_pair('obs_nonself_dq2_dq8_indirect_apc_si', 'obs_nonself_dq2_dq8_indirect_apc_si_se') host_ctl = _read_pair('obs_nonself_host_class1_cross_pres_lysis_pct', 'obs_nonself_host_class1_cross_pres_lysis_se') checks['nonself_seam_or_alpha3_dq2_dq8_and_class1_cleared'] = ( dq_si is not None and host_ctl is not None and (dq_si[0] + z95 * dq_si[1]) <= 1.5 and (host_ctl[0] + z95 * host_ctl[1]) <= 5.0 ) failed = [k for k, ok in checks.items() if not ok] gate_pass = len(failed) == 0 if gate_pass: if sct_ratio_mean >= 4.0: mode = 'PASS_S10_STATIC_RATIO_AND_PAIRED_FUNCTIONAL_GATE_V3' else: mode = 'PASS_S10_FUNCTIONAL_SELECTIVITY_WITH_SUB4_SCT_RATIO_RECORDED_V3' else: mode = 'HOLD_S10_FUNCTIONAL_OR_BIOPHYSICAL_GATE_V3' return { 'gate_pass': gate_pass, 'status': mode, 'construct_id': cid, 'floor_mode': floor_mode, 'recorded_sct_kd_nkg2a_upper95_uM': round(ka_u95, 4), 'recorded_sct_kd_nkg2c_lower95_uM': round(kc_l95, 4), 'recorded_sct_kc_over_ka_mean': round(sct_ratio_mean, 4), 'checks': checks, 'failed_checks': failed, } def _betacf_iip0024(a, b, x): # The binomial inversion path uses extended precision and requires verified convergence. extended = isinstance(x, Decimal) max_it = 16384 if extended else 240 eps = Decimal('1e-29') if extended else 3.0e-15 fpmin = Decimal('1e-48') if extended else 1e-300 qab = a + b qap = a + 1 qam = a - 1 c = 1 d = 1 - qab * x / qap if abs(d) < fpmin: d = fpmin d = 1 / d h = d for m in range(1, max_it + 1): m2 = 2 * m aa = m * (b - m) * x / ((qam + m2) * (a + m2)) d = 1 + aa * d if abs(d) < fpmin: d = fpmin c = 1 + aa / c if abs(c) < fpmin: c = fpmin d = 1 / d h *= d * c aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2)) d = 1 + aa * d if abs(d) < fpmin: d = fpmin c = 1 + aa / c if abs(c) < fpmin: c = fpmin d = 1 / d del_v = d * c h *= del_v if abs(del_v - 1) < eps: return h if extended: raise ArithmeticError('incomplete beta continued fraction did not converge') return h def _reg_inc_beta_iip0024(a: float, b: float, x: float) -> float: if x <= 0.0: return 0.0 if x >= 1.0: return 1.0 bt = math.exp( math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b) + a * math.log(x) + b * math.log(1.0 - x) ) if x < (a + 1.0) / (a + b + 2.0): return bt * _betacf_iip0024(a, b, x) / a return 1.0 - bt * _betacf_iip0024(b, a, 1.0 - x) / b def student_t_cdf(t_val: float, df: float) -> float: if df <= 0.0 or not math.isfinite(df) or not math.isfinite(t_val): raise ValueError('student_t_cdf requires finite positive df and finite t_val (IIP-0024; DEC-103)') x = df / (df + t_val * t_val) ib = _reg_inc_beta_iip0024(0.5 * df, 0.5, x) return 1.0 - 0.5 * ib if t_val >= 0.0 else 0.5 * ib def student_t_ppf(p: float, df: float) -> float: if not (0.0 < p < 1.0) or not math.isfinite(df) or df <= 0.0: raise ValueError('student_t_ppf requires p in (0, 1) and finite df > 0 (IIP-0024; DEC-103)') if abs(p - 0.5) < 1e-15: return 0.0 if p < 0.5: return -student_t_ppf(1.0 - p, df) lo, hi = 0.0, 200.0 while student_t_cdf(hi, df) < p: hi *= 2.0 for _ in range(96): mid = 0.5 * (lo + hi) if student_t_cdf(mid, df) < p: lo = mid else: hi = mid return 0.5 * (lo + hi) def student_t_ppf_one_sided_95(df: float) -> float: """Exact one-sided 95% Student-t critical value t_{0.95, df} (IIP-0024; DEC-103).""" return student_t_ppf(0.95, df) def student_t_q95(n_independent_units: int) -> float: """Exact one-sided 95% Student-t critical value for n >= 2 independent units (df = n - 1; IIP-0024; DEC-103).""" if type(n_independent_units) is not int or n_independent_units < 2: raise ValueError('student_t_q95 requires strict integer n_independent_units >= 2 (IIP-0024; DEC-103)') return student_t_ppf(0.95, float(n_independent_units - 1)) @lru_cache(maxsize=512) def _log_gamma_positive_integer(n: int) -> Decimal: # Called in the fixed-precision context below. Use exact factorials for small parameters # and the log-gamma Stirling expansion for larger ones. At n >= 64, the first # omitted term has magnitude below 2e-28. if n < 64: return Decimal(math.factorial(n - 1)).ln() z = Decimal(n) inv2 = 1 / (z * z) correction = Decimal(0) for numerator, denominator in ( (-3617, 122400), (1, 156), (-691, 360360), (1, 1188), (-1, 1680), (1, 1260), (-1, 360), (1, 12), ): correction = correction * inv2 + Decimal(numerator) / denominator half_log_two_pi = Decimal('0.91893853320467274178032973640561763986139747363778') return (z - Decimal('0.5')) * z.ln() - z + half_log_two_pi + correction / z @lru_cache(maxsize=1024) def _cp_interior_limit(k: int, n: int, alpha: float, upper: bool) -> float: # Invert the requested beta tail. Evaluating its smaller tail directly avoids # losing a small alpha to subtraction from 1. Round the bracket outward cleanly. a, b = (k + 1, n - k) if upper else (k, n - k + 1) lo, hi = 0.0, 1.0 with localcontext(Context(prec=96)): alpha_dec = Decimal.from_float(alpha) log_inv_beta = _log_gamma_positive_integer(a + b) - _log_gamma_positive_integer(a) - _log_gamma_positive_integer(b) split = Decimal(a + 1) / (a + b + 2) def _is_below(p_float: float) -> bool: x = Decimal.from_float(p_float) y = Decimal(1) - x factor = (log_inv_beta + a * x.ln() + b * y.ln()).exp() if x < split: tail = factor * _betacf_iip0024(Decimal(a), Decimal(b), x) / a if factor else Decimal(0) ok_range = Decimal(0) <= tail <= Decimal(1) if not ok_range: raise ArithmeticError('regularized beta probability outside [0, 1]') return tail < (Decimal(1) - alpha_dec if upper else alpha_dec) tail = factor * _betacf_iip0024(Decimal(b), Decimal(a), y) / b if factor else Decimal(0) ok_range = Decimal(0) <= tail <= Decimal(1) if not ok_range: raise ArithmeticError('regularized beta probability outside [0, 1]') return tail > (alpha_dec if upper else Decimal(1) - alpha_dec) while True: mid = (lo + hi) * 0.5 if mid == lo or mid == hi: cand = hi if upper else lo if 0.0 < cand < 1.0: try: # Ensure exact outward rounding at the final float64 endpoint without wasting an extra ULP: if upper and _is_below(cand): cand = min(1.0, math.nextafter(cand, 1.0)) elif (not upper) and not _is_below(cand): cand = max(0.0, math.nextafter(cand, 0.0)) except ArithmeticError: cand = min(1.0, math.nextafter(hi, 1.0)) if upper else max(0.0, math.nextafter(lo, 0.0)) return cand