OpenIslet

scripts/run_layer5_personalized_patient_compiler.py

Layer 5: Personalized Patient-DNA & Multiplex CRISPR Compiler + 10,000-Patient Global Population Simulator (v0.8.0)

script Release v0.8.0 gen-eb955f8266035aba 494 KB · 31 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_layer5_personalized_patient_compiler.py · SHA-256: 0b285369ac6e4409…

    single_200cm2_pouch_forced_eval = _validate_strict_bool(
        _get_opt(patient, "single_200cm2_pouch_forced_without_modular_scaling", False),
        "single_200cm2_pouch_forced_without_modular_scaling",
    )
    tyk2_indep_ifng_dr3_dq8_hazard_eval = _validate_strict_bool(
        _get_opt(patient, "tyk2_independent_ifng_tnfa_or_indirect_dr3_dq8_hazard", False),
        "tyk2_independent_ifng_tnfa_or_indirect_dr3_dq8_hazard",
    )
    gate0_indirect_dr3_dq8_cleared_eval = _validate_strict_bool(
        _get_opt(patient, "gate0_indirect_dr3_dq8_and_ifng_tnfa_stress_cleared", False),
        "gate0_indirect_dr3_dq8_and_ifng_tnfa_stress_cleared",
    )
    bw4_kir3dl1_educated_eval = _validate_strict_bool(
        _get_opt(patient, "recipient_bw4_kir3dl1_educated", False),
        "recipient_bw4_kir3dl1_educated",
    )
    bw4_kir3dl1_cleared_eval = _validate_strict_bool(
        _get_opt(patient, "bw4_kir3dl1_sorted_nk_lysis_cleared", False),
        "bw4_kir3dl1_sorted_nk_lysis_cleared",
    )
    if kir_nk >= 0.30 and not has_arm1b_floor:
        p_surv -= 0.45 * (kir_nk - 0.25)
    elif (
        kir_nk >= 0.30
        and not has_plan_b_hlac
        and arm1b_cd47_sirpa_nk_cliff_unverified_eval
        and not track1_sorted_kir_nk_cleared_eval
    ):
        p_surv -= 0.35 * (kir_nk - 0.25)
    if kir_nk >= 0.48 and not has_plan_b_hlac:
        p_surv -= 0.28 * (kir_nk - 0.45)
    # Propagate recipient HLA-B/A Bw4+ KIR3DL1-educated missing-self NK hazard when sorted KIR3DL1+NKG2A- NK clearance is unverified (L5A-01 [DEC-077]):
    if bw4_kir3dl1_educated_eval and not bw4_kir3dl1_cleared_eval:
        p_surv -= 0.18

    # 1b. Propagate 4-digit HLA-C alloreactive T-cell, anti-HLA-C heavy-chain DSA, activating KIR2DS1,
    # anti-HLA-E DSA, high-cPRA sensitization, and CMV NKG2C+ adaptive NK hazards across BOTH endogenous HLA-C
    # and fixed-peptide B'-SCT (AUDIT-REF, AUDIT-REF, & AUDIT-REF;
    # Aubert et al. 2014 PMID:24804568; Whitelegg et al. 2005 PMID:16034111; Yu et al. 2002 PMID:11907065; Stewart et al. 2005 PMID:16141329):
    raw_eval_hlac_rel = _get_opt(
        patient,
        "hlac_4digit_relation",
        "UNSPECIFIED_ASSUMED_4DIGIT_OR_B_PRIME_SCT" if is_b_prime_sct else "SELF_4DIGIT_MATCH",
    )
    if not isinstance(raw_eval_hlac_rel, str) or not raw_eval_hlac_rel.strip():
        raise ValueError(f"Invalid hlac_4digit_relation {raw_eval_hlac_rel!r}: must be a non-empty string (AUDIT-REF)")
    hlac_4digit_rel = raw_eval_hlac_rel.strip().upper()
    if hlac_4digit_rel not in _VALID_HLAC_4DIGIT_RELATIONS:
        raise ValueError(f"Invalid hlac_4digit_relation {hlac_4digit_rel!r} (AUDIT-REF)")
    has_anti_hlac_dsa = _validate_strict_bool(
        _get_opt(patient, "recipient_anti_hla_c_dsa", False), "recipient_anti_hla_c_dsa"
    )
    sct_crossmatch_cleared = _validate_strict_bool(
        _get_opt(patient, "b_prime_sct_recipient_crossmatch_and_mlc_cleared", False),
        "b_prime_sct_recipient_crossmatch_and_mlc_cleared",
    )
    sct_serum_crossmatch_neg = _validate_strict_bool(
        _get_opt(patient, "b_prime_sct_exact_product_serum_cdc_adcc_crossmatch_negative", False),
        "b_prime_sct_exact_product_serum_cdc_adcc_crossmatch_negative",
    )
    sct_cd8_mlc_neg = _validate_strict_bool(
        _get_opt(patient, "b_prime_sct_recipient_cd8_mlc_negative", False),
        "b_prime_sct_recipient_cd8_mlc_negative",
    )
    has_activating_kir2ds1_hazard = _validate_strict_bool(
        _get_opt(patient, "recipient_activating_kir2ds1_unlicensed_hazard", False),
        "recipient_activating_kir2ds1_unlicensed_hazard",
    )
    kir2ds1_cleared = _validate_strict_bool(
        _get_opt(patient, "b_prime_sct_kir2ds1_non_activation_cleared", False),
        "b_prime_sct_kir2ds1_non_activation_cleared",
    )
    has_anti_hlae_dsa = _validate_strict_bool(
        _get_opt(patient, "recipient_anti_hla_e_dsa", False), "recipient_anti_hla_e_dsa"
    )
    exact_hlae_sct_serum_crossmatch_neg = _validate_strict_bool(
        _get_opt(patient, "exact_hlae_sct_serum_crossmatch_negative", False),
        "exact_hlae_sct_serum_crossmatch_negative",
    )
    exact_product_non_hla_and_sct_flow_crossmatch_neg = _validate_strict_bool(
        _get_opt(patient, "exact_product_non_hla_and_sct_flow_crossmatch_negative", False),
        "exact_product_non_hla_and_sct_flow_crossmatch_negative",
    )
    gate0a_nkg2c_adaptive_nk_cleared = _validate_strict_bool(
        _get_opt(patient, "gate0a_nkg2c_adaptive_nk_cleared", False),
        "gate0a_nkg2c_adaptive_nk_cleared",
    )
    recipient_cpra_pct = _validate_finite_float(
        _get_opt(patient, "recipient_cpra_pct", 0.0), "recipient_cpra_pct", 0.0, 100.0
    )
    cmv_nkg2c_adaptive_nk_fraction = _validate_finite_float(
        _get_opt(patient, "cmv_nkg2c_adaptive_nk_fraction", 0.0), "cmv_nkg2c_adaptive_nk_fraction", 0.0, 1.0
    )
    sct_cd8_mlc_explicit_false_eval = (
        "b_prime_sct_recipient_cd8_mlc_negative" in patient
        and patient.get("b_prime_sct_recipient_cd8_mlc_negative") is False
    )
    sct_serum_xm_explicit_false_eval = (
        "b_prime_sct_exact_product_serum_cdc_adcc_crossmatch_negative" in patient
        and patient.get("b_prime_sct_exact_product_serum_cdc_adcc_crossmatch_negative") is False
    )
    anti_hlac_dsa_cleared = bool(
        is_b_prime_sct and sct_serum_crossmatch_neg and not sct_serum_xm_explicit_false_eval
    )
    cd8_mlc_cleared = bool(
        is_b_prime_sct
        and not sct_cd8_mlc_explicit_false_eval
        and not sct_serum_xm_explicit_false_eval
        and (sct_cd8_mlc_neg or sct_crossmatch_cleared)
    )
    _c2_bearing_sct_ids = {
        "ISLET1_B_SCT_C2_CIITA",
        "ISLET1_B_SCT_C2_CIITA_TYK2_PE",
        "ISLET1_B_SCT_C1C2_CIITA",
        "ISLET1_B_SCT_C1C2_CIITA_TYK2_PE",
    }
    if is_plan_b_arch:
        if has_anti_hlac_dsa and not anti_hlac_dsa_cleared:
            p_surv -= 0.40
        is_c2_b_prime_sct = is_b_prime_sct and (
            pvid_str in _c2_bearing_sct_ids
            or "C0401" in loc1_str
            or "C*04:01" in prod_def_hlac_str
            or raw_recip_kir in {"C2/C2", "C1/C2"}
        )
        if hlac_4digit_rel == "KIR_LIGAND_MISMATCH":
            p_surv -= 0.55
        elif (
            hlac_4digit_rel in {"KIR_MATCH_FOREIGN_4DIGIT_ALLO", "UNSPECIFIED_ASSUMED_4DIGIT_OR_B_PRIME_SCT", "UNSPECIFIED"}
            or is_c2_b_prime_sct
            or (is_b_prime_sct and (sct_cd8_mlc_explicit_false_eval or sct_serum_xm_explicit_false_eval))
        ) and not cd8_mlc_cleared:
            p_surv -= (0.08 if is_b_prime_sct else 0.25)
        if (
            has_activating_kir2ds1_hazard
            and not kir2ds1_cleared
            and (
                pvid_str in _c2_bearing_sct_ids
                or "C0401" in loc1_str
                or "C*04:01" in prod_def_hlac_str
                or raw_recip_kir in {"C2/C2", "C1/C2"}
            )
        ):
            p_surv -= 0.12
    if has_anti_hlae_dsa and not exact_hlae_sct_serum_crossmatch_neg:
        p_surv -= 0.35
    exact_product_crossmatch_explicit_false_eval = (
        "exact_product_non_hla_and_sct_flow_crossmatch_negative" in patient
        and patient.get("exact_product_non_hla_and_sct_flow_crossmatch_negative") is False
    )
    if (
        exact_product_crossmatch_explicit_false_eval
        or (
            "TRACK_3" not in track_str
            and recipient_cpra_pct >= 80.0
            and not exact_product_non_hla_and_sct_flow_crossmatch_neg
        )
    ):
        p_surv -= 0.25
    if cmv_nkg2c_adaptive_nk_fraction >= 0.25 and not gate0a_nkg2c_adaptive_nk_cleared:
        p_surv -= 0.12

    # 2. Check high-IFN-I / high-autoantigen / IFIH1 / PTPN22 T-cell protection via TYK2 P1104A shield:
    # A graft has biological TYK2 P1104A protection if EITHER edited via OI-GRNA-05 OR naturally homozygous C/C on the graft genome (AUDIT-REF):
    is_track3 = "TRACK_3" in track_str
    raw_recip_tyk2 = str(
        _get_opt(patient, "recipient_tyk2_rs34536443_genotype", _get_opt(patient, "tyk2_rs34536443_genotype", "G/G"))
    ).strip().upper()
    raw_donor_tyk2 = str(_get_opt(patient, "donor_tyk2_rs34536443_genotype", "G/G")).strip().upper()
    for label, genotype in (("recipient_tyk2_rs34536443_genotype", raw_recip_tyk2),
                            ("donor_tyk2_rs34536443_genotype", raw_donor_tyk2)):
        if genotype not in VALID_TYK2_RS34536443_GENOTYPES:
            raise ValueError(f"Invalid {label} {genotype!r}")
    starting_graft_tyk2 = raw_recip_tyk2 if is_track3 else raw_donor_tyk2
    naturally_protected_tyk2_cc = starting_graft_tyk2 == "C/C"
    # Derived labels cannot grant protection contrary to the actual edited genome.
    for block, key, expected in (
        (dosing_block, "already_homozygous_tyk2_p1104a", naturally_protected_tyk2_cc),
        (compiled_regimen, "final_graft_tyk2_p1104a_protective_state", naturally_protected_tyk2_cc or tyk2_flag),
    ):
        if key in block and _validate_strict_bool(block[key], key) != expected:
            raise ValueError(f"Inconsistent derived TYK2 protection field {key!r}")
    has_edited_tyk2_shield = (
        tyk2_flag
        and "OI-GRNA-05_TYK2_P1104A_PEMAX_EPEGRNA_OPTIONAL" in panel
    )
    has_biological_tyk2_shield = bool(has_edited_tyk2_shield or naturally_protected_tyk2_cc)

    ifih1_mod = 0.08 if ifih1_g == "T/T" else (0.03 if ifih1_g in {"C/T", "T/C"} else 0.0)
    ptpn22_mod = 0.10 if ptpn22_g == "A/A" else (0.04 if ptpn22_g in {"G/A", "A/G"} else 0.0)
    hyper_ifn_stress = max(0.0, (ifn1_pct - 75.0) / 25.0) * max(0.0, auto_z - 1.5) + ifih1_mod + ptpn22_mod
    needs_tyk2 = (ifn1_pct >= 80.0 or auto_z >= 2.0 or ifih1_g == "T/T" or ptpn22_g == "A/A")
    if needs_tyk2 and not has_biological_tyk2_shield:
        p_surv -= 0.095 * (1.0 + hyper_ifn_stress)
    else:
        p_surv -= 0.010 * hyper_ifn_stress
    if (auto_z >= 2.0 or tyk2_indep_ifng_dr3_dq8_hazard_eval) and not gate0_indirect_dr3_dq8_cleared_eval:
        p_surv -= 0.14

    # 3. Propagate recipient HLA-E genotype (*01:01 vs *01:03 endogenous NKG2A licensing tuning; AUDIT-REF)
    # and causal SIRPa V1/V2/V8 macrophage clearance risk when CD47 shield is absent or compromised (AUDIT-REF):
    if hla_e_g == "*01:01/*01:01":
        p_surv -= 0.004 if has_arm1b_floor else 0.035
    elif hla_e_g in {"*01:01/*01:03", "*01:03/*01:01"}:
        p_surv -= 0.002 if has_arm1b_floor else 0.015
    plan_b_declares_cd47 = has_plan_b_hlac and prod_def_cd47_ok and ((("CD47" in loc1_str) and ("CD47" in plan_str)) or is_b_prime_sct)
    # Note: SIRPA v8 binding to CD47-IgV-GPI is unmeasured (`HOLD_CD47_SIRPA_BINDING_COMPROMISED_REQUIRES_ALTERNATIVE_STEALTH_QUALIFICATION`),
    # so `v8` carriers are treated as unverified CD47-SIRPa stealth unless explicitly cleared (L5A-01 [DEC-077]):
    has_cd47_shield = ((("CD47" in loc1_str) and not is_plan_b_arch) or plan_b_declares_cd47) and (not cd47_compromised) and ("v8" not in sirpa_g)
    # Propagate SP-3C (HLA-C*07:02 VMAPRALLL, UniProt P10321) leader deficit when exogenous HLA-E SCT is absent (S55R2-D5-02):
    if is_plan_b_arch and prod_def_leader_class == "SP-3C" and not plan_b_declares_cd47:
        p_surv -= 0.04
    if not has_cd47_shield:
        if sirpa_g == "v1/v1":
            p_surv -= 0.22
        elif sirpa_g == "v1/v2":
            p_surv -= 0.26
        else:
            p_surv -= 0.32

    # 4. Propagate local areal loading against the selected physical route (BPR-09 / DEC-133),
    # preserving the forced 200 cm^2 single-pouch stress control and optional legacy fixed-200 cm^2 provenance:
    areal_loading_single_pouch = dose_ieq / 200.0
    if single_200cm2_pouch_forced_eval:
        effective_areal_loading = areal_loading_single_pouch
    elif loading_area_mode == "selected_physical_route":
        raw_mod_load = dosing_block.get("actual_areal_loading_ieq_per_cm2_across_modular_cassettes")
        raw_mod_cassettes = dosing_block.get("modular_100cm2_omental_cassette_count_at_1500_ieq_cm2")
        if raw_mod_load is not None:
            effective_areal_loading = _validate_finite_float(
                raw_mod_load, "actual_areal_loading_ieq_per_cm2_across_modular_cassettes", 50.0, 20000.0
            )
        elif raw_mod_cassettes is not None and not isinstance(raw_mod_cassettes, bool) and int(raw_mod_cassettes) >= 1:
            effective_areal_loading = dose_ieq / (float(int(raw_mod_cassettes)) * 100.0)
        else:
            effective_areal_loading = areal_loading_single_pouch
    else:
        effective_areal_loading = areal_loading_single_pouch

    if single_200cm2_pouch_forced_eval and effective_areal_loading > 1500.0:
        p_surv -= min(0.65, 0.38 * ((effective_areal_loading - 1500.0) / 1000.0))
    elif effective_areal_loading > 2500.0:
        p_surv -= 0.045 * ((effective_areal_loading - 2500.0) / 2500.0)
    elif effective_areal_loading < 2500.0:
        p_surv -= 0.008 * ((2500.0 - effective_areal_loading) / 2500.0)

    if burden_pen > 0.0 and dose_ieq > 500000.0:
        p_surv -= burden_pen * ((dose_ieq - 500000.0) / 100000.0)

    # 5. Check active editing-genome failure if the genome being edited (recipient in Track 3, donor MCB in Track 1/2) carries a rare protospacer SNP without custom gRNA or with PAM disruption (AUDIT-REF & AUDIT-REF):