OpenIslet

scripts/run_layer3_structural_binder_plane.py

Layer 3: 3D Structural & Protein Interface Verification Plane (v0.8.0)

script Release v0.8.0 gen-eb955f8266035aba 410 KB · 25 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/run_layer3_structural_binder_plane.py · SHA-256: a070de200dfefd3d…

                "locus1_single_aav6_packaged_bp_if_appended": {"Embodiment_A_star": 5750, "Arm_1B": 5581, "aav6_cap_bp": 4700},
            },
        },
    }


def derive_required_product_functions_iip_0024(product_def: Mapping[str, Any]) -> Tuple[str, ...]:
    """
    Derive applicable Layer-3 engineered product functions directly from the registered
    product profile so Design B2 (5/5), Design Z (5/5), and Track-2 B'-SCT (6/6) are
    evaluated on their exact molecular manifests without Design-A HLA-E deadlock (IIP-0024; DEC-103).
    """
    pvid = str(product_def.get("product_variant_id", ""))
    if pvid.startswith("ISLET1_B_SCT_"):
        return ("HLA-E", "HLA-C_SCT", "CD47", "SERPINB9", "PD-L1", "iCasp9")
    if str(product_def.get("family", "A")) == "B":
        return ("HLA-C_endogenous_KIR", "CD47", "SERPINB9", "PD-L1", "iCasp9")
    return ("HLA-E", "CD47", "SERPINB9", "PD-L1", "iCasp9")


def evaluate_product_function_manifest_iip_0024(
    product_def: Mapping[str, Any],
    *,
    reference_coordinate_geometry_passed: bool,
    experimental_function_status_by_target: Mapping[str, str],
) -> Dict[str, Any]:
    """Separate coordinate-geometry verification from exact-product functional completion (IIP-0024; DEC-103)."""
    req_funcs = derive_required_product_functions_iip_0024(product_def)
    resolved = [fn for fn in req_funcs if experimental_function_status_by_target.get(fn) == "experimentally_verified"]
    unresolved = [fn for fn in req_funcs if experimental_function_status_by_target.get(fn) != "experimentally_verified"]
    func_ok = len(unresolved) == 0
    return {
        "product_variant_id": str(product_def["product_variant_id"]),
        "family": str(product_def.get("family", "")),
        "execution_supported": bool(product_def.get("execution_supported", False)),
        "required_product_function_count": len(req_funcs),
        "required_product_functions": list(req_funcs),
        "resolved_product_function_count": len(resolved),
        "unresolved_product_functions": unresolved,
        "structural_coordinate_geometry_status": "PASS" if reference_coordinate_geometry_passed else "FAIL",
        "functional_qualification_status": "PASS" if func_ok else "HOLD_UNRESOLVED_EXPERIMENTAL_PRODUCT_FUNCTIONS",
        "all_applicable_product_functions_resolved": func_ok,
    }


