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…

        ],
    }


def scan_disjoint_inverted_repeats_with_mismatches(
    seq: str,
    *,
    min_k: int = 20,
    max_mismatches: int = 1,
    cluster_within_bp: Optional[int] = None,
) -> List[Dict[str, Any]]:
    """
    Exact scanner for all qualifying disjoint width-`min_k` inverted-repeat pairs
    with `<= max_mismatches` mismatches (IIP-0031; DEC-110).
    Validates strict ASCII `ACGTacgt` alphabet before uppercasing, supports `min_k >= 1`
    and arbitrary `max_mismatches >= 0` (using exact disjoint pair evaluation whenever
    `max_mismatches >= min_k`, and exact pigeonhole seed partitioning otherwise), and
    returns every exact disjoint `(pos1_a, pos1_b)` pair of width `min_k` together with
    its maximal ungapped disjoint extension `maximal_stem_bp` unless `cluster_within_bp`
    is explicitly supplied.
    """
    if not isinstance(seq, str):
        raise ValueError("Inverted-repeat scan sequence must be a string")
    if not all(ch in "ACGTacgt" for ch in seq):
        raise ValueError("Inverted-repeat scan sequence contains non-ACGT symbols")
    if type(min_k) is not int or min_k < 1:
        raise ValueError("min_k must be an integer >= 1")
    if type(max_mismatches) is not int or max_mismatches < 0:
        raise ValueError("max_mismatches must be an integer >= 0")
    if cluster_within_bp is not None and (type(cluster_within_bp) is not int or cluster_within_bp < 0):
        raise ValueError("cluster_within_bp must be None or an integer >= 0")
    seq_u = seq.upper()
    n = len(seq_u)
    if n < 2 * min_k:
        return []
    rc_map = {"A": "T", "C": "G", "G": "C", "T": "A"}
    n_chunks = max_mismatches + 1
    seed_k = min_k // n_chunks
    raw_hits: List[Tuple[int, int, int, int, str, str]] = []

    def _maximal_stem_from_pair(i0: int, j0: int, base_mm: int) -> int:
        # Extend antiparallel stem inward (right from i0+min_k, left from j0) and outward (left from i0, right from j0+min_k):
        i_lo, i_hi = i0, i0 + min_k
        j_lo, j_hi = j0, j0 + min_k
        cur_mm = base_mm
        while i_hi <= j_lo - 1:
            if seq_u[i_hi] != rc_map[seq_u[j_lo - 1]]:
                if cur_mm + 1 > max_mismatches:
                    break
                cur_mm += 1
            i_hi += 1
            j_lo -= 1
        while i_lo > 0 and j_hi < n:
            if seq_u[i_lo - 1] != rc_map[seq_u[j_hi]]:
                if cur_mm + 1 > max_mismatches:
                    break
                cur_mm += 1
            i_lo -= 1
            j_hi += 1
        return i_hi - i_lo

    if seed_k < 1:
        for i in range(n - 2 * min_k + 1):
            a = seq_u[i : i + min_k]
            for j in range(i + min_k, n - min_k + 1):
                b = seq_u[j : j + min_k]
                mm = sum(x != rc_map[y] for x, y in zip(a, reversed(b)))
                if mm <= max_mismatches:
                    raw_hits.append((i + 1, j + 1, mm, _maximal_stem_from_pair(i, j, mm), a, b))
    else:
        pos: Dict[str, List[int]] = {}
        for i in range(n - seed_k + 1):
            pos.setdefault(seq_u[i : i + seed_k], []).append(i)
        chunk_offsets = [c * seed_k for c in range(n_chunks - 1)] + [min_k - seed_k]
        seen_pairs: Set[Tuple[int, int]] = set()
        for i in range(n - 2 * min_k + 1):
            cand_js: Set[int] = set()
            for off in chunk_offsets:
                chunk = seq_u[i + off : i + off + seed_k]
                rc_chunk = revcomp(chunk)
                b_rel = min_k - off - seed_k
                for p in pos.get(rc_chunk, []):
                    j = p - b_rel
                    if j >= i + min_k and j + min_k <= n:
                        cand_js.add(j)
            a = seq_u[i : i + min_k]
            for j in sorted(cand_js):
                if (i, j) in seen_pairs:
                    continue
                b = seq_u[j : j + min_k]
                mm = sum(x != rc_map[y] for x, y in zip(a, reversed(b)))
                if mm <= max_mismatches:
                    seen_pairs.add((i, j))
                    raw_hits.append((i + 1, j + 1, mm, _maximal_stem_from_pair(i, j, mm), a, b))

    results: List[Dict[str, Any]] = []
    for p1, p2, mm, max_stem, a, b in raw_hits:
        if (
            cluster_within_bp is not None
            and results
            and abs(p1 - results[-1]["pos1_a"]) <= cluster_within_bp
            and abs(p2 - results[-1]["pos1_b"]) <= cluster_within_bp
        ):
            continue
        results.append({
            "length_bp": min_k,
            "maximal_stem_bp": max_stem,
            "mismatches": mm,
            "pos1_a": p1,
            "pos1_b": p2,
            "seq_a": a,
            "seq_b": b,
        })
    return results


