OpenIslet

scripts/run_layer2_perturbation_and_regulatory_plane.py

Layer 2: Single-Cell Perturbation & Regulatory Sequence Plane (v0.8.0)

script Release v0.8.0 gen-eb955f8266035aba 1.13 MB · 71 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_layer2_perturbation_and_regulatory_plane.py · SHA-256: 3220e68f97203dbc…

        "audited_extracellular_chains": {'Human_CAT_P04040': {'length_aa': 527, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '0/2074', 'foreign_8_11mer_pct': 0.0, 'unique_foreign_8_11mers': 0, 'positional_foreign_15mers': '0/513', 'foreign_15mer_pct': 0.0, 'unique_foreign_15mers': 0, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'Bovine_CAT_P00432': {'length_aa': 527, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '1049/2074', 'foreign_8_11mer_pct': 50.58, 'unique_foreign_8_11mers': 1049, 'positional_foreign_15mers': '321/513', 'foreign_15mer_pct': 62.57, 'unique_foreign_15mers': 321, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'Aspergillus_CAT_P55303': {'length_aa': 730, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '2884/2886', 'foreign_8_11mer_pct': 99.93, 'unique_foreign_8_11mers': 2884, 'positional_foreign_15mers': '716/716', 'foreign_15mer_pct': 100.0, 'unique_foreign_15mers': 716, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'SA_P22629_1based_37_163': {'length_aa': 127, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '474/474', 'foreign_8_11mer_pct': 100.0, 'unique_foreign_8_11mers': 474, 'positional_foreign_15mers': '113/113', 'foreign_15mer_pct': 100.0, 'unique_foreign_15mers': 113, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 132, 'pairs_8_11mer_x_14_hlac_le_100nM': 31, 'pairs_9mer_x_14_hlac_le_500nM': 84, 'pairs_9mer_x_14_hlac_le_100nM': 23, 'strongest_HLA_C0304_hit': [40.92, 'ATDGSGTAL']}}, 'SA_mature_P22629_25_183': {'length_aa': 159, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '602/602', 'foreign_8_11mer_pct': 100.0, 'unique_foreign_8_11mers': 602, 'positional_foreign_15mers': '145/145', 'foreign_15mer_pct': 100.0, 'unique_foreign_15mers': 145, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'CFH_19_448': {'length_aa': 430, 'sequence_sha256': None, 'sequence': '', 'positional_foreign_8_11mers': '0/1686', 'foreign_8_11mer_pct': 0.0, 'unique_foreign_8_11mers': 0, 'positional_foreign_15mers': '0/416', 'foreign_15mer_pct': 0.0, 'unique_foreign_15mers': 0, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'THBD_363_497_Cys': {'length_aa': 136, 'sequence_sha256': None, 'sequence': '', 'positional_foreign_8_11mers': '4/510', 'foreign_8_11mer_pct': 0.78, 'unique_foreign_8_11mers': 4, 'positional_foreign_15mers': '1/122', 'foreign_15mer_pct': 0.82, 'unique_foreign_15mers': 1, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 0, 'pairs_8_11mer_x_14_hlac_le_100nM': 0, 'pairs_9mer_x_14_hlac_le_500nM': 0, 'pairs_9mer_x_14_hlac_le_100nM': 0, 'strongest_HLA_C0304_hit': [8694.97, 'SGSGEPPPC']}}, 'H1a_tagless_ADIPOQ_FasL_232aa': {'length_aa': 232, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '34/894', 'foreign_8_11mer_pct': 3.8, 'unique_foreign_8_11mers': 34, 'positional_foreign_15mers': '14/218', 'foreign_15mer_pct': 6.42, 'unique_foreign_15mers': 14, 'mhcflurry_2p3p28_14_hlac_summary': None}, 'H1a_CysGGS_ADIPOQ_FasL_236aa': {'length_aa': 236, 'sequence_sha256': None, 'sequence': '', 'positional_foreign_8_11mers': '50/910', 'foreign_8_11mer_pct': 5.49, 'unique_foreign_8_11mers': 50, 'positional_foreign_15mers': '18/222', 'foreign_15mer_pct': 8.11, 'unique_foreign_15mers': 18, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 0, 'pairs_8_11mer_x_14_hlac_le_100nM': 0, 'pairs_9mer_x_14_hlac_le_500nM': 0, 'pairs_9mer_x_14_hlac_le_100nM': 0, 'strongest_HLA_C0304_hit': [9273.96, 'GEPGEGEKKEL']}}, 'T0_CysGGS_TNC_FasL_177aa': {'length_aa': 177, 'sequence_sha256': None, 'sequence': '', 'positional_foreign_8_11mers': '49/674', 'foreign_8_11mer_pct': 7.27, 'unique_foreign_8_11mers': 49, 'positional_foreign_15mers': '18/163', 'foreign_15mer_pct': 11.04, 'unique_foreign_15mers': 18, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 3, 'pairs_8_11mer_x_14_hlac_le_100nM': 0, 'pairs_9mer_x_14_hlac_le_500nM': 3, 'pairs_9mer_x_14_hlac_le_100nM': 0, 'strongest_HLA_C0304_hit': [3749.52, 'SLREQEKKEL']}}, 'IgG1Fc_LALAPG_direct_FasL_368aa': {'length_aa': 368, 'sequence_sha256': None, 'sequence': '', 'positional_foreign_8_11mers': '106/1438', 'foreign_8_11mer_pct': 7.37, 'unique_foreign_8_11mers': 106, 'positional_foreign_15mers': '42/354', 'foreign_15mer_pct': 11.86, 'unique_foreign_15mers': 42, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 65, 'pairs_8_11mer_x_14_hlac_le_100nM': 25, 'pairs_9mer_x_14_hlac_le_500nM': 38, 'pairs_9mer_x_14_hlac_le_100nM': 20, 'strongest_HLA_C0304_hit': [27.61, 'AAGGPSVFL']}}, 'Human_CAT_P04040_MetCleaved_2_527': {'length_aa': 526, 'sequence_sha256': None, 'sequence': None, 'positional_foreign_8_11mers': '0/2070', 'foreign_8_11mer_pct': 0.0, 'unique_foreign_8_11mers': 0, 'positional_foreign_15mers': '0/512', 'foreign_15mer_pct': 0.0, 'unique_foreign_15mers': 0, 'mhcflurry_2p3p28_14_hlac_summary': {'pairs_8_11mer_x_14_hlac_le_500nM': 0, 'pairs_8_11mer_x_14_hlac_le_100nM': 0}}},
    }