def _build_product_function_manifest_resolution_iip_0024(
    *,
    ref_geometry_passed: bool,
    serpinb9_func_status: str,
    cd47_pan_allele_status: str,
) -> Dict[str, Any]:
    """Build product-scoped Layer-3 function manifest audit across Design A, Design B2, Design Z, and B'-SCT (IIP-0024; DEC-103)."""
    from scripts.product_contract import get_product_definition

    baseline_exp_status: Dict[str, str] = {
        "HLA-E": "unresolved_pending_wetlab",
        "HLA-C_endogenous_KIR": "unresolved_pending_wetlab",
        "HLA-C_SCT": "unresolved_pending_wetlab",
        "CD47": cd47_pan_allele_status,
        "SERPINB9": serpinb9_func_status,
        "PD-L1": "unresolved_pending_wetlab",
        "iCasp9": "unresolved_pending_wetlab",
    }
    audited_ids = (
        "ISLET1_A4_ARM1B",
        "ISLET1_A4_X_MOL_RNLS_WT",
        "ISLET1_Z_X_MOL_140UM_ZERO_CAO2",
        "ISLET1_B2_5LOCUS_BASE",
        "ISLET1_B_SCT_C1_CIITA",
    )
    resolved_defs = {pid: get_product_definition(pid, root=REPO_ROOT, require_staged_fastas=False) for pid in audited_ids}
    baseline_profiles = {
        pid: evaluate_product_function_manifest_iip_0024(
            resolved_defs[pid],
            reference_coordinate_geometry_passed=ref_geometry_passed,
            experimental_function_status_by_target=baseline_exp_status,
        )
        for pid in audited_ids
    }
    return {
        "proposal_id": "IIP-0024",
        "decision_id": "DEC-103",
        "staged_design_b2_c1_c5_pair_sha256_reference": "82c351af082e0f97361e715c36e2ab5872b8d097efa1c9f59dfda53ea60496ea",
        "staged_design_b2_materialization_note": (
            "ISLET1_B2_5LOCUS_BASE remains held at execution_supported=False (STAGE0_RESEARCH_COMPARATOR_PENDING_ALLELE_QC) "
            "pending edited-allele QC; its 5/5 required functional manifest replaces HLA-E SCT with HLA-C_endogenous_KIR."
        ),
        "function_counts_by_route": {
            "ISLET1_A4_ARM1B": len(derive_required_product_functions_iip_0024(resolved_defs["ISLET1_A4_ARM1B"])),
            "ISLET1_Z_X_MOL_140UM_ZERO_CAO2": len(derive_required_product_functions_iip_0024(resolved_defs["ISLET1_Z_X_MOL_140UM_ZERO_CAO2"])),
            "ISLET1_B2_5LOCUS_BASE": len(derive_required_product_functions_iip_0024(resolved_defs["ISLET1_B2_5LOCUS_BASE"])),
            "ISLET1_B_SCT_C1_CIITA": len(derive_required_product_functions_iip_0024(resolved_defs["ISLET1_B_SCT_C1_CIITA"])),
        },
        "baseline_uncalibrated_product_profiles": baseline_profiles,
    }


