OpenIslet

scripts/wetlab_contract.py

One measurement contract for Python ingestion and generated MCP metadata.

script Release v0.8.0 gen-eb955f8266035aba 913 KB · 68 sections

Use this file in any AI chat (paste-only or attachment-capable):

Download as text (.txt) Download section (.txt)

Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/wetlab_contract.py · SHA-256: 6bb3955530f8f05d…

                flo, fhi = float(lo_g), float(hi_g)
                if flo > 1.25 or fhi < 0.80:
                    _add_f('FAIL_S11_16P13_CIS_SENTINEL_TOST_BREACHED')
                elif flo < 0.80 or fhi > 1.25:
                    _add_h('HOLD_S11_INCONCLUSIVE_16P13_CIS_SENTINEL_TOST')

        cn16 = view.get('obs_16p13_ddpcr_cn_ratio')
        baf16 = view.get('obs_16p13_phased_baf')
        if not _is_strict_finite_number_iip0024(cn16) or not _is_strict_finite_number_iip0024(baf16):
            _add_h('HOLD_S11_MISSING_16P13_CN_OR_PHASED_BAF')
        elif not (
            Decimal('1.85') <= Decimal(str(cn16)) <= Decimal('2.15')
            and Decimal('0.45') <= Decimal(str(baf16)) <= Decimal('0.55')
        ):
            _add_f('FAIL_S11_16P13_LOH_OR_CN_BREACHED')

        # 7. Quantitative Locus-2 DEC-107 5'-RACE across ALL 4/4 promoter-time datasets (reconciling legacy aliases & 0 <= k <= n):
        eef0_n_vals, eef0_n_conf = _collect_alias_vals(('obs_l2_v3_unit1_eef1a1_resting_0h_5race_umi_n_eff', 'obs_l2_v3_unit1_eef1a1_5race_umi_n_eff'))
        eef0_k_vals, eef0_k_conf = _collect_alias_vals(('obs_l2_v3_unit1_eef1a1_resting_0h_unexpected_tss_umi_k', 'obs_l2_v3_unit1_eef1a1_unexpected_tss_umi_k'))
        if eef0_n_conf or eef0_k_conf:
            _add_h('HOLD_S11_CONFLICTING_RACE_ALIASES')
        race_4_specs = (
            (eef0_n_vals, eef0_k_vals),
            (
                [view['obs_l2_v3_unit1_eef1a1_stim_24h_5race_umi_n_eff']] if 'obs_l2_v3_unit1_eef1a1_stim_24h_5race_umi_n_eff' in view else [],
                [view['obs_l2_v3_unit1_eef1a1_stim_24h_unexpected_tss_umi_k']] if 'obs_l2_v3_unit1_eef1a1_stim_24h_unexpected_tss_umi_k' in view else [],
            ),
            (
                [view['obs_l2_v3_unit2_resting_0h_5race_umi_n_eff']] if 'obs_l2_v3_unit2_resting_0h_5race_umi_n_eff' in view else [],
                [view['obs_l2_v3_unit2_resting_0h_unexpected_tss_umi_k']] if 'obs_l2_v3_unit2_resting_0h_unexpected_tss_umi_k' in view else [],
            ),
            (
                [view['obs_l2_v3_unit2_stim_24h_5race_umi_n_eff']] if 'obs_l2_v3_unit2_stim_24h_5race_umi_n_eff' in view else [],
                [view['obs_l2_v3_unit2_stim_24h_unexpected_tss_umi_k']] if 'obs_l2_v3_unit2_stim_24h_unexpected_tss_umi_k' in view else [],
            ),
        )
        for rn_cands, rk_cands in race_4_specs:
            if not rn_cands or not rk_cands:
                _add_h('HOLD_S11_MISSING_LOCUS2_DUAL_PROMOTER_5PRIME_RACE_3689')
                continue
            ds_valid_any = False
            ds_invalid_any = False
            for rn_v in rn_cands:
                for rk_v in rk_cands:
                    if (
                        not _is_strict_int_iip0024(rn_v)
                        or rn_v < 3689
                        or rn_v.bit_length() > 53
                        or not _is_strict_int_iip0024(rk_v)
                        or rk_v < 0
                        or rk_v > rn_v
                    ):
                        ds_invalid_any = True
                        if _is_strict_int_iip0024(rn_v) and _is_strict_int_iip0024(rk_v) and rk_v > rn_v:
                            _add_h('HOLD_S11_INVALID_RACE_COUNT_EXCEEDS_DENOMINATOR')
                    else:
                        ds_valid_any = True
                        if rk_v > 0:
                            _add_f('FAIL_S11_LOCUS2_UNEXPECTED_5PRIME_RACE_TSS')
            if not ds_valid_any or ds_invalid_any:
                _add_h('HOLD_S11_MISSING_LOCUS2_DUAL_PROMOTER_5PRIME_RACE_3689')

        is_syn5_donor = (
            l2_binding.get('unit2_promoter') == 'Syn_5xISRE_minP'
            if isinstance(l2_binding, dict)
            else (donor_id != 'L2_V3_CD274P_ZERO_TATATA')
        )
        if is_syn5_donor:
            u2_tss_pos = view.get('obs_l2_v3_unit2_tss_pos1_in_donor')
            if not _is_strict_int_iip0024(u2_tss_pos):
                _add_h('HOLD_S11_PENDING_SYN5XISRE_5PRIME_RACE_TSS_COORDINATE')
            elif u2_tss_pos <= 626 or u2_tss_pos > 698:
                _add_f('FAIL_S11_SYN5XISRE_UPSTREAM_ATG_INCLUDED_OR_INVALID_TSS')

        # 8. Quantitative LONGITUDINAL Stage-0 AND Stage-7 bisulfite CpG methylation & CTCF ChIP:
        ok_me0_eef, me0_eef_l95, me0_eef_u95 = _interval_t('obs_l2_v3_stage0_eef1a1_cpg_methylation_pct', 'obs_l2_v3_stage0_eef1a1_cpg_methylation_se')
        ok_me0_chs4, me0_chs4_l95, me0_chs4_u95 = _interval_t('obs_l2_v3_stage0_chs4_cpg_methylation_pct', 'obs_l2_v3_stage0_chs4_cpg_methylation_se')
        ok_me0_dm1, me0_dm1_l95, me0_dm1_u95 = _interval_t('obs_l2_v3_stage0_dm1_cpg_methylation_pct', 'obs_l2_v3_stage0_dm1_cpg_methylation_se')
        ok_me7_eef, me7_eef_l95, me7_eef_u95 = _interval_t('obs_l2_v3_stage7_eef1a1_cpg_methylation_pct', 'obs_l2_v3_stage7_eef1a1_cpg_methylation_se')
        ok_me7_chs4, me7_chs4_l95, me7_chs4_u95 = _interval_t('obs_l2_v3_stage7_chs4_cpg_methylation_pct', 'obs_l2_v3_stage7_chs4_cpg_methylation_se')
        ok_me7_dm1, me7_dm1_l95, me7_dm1_u95 = _interval_t('obs_l2_v3_stage7_dm1_cpg_methylation_pct', 'obs_l2_v3_stage7_dm1_cpg_methylation_se')
        ok_ch0_chs4, ch0_chs4_l95, _ch0_chs4_u95 = _interval_t('obs_l2_v3_stage0_chs4_ctcf_chip_fold_enrichment', 'obs_l2_v3_stage0_chs4_ctcf_chip_fold_enrichment_se', lo_dom=0.0, hi_dom=1000.0)
        ok_ch0_dm1, ch0_dm1_l95, _ch0_dm1_u95 = _interval_t('obs_l2_v3_stage0_dm1_ctcf_chip_fold_enrichment', 'obs_l2_v3_stage0_dm1_ctcf_chip_fold_enrichment_se', lo_dom=0.0, hi_dom=1000.0)
        ok_ch7_chs4, ch7_chs4_l95, _ch7_chs4_u95 = _interval_t('obs_l2_v3_stage7_chs4_ctcf_chip_fold_enrichment', 'obs_l2_v3_stage7_chs4_ctcf_chip_fold_enrichment_se', lo_dom=0.0, hi_dom=1000.0)
        ok_ch7_dm1, ch7_dm1_l95, _ch7_dm1_u95 = _interval_t('obs_l2_v3_stage7_dm1_ctcf_chip_fold_enrichment', 'obs_l2_v3_stage7_dm1_ctcf_chip_fold_enrichment_se', lo_dom=0.0, hi_dom=1000.0)

        meth_tuples = (
            (ok_me0_eef, me0_eef_l95, me0_eef_u95),
            (ok_me0_chs4, me0_chs4_l95, me0_chs4_u95),
            (ok_me0_dm1, me0_dm1_l95, me0_dm1_u95),
            (ok_me7_eef, me7_eef_l95, me7_eef_u95),
            (ok_me7_chs4, me7_chs4_l95, me7_chs4_u95),
            (ok_me7_dm1, me7_dm1_l95, me7_dm1_u95),
        )
        if any(ok_m and ml is not None and ml > 20.0 for (ok_m, ml, _mu) in meth_tuples):
            _add_f('FAIL_S11_LOCUS2_CPG_HYPERMETHYLATION_EXCEEDS_20PCT')
        elif any(ok_m and mu is not None and mu > 20.0 for (ok_m, _ml, mu) in meth_tuples):
            _add_h('HOLD_S11_INCONCLUSIVE_LOCUS2_CPG_METHYLATION_STRADDLES_20PCT')

        if not all((ok_me0_eef, ok_me0_chs4, ok_me0_dm1, ok_me7_eef, ok_me7_chs4, ok_me7_dm1, ok_ch0_chs4, ok_ch0_dm1, ok_ch7_chs4, ok_ch7_dm1)):
            _add_h('HOLD_S11_MISSING_LONGITUDINAL_STAGE0_AND_STAGE7_METHYLATION_OR_CTCF_PANEL')
        elif any(cl is not None and cl < 5.0 for cl in (ch0_chs4_l95, ch0_dm1_l95, ch7_chs4_l95, ch7_dm1_l95)):
            _add_h('HOLD_S11_INSUFFICIENT_CTCF_CHIP_OCCUPANCY_BELOW_5X')

        if donor_id == 'L2_V3_BARRIER_CHS4_250' or pvid_canon == 'ISLET1_A4_L2_V3_BARRIER_CHS4_250':
            _bar_cmp_id = view.get('obs_l2_v3_barrier_matched_80bp_comparator_id')
            _bar_cmp_sha = view.get('obs_l2_v3_barrier_matched_80bp_comparator_sha256')
            _bar_cmp_sid = view.get('obs_l2_v3_barrier_matched_80bp_comparator_sample_id')
            ok_bar_me7, _bm7_l, _bm7_u = _interval_t('obs_l2_v3_barrier_matched_80bp_stage7_eef1a1_cpg_methylation_pct', 'obs_l2_v3_barrier_matched_80bp_stage7_eef1a1_cpg_methylation_se')
            ok_bar_ch7, _bc7_l, _bc7_u = _interval_t('obs_l2_v3_barrier_matched_80bp_stage7_chs4_ctcf_chip_fold_enrichment', 'obs_l2_v3_barrier_matched_80bp_stage7_chs4_ctcf_chip_fold_enrichment_se', lo_dom=0.0, hi_dom=1000.0)
            _exp_80bp_id = getattr(p_mod, 'LOCUS2_V3_BARRIER_MATCHED_80BP_COMPARATOR_ID', 'L2_V3_CO_PRIMARY_CD200')
            _exp_80bp_sha = '723e052172adfe5ae6c74e35e44cf727202d2716010772041d8c12abd76529c8'
            if (
                _bar_cmp_id != _exp_80bp_id
                or _bar_cmp_sha != _exp_80bp_sha
                or not isinstance(_bar_cmp_sid, str)
                or not _bar_cmp_sid.strip()
                or _bar_cmp_sid != _bar_cmp_sid.strip()
                or (isinstance(eff_sample_id, str) and _bar_cmp_sid == eff_sample_id)
                or (isinstance(view.get('rnai_attribution_scramble_sample_id'), str) and _bar_cmp_sid == view.get('rnai_attribution_scramble_sample_id'))
                or (isinstance(view.get('rnai_attribution_intronless_sample_id'), str) and _bar_cmp_sid == view.get('rnai_attribution_intronless_sample_id'))
                or not ok_bar_me7
                or not ok_bar_ch7
            ):
                _add_h('HOLD_S11_MISSING_LINKED_BARRIER_80BP_COMPARATOR_MEASUREMENT')

        # 9. Conditional MANF / CCL22 cargo-specific quantitative gates:
        unit2_cargo = l2_binding.get('unit2_cargo') if isinstance(l2_binding, dict) else None
        if unit2_cargo == 'MANF':
            ok_mnf_l3, mnf_l3_l95, _mnf_l3_u95 = _interval_t('obs_l2_v3_manf_locus3_nfkb_suppression_ratio', 'obs_l2_v3_manf_locus3_nfkb_suppression_se', lo_dom=0.0, hi_dom=10.0)
            ok_mnf_ki, mnf_ki_l95, mnf_ki_u95 = _interval_t('obs_l2_v3_manf_ki67_prolif_pct', 'obs_l2_v3_manf_ki67_prolif_se')
            ok_mnf_oc, mnf_oc_l95, _mnf_oc_u95 = _interval_t('obs_l2_v3_manf_ocr_ratio_vs_ctrl', 'obs_l2_v3_manf_ocr_ratio_vs_ctrl_se', lo_dom=0.0, hi_dom=10.0)
            if not all((ok_mnf_l3, ok_mnf_ki, ok_mnf_oc)):
                _add_h('HOLD_S11_MISSING_CONDITIONAL_MANF_GY001_PROLIF_OR_OCR_PANEL')
            elif (mnf_l3_l95 is not None and mnf_l3_l95 < 0.70) or (mnf_ki_u95 is not None and mnf_ki_u95 > 2.0) or (mnf_oc_l95 is not None and mnf_oc_l95 < 0.85):
                _add_f('FAIL_S11_CONDITIONAL_MANF_GY001_PROLIF_OR_OCR_BREACHED')
        elif unit2_cargo == 'CCL22':
            ok_ccl_r, ccl_r_l95, _ccl_r_u95 = _interval_t('obs_l2_v3_ccl22_treg_to_effector_chemotaxis_ratio', 'obs_l2_v3_ccl22_treg_to_effector_chemotaxis_se', lo_dom=0.0, hi_dom=100.0)
            ok_ccl_e, _ccl_e_l95, ccl_e_u95 = _interval_t('obs_l2_v3_ccl22_effector_chemotaxis_fold_ctrl', 'obs_l2_v3_ccl22_effector_chemotaxis_fold_se', lo_dom=0.0, hi_dom=100.0)
            if not all((ok_ccl_r, ok_ccl_e)):
                _add_h('HOLD_S11_MISSING_CONDITIONAL_CCL22_CHEMOTAXIS_PANEL')
            elif (ccl_r_l95 is not None and ccl_r_l95 < 2.0) or (ccl_e_u95 is not None and ccl_e_u95 > 1.25):
                _add_f('FAIL_S11_CONDITIONAL_CCL22_TREG_EFFECTOR_CHEMOTAXIS_BREACHED')

        # 10. Quantitative active-RNAi (miR) processing/KD/joint-stratum NK lysis & matched same-background B2M-/- attribution controls:
        if has_mir_active:
            ok_m375, m375_l95, m375_u95 = _interval_t('obs_l2_v3_small_rnaseq_mir375_retention_pct', 'obs_l2_v3_small_rnaseq_mir375_retention_se', lo_dom=0.0, hi_dom=200.0)
            ok_spl, spl_l95, spl_u95 = _interval_t('obs_l2_v3_intron_splicing_efficiency_pct', 'obs_l2_v3_intron_splicing_efficiency_se')
            ok_c276, c276_l95, c276_u95 = _interval_t('obs_l2_v3_cd276_kd_pct', 'obs_l2_v3_cd276_kd_se')
            ok_pvr, pvr_l95, pvr_u95 = _interval_t('obs_l2_v3_pvr_kd_pct', 'obs_l2_v3_pvr_kd_se')
            ok_nk, nk_l95, nk_u95 = _interval_t('obs_l2_v3_joint_stratum_nk_lysis_pct', 'obs_l2_v3_joint_stratum_nk_lysis_se')

            if ok_m375 and m375_u95 is not None and m375_u95 < 90.0:
                _add_f('FAIL_S11_ACTIVE_MIR_MIR375_DEPLETION_BELOW_90PCT')
            elif ok_m375 and m375_l95 is not None and m375_l95 < 90.0:
                _add_h('HOLD_S11_INCONCLUSIVE_MIR375_RETENTION_STRADDLES_90PCT')
            if ok_spl and spl_u95 is not None and spl_u95 < 85.0:
                _add_f('FAIL_S11_ACTIVE_MIR_INTRON_SPLICING_BELOW_85PCT')
            elif ok_spl and spl_l95 is not None and spl_l95 < 85.0:
                _add_h('HOLD_S11_INCONCLUSIVE_INTRON_SPLICING_STRADDLES_85PCT')
            if (ok_c276 and c276_u95 is not None and c276_u95 < 70.0) or (ok_pvr and pvr_u95 is not None and pvr_u95 < 70.0):
                _add_f('FAIL_S11_ACTIVE_MIR_CD276_OR_PVR_KD_BELOW_70PCT')
            elif (ok_c276 and c276_l95 is not None and c276_l95 < 70.0) or (ok_pvr and pvr_l95 is not None and pvr_l95 < 70.0):
                _add_h('HOLD_S11_INCONCLUSIVE_CD276_OR_PVR_KD_STRADDLES_70PCT')
            if ok_nk and nk_l95 is not None and nk_l95 > 8.0:
                _add_f('FAIL_S11_ACTIVE_MIR_JOINT_STRATUM_NK_LYSIS_EXCEEDS_8PCT')
            elif ok_nk and nk_u95 is not None and nk_u95 > 8.0:
                _add_h('HOLD_S11_INCONCLUSIVE_JOINT_STRATUM_NK_LYSIS_STRADDLES_8PCT')
            if not all((ok_m375, ok_spl, ok_c276, ok_pvr, ok_nk)):
                _add_h('HOLD_S11_MISSING_ACTIVE_MIR_PROCESSING_KD_OR_JOINT_STRATUM_NK_PANEL')

        scr_id = view.get('rnai_attribution_scramble_control_id')
        scr_bg = view.get('rnai_attribution_scramble_background_id')
        scr_sha = view.get('rnai_attribution_scramble_control_sha256')
        scr_sid = view.get('rnai_attribution_scramble_sample_id')
        intr_id = view.get('rnai_attribution_intronless_control_id')
        intr_bg = view.get('rnai_attribution_intronless_background_id')
        intr_sha = view.get('rnai_attribution_intronless_control_sha256')
        intr_sid = view.get('rnai_attribution_intronless_sample_id')
        rnai_claimed = view.get('rnai_specific_benefit_claimed') is True
        rnai_ctrl_flags_bad = (
            view.get('l2_v3_same_background_intronless_control_linked_to_cargo') is False
            or view.get('l2_v3_same_background_scramble_control_linked_to_cargo') is False
        )
        if rnai_claimed or has_mir_active or rnai_ctrl_flags_bad:
            _exp_scr_id = p_mod.LOCUS2_V3_SCRAMBLE_CONTROL_ID
            _pinned_intr_map = p_mod.LOCUS2_V3_PINNED_B2MKO_INTRONLESS_CONTROL_IDS
            _exp_intr_id = _pinned_intr_map.get(str(unit2_cargo)) if unit2_cargo is not None else None
            registered = {binding['locus2_donor_id']: binding for binding in p_mod.LOCUS2_V3_DONOR_BINDINGS.values()}
            scramble_reference = registered.get(_exp_scr_id, {})
            _exp_scr_sha = scramble_reference.get('locus2_donor_sha256')
            intronless_reference = registered.get(_exp_intr_id, {})
            _hex64_ok = lambda s: isinstance(s, str) and bool(re.fullmatch(r'[0-9a-f]{64}', s))
            _sid_ok = lambda s: isinstance(s, str) and bool(s.strip()) and s == s.strip()
            matched_ctrls_valid = (
                isinstance(scr_id, str)
                and scr_id == _exp_scr_id
                and scramble_reference.get('unit2_cargo') == unit2_cargo
                and _hex64_ok(_exp_scr_sha)
                and scr_sha == _exp_scr_sha
                and isinstance(scr_bg, str)
                and bool(bg_id)
                and scr_bg == bg_id
                and _sid_ok(scr_sid)
                and isinstance(intr_id, str)
                and _exp_intr_id is not None
                and intr_id == _exp_intr_id
                and _hex64_ok(intr_sha)
                and intr_sha != _exp_scr_sha
                and (expected_donor_sha is None or intr_sha != expected_donor_sha)
                and isinstance(intr_bg, str)
                and intr_bg == bg_id
                and not intr_id.startswith('L2_V3_DESIGN_B_')
                and not intr_bg.upper().startswith('DESIGN_B')
                and _sid_ok(intr_sid)
                and intr_sid != scr_sid
                and (not isinstance(eff_sample_id, str) or (scr_sid != eff_sample_id and intr_sid != eff_sample_id))
            )
            if rnai_claimed or has_mir_active:
                if (
                    view.get('l2_v3_same_background_intronless_control_linked_to_cargo') is not True
                    or view.get('l2_v3_same_background_scramble_control_linked_to_cargo') is not True
                    or not matched_ctrls_valid
                ):
                    _add_h('HOLD_S11_RNAI_ATTRIBUTION_REQUIRES_SAME_BACKGROUND_CARGO_LINKED_INTRONLESS_AND_SCRAMBLE_CONTROLS')
            elif rnai_ctrl_flags_bad:
                _add_h('HOLD_S11_RNAI_ATTRIBUTION_REQUIRES_SAME_BACKGROUND_CARGO_LINKED_INTRONLESS_AND_SCRAMBLE_CONTROLS')
            if (rnai_claimed or has_mir_active) and not (
                _hex64_ok(intr_sha)
                and len(set(intr_sha)) > 1
                and intr_sha == intronless_reference.get('locus2_donor_sha256')
                and intronless_reference.get('unit2_cargo') == unit2_cargo
                and intronless_reference.get('background_id') == bg_id
            ):
                _add_h('HOLD_S11_UNVERIFIED_INTRONLESS_CONTROL_DIGEST')

        _add_h('HOLD_S11_UNVERIFIED_ATTRIBUTION_CONTROL_MEASUREMENTS')
        return v_fails, v_holds

    if conflicting_overlap_keys:
        base_unconflicted = {
            k: v for k, v in merged.items() if k not in conflicting_overlap_keys
        }
        view_outer = dict(base_unconflicted)
        view_nested = dict(base_unconflicted)
        for ck in conflicting_overlap_keys:
            ov = payload.get(ck)
            nv = rec_in.get(ck)
            if ov is not None and ov != 'UNAVAILABLE':
                view_outer[ck] = ov