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…

                        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
        and rqc_6dfx_lot is not None
        and len(collided_reagents) == 0
    )
    derived_summary["register_qc_excluded_from_spr_assay_batch_counts"] = True

    # 2b. Quantitative SEC-MALS / DLS monomer & 1:1 co-formulation stability (cocktail-bound BOTH leads; >= 95.0% monomer,
    #     PDI <= 0.15) evaluated STRICTLY BEFORE SPR and donor biological adjudication (R13 0039-B2 / 0037-M1 + R14 0039-B3 + R15 0037-B1/0039-B1)!
    #     Out-of-spec physical protein material (< 95.0% monomer, PDI > 0.15, or detected aggregates) is an UNQUALIFIED
    #     MATERIAL INPUT (HOLD_INVALID_OR_MISSING_INPUT, failure_domain=None), NOT a biological target FAIL, AND every
    #     SPR and cocktail donor assay MUST positively link ALL of its expression batches to passing construct/cocktail
    #     SEC-MALS/DLS material qualification before contributing to biological PASS/FAIL or batch-linkage credit!
    derived_summary["physicochemical_out_of_spec_disposition"] = "HOLD_INVALID_OR_MISSING_INPUT_UNQUALIFIED_MATERIAL"
    derived_summary["spr_material_qualification_reconciled_before_bio"] = True
    derived_summary["donor_material_qualification_reconciled_before_bio"] = True
    sec = observation.get("sec_mals_dls_monomer_coformulation")
    sec_comp1_qualified_batches: set[str] = set()
    sec_comp2_qualified_batches: set[str] = set()
    sec_cocktail_qualified = False
    sec_cocktail_qualified_batches: set[str] = set()
    sec_cocktail_qualified_comp1_batches: set[str] = set()
    sec_cocktail_qualified_comp2_batches: set[str] = set()
    qualified_passing_mixtures: dict[str, tuple[str, str]] = {}
    conflicted_mixture_ids: set[str] = set()
    if not isinstance(sec, dict):
        _escalate(
            "HOLD_INVALID_OR_MISSING_INPUT",
            "PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: Missing typed sec_mals_dls_monomer_coformulation record",
        )
        derived_summary["sec_mals_dls"] = {
            "comp1_monomer_pct": None,
            "comp2_monomer_pct": None,
            "cocktail_1to1_monomer_pct": None,
            "dls_pdi": None,
            "no_aggregates_or_precipitation": None,
            "physicochemical_qc_classification": "MISSING_OR_MALFORMED_OR_CONTRADICTORY_INPUT_HOLD",
            "qualified_cocktail_batch_ids": [],
            "qualified_coformulation_mixtures": {},
            "qualified_coformulation_mixtures_count": 0,
            "per_mixture_coformulation_binding_enforced": True,
            "coformulation_1to1_molar_ratio_verification_scope": (
                "LABORATORY_ATTESTED_NOT_INDEPENDENTLY_MEASURED_BY_SEC_MALS_OR_DLS"
            ),
            "all_qualified_mixtures_equimolar_1to1_attested": False,
        }
    else:
        sec_input_missing_or_malformed = False
        sec_valid_out_of_spec = False
        sec_top_qc_bad = False
        tmp_ref_sec: set[str] = set()
        tmp_c1_sec: set[str] = set()
        tmp_c2_sec: set[str] = set()
        sec_b_err = _shield_validate_batch_ids(
            sec.get("expression_batch_ids"),
            "sec_mals_dls_monomer_coformulation",
            passing_batch_constructs,
            tmp_ref_sec,
            require_cocktail_both_leads=True,
            role_comp1_batches=tmp_c1_sec,
            role_comp2_batches=tmp_c2_sec,
        )
        if sec_b_err is not None:
            sec_input_missing_or_malformed = True
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {sec_b_err}")
        hflag, hflag_err = _shield_check_num(sec.get("protenix_homodimer_flag_iptm_addressed"), "protenix_homodimer_flag_iptm_addressed", lo=0.0, hi=1.0)
        hflag_ok = bool(hflag_err is None and hflag is not None and abs(hflag - 0.6691) <= 1e-4)
        if not hflag_ok:
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: SEC-MALS record must explicitly address Comp2 "
                f"Protenix-v1 homodimer flag 0.6691 (got {sec.get('protenix_homodimer_flag_iptm_addressed')!r})",
            )

        def _extract_sec_pct(primary_k: str, alias_k: str) -> float | None:
            nonlocal sec_input_missing_or_malformed
            has_p = primary_k in sec and sec[primary_k] is not None
            has_a = alias_k in sec and sec[alias_k] is not None
            if has_p and has_a:
                pv, perr = _shield_check_num(sec[primary_k], f"sec_mals_dls.{primary_k}", lo=0.0, hi=100.0)
                av, aerr = _shield_check_num(sec[alias_k], f"sec_mals_dls.{alias_k}", lo=0.0, hi=100.0)
                if perr is not None:
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {perr}")
                    return None
                if aerr is not None:
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {aerr}")
                    return None
                if pv is not None and av is not None and abs(pv - av) > 1e-9:
                    sec_input_missing_or_malformed = True
                    _escalate(
                        "HOLD_INVALID_OR_MISSING_INPUT",
                        f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.{primary_k}={pv} disagrees with alias {alias_k}={av}",
                    )
                    return None
                return pv
            if has_p:
                pv, perr = _shield_check_num(sec[primary_k], f"sec_mals_dls.{primary_k}", lo=0.0, hi=100.0)
                if perr is not None:
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {perr}")
                    return None
                return pv
            if has_a:
                av, aerr = _shield_check_num(sec[alias_k], f"sec_mals_dls.{alias_k}", lo=0.0, hi=100.0)
                if aerr is not None:
                    sec_input_missing_or_malformed = True
                    _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {aerr}")
                    return None
                return av
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls missing required {primary_k} (or alias {alias_k})",
            )
            return None

        c1_mon = _extract_sec_pct("comp1_monomer_pct", "comp1_qf09_pi2_monomer_fraction_pct")
        c2_mon = _extract_sec_pct("comp2_monomer_pct", "comp2_t006_monomer_fraction_pct")
        ck_mon = _extract_sec_pct("cocktail_1to1_monomer_pct", "cocktail_1to1_monomer_fraction_pct")
        dls_pdi, pdi_err = _shield_check_num(sec.get("dls_pdi"), "sec_mals_dls.dls_pdi", lo=0.0, hi=1.0)
        if pdi_err is not None:
            sec_input_missing_or_malformed = True
            _escalate("HOLD_INVALID_OR_MISSING_INPUT", f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: {pdi_err}")
        no_agg = sec.get("no_aggregates_or_precipitation")
        if type(no_agg) is not bool:
            sec_input_missing_or_malformed = True
            _escalate(
                "HOLD_INVALID_OR_MISSING_INPUT",
                f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.no_aggregates_or_precipitation must be a strict boolean (got {no_agg!r})",
            )

        c1_qc = sec.get("comp1_qf09_pi2_alone_monomer_qc_passed")
        c2_qc = sec.get("comp2_t006_alone_monomer_qc_passed")
        ck_qc = sec.get("cocktail_1to1_coformulation_qc_passed")
        for qc_lbl, qc_val in (
            ("comp1_qf09_pi2_alone_monomer_qc_passed", c1_qc),
            ("comp2_t006_alone_monomer_qc_passed", c2_qc),
            ("cocktail_1to1_coformulation_qc_passed", ck_qc),
        ):
            if type(qc_val) is not bool:
                sec_input_missing_or_malformed = True
                _escalate(
                    "HOLD_INVALID_OR_MISSING_INPUT",
                    f"PHYSICOCHEMICAL_MISSING_OR_MALFORMED_INPUT_HOLD: sec_mals_dls.{qc_lbl} must be a strict boolean (got {qc_val!r})",
                )

        quant_sec_failed = False
        c1_oos = bool(c1_mon is not None and c1_mon < _SHIELD_MIN_MONOMER_PCT)
        c2_oos = bool(c2_mon is not None and c2_mon < _SHIELD_MIN_MONOMER_PCT)
        ck_oos = bool(ck_mon is not None and ck_mon < _SHIELD_MIN_MONOMER_PCT)
        pdi_oos = bool(dls_pdi is not None and dls_pdi > _SHIELD_MAX_DLS_PDI)
        agg_oos = bool(no_agg is False)

        for mon_v, mon_oos, mon_qc, mon_lbl in (
            (c1_mon, c1_oos, c1_qc, "comp1_monomer_pct"),
            (c2_mon, c2_oos, c2_qc, "comp2_monomer_pct"),
            (ck_mon, ck_oos, ck_qc, "cocktail_1to1_monomer_pct"),