def _build_icasp9_1nw9_1jxq_insertion_code_audit_iip_0024() -> Dict[str, Any]:
    """
    Compute subordinate CC0 1NW9 x 1JXQ XIAP-BIR3 vs CASP9 homodimer steric superposition
    under BOTH insertion-code policies (565/746 default excluding insertion codes vs 562/746
    retaining insertion codes < 2.2 A; Shiozaki et al. 2003 PMID:12620238; Renatus et al. 2001
    PMID:11734640; IIP-0024; DEC-103).
    """
    snaps = verify_structural_reference_inputs("pdb_00001nw9.cif", "pdb_00001jxq.cif")
    policies: Dict[str, Any] = {}
    for label, keep_ins in (("default_exclude_insertion_codes", False), ("retain_insertion_codes", True)):
        nw9_atoms = [
            a for a in parse_mmcif_atoms(
                STRUCT_DIR / "pdb_00001nw9.cif",
                keep_insertion_codes=keep_ins,
                source_bytes=snaps["pdb_00001nw9.cif"],
                include_zero_occupancy=False,
            )
            if a["group"] == "ATOM"
        ]
        jxq_atoms = [
            a for a in parse_mmcif_atoms(
                STRUCT_DIR / "pdb_00001jxq.cif",
                keep_insertion_codes=keep_ins,
                source_bytes=snaps["pdb_00001jxq.cif"],
                include_zero_occupancy=False,
            )
            if a["group"] == "ATOM"
        ]
        nw9_by_c: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
        for a in nw9_atoms:
            nw9_by_c[a["chain"]].append(a)
        jxq_by_c: Dict[str, List[Dict[str, Any]]] = defaultdict(list)
        for a in jxq_atoms:
            jxq_by_c[a["chain"]].append(a)
        nw9_sorted = sorted(nw9_by_c.items(), key=lambda kv: len(kv[1]), reverse=True)
        casp_atoms, bir3_atoms = nw9_sorted[0][1], nw9_sorted[1][1]
        ca1 = [a for a in casp_atoms if a["atom_id"] == "CA"]
        ca2 = [a for a in jxq_by_c["A"] if a["atom_id"] == "CA"]
        sm = difflib.SequenceMatcher(None, [a["comp_id"] for a in ca1], [a["comp_id"] for a in ca2], autojunk=False)
        mp = [(ca1[i + k], ca2[j + k]) for i, j, n_b in sm.get_matching_blocks() for k in range(n_b)]
        P = np.array([[p[0]["x"], p[0]["y"], p[0]["z"]] for p in mp], dtype=np.float64)
        Q = np.array([[p[1]["x"], p[1]["y"], p[1]["z"]] for p in mp], dtype=np.float64)
        pc, qc = P.mean(axis=0), Q.mean(axis=0)
        H = np.einsum("ij,ik->jk", P - pc, Q - qc)
        U, _, Vt = np.linalg.svd(H)
        if np.linalg.det(U) * np.linalg.det(Vt) < 0:
            U[:, -1] *= -1
        R = np.einsum("ij,jk->ik", U, Vt)
        P_al = np.einsum("ij,jk->ik", P - pc, R) + qc
        rmsd = float(np.sqrt(np.mean(np.sum((P_al - Q) ** 2, axis=1))))
        b_xyz = np.einsum("ij,jk->ik", np.array([[a["x"], a["y"], a["z"]] for a in bir3_atoms if a["element"] != "H"]) - pc, R) + qc
        p_xyz = np.array([[a["x"], a["y"], a["z"]] for a in jxq_by_c["B"] if a["element"] != "H"])
        d2 = np.sum((b_xyz[:, None, :] - p_xyz[None, :, :]) ** 2, axis=2)
        policies[label] = {
            "keep_insertion_codes": keep_ins,
            "allowed_alt_ids": [".", "?", "A"],
            "include_zero_occupancy": False,
            "aligned_ca_residues": len(mp),
            "ca_rmsd_angstrom": round(rmsd, 4),
            "xiap_bir3_heavy_atoms_total": int(len(b_xyz)),
            "xiap_bir3_heavy_atoms_lt_2p2A": int(np.sum(np.any(d2 < (2.2 ** 2), axis=1))),
            "heavy_atom_clash_pairs_lt_2p2A": int(np.sum(d2 < (2.2 ** 2))),
            "role": "SUBORDINATE_NON_BLOCKING_DIAGNOSTIC_ONLY",
        }
    if (
        policies["default_exclude_insertion_codes"]["xiap_bir3_heavy_atoms_total"] != 746
        or policies["default_exclude_insertion_codes"]["xiap_bir3_heavy_atoms_lt_2p2A"] != 565
        or policies["retain_insertion_codes"]["xiap_bir3_heavy_atoms_total"] != 746
        or policies["retain_insertion_codes"]["xiap_bir3_heavy_atoms_lt_2p2A"] != 562
    ):
        raise ValueError(f"Unexpected 1NW9 x 1JXQ insertion-code superposition counts: {policies}")
    return {
        "proposal_id": "IIP-0024",
        "decision_id": "DEC-103",
        "primary_citations": ["PMID:12620238", "PMID:11734640", "PMID:22047558"],
        "policies": policies,
    }


