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…
return False
d_val = Decimal(str(val))
if d_val < Decimal('0') or d_val > Decimal('1'):
return False
return abs(d_val - Decimal('0.50')) > Decimal('0.05')
def _extract_per_locus_baf_dict_iip0026(payload: dict[str, Any]) -> dict[str, Any] | None:
raw_dict = payload.get('post_edit_per_locus_segmented_baf', payload.get('per_locus_segmented_baf'))
if (
'post_edit_per_locus_segmented_baf' in payload
and 'per_locus_segmented_baf' in payload
and payload['post_edit_per_locus_segmented_baf'] != payload['per_locus_segmented_baf']
):
return None
loci_pairs = (
('chr19q', 'obs_post_edit_segmented_baf_chr19q'),
('chr11p15', 'obs_post_edit_segmented_baf_chr11p15'),
('chr6p21', 'obs_post_edit_segmented_baf_chr6p21'),
)
if isinstance(raw_dict, dict):
for loc, ok in loci_pairs:
if ok in payload:
ov = payload[ok]
if ov is None or ov == 'UNAVAILABLE' or (loc in raw_dict and raw_dict[loc] != ov):
return None
return dict(raw_dict)
if raw_dict is not None:
return None
assembled: dict[str, Any] = {}
for loc, ok in loci_pairs:
if ok in payload and payload[ok] is not None and payload[ok] != 'UNAVAILABLE':
assembled[loc] = payload[ok]
return assembled if assembled else None
def _reconcile_iip0026_aliases_and_baf_iip0031(
containers: tuple[dict[str, Any], ...] | list[dict[str, Any]],
) -> tuple[dict[str, Any], dict[str, list[Any]], dict[str, Any] | None, dict[str, list[Any]], bool]:
"""
Canonicalize all 10 Design-B/TYK2 alias pairs and all BAF representations across containers.
Never lets a preferred 0-valued spelling shadow a positive adverse spelling, and marks any
alias/null/type disagreement as `has_conflict=True` while retaining all valid candidates.
"""
clean_aliases: dict[str, Any] = {}
alias_cands: dict[str, list[Any]] = {}
has_conflict = False
def _val_sig(x: Any) -> str | None:
if type(x) is bool:
return f'bool:{x}'
if _is_strict_int_iip0024(x):
if abs(x).bit_length() > 53:
return None
return f'num:{Decimal(x).normalize()}'
if _is_strict_finite_number_iip0024(x):
return f'num:{Decimal(str(x)).normalize()}'
try:
return 'json:' + json.dumps(x, sort_keys=True, separators=(',', ':'), allow_nan=False)
except (TypeError, ValueError, OverflowError, RecursionError):
return None
for short_k, obs_k, req_int in _IIP0026_DESIGNB_TYK2_ALIAS_PAIRS:
raw_vals: list[Any] = []
for c in containers:
if not isinstance(c, dict):
continue
for ak in (obs_k, short_k):
if ak in c:
raw_vals.append(c[ak])
if not raw_vals:
continue
has_null = any(v is None or v == 'UNAVAILABLE' for v in raw_vals)
non_null = [v for v in raw_vals if v is not None and v != 'UNAVAILABLE']
valid_cands: list[Any] = []
seen_sigs: set[str] = set()
bad_val = False
for v in non_null:
if req_int:
if not _is_strict_int_iip0024(v) or abs(v).bit_length() > 53:
bad_val = True
continue
elif not _is_strict_finite_number_iip0024(v):
bad_val = True
continue
sig = _val_sig(v)
if sig is None:
bad_val = True
continue
if sig not in seen_sigs:
seen_sigs.add(sig)
valid_cands.append(int(v) if req_int else float(v))
if valid_cands:
alias_cands[obs_k] = valid_cands
if bad_val or (has_null and bool(non_null)) or len(valid_cands) > 1:
has_conflict = True
elif len(valid_cands) == 1:
clean_aliases[obs_k] = valid_cands[0]
clean_aliases[short_k] = valid_cands[0]
loci_pairs = (
('chr19q', 'obs_post_edit_segmented_baf_chr19q'),
('chr11p15', 'obs_post_edit_segmented_baf_chr11p15'),
('chr6p21', 'obs_post_edit_segmented_baf_chr6p21'),
)
baf_locus_cands: dict[str, list[Any]] = {loc: [] for loc, _ in loci_pairs}
baf_bad = False
baf_any_present = False
locus_raw: dict[str, list[Any]] = {loc: [] for loc, _ in loci_pairs}
for c in containers:
if not isinstance(c, dict):
continue
for map_k in ('post_edit_per_locus_segmented_baf', 'per_locus_segmented_baf'):
if map_k in c:
baf_any_present = True
mv = c[map_k]
if not isinstance(mv, dict):
baf_bad = True
else:
for mk_sub, mv_sub in mv.items():
if not isinstance(mk_sub, str):
baf_bad = True
elif mk_sub in locus_raw:
locus_raw[mk_sub].append(mv_sub)
for loc, ok in loci_pairs:
if ok in c:
baf_any_present = True
locus_raw[loc].append(c[ok])
clean_baf: dict[str, Any] = {}
for loc, _ in loci_pairs:
lvals = locus_raw[loc]
if not lvals:
continue
l_null = any(v is None or v == 'UNAVAILABLE' for v in lvals)
l_non = [v for v in lvals if v is not None and v != 'UNAVAILABLE']
l_valid: list[Any] = []
l_sigs: set[str] = set()
for v in l_non:
if not _is_strict_finite_number_iip0024(v) or not (0.0 <= float(v) <= 1.0):
baf_bad = True
continue
sig = _val_sig(v)
if sig is None:
baf_bad = True
continue
if sig not in l_sigs:
l_sigs.add(sig)
l_valid.append(v)
baf_locus_cands[loc] = l_valid
if (l_null and bool(l_non)) or len(l_valid) > 1:
baf_bad = True
elif len(l_valid) == 1:
clean_baf[loc] = l_valid[0]
if baf_bad:
has_conflict = True
clean_baf_out: dict[str, Any] | None = None
else:
clean_baf_out = clean_baf if (baf_any_present and clean_baf) else None
return clean_aliases, alias_cands, clean_baf_out, baf_locus_cands, has_conflict
def _evaluate_iip0026_candidate_fails_iip0031(
reg: dict[str, Any],
containers: tuple[dict[str, Any], ...] | list[dict[str, Any]],
alias_cands: dict[str, list[Any]],
baf_locus_cands: dict[str, list[Any]],
) -> list[str]:
"""Evaluate all non-null candidate values across containers so adverse Design-B/TYK2/BAF stops are never shadowed."""
cand_fails: list[str] = []
def _add(code: str) -> None:
if code not in cand_fails:
cand_fails.append(code)
for loc in reg['required_baf_loci']:
for bv in baf_locus_cands.get(loc, ()):
if _baf_locus_discordant_iip0031(bv):
_add('FAIL_DISCORDANT_PER_LOCUS_SEGMENTED_BAF')
def _any_bool_true(k: str) -> bool:
return any(isinstance(c, dict) and c.get(k) is True for c in containers)
def _all_bool_not_true(k: str) -> bool:
return not _any_bool_true(k)
if reg['requires_endogenous_hlac_and_locus5_gates']:
if _any_bool_true('design_b_hlac_dt9_positive') and _any_bool_true('design_b_hlae_3d12_positive') and _all_bool_not_true('design_b_hlac_allele_specific_surface_or_phased_mrna_verified'):
for dv in alias_cands.get('obs_design_b_hlac_dt9_matched_hlae_only_control_delta_ratio', ()):
if 0.0 < float(dv) < 2.0:
_add('FAIL_HLAC_SURFACE_LOSS_MASKED_BY_DT9_HLAE_CROSSREACTIVITY')
max_donor_bp = 4078 if reg['bound_c1_c5_pair_sha256'] == DESIGN_B_C1_C5_PAIR_SHA256 else 10000
for _rc_c in containers:
if not isinstance(_rc_c, dict):
continue
for _rk_chk in (
'design_b_locus5_5prime_race_coord_bp', 'obs_design_b_locus5_5prime_race_coord_bp',
'design_b_locus5_3prime_race_coord_bp', 'obs_design_b_locus5_3prime_race_coord_bp',
):
if _rk_chk in _rc_c and _rc_c[_rk_chk] is not None and _rc_c[_rk_chk] != 'UNAVAILABLE':
_rv_chk = _rc_c[_rk_chk]
if not _is_strict_int_iip0024(_rv_chk) or _rv_chk <= 0 or _rv_chk > max_donor_bp:
_add('FAIL_INVALID_RACE_COORDINATES')
r5_list = alias_cands.get('obs_design_b_locus5_5prime_race_coord_bp', [])
r3_list = alias_cands.get('obs_design_b_locus5_3prime_race_coord_bp', [])
for r5 in r5_list:
for r3 in r3_list:
if not (0 < int(r5) < int(r3) <= max_donor_bp):
_add('FAIL_INVALID_RACE_COORDINATES')
for c in containers:
if isinstance(c, dict) and 'design_b_locus5_pdl1_5prime_race_coord_bp' in c:
pdl1_v = c['design_b_locus5_pdl1_5prime_race_coord_bp']
if pdl1_v is not None and pdl1_v != 'UNRESOLVED':
if not _is_strict_int_iip0024(pdl1_v) or pdl1_v <= 0 or pdl1_v > 2381:
_add('FAIL_INVALID_RACE_COORDINATES')
elif pdl1_v <= 2308:
_add('FAIL_MATURE_TRANSCRIPT_INCLUDES_UPSTREAM_OUT_OF_FRAME_KOZAK_ORF')
for uk in alias_cands.get('obs_design_b_locus5_aberrant_umi_k', ()):
if _is_strict_int_iip0024(uk) and uk > 0:
_add('FAIL_DESIGNB_LOCUS5_CHIMERIC_TRANSCRIPT_DETECTED')
if reg['requires_tyk2_pe_gates']:
has_pe4 = any(isinstance(c, dict) and c.get('tyk2_pe_modality') == 'PE4MAX_MSI_GATED' for c in containers)
if has_pe4 and _any_bool_true('tyk2_phased_haplotype_sap_verified'):
for mk in alias_cands.get('obs_tyk2_pe4max_pentaplex_msi_shifts_k', ()):
if _is_strict_int_iip0024(mk) and mk > 0:
_add('FAIL_MSI_DETECTED')
for cd in alias_cands.get('obs_tyk2_pe4max_coding_or_splice_de_novo_indels', ()):
if _is_strict_int_iip0024(cd) and cd > 0:
_add('FAIL_PE4MAX_CODING_OR_SPLICE_DE_NOVO_INDEL_DETECTED')
for cc in alias_cands.get('obs_tyk2_pe4max_wgs_repeat_indels_clone', ()):
for ct in alias_cands.get('obs_tyk2_pe4max_wgs_repeat_indels_ctrl', ()):
if _is_strict_int_iip0024(cc) and cc >= 0 and _is_strict_int_iip0024(ct) and ct >= 0:
cap = (ct + 5) if ct <= 25 else int(math.floor(1.2 * ct))
if cc > cap:
_add('FAIL_PE4MAX_REPEAT_INDEL_CAP_EXCEEDED')
return cand_fails
_S11_MEAN_SE_PAIRS_IIP0031: tuple[tuple[str, str], ...] = (
('obs_l2_v3_ap_c5b9_pct', 'obs_l2_v3_ap_c5b9_se'),
('obs_l2_v3_classical_c4b2a_lysis_pct', 'obs_l2_v3_classical_c4b2a_lysis_se'),
('obs_l2_v3_lectin_c4b2a_lysis_pct', 'obs_l2_v3_lectin_c4b2a_lysis_se'),
('obs_l2_v3_overall_20pct_nhs_lysis_pct', 'obs_l2_v3_overall_20pct_nhs_lysis_se'),
('obs_l2_v3_intact_serum_adcc_lysis_pct', 'obs_l2_v3_intact_serum_adcc_lysis_se'),
('obs_l2_v3_igg_depleted_serum_adcc_lysis_pct', 'obs_l2_v3_igg_depleted_serum_adcc_lysis_se'),
('obs_l2_v3_igg_increment_adcc_lysis_pct', 'obs_l2_v3_igg_increment_adcc_lysis_se'),
('obs_l2_v3_exact_stack_effero_abs_pct', 'obs_l2_v3_exact_stack_effero_abs_se'),