def _build_locus3_multimir_and_cbe_needle_manifest_iip_0018(
    iip_0015_manifest: Dict[str, Any],
) -> Dict[str, Any]:
    """Build deterministic Locus-3 Multi-miR Mode-B & CBE needle qualification manifest (IIP-0018; DEC-096)."""
    l3_fasta_path = REPO_ROOT / "data/constructs/islet1_locus3_multimir_modeb_comparators_v0.8.0.fasta"
    if not l3_fasta_path.is_file():
        raise FileNotFoundError(f"Missing required Locus-3 comparator FASTA: {l3_fasta_path}")

    def _req_0018(cond: bool, msg: str) -> None:
        if not cond:
            raise ValueError(msg)

    raw_fasta = l3_fasta_path.read_text(encoding="utf-8")
    fasta_sha256 = hashlib.sha256(l3_fasta_path.read_bytes()).hexdigest()
    _req_0018(
        fasta_sha256 == _IIP_0018_OFFLINE_REFERENCE_BUNDLE["comparator_fasta_sha256"],
        f"Locus-3 comparator FASTA SHA-256 mismatch: {fasta_sha256}",
    )

    l3_pam_list = iip_0015_manifest["pam_seed_shielded_synthesis_ready_manifest"][
        "locus3_cxcl10_ha_r_nt17_g_to_c_pam_shielded"
    ]
    wt_parent_seq = next(
        x["pam_shielded_donor_sequence"]
        for x in l3_pam_list
        if x["construct_id"] == "L3_BFB_PDL1_SYN4_PAM_SHIELDED_4078"
    ).upper()

    flank_up = _IIP_0018_OFFLINE_REFERENCE_BUNDLE["chr4_flank_up_300_minus_strand"]
    flank_dn = _IIP_0018_OFFLINE_REFERENCE_BUNDLE["chr4_flank_dn_300_minus_strand"]
    _req_0018(len(flank_up) == 300 and len(flank_dn) == 300, "Invalid Locus-3 flank length")

    codon_table = {
        "TTT": "F", "TTC": "F", "TTA": "L", "TTG": "L",
        "CTT": "L", "CTC": "L", "CTA": "L", "CTG": "L",
        "ATT": "I", "ATC": "I", "ATA": "I", "ATG": "M",
        "GTT": "V", "GTC": "V", "GTA": "V", "GTG": "V",
        "TCT": "S", "TCC": "S", "TCA": "S", "TCG": "S",
        "CCT": "P", "CCC": "P", "CCA": "P", "CCG": "P",
        "ACT": "T", "ACC": "T", "ACA": "T", "ACG": "T",
        "GCT": "A", "GCC": "A", "GCA": "A", "GCG": "A",
        "TAT": "Y", "TAC": "Y", "TAA": "*", "TAG": "*",
        "CAT": "H", "CAC": "H", "CAA": "Q", "CAG": "Q",
        "AAT": "N", "AAC": "N", "AAA": "K", "AAG": "K",
        "GAT": "D", "GAC": "D", "GAA": "E", "GAG": "E",
        "TGT": "C", "TGC": "C", "TGA": "*", "TGG": "W",
        "CGT": "R", "CGC": "R", "CGA": "R", "CGG": "R",
        "AGT": "S", "AGC": "S", "AGA": "R", "AGG": "R",
        "GGT": "G", "GGC": "G", "GGA": "G", "GGG": "G",
    }

    def _translate_orf(dna_seq: str) -> str:
        _req_0018(len(dna_seq) % 3 == 0, "ORF length must be divisible by 3")
        aas = [codon_table[dna_seq[i : i + 3]] for i in range(0, len(dna_seq), 3)]
        _req_0018(aas[-1] == "*" and "*" not in aas[:-1], "ORF stop codon invariant failed")
        return "".join(aas[:-1])

    def _kmers(prot_seq: str, lengths: Tuple[int, ...]) -> set[str]:
        out_set: set[str] = set()
        for k_len in lengths:
            for idx in range(0, len(prot_seq) - k_len + 1):
                out_set.add(prot_seq[idx : idx + k_len])
        return out_set

    sb9_wt_prot = _translate_orf(wt_parent_seq[888:2019])
    pdl1_wt_start = wt_parent_seq.index("ATGAGAATCTTCGCC")
    pdl1_wt_prot = _translate_orf(wt_parent_seq[pdl1_wt_start : pdl1_wt_start + 873])
    wt_c1 = _kmers(sb9_wt_prot, (8, 9, 10, 11)) | _kmers(pdl1_wt_prot, (8, 9, 10, 11))
    wt_c2 = _kmers(sb9_wt_prot, (15,)) | _kmers(pdl1_wt_prot, (15,))

    parsed_records = []
    for block in raw_fasta.strip().split(">")[1:]:
        lines = block.splitlines()
        hdr = lines[0].strip()
        cid = hdr.split()[0]
        seq = "".join(lines[1:]).strip().upper()
        seq_sha = hashlib.sha256(seq.encode("utf-8")).hexdigest()
        _req_0018(f"sha256={seq_sha}" in hdr, f"Header SHA mismatch on {cid}")

        is_parent = cid == "ARM1_L3_WT_PARENT_4078"
        if is_parent:
            _req_0018(seq == wt_parent_seq and len(seq) == 4078, f"Parent donor mismatch on {cid}")
            c_end = 3478
            exon1_9nt = None
            intron_317bp = None
            spliced_5utr = seq[873:888]
            unspliced_ins = ""
            sb9_dna = seq[888:2019]
        else:
            _req_0018(len(seq) == 4398, f"Expected 4398 bp donor on {cid}")
            c_end = 3798
            unspliced_ins = seq[879:1199]
            _req_0018(len(unspliced_ins) == 320 and unspliced_ins[:3] == "CAG", f"Invalid insert header on {cid}")
            intron_317bp = unspliced_ins[3:]
            _req_0018(
                intron_317bp.startswith("GTAAGT") and intron_317bp.endswith("TCTCTTCCAG"),
                f"Invalid synthetic intron splice termini on {cid}",
            )
            exon1_9nt = seq[873:882]
            _req_0018(exon1_9nt == "CAGAGACAG", f"Unexpected exon-1 9-nt leader on {cid}")
            spliced_5utr = exon1_9nt + seq[1199:1208]
            _req_0018(spliced_5utr == "CAGAGACAGGCCGCCACC", f"Unexpected mature spliced 5'-UTR on {cid}")
            sb9_dna = seq[1208:2339]

        _req_0018("ATG" not in spliced_5utr and "ATG" not in unspliced_ins, f"Upstream ATG detected on {cid}")
        pdl1_pos = seq.index("ATGAGAATCTTCGCC")
        sb9_prot = _translate_orf(sb9_dna)
        pdl1_prot = _translate_orf(seq[pdl1_pos : pdl1_pos + 873])
        _req_0018(pdl1_prot == pdl1_wt_prot, f"PD-L1 ORF mismatch on {cid}")

        novel_c1 = sorted((_kmers(sb9_prot, (8, 9, 10, 11)) | _kmers(pdl1_prot, (8, 9, 10, 11))) - wt_c1)
        novel_c2 = sorted((_kmers(sb9_prot, (15,)) | _kmers(pdl1_prot, (15,))) - wt_c2)
        if cid == "ARM6_L3_V2_INTRON_DUPLEX_IL1R1_FAS_C341S_TCA_4398":
            _req_0018(len(novel_c1) == 38 and len(novel_c2) == 15, f"Unexpected C341S neo-peptide count on {cid}")
        else:
            _req_0018(len(novel_c1) == 0 and len(novel_c2) == 0, f"Unexpected novel k-mer on {cid}")

        gram_donor = scan_donors_and_acceptors_both_strands(seq, cargo_bounds_1based=(601, c_end))
        cargo_counts = {
            "+don": sum(1 for d in gram_donor["donors"] if d["in_cargo"] and d["strand_relative_to_input"] == "+"),
            "-don": sum(1 for d in gram_donor["donors"] if d["in_cargo"] and d["strand_relative_to_input"] == "-"),
            "+acc": sum(1 for a in gram_donor["acceptors"] if a["in_cargo"] and a["strand_relative_to_input"] == "+"),
            "-acc": sum(1 for a in gram_donor["acceptors"] if a["in_cargo"] and a["strand_relative_to_input"] == "-"),
        }
        integrated_seq = flank_up + seq + flank_dn
        gram_int = scan_donors_and_acceptors_both_strands(
            integrated_seq,
            cargo_bounds_1based=(300 + 601, 300 + c_end),
        )
        int_cargo_counts = {
            "+don": sum(1 for d in gram_int["donors"] if d["in_cargo"] and d["strand_relative_to_input"] == "+"),
            "-don": sum(1 for d in gram_int["donors"] if d["in_cargo"] and d["strand_relative_to_input"] == "-"),
            "+acc": sum(1 for a in gram_int["acceptors"] if a["in_cargo"] and a["strand_relative_to_input"] == "+"),
            "-acc": sum(1 for a in gram_int["acceptors"] if a["in_cargo"] and a["strand_relative_to_input"] == "-"),
        }
        _req_0018(cargo_counts == int_cargo_counts, f"Integrated cargo splice-motif mismatch on {cid}")
        if is_parent:
            _req_0018(
                cargo_counts == {"+don": 10, "-don": 12, "+acc": 21, "-acc": 58},
                f"Unexpected parent cargo motif counts on {cid}",
            )
            _req_0018(
                len(gram_donor["donors"]) == 30 and len(gram_int["donors"]) == 38,
                f"Unexpected parent total donor counts on {cid}",
            )
        else:
            _req_0018(
                cargo_counts == {"+don": 11, "-don": 12, "+acc": 22, "-acc": 58},
                f"Unexpected Mode-B cargo motif counts on {cid}",
            )
            _req_0018(
                len(gram_donor["donors"]) == 31 and len(gram_int["donors"]) == 39,
                f"Unexpected Mode-B total donor counts on {cid}",
            )

        parsed_records.append({
            "construct_id": cid,
            "donor_length_bp": len(seq),
            "packaged_aav6_length_bp": len(seq) + 290,
            "packaged_aav6_headroom_bp": 4700 - (len(seq) + 290),
            "donor_sha256": seq_sha,
            "exon1_9nt": exon1_9nt,
            "excised_intron_length_bp": 0 if intron_317bp is None else len(intron_317bp),
            "mature_spliced_5utr": spliced_5utr,
            "spliced_5utr_length_nt": len(spliced_5utr),
            "unspliced_insert_atg_count": unspliced_ins.count("ATG"),
            "spliced_5utr_atg_count": spliced_5utr.count("ATG"),
            "isolated_donor_total_donors_both_strands": len(gram_donor["donors"]),
            "integrated_chr4_plus_2x300bp_total_donors_both_strands": len(gram_int["donors"]),
            "cargo_two_strand_motif_counts": cargo_counts,
            "novel_8_to_11mers_vs_arm1": len(novel_c1),
            "novel_15mers_vs_arm1": len(novel_c2),
            "promotion_status": (
                "BASELINE_CONTROL"
                if is_parent
                else (
                    "HELD_MODE_B_PRIMARY_CHALLENGER_PENDING_2X2_AND_STAGE0_QUALIFICATION"
                    if cid == "ARM5_L3_V2_INTRON_DUPLEX_IL1R1_FAS_WT_SB9_4398"
                    else "HELD_MODE_B_FACTORIAL_OR_EXPLORATORY_COMPARATOR"
                )
            ),
        })

    _req_0018(len(parsed_records) == 6, "Expected 6 Locus-3 comparator records")
    umi_bound_3689 = round(100.0 * (1.0 - (0.025 ** (1.0 / 3689.0))), 8)
    cbe_bound_50k = round(100.0 * (1.0 - (0.05 ** (1.0 / 50000.0))), 6)

    return {
        "proposal_id": "IIP-0018",
        "decision_id": "DEC-096",
        "evidence_class": "pre_registered_mode_b_locus3_multimir_challenger_suite_and_standalone_cbe_comparator_ledger",
        "canonical_fasta_and_ranking_invariance": {
            "canonical_scenario_a_fasta": "data/constructs/islet1_scenario_a_constructs_v0.7.0.fasta",
            "canonical_scenario_a_fasta_untouched": True,
            "layer1_stack_rankings_untouched": True,
            "illustrative_hurdle3_vs_biological_gate3_distinction": (
                "Illustrative Hurdle 3 (autoimmune bystander survival) is distinct from biological Gate 3 (innate NK, macrophage, and complement protection); "
                "neither computational pre-registration nor a single Stage-0 assay recalibrates the blueprint's subjective hurdle percentages."
            ),
            "product_background_separation": (
                "K = 4, B = 0 applies strictly to replacing the Locus-3 donor within the four-locus Design A background. "
                "Design B retains endogenous/engineered HLA-C under GY-005 with a distinct edit ledger and presentation profile; "
                "Design A/X and Design B backgrounds require separately identified qualification runs (especially for ARM6 C341S +38 8-11mers)."
            ),
        },
        "mode_b_comparator_fasta": {
            "relative_path": "data/constructs/islet1_locus3_multimir_modeb_comparators_v0.8.0.fasta",
            "construct_count": 6,
            "sha256": fasta_sha256,
            "ha_l_grch38_chr4_minus": _IIP_0018_OFFLINE_REFERENCE_BUNDLE["ha_l_grch38_chr4_minus"],
            "ha_r_grch38_chr4_minus": _IIP_0018_OFFLINE_REFERENCE_BUNDLE["ha_r_grch38_chr4_minus"],
            "outer_chromosomal_flank_verification_bp": 300,
        },
        "cassette_anatomy_and_scanner_scope": {
            "inserted_cassette_bp": 320,
            "retained_exon1_anchor_bp": 3,
            "excised_synthetic_intron_bp": 317,
            "exon1_sequence_9nt": "CAGAGACAG",
            "exon2_kozak_leader_9nt": "GCCGCCACC",
            "mature_spliced_5utr_18nt": "CAGAGACAGGCCGCCACC",
            "hairpin_1_mir_e_v2_target_22nt": {"IL1R1": "CACTTTAGTGGAGTCAAAGATA", "NT1_v2": "CACGGTAACGCTAACTACGCTA"},
            "hairpin_2_mir_16_2_v2_target_22nt": {"FAS": "AACTCCAAGGGATTGGAATTGA", "NT2": "GCCTAGTCTGATTAGTTTCGGA"},
            "seed_audit_summary": _IIP_0018_OFFLINE_REFERENCE_BUNDLE["seed_audit"],
            "motif_scanner_scope_limitation": (
                "The Layer-2 two-strand splice scanner uses positional consensus/log-odds for 5' donors and a 23-mer polypyrimidine heuristic for 3' acceptors—not "
                "the dependency-aware MaxEnt model of Yeo & Burge 2004 (PMID:15285897)—and does not model non-GT introns, shifted U1 register, branchpoint competition, "
                "or exonic/intronic splicing enhancers/silencers. Furthermore, avoiding IL1R1/FAS seed matches in NT1_v2/NT2 does not screen the rest of the beta-cell "
                "transcriptome, passenger strands, or 5'-isomiRs (Fellmann 2013 PMID:24332856; Schirle 2014 PMID:25359968; Amen 2022 PMID:35880017). "
                "Quantitative spliced-fraction (>=85% spliced / <=15% intron retention), recovery-calibrated two-strand UMI chimeras (>=3,689 effective molecules -> "
                f"{umi_bound_3689}% two-sided 95% upper bound; Kivioja 2011 PMID:22101854), transcriptome-wide RNA-seq, absolute AGO-RIP small-RNA-seq "
                "(Poy 2004 PMID:15538371; Grimm 2006 PMID:16724069), and resting/low-IFN SERPINB9+PD-L1 retention (GY-001) are mandatory."
            ),
        },
        "pre_synthesis_2x2_transient_falsifier_and_human_gsis_tradeoff": {
            "design": (