scripts/run_layer2_perturbation_and_regulatory_plane.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/run_layer2_perturbation_and_regulatory_plane.py · SHA-256: 3220e68f97203dbc…
# [CD47 Signal Peptide M1..A18 (18 aa) + IgV & Stalk Q19..S138 (120 aa)] + [CD55 GPI signal P345..T381 (37 aa)] = 175 aa
cd47_igv_gpi_aa = cd47_full[:138] + cd55_full[344:381]
if not (len(cd47_igv_gpi_aa) == 175):
raise ValueError('Molecular source invariant failed: len(cd47_igv_gpi_aa) == 175')
# 3. Canonical 22-aa GSG-P2A ribosomal skipping peptide (66 bp):
# Ribosomal skipping between Gly21 and Pro22 leaves an exact 21-aa C-terminal stub (GSGATNFSLLKQAGDVEENPG)
# on the upstream protein and a 1-aa Proline prefix on the downstream signal peptide.
# NO cytoplasmic Furin RAKR motif is included after HLA-E or SERPINB9 (closing R01 & V70-02).
gsg_p2a_22_aa = "GSGATNFSLLKQAGDVEENPGP"
if len(gsg_p2a_22_aa) != 22 or not gsg_p2a_22_aa.endswith("GDVEENPGP"):
raise ValueError(f"Invalid GSG-P2A peptide sequence: {gsg_p2a_22_aa}")
# 4. iCasp9 inducible suicide switch (Straathof et al. 2005 PMID:15728125):
# 108-aa FKBP12(F36V) (M1..E108, where V37 corresponds to mature F36V) + 6-aa SGGGSG linker + 282-aa dCARD-CASP9 (G135..S416) = 396 aa (1,191 bp with TGA)
fkbp12_v36_aa = FKBP12_F36V_REFERENCE_AA
if not (len(fkbp12_v36_aa) == 108):
raise ValueError('Molecular source invariant failed: len(fkbp12_v36_aa) == 108')
icasp9_aa = fkbp12_v36_aa + "SGGGSG" + casp9_full[134:416]
if not (len(icasp9_aa) == 396):
raise ValueError('Molecular source invariant failed: len(icasp9_aa) == 396')
# Back-translate all coding modules
hlae_sct_dna_nostop = back_translate_clean(hlae_sct_aa, include_stop=False)
hlae_sct_dna_stop = back_translate_clean(hlae_sct_aa, include_stop=True)
gsg_p2a_dna = back_translate_clean(gsg_p2a_22_aa, include_stop=False)
cd47_igv_gpi_dna = back_translate_clean(cd47_igv_gpi_aa, include_stop=True)
serpinb9_dna = back_translate_clean(serpinb9_full, include_stop=False)
pdl1_dna = back_translate_clean(pdl1_full, include_stop=True)
icasp9_dna = back_translate_clean(icasp9_aa, include_stop=True)
# Authentic GRCh38 non-overlapping homology arms & GenBank regulatory elements
b2m_ha_l_800 = elems["B2M_HA_L_800bp"]["sequence"]
b2m_ha_r_800 = elems["B2M_HA_R_800bp"]["sequence"]
b2m_ha_l_600 = elems["B2M_HA_L_600bp"]["sequence"]
b2m_ha_r_600 = elems["B2M_HA_R_600bp"]["sequence"]
cxcl10_ha_l_600 = elems["CXCL10_HA_L_600bp_minus"]["sequence"]
cxcl10_ha_r_600 = elems["CXCL10_HA_R_600bp_minus"]["sequence"]
aavs1_ha_l_600 = elems["AAVS1_HA_L_600bp_minus"]["sequence"]
aavs1_ha_r_600 = elems["AAVS1_HA_R_600bp_minus"]["sequence"]
ucoe_ref_entry = elems["CBX3_A2UCOE_455bp_exact"]
cbx3_a2ucoe_455 = ucoe_ref_entry["sequence"]
eef1a1_core_230 = elems["EEF1A1_core_promoter_230bp"]["sequence"]
wpre_mut6_589 = elems["WPREmut6_589bp_J04514_mut6"]["sequence"]
bgh_polya_225 = elems["bGH_polyA_225bp_M57764"]["sequence"]
sv40_polya_122 = elems["SV40_late_polyA_122bp_J02400"]["sequence"]
# Verify authentic Woodchuck Hepatitis Virus J04514.1 WPREmut6 identity (closes AUDIT-REF)
if not (wpre_mut6_589.startswith("AATCAACCTCTGGATTACAAAATTTGTGAAAGATTGACTGGTATTCTTAACTATGTTGCT")):
raise ValueError('Molecular source invariant failed: wpre_mut6_589.startswith("AATCAACCTCTGGATTACAAAATTTGTGAAAGATTGACTGGTATTCTTAACTATGTTGCT")')
if not (wpre_mut6_589[410:419] == "TTGGCTGCT"):
raise ValueError("WHX start codon ATG at J04514.1:1503 must be ablated to TTG per Zanta-Boussif 2009 (PMID:19262615)")
# Verify authentic upstream EEF1A1 core promoter (chr6:73521000-73521229:-1, spanning -197..+33 around TSS 73521032 of ENST00000309268.11, closes AUDIT-REF & 066)
if not (eef1a1_core_230.startswith("GCTCCGGTGCCCGTCAGTGGGCAGAGCGCACATCGCCCACAGTCCCCGAGAAGTTGGGGG")):
raise ValueError('Molecular source invariant failed: eef1a1_core_230.startswith("GCTCCGGTGCCCGTCAGTGGGCAGAGCGCACATCGCCCACAGTCCCCGAGAAGTTGGGGG")')
# Verify zero homology arm overlap and canonical polyA hexamers
if not (b2m_ha_l_800 != b2m_ha_r_800 and b2m_ha_l_800 != revcomp(b2m_ha_r_800)):
raise ValueError('Molecular source invariant failed: b2m_ha_l_800 != b2m_ha_r_800 and b2m_ha_l_800 != revcomp(b2m_ha_r_800)')
if not (b2m_ha_l_600 != b2m_ha_r_600 and b2m_ha_l_600 != revcomp(b2m_ha_r_600)):
raise ValueError('Molecular source invariant failed: b2m_ha_l_600 != b2m_ha_r_600 and b2m_ha_l_600 != revcomp(b2m_ha_r_600)')
if not (cxcl10_ha_l_600 != cxcl10_ha_r_600 and cxcl10_ha_l_600 != revcomp(cxcl10_ha_r_600)):
raise ValueError('Molecular source invariant failed: cxcl10_ha_l_600 != cxcl10_ha_r_600 and cxcl10_ha_l_600 != revcomp(cxcl10_ha_r_600)')
if not (aavs1_ha_l_600 != aavs1_ha_r_600 and aavs1_ha_l_600 != revcomp(aavs1_ha_r_600)):
raise ValueError('Molecular source invariant failed: aavs1_ha_l_600 != aavs1_ha_r_600 and aavs1_ha_l_600 != revcomp(aavs1_ha_r_600)')
if not ("AATAAA" in bgh_polya_225):
raise ValueError('Molecular source invariant failed: "AATAAA" in bgh_polya_225')
if not ("AATAAA" in sv40_polya_122):
raise ValueError('Molecular source invariant failed: "AATAAA" in sv40_polya_122')
# Dynamically compute UCOE genomic coordinates, sequence SHA-256 identity, and overlap against AlphaGenome AVI window (closes 005 & 019)
avi_ucoe_interval = avi_summary["window_saturation_mutagenesis_summaries"]["CBX3_A2UCOE_455bp_window"]["genomic_interval_grch38"]
avi_u_chrom, avi_u_span = avi_ucoe_interval.split(":")
avi_u_start, avi_u_end = [int(x) for x in avi_u_span.split("-")]
ucoe_chrom = str(ucoe_ref_entry.get("chrom", "chr7"))
ucoe_start = int(ucoe_ref_entry.get("start_1based", 26200580))
ucoe_end = int(ucoe_ref_entry.get("end_1based", 26201034))
ucoe_coord_str = f"{ucoe_chrom}:{ucoe_start}-{ucoe_end}"
ucoe_expected_sha256 = "6a4a4ad89dc417532b4066c02f8ff660a30706d7e56691e1d376cfb72e79b0d9"
ucoe_seq_sha_ok = bool(
sha256_str(cbx3_a2ucoe_455) == ucoe_expected_sha256
and sha256_str(cbx3_a2ucoe_455) == str(ucoe_ref_entry.get("sha256", ucoe_expected_sha256))
)
ucoe_overlap_bp = (
max(0, min(ucoe_end, avi_u_end) - max(ucoe_start, avi_u_start) + 1)
if (ucoe_chrom == avi_u_chrom and ucoe_seq_sha_ok)
else 0
)
ucoe_overlap_frac = round(ucoe_overlap_bp / float(max(1, len(cbx3_a2ucoe_455))), 4)
ucoe_avi_match_str = (
f"{ucoe_coord_str} (100% {ucoe_overlap_bp}/{len(cbx3_a2ucoe_455)} bp match, SHA-256 verified)"
if (ucoe_overlap_bp == len(cbx3_a2ucoe_455) and ucoe_coord_str == avi_ucoe_interval and ucoe_seq_sha_ok)
else f"MISMATCH: {ucoe_coord_str} vs AVI {avi_ucoe_interval} ({ucoe_overlap_bp}/{len(cbx3_a2ucoe_455)} bp overlap, sha256_ok={ucoe_seq_sha_ok})"
)
# Additional well-characterized regulatory elements with explicit provenance
chimeric_intron_133 = (
"GTAAGTATCAAGGTTACAAGACAGGTTTAAGGAGACCAATAGAAACTGGGCTTGTCGAGACAGAGAAGACTCTTGCGTTT"
"CTGATAGGCACCTATTGGTCTTACTGACATCCACTTTGCCTTTCTCTCCACAG"
)
kozak_9 = "GCCGCCACC"
# Synthetic 5xISRE (5x AGTTTCATTTTCC) + Promega pGL4 minimal TATA promoter (minP, TATATAAT) + polylinker (148 bp)
syn_5xisre_minp_148 = (
"AGTTTCATTTTCCAGTTTCATTTTCCAGTTTCATTTTCCAGTTTCATTTTCCAGTTTCATTTTCC"
"CTCGAGTAGAGGGTATATAATGGAAGCTCGACTTCCAGCTTGGCAATCCGGTACTGTTGGTAAAGCCACC"
"GGTACCGAGCTCG"
)
if not (len(syn_5xisre_minp_148) == 148):
raise ValueError('Molecular source invariant failed: len(syn_5xisre_minp_148) == 148')
chs4_core_250 = (
"GAGGGACAGCCCCCCCCCAAAGCCCCCAGGGATGTAATTACGTCCCTCCCCCGCTAGGGGGCAGCAGCGAGCCGCCCGGG"
"GCTCCGCTCCGGTCCGGCGCTCCCCCCGCATCCCCGAGCCGGCAGCGTGCGGGGACAGCCCGGGCACGGGGAAGGTGGCA"
"CGGGATCGCTTTCCTCTGAACGCTTCTCGCTGCTCTTTGAGCCTGCAGACACCTGGGGGGATACGGGGAAAAAGCTT"
"AGGCTGCCGCGGG"
)
if not (len(chs4_core_250) == 250):
raise ValueError('Molecular source invariant failed: len(chs4_core_250) == 250')
# Assemble the 4 authentic constructs and build their part-by-part ledger:
# 1. LOCUS 1 ARM 1A (Bicistronic B2M::HLA-E-SCT-GSG-P2A-CD47-IgV-GPI, no cytoplasmic Furin, 4,880 bp donor):
arm_1a_orf = hlae_sct_dna_nostop + gsg_p2a_dna + cd47_igv_gpi_dna
if not (len(arm_1a_orf) == 1500 + 66 + 528):
raise ValueError('Molecular source invariant failed: len(arm_1a_orf) == 1500 + 66 + 528')
arm_1a_parts = [
("B2M_HA_L_800bp", ha_source("B2M_HA_L_800bp"), "+", b2m_ha_l_800),
("EEF1A1_core_promoter_230bp", "GRCh38:chr6:73521000-73521229:-1(ENST00000309268.11_TSS_73521032)", "+", eef1a1_core_230),
("Chimeric_Intron_133bp", "pCI-neo_human_beta_globin_IgG_intron", "+", chimeric_intron_133),
("Kozak_9bp", "Consensus_Kozak_GCCGCCACC", "+", kozak_9),
("HLAE_0103_VMAPRTLVL_SCT_full_tail_CDS_1500bp", "UniProt:P61769+P13747(22-358)", "+", hlae_sct_dna_nostop),
("GSG_P2A_CDS_66bp", "Porcine_teschovirus_1_2A_GSG", "+", gsg_p2a_dna),
("CD47_IgV_CD55_GPI_CDS_528bp", "UniProt:Q08722(1-138_S138)+P08174(345-381_P345)", "+", cd47_igv_gpi_dna),
("WPREmut6_589bp", "GenBank:J04514.1:1093-1681_WHV_WPRE_1503AtoT_WHX_Start_Ablated(single_start_mutation_distinct_from_full_WE2_mut6_PMID:19262615)", "+", wpre_mut6_589),
("bGH_polyA_225bp", "GenBank:M57764.1:2310-2534", "+", bgh_polya_225),
("B2M_HA_R_800bp", ha_source("B2M_HA_R_800bp"), "+", b2m_ha_r_800),
]
arm_1a_seq = "".join(p[3] for p in arm_1a_parts)
if not (len(arm_1a_seq) == 4880):
raise ValueError('Molecular source invariant failed: len(arm_1a_seq) == 4880')
# 2. LOCUS 1 ARM 1B (Bidirectional 2A-free B2M::[CD47-IgV-GPI <- EEF1A1_core | Syn_5xISRE -> HLA-E-SCT], 3,974 bp donor, 4,264 bp with 2xITRs <= 4,700 bp):
# Note (closes AUDIT-REF & 022): Antisense CD47-IgV-GPI is driven directly by the antisense constitutive EEF1A1 core promoter (chr6:73521000-73521229:-1)
# and terminated by SV40_late_polyA_122bp_antisense (orientation '-'), while sense HLA-E*01:03 SCT is driven by the sense 5xISRE-minP promoter.
arm_1b_parts = [
("B2M_HA_L_600bp", ha_source("B2M_HA_L_600bp"), "+", b2m_ha_l_600),
("SV40_late_polyA_122bp_antisense", "GenBank:J02400.1:2580-2701", "-", revcomp(sv40_polya_122)),
("CD47_IgV_CD55_GPI_CDS_528bp_antisense", "UniProt:Q08722(1-138_S138)+P08174(345-381_P345)", "-", revcomp(cd47_igv_gpi_dna)),
("Kozak_9bp_antisense", "Consensus_Kozak_GCCGCCACC", "-", revcomp(kozak_9)),
("EEF1A1_core_promoter_230bp_antisense", "GRCh38:chr6:73521000-73521229:-1(ENST00000309268.11_TSS_73521032)", "-", revcomp(eef1a1_core_230)),
("Syn_5xISRE_minP_148bp_sense", "Synthetic_5xISRE(5x_AGTTTCATTTTCC)_plus_Promega_pGL4_minP_TATATAAT_polylinker(distinct_from_Ad2_MLP_J01917.1:6000-6058)", "+", syn_5xisre_minp_148),
("Kozak_9bp_sense", "Consensus_Kozak_GCCGCCACC", "+", kozak_9),
("HLAE_0103_VMAPRTLVL_SCT_full_tail_stop_CDS_1503bp", "UniProt:P61769+P13747(22-358)", "+", hlae_sct_dna_stop),
("bGH_polyA_225bp_sense", "GenBank:M57764.1:2310-2534", "+", bgh_polya_225),
("B2M_HA_R_600bp", ha_source("B2M_HA_R_600bp"), "+", b2m_ha_r_600),
]
arm_1b_seq = "".join(p[3] for p in arm_1b_parts)
if not (len(arm_1b_seq) == 3974):
raise ValueError('Molecular source invariant failed: len(arm_1b_seq) == 3974')
# 3. LOCUS 3 (CXCL10::SERPINB9-GSG-P2A-PD-L1, 4,090 bp donor, 4,380 bp with 2xITRs <= 4,700 bp):
locus_3_orf = serpinb9_dna + gsg_p2a_dna + pdl1_dna
if not (len(locus_3_orf) == 1128 + 66 + 873):
raise ValueError('Molecular source invariant failed: len(locus_3_orf) == 1128 + 66 + 873')
locus_3_parts = [
("CXCL10_HA_L_600bp_minus", ha_source("CXCL10_HA_L_600bp_minus"), "+", cxcl10_ha_l_600),
("Kozak_9bp", "Consensus_Kozak_GCCGCCACC", "+", kozak_9),
("SERPINB9_CDS_1128bp", "UniProt:P50453(1-376)", "+", serpinb9_dna),
("GSG_P2A_CDS_66bp", "Porcine_teschovirus_1_2A_GSG", "+", gsg_p2a_dna),
("PDL1_full_stop_CDS_873bp", "UniProt:Q9NZQ7(1-290)", "+", pdl1_dna),
("WPREmut6_589bp", "GenBank:J04514.1:1093-1681_WHV_WPRE_1503AtoT_WHX_Start_Ablated(single_start_mutation_distinct_from_full_WE2_mut6_PMID:19262615)", "+", wpre_mut6_589),
("bGH_polyA_225bp", "GenBank:M57764.1:2310-2534", "+", bgh_polya_225),
("CXCL10_HA_R_600bp_minus", ha_source("CXCL10_HA_R_600bp_minus"), "+", cxcl10_ha_r_600),
]
locus_3_seq = "".join(p[3] for p in locus_3_parts)
if not (len(locus_3_seq) == 4090):
raise ValueError('Molecular source invariant failed: len(locus_3_seq) == 4090')
# 4. LOCUS 4 (AAVS1-[cHS4]-[455bp HNRNPA2B1/CBX3-A2UCOE candidate]-[EEF1A1-core]-iCasp9, 3,457 bp donor, 3,747 bp with 2xITRs <= 4,700 bp):
locus_4_parts = [
("AAVS1_HA_L_600bp_minus", ha_source("AAVS1_HA_L_600bp_minus"), "+", aavs1_ha_l_600),
("cHS4_core_insulator_250bp", "GenBank:U78775.2:10-244_Gallus_HS4_235bp_exact_block_with_native_adjacent_adaptation_and_adapter_flanks(217bp_match_to_U78775.1;Chung_1997_PMID:9012826;Bell_1999_PMID:10458613)", "+", chs4_core_250),
("CBX3_A2UCOE_455bp_exact", f"GRCh38:{ucoe_coord_str}:+1(HNRNPA2B1_exon1_TSS_proximal_CpG_core_candidate_distinct_from_Zhang_2017_PMID:28860464_BglI_BsrBI_CBX3_side_chr7:26201449-26201911)", "+", cbx3_a2ucoe_455),
("EEF1A1_core_promoter_230bp", "GRCh38:chr6:73521000-73521229:-1(ENST00000309268.11_TSS_73521032)", "+", eef1a1_core_230),
("Kozak_9bp", "Consensus_Kozak_GCCGCCACC", "+", kozak_9),
("iCasp9_FKBP12F36V_dCasp9_stop_CDS_1191bp", "PMID:15728125+UniProt:P55211(135-416)", "+", icasp9_dna),
("SV40_late_polyA_122bp", "GenBank:J02400.1:2580-2701", "+", sv40_polya_122),
("AAVS1_HA_R_600bp_minus", ha_source("AAVS1_HA_R_600bp_minus"), "+", aavs1_ha_r_600),
]
locus_4_seq = "".join(p[3] for p in locus_4_parts)
if not (len(locus_4_seq) == 3457):
raise ValueError('Molecular source invariant failed: len(locus_4_seq) == 3457')
# Validate the frozen noncoding design before creating any donor file.
for parts in (arm_1a_parts, arm_1b_parts, locus_3_parts, locus_4_parts):
for name, _, _, sequence in parts:
if name in REFERENCE_REGULATORY_PART_SHA256 and sha256_str(sequence) != REFERENCE_REGULATORY_PART_SHA256[name]:
raise ValueError(f"Noncoding sequence differs from the fixed reference part: {name}")
# Write FASTA file
FASTA_OUT.parent.mkdir(parents=True, exist_ok=True)
fasta_records = [
("ISLET1_LOCUS1_ARM1A_B2M_HLAE_SCT_GSGP2A_CD47_IGV_GPI_4880bp", arm_1a_seq),
("ISLET1_LOCUS1_ARM1B_B2M_BIDIR_2AFREE_CD47_IGV_GPI_HLAE_SCT_3974bp", arm_1b_seq),
("ISLET1_LOCUS3_CXCL10_SERPINB9_GSGP2A_PDL1_NO_FURIN_4090bp", locus_3_seq),
("ISLET1_LOCUS4_AAVS1_CHS4_455BP_CBX3_A2UCOE_EEF1A1_ICASP9_3457bp", locus_4_seq),
]
with FASTA_OUT.open("w", encoding="utf-8") as f:
for hdr, seq in fasta_records:
f.write(f">{hdr} length={len(seq)} sha256={sha256_str(seq)}\n")
for i in range(0, len(seq), 80):
f.write(seq[i : i + 80] + "\n")
def _longest_exact_repeat_and_ge20_count(s: str) -> Tuple[int, int]:
n = len(s)
max_rep = 0
seen20: Dict[str, int] = {}
for i in range(max(0, n - 19)):
k20 = s[i : i + 20]
seen20[k20] = seen20.get(k20, 0) + 1
ge20_cnt = sum(1 for c in seen20.values() if c >= 2)
for rlen in range(min(120, n - 1), 0, -1):
seen_r = set()
found = False
for i in range(n - rlen + 1):
sub = s[i : i + rlen]
if sub in seen_r:
max_rep = rlen
found = True
break
seen_r.add(sub)
if found:
break
return max_rep, ge20_cnt
def _max_disjoint_inverted_repeat_stem_bp(s: str, max_k: int = 40) -> int:
n = len(s)
for k in range(min(max_k, n // 2), 0, -1):
pos: Dict[str, List[int]] = {}
for i in range(n - k + 1):
sub = s[i : i + k]
pos.setdefault(sub, []).append(i)
for i in range(n - k + 1):
rc_sub = revcomp(s[i : i + k])
if rc_sub in pos and any(abs(j - i) >= k for j in pos[rc_sub]):
return k
return 0
def _count_disjoint_inverted_repeat_stems_ge_k(s: str, min_k: int = 16) -> int:
n = len(s)
if n < 2 * min_k: