OpenIslet

scripts/verify_iip0044_foundry_v1_3_biologics.py

IIP-0044 / DEC-123 (CAND-035 / EXP-032): OpenIslet Foundry v1.3 Clean-Room

script Release v0.8.0 gen-eb955f8266035aba 619 KB · 44 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_iip0044_foundry_v1_3_biologics.py · SHA-256: 1d6c53a0817f6254…

                if ld in c_set:
                    lead_batch_counts[ld] += 1
        if len(passing_batch_constructs) < _SHIELD_MIN_BATCHES_N:
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"Under-replicated passing expression_batches: {len(passing_batch_constructs)} < {_SHIELD_MIN_BATCHES_N}",
            )
        for ld, cnt in lead_batch_counts.items():
            if cnt < _SHIELD_MIN_BATCHES_N:
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"Construct {ld} covered by only {cnt} passing batches (< {_SHIELD_MIN_BATCHES_N})",
                )

    # 2. Register QC records (R12 0039-B1 + R14 0039-B2 + R17 0044-B1/B2: validated and reconciled STRICTLY BEFORE
    #    on-target/off-target SPR biological adjudication and batch-linkage credit! Cocktail-bound batches covering
    #    BOTH Comp1 HIP11 and Comp2 WT15/6DFX validate parent coverage ONLY and NEVER credit comp1_assay_batches or
    #    comp2_assay_batches (which count ONLY validated SPR/comparator rows). Mandatory pairwise-distinct peptide
    #    reagent lot IDs are enforced across HIP11, 15-aa WT InsB:9-23, 12-aa 6DFX, and optional 14-aa 1JK8 if present
    #    BEFORE granting dependent records biological or batch credit, while retaining independent qualified evidence!)
    rqc = observation.get("register_qc_records")
    hip11_exchange_qc_valid = False
    r6dfx_qc_valid = False
    wt15_register_qc_valid = False
    r1jk8_qc_valid = False
    rqc_hip11_lot: str | None = None
    rqc_wt_lot: str | None = None
    rqc_6dfx_lot: str | None = None
    rqc_1jk8_lot: str | None = None
    rqc_comp1_batches: set[str] = set()
    rqc_comp2_batches: set[str] = set()
    collided_reagents: set[str] = set()
    if not isinstance(rqc, dict):
        _escalate("HOLD_INVALID_OR_MISSING_INPUT", "Missing typed register_qc_records object")
        derived_summary["optional_1jk8_physical_control_present"] = False
    else:
        tmp_ref_rqc: set[str] = set()
        tmp_c1_rqc: set[str] = set()
        tmp_c2_rqc: set[str] = set()
        rqc_b_err = _shield_validate_batch_ids(
            rqc.get("expression_batch_ids"),
            "register_qc_records",
            passing_batch_constructs,
            tmp_ref_rqc,
            require_cocktail_both_leads=True,
            role_comp1_batches=tmp_c1_rqc,
            role_comp2_batches=tmp_c2_rqc,
        )
        rqc_batch_valid = rqc_b_err is None
        if rqc_b_err is not None:
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", rqc_b_err)
        else:
            rqc_comp1_batches = set(tmp_c1_rqc)
            rqc_comp2_batches = set(tmp_c2_rqc)
        raw_rqc_hip11_lot = rqc.get("hip11_peptide_reagent_lot_id")
        if (
            type(raw_rqc_hip11_lot) is not str
            or not raw_rqc_hip11_lot
            or raw_rqc_hip11_lot != raw_rqc_hip11_lot.strip()
            or not _SHIELD_STRICT_ID_RE.match(raw_rqc_hip11_lot)
        ):
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"register_qc_records missing valid unpadded hip11_peptide_reagent_lot_id "
                f"(got {raw_rqc_hip11_lot!r})",
            )
        else:
            rqc_hip11_lot = raw_rqc_hip11_lot
        hip11_flag_ok = rqc.get("hip11_dq25_dq8t_dq22_exchange_qc_passed") is True
        if not hip11_flag_ok:
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", "HIP11 peptide-exchange QC across DQ2.5/DQ8-trans/DQ2.2 not passed")
        wt15_rec = rqc.get("native_wt_insb9_23_15aa")
        wt15_rec_metrics_ok = False
        if (
            not isinstance(wt15_rec, dict)
            or wt15_rec.get("sequence") != "SHLVEALYLVCGERG"
            or wt15_rec.get("lcms_identity_passed") is not True
            or wt15_rec.get("register2_occupancy_qc_passed") is not True
        ):
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", "Native 15-aa WT InsB:9-23 (SHLVEALYLVCGERG) LC-MS & Register-2 QC missing or failed")
        else:
            raw_rqc_wt_lot = wt15_rec.get("peptide_reagent_lot_id")
            if (
                type(raw_rqc_wt_lot) is not str
                or not raw_rqc_wt_lot
                or raw_rqc_wt_lot != raw_rqc_wt_lot.strip()
                or not _SHIELD_STRICT_ID_RE.match(raw_rqc_wt_lot)
            ):
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"register_qc_records.native_wt_insb9_23_15aa missing valid unpadded peptide_reagent_lot_id "
                    f"(got {raw_rqc_wt_lot!r})",
                )
            else:
                rqc_wt_lot = raw_rqc_wt_lot
                wt15_rec_metrics_ok = True
        r6dfx_rec = rqc.get("engineered_6dfx_p8e9e_12aa")
        r6dfx_rec_metrics_ok = False
        if (
            not isinstance(r6dfx_rec, dict)
            or r6dfx_rec.get("sequence") != "VEELYLVAGEEG"
            or r6dfx_rec.get("qc_passed") is not True
        ):
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", "12-aa PDB 6DFX p8E9E Register-3 comparator (VEELYLVAGEEG) QC record missing or failed")
        else:
            raw_rqc_6dfx_lot = r6dfx_rec.get("peptide_reagent_lot_id")
            if (
                type(raw_rqc_6dfx_lot) is not str
                or not raw_rqc_6dfx_lot
                or raw_rqc_6dfx_lot != raw_rqc_6dfx_lot.strip()
                or not _SHIELD_STRICT_ID_RE.match(raw_rqc_6dfx_lot)
            ):
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"register_qc_records.engineered_6dfx_p8e9e_12aa missing valid unpadded peptide_reagent_lot_id "
                    f"(got {raw_rqc_6dfx_lot!r})",
                )
            else:
                rqc_6dfx_lot = raw_rqc_6dfx_lot
                r6dfx_rec_metrics_ok = True
        r1jk8_present = False
        r1jk8_metrics_ok = False
        r1jk8_lot_syntax_ok = True
        r1jk8_batch_ok = False
        parsed_rqc_1jk8_lot: str | None = None
        tmp_ref_1jk8: set[str] = set()
        if "crystal_ref_1jk8_14aa" in rqc and rqc["crystal_ref_1jk8_14aa"] is not None:
            r1jk8_present = True
            derived_summary["optional_1jk8_physical_control_present"] = True
            r1jk8_rec = rqc["crystal_ref_1jk8_14aa"]
            if (
                not isinstance(r1jk8_rec, dict)
                or r1jk8_rec.get("sequence") != "LVEALYLVCGERGG"
                or r1jk8_rec.get("qc_passed") is not True
            ):
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    "Optional 14-aa PDB 1JK8 physical control was supplied but failed sequence (LVEALYLVCGERGG) or qc_passed validation",
                )
                if isinstance(r1jk8_rec, dict) and "peptide_reagent_lot_id" in r1jk8_rec:
                    raw_rqc_1jk8_lot = r1jk8_rec["peptide_reagent_lot_id"]
                    if (
                        type(raw_rqc_1jk8_lot) is str
                        and bool(raw_rqc_1jk8_lot)
                        and raw_rqc_1jk8_lot == raw_rqc_1jk8_lot.strip()
                        and bool(_SHIELD_STRICT_ID_RE.match(raw_rqc_1jk8_lot))
                    ):
                        parsed_rqc_1jk8_lot = raw_rqc_1jk8_lot
            else:
                r1jk8_metrics_ok = True
                if "peptide_reagent_lot_id" in r1jk8_rec:
                    raw_rqc_1jk8_lot = r1jk8_rec["peptide_reagent_lot_id"]
                    if (
                        type(raw_rqc_1jk8_lot) is not str
                        or not raw_rqc_1jk8_lot
                        or raw_rqc_1jk8_lot != raw_rqc_1jk8_lot.strip()
                        or not _SHIELD_STRICT_ID_RE.match(raw_rqc_1jk8_lot)
                    ):
                        r1jk8_lot_syntax_ok = False
                        _escalate(
                            "HOLD_INVALID_OR_MISSING_INPUT",
                            f"Optional register_qc_records.crystal_ref_1jk8_14aa.peptide_reagent_lot_id was supplied "
                            f"but is invalid (must be a non-empty unpadded string matching {_SHIELD_STRICT_ID_RE.pattern}; "
                            f"got {raw_rqc_1jk8_lot!r})",
                        )
                    else:
                        parsed_rqc_1jk8_lot = raw_rqc_1jk8_lot
                tmp_c2_1jk8: set[str] = set()
                b1jk8_err = _shield_validate_batch_ids(
                    r1jk8_rec.get("expression_batch_ids"),
                    "register_qc_records.crystal_ref_1jk8_14aa",
                    passing_batch_constructs,
                    tmp_ref_1jk8,
                    required_construct_all=_SHIELD_COMP2_LEAD,
                    role_comp2_batches=tmp_c2_1jk8,
                )
                if b1jk8_err is not None:
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", b1jk8_err)
                else:
                    r1jk8_batch_ok = True
        else:
            derived_summary["optional_1jk8_physical_control_present"] = False

        # R17 0044-B2: Enforce pairwise-distinct peptide reagent lot IDs across ALL mandatory peptide reagents
        # (HIP11, WT15, 6DFX, plus optional 1JK8 if supplied) strictly BEFORE granting dependent records
        # biological or batch-linkage credit, while preserving independent qualified evidence!
        lot_owners: dict[str, list[str]] = {}
        for reagent_lbl, lot_val in (
            ("hip11", rqc_hip11_lot),
            ("wt15", rqc_wt_lot),
            ("6dfx", rqc_6dfx_lot),
            ("1jk8", parsed_rqc_1jk8_lot),
        ):
            if lot_val is not None:
                lot_owners.setdefault(lot_val, []).append(reagent_lbl)
        for dup_lot, owners in sorted(lot_owners.items()):
            if len(owners) > 1:
                collided_reagents.update(owners)
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"Duplicate peptide reagent lot ID {dup_lot!r} shared across distinct synthetic peptides "
                    f"{owners}; pairwise-distinct peptide_reagent_lot_id values are mandatory across HIP11, "
                    f"WT15, 6DFX (and optional 1JK8 when present) before biological or batch credit",
                )

        hip11_exchange_qc_valid = bool(
            rqc_batch_valid
            and hip11_flag_ok
            and rqc_hip11_lot is not None
            and "hip11" not in collided_reagents
            and len(rqc_comp1_batches) > 0
        )
        r6dfx_qc_valid = bool(
            rqc_batch_valid
            and r6dfx_rec_metrics_ok
            and rqc_6dfx_lot is not None
            and "6dfx" not in collided_reagents
            and len(rqc_comp2_batches) > 0
        )
        wt15_register_qc_valid = bool(
            rqc_batch_valid
            and wt15_rec_metrics_ok
            and rqc_wt_lot is not None
            and "wt15" not in collided_reagents
            and r6dfx_qc_valid
            and len(rqc_comp2_batches) > 0
        )
        if rqc_batch_valid and hip11_exchange_qc_valid and wt15_register_qc_valid:
            # R17 0044-B1: register_qc_records validates parent reagent/batch coverage ONLY and NEVER
            # credits comp1_assay_batches or comp2_assay_batches (which count ONLY validated SPR/comparator rows)!
            referenced_batches.update(tmp_ref_rqc)
        if (
            r1jk8_present
            and r1jk8_metrics_ok
            and r1jk8_lot_syntax_ok
            and r1jk8_batch_ok
            and "1jk8" not in collided_reagents
        ):
            r1jk8_qc_valid = True
            rqc_1jk8_lot = parsed_rqc_1jk8_lot
            referenced_batches.update(tmp_ref_1jk8)
    derived_summary["hip11_register_qc_reconciled_before_bio"] = True
    derived_summary["hip11_reagent_lot_and_batch_reconciled_before_bio"] = True
    derived_summary["wt15_register_qc_reconciled_before_bio"] = True
    derived_summary["wt15_offtarget_register_qc_reconciled_before_bio"] = True
    derived_summary["r6dfx_reagent_lot_reconciled_before_bio"] = True
    derived_summary["mandatory_peptide_reagent_lots_pairwise_distinct"] = bool(
        isinstance(rqc, dict)
        and rqc_hip11_lot is not None
        and rqc_wt_lot is not None