_IIP0031_TRANSCRIPT_PINS = {
    "pvr_refseq_isoforms_4": {
        "NM_001135768.3": "2ef24fc0974fcb43bd4424146aa7a142621a92b8b77860e92830a7d57d16cff9",
        "NM_001135769.3": "9542fd4f187f2c5a604f50446a6152673717873a82b94f744a3b268809af5960",
        "NM_001135770.3": "419c3ac33f73fa4fab71d01ba0cceb42f5ee3273a682d635014a97b8a901a849",
        "NM_006505.5": "14a36f7386490c060695e1ec5cd588b4c9c07be5d71641bfe1888ca2bf56929a",
    },
    "cd276_refseq_isoforms_4": {
        "NM_001024736.2": "7a89c6a6555555531e2f8eff270ee62bd6ee93f90eb1b950ca97ba673baf65ce",
        "NM_001329628.2": "19ec2749d06fe9645e7fc55b6ac78401b6075c6864a93dd30458ee2539f245b8",
        "NM_001329629.2": "db51d99b1b5cb052b09c9c7f83f635afc484924f85b619aff6a1be4ed8666a03",
        "NM_025240.3": "0bd1628a649a1ca8a39cb5058dc07b0890a42707465fe314a24631f0089c98f5",
    },
}
_IIP0031_CHR16_2001BP_SHA256 = "ec47ca4d65be793a536354da3d81c3188c4021598f4cecee749e2ff2a89b5e36"
_IIP0031_DM1_80BP_SHA256 = "801f07cae8f29ec2c75d02cc1ce52e3728566d24d0595d4722a318dee0a097f3"
_IIP0031_CIITA_EXON5_200BP_SHA256 = "10c69c9dcafd39f2935f26a87a1283ef4dc4875ef259d77b1fef8e5ba4e82daa"
_IIP0031_CIITA_REFSEQ_PINS: Dict[str, Tuple[int, int, str, Tuple[Tuple[int, int], ...], str, str]] = {
    "NM_000246.4": (
        130, 3522, "aebd7a44ddee5e33c6b8e9ef6fc2a187ac3aaa48d8b3e7fe5a4ba0c85784f518",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 565), (566, 610), (611, 757), (758, 901), (902, 1066), (1067, 1135), (1136, 2786), (2787, 2945), (2946, 3017), (3018, 3098), (3099, 3191), (3192, 3278), (3279, 3362), (3363, 3446), (3447, 3544), (3545, 16061)),
        "variant_2_canonical_pIII_78bp_Ex5",
        "canonical pIII / FIII (exon1 1..181, 78-bp Ex5, N-term MRCLAP...)",
    ),
    "NM_001286402.1": (
        134, 3529, "841888c82eb9d941c1d3441d4094a04cceafd96eae0da7a4bbb47cf9b4f308f2",
        ((1, 185), (186, 332), (333, 428), (429, 491), (492, 572), (573, 617), (618, 764), (765, 908), (909, 1073), (1074, 1142), (1143, 2793), (2794, 2952), (2953, 3024), (3025, 3105), (3106, 3198), (3199, 3285), (3286, 3369), (3370, 3453), (3454, 3551), (3552, 4657)),
        "variant_1_alt_FIII_pIII_81bp_Ex5",
        "pIII / alternative FIII (exon1 1..185, 81-bp Ex5, N-term MRCLAP...)",
    ),
    "NM_001286403.2": (
        130, 1770, "154afe08df9fc8a285b78c76dfd0f43869b63451404aec24c16d7868b6de0a00",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 565), (566, 610), (611, 754), (755, 919), (920, 988), (989, 1193), (1194, 1265), (1266, 1346), (1347, 1439), (1440, 1526), (1527, 1610), (1611, 1694), (1695, 1792), (1793, 14309)),
        "variant_3_short_pIII_prefix_78bp_Ex5",
        "shorter pIII-prefix isoform (shares exon1 1..181 with NM_000246.4, 78-bp Ex5, 546 aa)",
    ),
    "NM_001379330.1": (
        130, 3378, "2bac24c75173978aea51384b27e53a22e69f15a1346903e2596790b4f8d8fea4",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 568), (569, 613), (614, 757), (758, 922), (923, 991), (992, 2642), (2643, 2801), (2802, 2873), (2874, 2954), (2955, 3047), (3048, 3134), (3135, 3218), (3219, 3302), (3303, 3400), (3401, 4679)),
        "variant_5_pIII_81bp_Ex5",
        "pIII-prefix variant 5 (exon1 1..181, 81-bp Ex5, 1,082 aa)",
    ),
    "NM_001379331.1": (
        130, 3375, "5ddf0bee290880d504973c271ffe81d164d1f158f054cc38bbb8058efb3e938f",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 565), (566, 610), (611, 754), (755, 919), (920, 988), (989, 2639), (2640, 2798), (2799, 2870), (2871, 2951), (2952, 3044), (3045, 3131), (3132, 3215), (3216, 3299), (3300, 3397), (3398, 4676)),
        "variant_6_pIII_78bp_Ex5",
        "pIII-prefix variant 6 (exon1 1..181, 78-bp Ex5, 1,081 aa)",
    ),
    "NM_001379332.1": (
        130, 3525, "6f7085648132bcfa8d4d98a11200c42b4ba7c89ef151d9d69d59714197717560",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 568), (569, 613), (614, 760), (761, 904), (905, 1069), (1070, 1138), (1139, 2789), (2790, 2948), (2949, 3020), (3021, 3101), (3102, 3194), (3195, 3281), (3282, 3365), (3366, 3449), (3450, 3547), (3548, 4826)),
        "variant_7_pIII_81bp_Ex5",
        "pIII-prefix variant 7 (exon1 1..181, 81-bp Ex5, 1,131 aa)",
    ),
    "NM_001379333.1": (
        130, 3522, "03a68ae89b34641207b0560b72af5cfc7f63fd8e1f70f29767ab4ff81d2e7b41",
        ((1, 181), (182, 328), (329, 424), (425, 487), (488, 565), (566, 610), (611, 757), (758, 901), (902, 1066), (1067, 1135), (1136, 2786), (2787, 2945), (2946, 3017), (3018, 3098), (3099, 3191), (3192, 3278), (3279, 3362), (3363, 3446), (3447, 3544), (3545, 4823)),
        "variant_8_pIII_78bp_Ex5",
        "pIII-prefix variant 8 (exon1 1..181, 78-bp Ex5, 1,130 aa)",
    ),
    "NM_001379334.1": (
        97, 3420, "5a809c6442135d48b01b84e83a263a20d654e61ed131091765b182ea05978aa7",
        ((1, 76), (77, 223), (224, 319), (320, 382), (383, 463), (464, 508), (509, 655), (656, 799), (800, 964), (965, 1033), (1034, 2684), (2685, 2843), (2844, 2915), (2916, 2996), (2997, 3089), (3090, 3176), (3177, 3260), (3261, 3344), (3345, 3442), (3443, 4721)),
        "variant_9_IFNg_pIV_81bp_Ex5",
        "IFN-gamma-inducible pIV / FIV (noncoding exon1 1..76, Exon-2 ATG N-term MELGPL..., 81-bp Ex5, 1,107 aa)",
    ),
}
_IIP0031_JASPAR_6PWM_PINS: Dict[str, Tuple[str, int, str, str]] = {
    "MA0050.2": ("IRF1", 21, "082e0a83f20a173eb3788790f18dcc8131c29349b24ca37a0ef8046b9e4fb37e", "24cab749fa45ac2bfc0fdd1b7ce02e7473e6ca5f190bedeaffe9e1b7314e367a"),
    "MA0079.5": ("SP1", 9, "3f688e30c538bf39cc39a1f9c52d3c661ec4fb0f6c0d507859206c4e3e3a96d0", "79c93a2a42b6b1bddf611e1531811171977d4be313ed291e73d300d8eaf3357e"),
    "MA0105.4": ("NFKB1", 13, "3d3c7718a9308d4fb70c4ad70abbcf79e8fc0ac688616b5206ae69608e7b39c5", "31592be2b4d4120b1d2de96ffc2600ccb4de69ef3bb9a99d141a773c13bd3f0e"),
    "MA0107.1": ("RELA", 10, "7a765b29b62589558b9195fae9e6e4a62d917839e9b73655ca6afc6b7cd1f5c6", "f810a450edefd1e38ce0e0f81348478285254707b529e8edb47a9ff0164c57bb"),
    "MA0137.3": ("STAT1", 11, "d1088b63cf03a4c891ec4799c891e70725330815f9c2fd817d3297935a8d38ac", "68abc7d51226ebca191d010f0b202e0f727b53e48cc431f93c0294cc382565a0"),
    "MA0139.1": ("CTCF", 19, "c750c526e1fd8d747a728a0f9c6a9efd17cedc684f4ee69a4354c6760be68967", "987a2fd393dc17e886e40d26757622c88b70dc3ecf82c52e8e38aafaedc1b335"),
}
_IIP0031_PREEXISTING_6_FASTA_SHA256: Dict[str, str] = {
    "data/constructs/islet1_scenario_a_constructs_v0.7.0.fasta": "b5ba4e2b4bbbcc7d6cf1138975c5fae5c6ab074dda43973f071c671cd8cd1d88",
    "data/constructs/islet1_design_b_complete_constructs_v0.8.0.fasta": "4bb20389773c76b2705031ba675487825dfbd94d920a692cde75b7d56af22aa4",
    "data/constructs/islet1_locus3_multimir_modeb_comparators_v0.8.0.fasta": "22df54a27ef3f68b646b63dc036b11f5a66a9fcadb50378dfb8057e108c79ffa",
    "data/constructs/islet1_locus1_s10_modeb_comparators_v0.8.0.fasta": "a589ea72144cf1ad2cfa82e9b08c22e075746c63a9264e276a3bd4ec3f896854",
    "data/constructs/islet1_design_y_wildcard_comparators_v0.8.0.fasta": "c7f54b824a73cf9d40247d4fd251cb5c28a8eca4a1530b9661284200bb4cbabc",
    "data/constructs/nm_021999_5_archival_mrna.fasta": "4a4cb15d0544884a62d86ebf4137c5284edad8210b6e32e74c2eb0aff005d13e",
}
_IIP0031_PROXY_4_FASTAS: Tuple[str, ...] = (
    "data/constructs/islet1_scenario_a_constructs_v0.7.0.fasta",
    "data/constructs/islet1_design_b_complete_constructs_v0.8.0.fasta",
    "data/constructs/islet1_locus3_multimir_modeb_comparators_v0.8.0.fasta",
    "data/constructs/islet1_locus1_s10_modeb_comparators_v0.8.0.fasta",
)
_IIP0031_OI_15_PROXY_LENGTHS: Dict[str, int] = {
    "DesignA_L3_SERPINB9_PDL1_WPRE_bGH": 2890,
    "DesignA_L1_ARM1A_HLAE_CD47_WPRE_bGH": 2908,
    "DesignA_L1_ARM1B_HLAE_bGH": 1737,
    "DesignA_L1_ARM1B_CD47_SV40": 659,
    "DesignA_L4_iCasp9_SV40": 1322,
    "DesignB_c1_L5A_BFB_SYN6": 2599,
    "DesignB_c3_L5A_HUMAN": 2572,
    "DesignB_c5_L6_iCasp9": 1350,
    "DesignB_c5_L6_CD47": 687,
    "L3_ARM5_MultiMiR_SB9_PDL1": 2919,
    "S10_ASTAR_HLAE": 1752,
    "L2_V3_CFH_STP_GPI_CDS": 1083,
    "L2_V3_CD200_CDS": 837,
    "L2_V3_MANF_CDS": 549,
    "L2_V3_CCL22_CDS": 282,
}
_IIP0031_PARETO_REL = "data/extracted/islet1_combinatorial_pareto_and_ladder_v0.8.0.json"
_IIP0031_PARETO_SHA256 = "460ebc1939eb98b38627eaa7d6603c086e5a75c127fc1ddeced0d5a325305f40"
_IIP0031_HUMAN_22_REFSEQ_PINS: Dict[str, Tuple[str, int, int, str]] = {
    "CD46": ("NM_002389.4", 176, 1354, "5aaa0dc3b47b8378f2ee4b4c8a922314bb3d37bb8d2accfa4e6da3d31bc8ada7"),
    "CD47": ("NM_001777.4", 124, 1095, "4d0d4e7bb124fdc6a86d7cc09af5c601b3e59421a93bec2ebc026d8c4e857b58"),
    "CD55": ("NM_000574.5", 89, 1234, "69097f6406a43daeb14a3881a0b6d9c77629dd8b7eb2be579249d624a4933878"),
    "CD59": ("NM_203330.2", 278, 664, "9c93b1a9a67a85140769f2c09feb37725355a6066357cd54d177228a3fe1eaa7"),
    "CDH5": ("NM_001795.5", 88, 2442, "eca8977b9a0dd1164c16c9c2fd1dabced73e0a23d6f664ac5b23fe696f46bce6"),
    "CLEC16A": ("NM_015226.3", 155, 3316, "52b6a9f414e692606e5a6bf9ecdb32f3cc595e222a65182cad2d893931cf2fc0"),
    "DEXI": ("NM_014015.4", 464, 751, "127c13310e13ad969cf980f2aa6e016c45063b3023489035aafdf8e412382f78"),
    "GCG": ("NM_002054.5", 100, 642, "7e3860e8bf5ea697b2a2326a9a83dba62ee07439ba59b412034aa413a4e1199d"),
    "GCK": ("NM_000162.5", 487, 1884, "5d1a3f2f9bbc6b7f34e16e908406674a236ec2eff99d2169ea7fd22ccb2bd288"),
    "HLA-E": ("NM_005516.6", 24, 1100, "3bee2f46b3ee02931fac10809ad9423e5c59b155e79599a31d1b083a5a376869"),
    "INS": ("NM_000207.3", 60, 392, "2132509911c26572e7dfcaea140a3c893ab2cb6ec1d71cfe23b91fcf09ae4ff1"),
    "KDR": ("NM_002253.4", 303, 4373, "d073a219056b5d5a6ee14429142e863cb27e56e62b0fbd01856891f609c713e2"),
    "MAFA": ("NM_201589.4", 327, 1388, "f68563d4331e22ad4242bb8506b47a5d54ea2c68dffb03e5d07d2ef42fd6036e"),
    "NEUROD1": ("NM_002500.5", 100, 1170, "c2833f3f23f9c9c41d5c72e990d552264989a9de721b4b814ff69220007d61c4"),
    "NKX6-1": ("NM_006168.3", 1065, 2168, "d55a510ebcb2ab18806e881622a00b31ef6d6e94a2914df027761a6af42f2272"),
    "PCSK1": ("NM_000439.5", 207, 2468, "62593e784df126a9cdc48df003712b5f32b83213eaadb7e8a77b6b1c6d6b4df9"),
    "PDX1": ("NM_000209.4", 140, 991, "f08b47ded18ecba68fd30c0691895c5db3aa07db12eb172c30bc237c077150d5"),
    "PECAM1": ("NM_000442.5", 196, 2412, "f21e1e198379e7a6c99c567907319eb977e6c3679f8043e2e0a64607f509bada"),
    "PIGA": ("NM_002641.4", 86, 1540, "6fb573d0f9d3a1ef61c4435a223183faffc6bbd15a5c3f4c9b52520661dba6f4"),
    "PIGK": ("NM_005482.3", 29, 1216, "9e239db10fe98d3d7ca9050d16ace8e67f3dba57c6e08bb293bf72a4e5df23e4"),
    "SLC30A8": ("NM_173851.3", 334, 1443, "dfa4694f334ea6cade8390837f58735f5e49c5934fae93f33dc422e5c173c173"),
    "SOCS1": ("NM_003745.2", 177, 812, "a406261ebdfb581e9f57a897cc53137e63e3918755c85f5a21047cf47a33725a"),
}