def _build_locus2_cfh_stp_gpi_structural_provenance_iip_0031() -> Dict[str, Any]:
    """
    Dynamically compute experimental PDB 2WII (data/structures/2WII.pdb) and PDB 5O32
    (data/structures/5O32.pdb) structural contacts, disulfides, and domain distances from
    hash-verified coordinate snapshots on disk, and record external UniProtKB/Swiss-Prot
    sequence-census provenance for the Locus-2 CFH(1-264)-CD55(286-381)-GPI Mode-B v3
    research comparators (IIP-0031; DEC-110).
    Explicitly verifies and excludes the deposited non-native post-Glu264 Ala in 2WII
    from native Lys265 geometry claims.
    """
    snaps = verify_structural_reference_inputs("2WII.pdb", "5O32.pdb")
    pdb_2wii_bytes = snaps["2WII.pdb"]
    pdb_5o32_bytes = snaps["5O32.pdb"]
    pdb_2wii_sha = hashlib.sha256(pdb_2wii_bytes).hexdigest()
    pdb_5o32_sha = hashlib.sha256(pdb_5o32_bytes).hexdigest()

    aa3 = {
        "ALA": "A", "ARG": "R", "ASN": "N", "ASP": "D", "CYS": "C",
        "GLN": "Q", "GLU": "E", "GLY": "G", "HIS": "H", "ILE": "I",
        "LEU": "L", "LYS": "K", "MET": "M", "PHE": "F", "PRO": "P",
        "SER": "S", "THR": "T", "TRP": "W", "TYR": "Y", "VAL": "V",
    }

    def _parse_pdb_resolution(txt: str) -> float:
        m = re.search(r"REMARK\s+2\s+RESOLUTION\.\s+([0-9.]+)\s+ANGSTROMS", txt)
        if not m:
            raise ValueError("Missing REMARK 2 RESOLUTION header in PDB snapshot")
        return round(float(m.group(1)), 2)

    txt_2wii = pdb_2wii_bytes.decode("utf-8")
    res_2wii = _parse_pdb_resolution(txt_2wii)
    atoms_c3b: List[Tuple[str, int, str, str, float, float, float]] = []
    atoms_cfh: List[Tuple[str, int, str, str, float, float, float]] = []
    cfh_res_map: Dict[int, str] = {}
    cfh_resname_map: Dict[int, str] = {}
    for ln in txt_2wii.splitlines():
        if not ln.startswith("ATOM  ") or ln[16] not in (" ", "A"):
            continue
        aname = ln[12:16].strip()
        elem = ln[76:78].strip() if len(ln) >= 78 else aname[0]
        if elem == "H":
            continue
        resname, ch, resseq = ln[17:20].strip(), ln[21], int(ln[22:26])
        x, y, z = float(ln[30:38]), float(ln[38:46]), float(ln[46:54])
        if ch in ("A", "B"):
            atoms_c3b.append((ch, resseq, resname, aname, x, y, z))
        elif ch == "C":
            atoms_cfh.append((ch, resseq, resname, aname, x, y, z))
            cfh_res_map[resseq] = aa3.get(resname, "X")
            cfh_resname_map[resseq] = resname

    offset_2wii = 18
    if cfh_res_map.get(21 - offset_2wii) != "C" or cfh_res_map.get(62 - offset_2wii) != "I" or cfh_res_map.get(264 - offset_2wii) != "E":
        raise ValueError("Unexpected PDB 2WII chain-C residue numbering or Ile62/Glu264 identity")
    post_glu264_resname = cfh_resname_map.get(265 - offset_2wii)
    if post_glu264_resname != "ALA":
        raise ValueError(f"Expected non-native ALA at PDB 2WII C:{265 - offset_2wii}, got {post_glu264_resname}")

    sg_atoms = [
        (r + offset_2wii, x, y, z)
        for (_, r, rn, an, x, y, z) in atoms_cfh
        if rn == "CYS" and an == "SG" and (r + offset_2wii) <= 264
    ]
    disulfides: List[Dict[str, Any]] = []
    for i in range(len(sg_atoms)):
        for j in range(i + 1, len(sg_atoms)):
            d_sg = math.dist(sg_atoms[i][1:], sg_atoms[j][1:])
            if d_sg <= 2.5:
                disulfides.append({
                    "cys_a_p08603": sg_atoms[i][0],
                    "cys_b_p08603": sg_atoms[j][0],
                    "sg_sg_angstrom": round(d_sg, 3),
                })
    if len(disulfides) != 8: