scripts/wetlab_contract.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/wetlab_contract.py · SHA-256: 6bb3955530f8f05d…
)
has_icasp9_obs = 'obs_icasp9_log10_kill' in validated_stage0
has_vaf_obs = 'obs_tp53_ppm1d_bcor_vaf' in validated_stage0
has_cast_obs = 'obs_castseq_translocation_freq' in validated_stage0
has_chop_obs = ('obs_chop_coincidence_fold' in validated_stage0) or ('obs_gate0e_chop_log2fc' in validated_stage0)
has_hc10_obs = ('obs_hc10_surface_fold_wt' in validated_stage0) or ('obs_gate0e_hc10_surface_mfi_ratio' in validated_stage0)
mandatory_s9_safety_panel_complete = bool(
has_icasp9_obs and has_vaf_obs and has_cast_obs
)
full_5endpoint_s9_panel_complete = bool(
mandatory_s9_safety_panel_complete and has_chop_obs and has_hc10_obs
)
# Single shared order-independent Rule S9 iCasp9 / TP53-PPM1D-BCOR VAF / CAST-Seq replicate-count lookup across ALL 4 entry paths (IIP-0031; DEC-110):
_s9_n_resolved, _s9_n_conflict_groups, _s9_n_any_conflict = _resolve_s9_replicate_counts_detailed_iip0031(
(validated_stage0, observations)
)
_s9_sample_n_default = _s9_n_resolved.get('default')
_icasp9_sample_n = _s9_n_resolved.get('obs_icasp9_log10_kill')
if _icasp9_sample_n is None:
_icasp9_sample_n = _s9_sample_n_default
_icasp9_t_mult = (
student_t_ppf_one_sided_95(float(int(round(float(_icasp9_sample_n))) - 1))
if (
_icasp9_sample_n is not None
and type(_icasp9_sample_n) is not bool
and isinstance(_icasp9_sample_n, (int, float))
and math.isfinite(float(_icasp9_sample_n))
and float(_icasp9_sample_n) >= 2.0
)
else _student_t_mult_df5_default
)
icasp9_lower95 = (
validated_stage0['obs_icasp9_log10_kill'] - _icasp9_t_mult * validated_stage0_se.get('obs_icasp9_log10_kill_se', 0.0)
if has_icasp9_obs
else 4.5
)
_vaf_sample_n = _s9_n_resolved.get('obs_tp53_ppm1d_bcor_vaf')
if _vaf_sample_n is None:
_vaf_sample_n = _s9_sample_n_default
_cast_sample_n = _s9_n_resolved.get('obs_castseq_translocation_freq')
if _cast_sample_n is None:
_cast_sample_n = _s9_sample_n_default
tp53_duplex_depth_explicitly_supplied = 'obs_tp53_ppm1d_bcor_duplex_depth' in validated_stage0
vaf_duplex_depth = validated_stage0.get('obs_tp53_ppm1d_bcor_duplex_depth', 62958.0)
if has_vaf_obs:
_vaf_guard = _evaluate_s9_tp53_vaf_guard(
validated_stage0['obs_tp53_ppm1d_bcor_vaf'],
validated_stage0_se.get('obs_tp53_ppm1d_bcor_vaf_se', 0.0),
validated_stage0.get('obs_tp53_ppm1d_bcor_duplex_depth'),
sample_n=_vaf_sample_n,
mutant_molecules_k=validated_stage0.get('obs_tp53_ppm1d_bcor_mutant_molecules_k'),
require_explicit_depth=False,
endpoint_label='obs_tp53_ppm1d_bcor_vaf',
)
vaf_upper95 = float(_vaf_guard['upper95'])
tp53_duplex_depth_underpowered_below_62958 = bool(
tp53_duplex_depth_explicitly_supplied and _vaf_guard['depth_underpowered']
)
else:
vaf_upper95 = 0.0
tp53_duplex_depth_underpowered_below_62958 = bool(
tp53_duplex_depth_explicitly_supplied and vaf_duplex_depth < 62958.0
)
cast_duplex_depth_explicitly_supplied = 'obs_castseq_duplex_or_amplicon_depth' in validated_stage0
cast_duplex_depth = validated_stage0.get('obs_castseq_duplex_or_amplicon_depth', 62958.0)
if has_cast_obs:
_cast_guard = _evaluate_s9_tp53_vaf_guard(
validated_stage0['obs_castseq_translocation_freq'],
validated_stage0_se.get('obs_castseq_translocation_se', 0.0),
validated_stage0.get('obs_castseq_duplex_or_amplicon_depth'),
sample_n=_cast_sample_n,
mutant_molecules_k=validated_stage0.get('obs_castseq_mutant_junctions_k'),
require_explicit_depth=False,
endpoint_label='obs_castseq_translocation_freq',
)
cast_upper95 = float(_cast_guard['upper95'])
cast_duplex_depth_underpowered_below_62958 = bool(
cast_duplex_depth_explicitly_supplied and _cast_guard['depth_underpowered']
)
else:
cast_upper95 = 0.0
cast_duplex_depth_underpowered_below_62958 = bool(
cast_duplex_depth_explicitly_supplied and cast_duplex_depth < 62958.0
)
mcb_clonogenic_trials_explicitly_supplied = 'obs_mcb_effective_clonogenic_trials' in validated_stage0
mcb_clonogenic_trials = validated_stage0.get('obs_mcb_effective_clonogenic_trials', 0.0)
mcb_clonogenic_trials_underpowered_below_299573228 = bool(
mcb_clonogenic_trials_explicitly_supplied and mcb_clonogenic_trials < 299573228.0
)
mcb_survivors_k_explicitly_supplied = 'obs_mcb_clonogenic_survivor_count_k' in validated_stage0
mcb_survivors_k = validated_stage0.get('obs_mcb_clonogenic_survivor_count_k', 0.0)
mcb_clonogenic_survivor_detected = bool(
mcb_survivors_k_explicitly_supplied and mcb_survivors_k > 0.0
)
mcb_clonogenic_outcome_missing_when_trials_supplied = bool(
mcb_clonogenic_trials_explicitly_supplied and not mcb_survivors_k_explicitly_supplied
)
mcb_clonogenic_trials_missing_when_outcome_zero_supplied = bool(
mcb_survivors_k_explicitly_supplied and mcb_survivors_k == 0.0 and not mcb_clonogenic_trials_explicitly_supplied
)
chop_upper95_candidates = []
if 'obs_chop_coincidence_fold' in validated_stage0:
chop_upper95_candidates.append(
validated_stage0['obs_chop_coincidence_fold'] + 1.645 * validated_stage0_se.get('obs_chop_coincidence_se', 0.0)
)
if 'obs_gate0e_chop_log2fc' in validated_stage0:
chop_upper95_candidates.append(
2.0 ** (validated_stage0['obs_gate0e_chop_log2fc'] + 1.645 * validated_stage0_se.get('obs_gate0e_chop_log2fc_se', 0.0))
)
chop_upper95 = max(chop_upper95_candidates) if chop_upper95_candidates else chop_fold
hc10_upper95_candidates = []
if 'obs_hc10_surface_fold_wt' in validated_stage0:
hc10_upper95_candidates.append(
validated_stage0['obs_hc10_surface_fold_wt'] + 1.645 * validated_stage0_se.get('obs_hc10_surface_fold_se', 0.0)
)
if 'obs_gate0e_hc10_surface_mfi_ratio' in validated_stage0:
hc10_upper95_candidates.append(
validated_stage0['obs_gate0e_hc10_surface_mfi_ratio'] + 1.645 * validated_stage0_se.get('obs_gate0e_hc10_surface_mfi_se', 0.0)
)
hc10_upper95 = max(hc10_upper95_candidates) if hc10_upper95_candidates else hc10_fold
# Structured QP governance + verified repeat-batch ledger evidence required to reset a sticky S9 hard safety stop (AUDIT-REF, AUDIT-REF, AUDIT-REF & AUDIT-REF; DEC-042/043/044/071):
prior_s9_flag = bool(
prior_batch_sticky_s9_safety_stop is True
or observations.get('prior_batch_sticky_s9_safety_stop') is True
or str(observations.get('prior_variant_safety_status', '')).strip() in STICKY_S9_PRIOR_VARIANT_SAFETY_STATUSES
)
repeat_batch_ids = observations.get('reset_prior_safety_stop_repeat_batch_ids')
verified_ledger_passing = observations.get('verified_ledger_passing_s9_batch_ids')
cur_batch_id_str = str(observations.get('batch_id', '')).strip()
structured_s9_reset_valid = bool(
observations.get('reset_prior_safety_stop_after_investigation') is True
and (
(
isinstance(observations.get('reset_prior_safety_stop_reason_code'), str)
and observations.get('reset_prior_safety_stop_reason_code') in ALLOWED_STICKY_S9_RESET_REASON_CODES
)
or (
isinstance(observations.get('reset_prior_safety_stop_reason'), str)
and observations.get('reset_prior_safety_stop_reason') in ALLOWED_STICKY_S9_RESET_REASON_CODES
)
)
and isinstance(repeat_batch_ids, list)
and len(repeat_batch_ids) >= 2
and all(isinstance(x, str) and x.strip() for x in repeat_batch_ids)
and len({x.strip() for x in repeat_batch_ids}) >= 2
and (not cur_batch_id_str or cur_batch_id_str not in {x.strip() for x in repeat_batch_ids})
and isinstance(observations.get('reset_prior_safety_stop_independent_qp_approver_id'), str)
and len(observations['reset_prior_safety_stop_independent_qp_approver_id'].strip()) >= 3
and (
not observations.get('submitter_id')
or str(observations.get('submitter_id')).strip() != observations['reset_prior_safety_stop_independent_qp_approver_id'].strip()
)
and isinstance(verified_ledger_passing, (list, tuple, set))
and {x.strip() for x in repeat_batch_ids}.issubset({str(b).strip() for b in verified_ledger_passing if str(b).strip()})
)
sticky_prior_s9_stop = bool(prior_s9_flag and not structured_s9_reset_valid)
prior_s9_reset_history_retained = bool(prior_s9_flag and structured_s9_reset_valid)
# Rare-event trial denominators & exact Poisson/Clopper-Pearson upper95 bounds for residual PSC, hazard-state proliferative survivor, Step-1 10 nM AP1903 survivor, and proportion/percentage gates (AUDIT-REF, DEC-050/051/071; IIP-0024; DEC-103):
def _prop_or_pct_u95(mean_key: str, n_key: str, is_pct: bool = True) -> float:
nonlocal non_integer_scored_cell_or_trial_denominator
w_u95 = _upper95(mean_key)
if mean_key in validated_stage0 and n_key in validated_stage0 and validated_stage0[n_key] > 0.0:
n_c = validated_stage0[n_key]
if abs(n_c - round(n_c)) > 1e-9 or round(n_c) <= 0:
non_integer_scored_cell_or_trial_denominator = True
return 100.0 if is_pct else 1.0
n_int = int(round(n_c))
scale = 100.0 if is_pct else 1.0
k_ev = (validated_stage0[mean_key] / scale) * float(n_int)
if abs(k_ev - round(k_ev)) > 1e-6 or round(k_ev) < 0 or round(k_ev) > n_int:
non_integer_scored_cell_or_trial_denominator = True
return 100.0 if is_pct else 1.0
k_int = int(round(k_ev))
cp_rate = clopper_pearson_upper95_independent(k_int, n_int)
return max(w_u95, cp_rate * scale)
return w_u95
# Clonal mosaicism and Day-28 Gate 0V zero-event counts NEVER accept an explicit SE without an integer cells_scored denominator (IIP-0024; DEC-103):
strict_scored_cell_zero_keys = frozenset({
'obs_20q11_bcl2l1_single_cell_mosaic_fraction',
'obs_mcb_1q32_mdm4_single_cell_mosaic_fraction',
'obs_1q32_mdm4_single_cell_mosaic_fraction',
'obs_gate0v_day28_persistent_vp1_ie1_positive_cell_pct',
})
mosaic_explicit_se_missing_scored_cell_denominator = any(
mk in validated_stage0
and nk not in validated_stage0
and se_provenance_by_mean.get(mk) == 'EXPLICIT_SE_SUPPLIED'
for mk, nk in (
('obs_20q11_bcl2l1_single_cell_mosaic_fraction', 'obs_20q11_bcl2l1_single_cell_mosaic_cells_scored'),
('obs_mcb_1q32_mdm4_single_cell_mosaic_fraction', 'obs_mcb_1q32_mdm4_single_cell_mosaic_cells_scored'),
('obs_1q32_mdm4_single_cell_mosaic_fraction', 'obs_1q32_mdm4_single_cell_mosaic_cells_scored'),
)
)
zero_event_proportion_missing_denominator_or_explicit_se = any(
mk in validated_stage0
and validated_stage0[mk] == 0.0
and nk not in validated_stage0
and (
mk in strict_scored_cell_zero_keys
or se_provenance_by_mean.get(mk) != 'EXPLICIT_SE_SUPPLIED'
or _validated_se(mk) < 5e-4
)
for mk, nk in (
('obs_20q11_bcl2l1_single_cell_mosaic_fraction', 'obs_20q11_bcl2l1_single_cell_mosaic_cells_scored'),
('obs_mcb_1q32_mdm4_single_cell_mosaic_fraction', 'obs_mcb_1q32_mdm4_single_cell_mosaic_cells_scored'),
('obs_1q32_mdm4_single_cell_mosaic_fraction', 'obs_1q32_mdm4_single_cell_mosaic_cells_scored'),
('obs_17q_birc5_survivin_single_cell_mosaic_fraction', 'obs_17q_birc5_survivin_single_cell_mosaic_cells_scored'),
('obs_gate0v_day28_persistent_vp1_ie1_positive_cell_pct', 'obs_gate0v_day28_persistent_vp1_ie1_cells_scored'),
('obs_cd24_sox2_oct4_3d_escapee_pct', 'obs_cd24_sox2_oct4_3d_escapee_cells_scored'),
('obs_off_target_gp2_amylase_acinar_fraction', 'obs_off_target_gp2_amylase_acinar_cells_scored'),
('obs_off_target_krt19_cftr_ductal_fraction', 'obs_off_target_krt19_cftr_ductal_cells_scored'),
('obs_assembled_cluster_ki67_progenitor_pct', 'obs_assembled_cluster_ki67_progenitor_cells_scored'),
('obs_ductal_sox9_krt19_double_pos_pct', 'obs_ductal_sox9_krt19_double_pos_cells_scored'),
('obs_assembled_cluster_slc18a1_enterochromaffin_pct', 'obs_assembled_cluster_slc18a1_enterochromaffin_cells_scored'),
('obs_senescent_p16_sasp_positive_cell_pct', 'obs_senescent_p16_sasp_cells_scored'),
)
)
residual_psc_trials_supplied = 'obs_residual_psc_effective_trials' in validated_stage0
residual_psc_trials = validated_stage0.get('obs_residual_psc_effective_trials', 0.0)
residual_psc_validated_se = _validated_se('obs_residual_psc_effective_fraction')
residual_psc_trials_underpowered = bool(
(residual_psc_trials_supplied and residual_psc_trials < 299573228.0)
or (
'obs_residual_psc_effective_fraction' in validated_stage0
and validated_stage0['obs_residual_psc_effective_fraction'] <= 1e-8
and not residual_psc_trials_supplied
)
)
residual_psc_wald_upper95 = validated_stage0.get('obs_residual_psc_effective_fraction', 0.0) + 1.644854 * residual_psc_validated_se
residual_psc_poisson_upper95 = (
_poisson_exact_upper95_rate(
validated_stage0.get('obs_residual_psc_effective_fraction', 0.0) * residual_psc_trials,
residual_psc_trials,
)
if residual_psc_trials_supplied and residual_psc_trials > 0.0
else 0.0
)
residual_psc_upper95 = max(residual_psc_wald_upper95, residual_psc_poisson_upper95)
residual_psc_failed = bool(
'obs_residual_psc_effective_fraction' in validated_stage0