OpenIslet

scripts/verify_iip0042_master_multilayer_portfolio_crosswalk.py

IIP-0042 / DEC-121 (CAND-033 / EXP-030): Master Exact 2^20 Multi-Layer Portfolio

script Release v0.8.0 gen-eb955f8266035aba 881 KB · 63 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/verify_iip0042_master_multilayer_portfolio_crosswalk.py · SHA-256: 16964a158458b5cf…

                        )
                    raw_off_wt_lot = rec.get("peptide_reagent_lot_id")
                    off_wt_lot_id: str | None = None
                    if (
                        type(raw_off_wt_lot) is not str
                        or not raw_off_wt_lot
                        or raw_off_wt_lot != raw_off_wt_lot.strip()
                        or not _SHIELD_STRICT_ID_RE.match(raw_off_wt_lot)
                    ):
                        off_row_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"off_target[{oid_c}]: missing valid unpadded peptide_reagent_lot_id matching "
                            f"{_SHIELD_STRICT_ID_RE.pattern} (got {raw_off_wt_lot!r}); excluded from biological "
                            f"adjudication and batch-linkage credit",
                        )
                    else:
                        off_wt_lot_id = raw_off_wt_lot
                    if not wt15_register_qc_valid or rqc_wt_lot is None:
                        off_row_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"off_target[{oid_c}]: excluded from biological adjudication and batch-linkage credit "
                            f"because mandatory register_qc_records WT15/6DFX QC or reagent lot is missing or failed",
                        )
                    elif off_wt_lot_id is not None and rqc_wt_lot != off_wt_lot_id:
                        off_row_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"off_target[{oid_c}]: peptide_reagent_lot_id mismatch: off_target_controls="
                            f"{off_wt_lot_id!r} != register_qc_records={rqc_wt_lot!r}; "
                            f"WT15 off-target row excluded from biological adjudication and batch-linkage credit",
                        )
                    if b_err is None and (not tmp_c2_o or not tmp_c2_o.issubset(rqc_comp2_batches)):
                        off_row_valid = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"off_target[{oid_c}]: Comp2 expression_batch_ids={sorted(tmp_c2_o)} are not covered by "
                            f"parent register_qc_records Comp2 batches={sorted(rqc_comp2_batches)}; excluded from "
                            f"biological adjudication and batch-linkage credit",
                        )
            else:
                off_row_valid = False
                b_err = None
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"Unregistered off_target_control record rejected (excluded from biological FAIL adjudication): {oid_c!r}",
                )
            if b_err is not None:
                off_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", b_err)
            _, rep_err = _shield_check_strict_int(
                rec.get("replicates_n"), f"off_target[{oid_c}].replicates_n", min_val=_SHIELD_MIN_SPR_REPLICATES_N
            )
            if rep_err is not None:
                off_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", rep_err)
            sel_l, sel_l_err = _shield_check_num(rec.get("selectivity_l95_fold"), f"off_target[{oid_c}].selectivity_l95_fold", lo=0.0)
            sel_u, sel_u_err = _shield_check_num(rec.get("selectivity_u95_fold"), f"off_target[{oid_c}].selectivity_u95_fold", lo=0.0)
            if sel_l_err is not None:
                off_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", sel_l_err)
            if sel_u_err is not None:
                off_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", sel_u_err)
            if sel_l is not None and sel_u is not None and sel_l > sel_u:
                off_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"off_target[{oid_c}]: inverted selectivity CI L95={sel_l} > U95={sel_u}")
            if off_row_valid and sel_l is not None and sel_u is not None:
                referenced_batches.update(tmp_ref_o)
                comp1_assay_batches.update(tmp_c1_o)
                comp2_assay_batches.update(tmp_c2_o)
                if sel_u < 10.0:
                    spr_bio_fail = True
                    if oid_c in _SHIELD_MANDATORY_COMP1_OFF_TARGET_CONTROLS:
                        comp1_spr_bio_fail = True
                    elif oid_c in _SHIELD_MANDATORY_COMP2_OFF_TARGET_CONTROLS:
                        comp2_spr_bio_fail = True
                    _escalate("FAIL", f"off_target[{oid_c}]: selectivity U95={sel_u:.2f}x < 10.0x FAIL bar")
                elif sel_l < 50.0:
                    _escalate("HOLD", f"off_target[{oid_c}]: selectivity L95={sel_l:.2f}x < 50.0x PASS bar")
                elif oid_c in _SHIELD_MANDATORY_COMP1_OFF_TARGET_CONTROLS:
                    comp1_off_target_passed_controls.add(oid_c)
        missing_comp1_off = [o for o in _SHIELD_MANDATORY_COMP1_OFF_TARGET_CONTROLS if o not in seen_off]
        missing_comp2_off = [o for o in _SHIELD_MANDATORY_COMP2_OFF_TARGET_CONTROLS if o not in seen_off]
        if missing_comp1_off:
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Missing mandatory Component 1 off-target SPR controls: {missing_comp1_off}")
        if missing_comp2_off:
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Missing mandatory Component 2 off-target SPR controls: {missing_comp2_off}")

    # 5. Comp2 parallel comparators screened (R08 B3 + R15 0039-B2: two-pass order-independent canonical key &
    #    evidence_record_id uniqueness; all 6 required; EVERY comparator MUST carry closed-schema per-condition
    #    register_control_screens covering BOTH mandatory register analytes — native 15-aa WT InsB:9-23 [WT15]
    #    AND engineered 12-aa 6DFX p8E9E [6DFX] — bound to construct-specific batches, validated register_qc_records
    #    reagent lots/sequences, replicates_n >= 3, and unique evidence_record_id; optional physical 1JK8 allowed as
    #    omitted or JSON null and strictly validated if non-null!)
    _allowed_comp_row_keys = frozenset({
        "comparator_id",
        "screened",
        "replicates_n",
        "expression_batch_ids",
        "register_control_screens",
    })
    _allowed_comp_reg_keys = frozenset({
        "native_wt_insb9_23_15aa",
        "engineered_6dfx_p8e9e_12aa",
        "crystal_ref_1jk8_14aa",
    })
    _required_comp_cond_keys = frozenset({
        "screened",
        "peptide_sequence",
        "peptide_reagent_lot_id",
        "expression_batch_ids",
        "replicates_n",
        "evidence_record_id",
    })
    _required_comp_1jk8_keys = frozenset({
        "screened",
        "peptide_sequence",
        "expression_batch_ids",
        "replicates_n",
        "evidence_record_id",
    })
    _allowed_comp_1jk8_keys = _required_comp_1jk8_keys | frozenset({"peptide_reagent_lot_id"})
    comps_raw = observation.get("comp2_comparators_screened")
    seen_comps: set[str] = set()
    conflicted_comps: set[str] = set()
    conflicted_comp_ev_ids: set[str] = set()
    verified_comparators_both_registers: set[str] = set()
    comparator_shared_batches_by_id: dict[str, list[str]] = {}
    if not isinstance(comps_raw, list) or len(comps_raw) == 0:
        _escalate("HOLD_INVALID_OR_MISSING_INPUT", "Missing typed comp2_comparators_screened list (all 6 comparators required)")
    else:
        comp_counts: dict[str, int] = {}
        comp_ev_counts: dict[str, int] = {}
        for rec in comps_raw:
            if isinstance(rec, dict):
                cid = rec.get("comparator_id")
                if type(cid) is str and cid.strip():
                    cid_p = _shield_canonicalize_construct_id(cid)
                    comp_counts[cid_p] = comp_counts.get(cid_p, 0) + 1
                rc_screens = rec.get("register_control_screens")
                if isinstance(rc_screens, dict):
                    for cond_v in rc_screens.values():
                        if isinstance(cond_v, dict):
                            raw_ev = cond_v.get("evidence_record_id")
                            if type(raw_ev) is str and raw_ev.strip():
                                ev_norm = raw_ev.strip()
                                comp_ev_counts[ev_norm] = comp_ev_counts.get(ev_norm, 0) + 1
        conflicted_comps = {k for k, cnt in comp_counts.items() if cnt > 1}
        for ccid in sorted(conflicted_comps):
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"Duplicate/conflicting comparator canonical key: {ccid!r} (count={comp_counts[ccid]}); all occurrences excluded",
            )
        conflicted_comp_ev_ids = {k for k, cnt in comp_ev_counts.items() if cnt > 1}
        for cev in sorted(conflicted_comp_ev_ids):
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"Duplicate/conflicting comparator register-screen evidence_record_id: {cev!r} (count={comp_ev_counts[cev]}); "
                f"all affected comparator rows excluded from coverage and batch-linkage credit",
            )

        for idx, rec in enumerate(comps_raw):
            if not isinstance(rec, dict):
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"comp2_comparators_screened[{idx}] must be an object")
                continue
            cid = rec.get("comparator_id")
            if type(cid) is not str or not cid.strip():
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"comp2_comparators_screened[{idx}] missing comparator_id")
                continue
            cid_c = _shield_canonicalize_construct_id(cid)
            seen_comps.add(cid_c)
            if cid_c in conflicted_comps:
                continue
            if cid_c not in _SHIELD_MANDATORY_COMP2_COMPARATORS:
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Unregistered comp2_comparator record rejected: {cid_c!r}")
                continue
            comp_row_valid = True
            extra_comp_keys = sorted(set(rec.keys()) - _allowed_comp_row_keys)
            if extra_comp_keys:
                comp_row_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"comparator[{cid_c}] contains unexpected/untyped keys {extra_comp_keys}; "
                    f"strict closed schema with typed register_control_screens required",
                )
            tmp_ref_c: set[str] = set()
            tmp_c2_c: set[str] = set()
            b_err = _shield_validate_batch_ids(
                rec.get("expression_batch_ids"),
                f"comparator[{cid_c}]",
                passing_batch_constructs,
                tmp_ref_c,
                required_construct_all=cid_c,
                role_comp2_batches=tmp_c2_c if cid_c == _SHIELD_COMP2_LEAD else None,
            )
            if b_err is not None:
                comp_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", b_err)
            elif cid_c == _SHIELD_COMP2_LEAD:
                if not tmp_c2_c or not tmp_c2_c.issubset(rqc_comp2_batches):
                    comp_row_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"comparator[{cid_c}]: Comp2 expression_batch_ids={sorted(tmp_c2_c)} are not covered by "
                        f"parent register_qc_records Comp2 batches={sorted(rqc_comp2_batches)}",
                    )
                if not tmp_c2_c or not tmp_c2_c.issubset(sec_comp2_qualified_batches):
                    comp_row_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"comparator[{cid_c}]: Comp2 expression_batch_ids={sorted(tmp_c2_c)} lack passing Comp2 "
                        f"SEC-MALS/DLS physicochemical material qualification "
                        f"(qualified_comp2_batches={sorted(sec_comp2_qualified_batches)})",
                    )
            if rec.get("screened") is not True:
                comp_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"Comparator {cid_c!r} not marked screened=True")
            _, rep_err = _shield_check_strict_int(
                rec.get("replicates_n"), f"comparator[{cid_c}].replicates_n", min_val=_SHIELD_MIN_SPR_REPLICATES_N
            )
            if rep_err is not None:
                comp_row_valid = False
                _escalate("HOLD_INVALID_OR_MISSING_INPUT", rep_err)

            reg_screens = rec.get("register_control_screens")
            cond_wt_batches: set[str] = set()
            cond_6dfx_batches: set[str] = set()
            if not isinstance(reg_screens, dict):
                comp_row_valid = False
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"comparator[{cid_c}] missing mandatory typed register_control_screens object "
                    f"(must explicitly cover BOTH native_wt_insb9_23_15aa [WT15] AND engineered_6dfx_p8e9e_12aa [6DFX])",
                )
            else:
                extra_reg_keys = sorted(set(reg_screens.keys()) - _allowed_comp_reg_keys)
                if extra_reg_keys:
                    comp_row_valid = False
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"comparator[{cid_c}].register_control_screens contains unexpected register keys {extra_reg_keys}",
                    )
                for cond_key, exp_seq, exp_lot, cond_rqc_ok, out_batches in (
                    ("native_wt_insb9_23_15aa", "SHLVEALYLVCGERG", rqc_wt_lot, wt15_register_qc_valid, cond_wt_batches),
                    ("engineered_6dfx_p8e9e_12aa", "VEELYLVAGEEG", rqc_6dfx_lot, r6dfx_qc_valid, cond_6dfx_batches),
                ):
                    cond_obj = reg_screens.get(cond_key)
                    cond_lbl = f"comparator[{cid_c}].register_control_screens.{cond_key}"