scripts/extract_ring1_corpus.py
Use this file in any AI chat (paste-only or attachment-capable):
Pinned raw file: https://openislet.org/files/gen-eb955f8266035aba/scripts/extract_ring1_corpus.py · SHA-256: eee6ac565ed0fb16…
#!/usr/bin/env python3
"""
Step 5B (v0.3 Deep Quantitative & Canonical Graph Upgrade):
Grounded Full-Text JATS XML & Abstract Relational Extractor for Ring 1 (532 Studies).
Processes Study Data Dossiers in data/dossiers/{pmid}.json into schema-validated v0.2.0 records
in data/extracted/{pmid}.json.
Key Scientific Guarantees:
1. Perturbation vs. Passive Marker Discrimination: Genes are only added to
gate2_3_4_genetic_and_epitope_edits if the study text contains explicit perturbation context
(KO, CRISPR, knockdown, overexpression, mutation/variant, inhibitor, swap, etc.) near the gene,
preventing passive lineage immunostaining markers (e.g. routine INS/CHGA/SLC30A8 staining)
from polluting the perturbation atlas.
2. Real Quantitative Extraction: Parses actual numbers (fold-changes, paired percentages,
mean +/- SD/SEM, survival days/weeks, concentration units) from Results and Figure Legends
to populate treatment_mean, control_mean, measurement_unit, and log_response_ratio_or_smd.
3. True Clause Splitting for Effect Modifiers: Splits contrast sentences around pivot conjunctions
(whereas, in contrast, while, unlike, but not, however) or pairs adjacent sentences so
level_1_condition_and_effect and level_2_condition_and_effect contain real contrasting regimes
and modifier_variable_Z names the specific biological modifier.
4. Canonical Biological Node Resolution for Causal Edges: Resolves both upstream_node and
downstream_node to canonical biological entities (Genes, Pathways, Organelle States,
Immune Effectors, Vascular Cues) so multi-hop graph traversal (Node A -> Node B -> Node C)
operates genuinely across all 532 studies.
"""
from __future__ import annotations
import argparse
import copy
from decimal import Decimal, DecimalException, InvalidOperation
import hashlib
import json
import math
import re
from pathlib import Path
from typing import Any
BASE_DIR = Path(__file__).resolve().parent.parent
RAW_DIR = BASE_DIR / "data" / "raw"
DOSSIER_DIR = BASE_DIR / "data" / "dossiers"
CURATED_DIR = BASE_DIR / "data" / "curated"
EXTRACTED_DIR = BASE_DIR / "data" / "extracted"
# Curated landmark PMIDs that should never be overwritten by automated batch pass
CURATED_LANDMARK_PMIDS = {
"35241836", # Balboa/Otonkoski 2022 Nat Biotechnol
"37156915", # Hu/Schrepfer 2024 Nat Biotechnol
"42690930", # Kobaisi/Scharfmann 2026 Cell Reports
"32719542", # Cai/Yi/Kissler 2020 Nat Metab
"36289205", # Chandra/Otonkoski 2022 Nat Commun
"35559682", # Lei/Garcia 2022 Sci Adv
"34480863", # Aghazadeh/Nostro 2021 Cell Stem Cell
"36599351", # Gerace/Melton 2023 Cell Rep Med
"36055241", # Sintov/Melton 2022 Stem Cell Reports
"34407395", # Parent/Hebrok 2021 Cell Reports
"32814902", # Yoshihara/Evans 2020 Nature
"39988068", # MacDonald/Yi 2025 Mol Metab
"31900242", # Demine/Eizirik 2020 Stem Cell Res Ther
"32846125", # Augsornworawat/Millman 2020 Cell Reports
"42671571", # Luckett/Oram/Richardson/Gaulton 2026 Diabetologia
"26276263", # Martino/De Tata 2015 Diabetologia
"40757665", # Carlsson/Schrepfer 2025 NEJM (First-in-human UP421)
"40251154", # Hu/Schrepfer 2025 Nat Commun (PMN inhibition via CD99+CD200)
"39824805", # Carre/Mallone 2025 Nat Commun (IFN-a HLA-B antigen presentation)
"40112799", # van Tienhoven/Roep 2025 Cell (INS 3'UTR rs3842752 IRE1a mRNA decay)
"40782792", # Meng/Deng 2025 Cell Stem Cell (5-subtype PSC-islet hypoglycemia protection)
"41951603", # Aggarwal/Pepper 2026 Cell Death Dis (Necrostatin-1 RIPK1/RIPK3/pMLKL shield)
"41993274", # Martin-Vazquez/Eizirik 2026 (ISG15 bat-like antiviral resistance)
"41093834", # Shibue/Kulkarni 2025 Nat Commun (SEC31A CRISPR screen alpha-cell survival)
"41851084", # Misra/Nostro 2026 Nat Commun (NGN3 patterning & enterochromaffin control)
"42856627", # Cochrane/Hebrok 2026 (MAFA vs MAFA-S64F SC-islet maturation)
"41218919", # Oshima/Scharfmann 2026 Diabetes (IIP-0036/DEC-115: curated Tier_2B human EndoC-betaH1 + primary human CD8+ T-cell cytotoxicity, Jurkat engagement reporter, and EndoC-betaH5 conditioned-medium bystander dysfunction; local dossier Abstract_Only)
"41607298", # Lei/Huang 2026 Diabetes Obes Metab (PAD4 NETosis beta-cell damage)
"38754432", # Srivastava/Wan 2024 Immunity (CXCL16 OxLDL macrophage ferroptosis axis)
"41445610", # Luckett/EXE-T1D consortium 2025 medRxiv (Genetic risk in extremely early onset T1D <2 yr)
"39626097", # McGrail/Gaulton 2025 Diabetes Care (Non-DR3/DR4 T1D genetic discovery & GRS)
"41758964", # Bonfield/EXE-T1D Consortium 2026 Diabetes (Biallelic IL2RA LoF neonatal T1D)
"41617975", # Yi/Eizirik 2026 Cell Death Differ (Alpha vs beta immune/apoptosis & MEG3 KD)
"36030035", # Contreras/Templin 2022 Mol Metab (TNF-a RIPK1/RIPK3/MLKL beta-cell necroptosis)
"42430558", # Carlsson/Schrepfer 2026 NEJM (14-month UP421 human clinical follow-up)
"32569842", # Bosma/O'Brien 2020 (G6pc2 KO fasting/ketogenic hypoglycemia counter-evidence)
"34954144", # Overway/O'Brien 2022 (Human G6PC2 coding variants functional analysis)
"27626039", # Yagyu/Brenner 2016 (iCasp9 silencing & apoptosis resistance escape in hPSCs)
"35795779", # Wunderlich/Martin 2022 (Biallelic safe-harbor iCasp9 integration against escape)
"27183617", # Polukort 2016 (IL-10 promotion of mast-cell expansion counter-evidence)
"39529170", # Wu 2024 (Base editor / nickase structural variation and large deletions)
"37264229", # Lin 2023 Nat Immunol (HLA leader-peptide polymorphism & HLA-E/NKG2A recognition)
"28504668", # Gornalusse 2017 Nat Biotechnol (B2M::HLA-E single-chain trimer universal donor hPSCs)
"37820008", # Ghimire 2023 Sci Transl Med (CD47 receptor signaling inhibits beta-cell GSIS counter-evidence)
"38508139", # Xu 2024 Cell Rep Med (Signaling-incompetent CD47-IgV avoids cell death and angiogenesis inhibition)
"40668892", # Zhao 2025 Sci Transl Med (Directional donor-recipient SIRPA mismatch innate allorecognition)
"10531307", # Krych-Goldberg 1999 J Biol Chem (CR1 LHR-A [CCPs 1-7] 0.5% C5 convertase decay vs soluble sCR1 & spaced Site-1 + Site-2)
"16177096", # Krych-Goldberg 2005 J Immunol (CR1 Site-1/Site-2 C3b/C4b decay acceleration)
"18021279", # Sweet 2008 Am J Transplant (Two-center flow-through perifusion DeltaOCR(glc); n=14 paired preparation assessments; Figs. 1 & 6)
"21813778", # Westwell-Roper 2011 J Immunol (8-wk in vivo IL-1Ra hIAPP graft rescue, Fig. 5E anakinra macrophage TNF-alpha reduction downstream of IL-1R/MyD88, & distinct non-anakinra hIAPP vs rodent IAPP macrophage IL-1beta induction)
"23562090", # Moran 2013 Lancet (Systemic anakinra NCT00711503 & canakinumab NCT00947427 recent-onset T1D RCTs)
"24222351", # Westwell-Roper 2014 Diabetes (Islet-resident macrophage IL-1beta localization & clodronate macrophage depletion)
"25491100", # Westwell-Roper 2015 Diabetologia (IL-1 blockade reduces amyloid & improves proinsulin processing)
"29862647", # Naziruddin 2018 Am J Transplant (Autologous TPIAT sequentially recruited Baylor ANA+ETA/ETA vs Leicester 2006-2009 CTL & 6-mo SUITO P=0.103 vs 0.035)
"30710150", # Nair 2019 Nat Cell Biol (Recapitulating endocrine cell clustering for SC-beta maturation; 4 non-pooled arms)
"34504013", # Yang 2021 PNAS (Convection-enhanced macroencapsulation device ceMED STZ Lewis rats)
"37922880", # Gravina 2023 Cell Stem Cell (CD64t IgG-Fc capture on iPSC-endothelial cells)
"38844131", # Wymann 2024 J Biol Chem (Recombinant CR1 LHR-ABCC/BBCC/CSL040 Table-5 Wieslab & CH50; CC BY-NC-ND 4.0)
"38993891", # Einstein 2023 Front Transplant (Empty passive subcutaneous sham macrodevice 19F-MRS longitudinal mean PO2 12 +/- 5 mmHg & 7d explant OCR)
"40789835", # Pham 2025 Nat Commun (Implantable electrochemical O2 generator iEOG Day-879 & human islets)
"40812299", # Hu 2025 Cell Stem Cell (SC291 Cas12b B2M/CIITA/TRAC KO + CD47 OE HIP CAR-T under Flu/Cy lymphodepletion)
"40976248", # Mathieu 2025 Lancet (MELD-ATG low-dose 0.5 mg/kg Day-1 active + Day-2 placebo Phase 2 T1D RCT)
"41543848", # Gou 2026 Xenotransplantation (Full-length human hCD64 on WT/GTKO porcine endothelial cells: CDC/ADCC & absent human IgM binding)
"41595986", # Brandhorst 2025 Bioengineering (Perfluorodecalin O2 nanoemulsion scaffold viability, ~6x Fig. 8 150-min insulin vs unoxygenated beta-gel, & delayed encapsulated egress)
"41604464", # Bochenek 2026 Sci Transl Med (Local GW2580 CSF1R-inhibitor NHP allogeneic islet survival)
"41870990", # Kato 2026 Am J Physiol Cell Physiol (Surgical in-place mm -> mmHg unit repair on eff1/eff2 & primary-islet arm binding on eff2; all 5 effect IDs preserved)
"42627334", # So 2026 Diabetes Care (BANDIT 96-week off-drug baricitinib paired-means LRR 0.43 +/- 0.05 vs 0.35 +/- 0.06 nmol/L & Fig. 2 wk 0-48 within-group trajectories)
"19555051", # Gerencser 2009 Anal Chem (ACS AuthorChoice; 22.7 +/- 2.5 uL n=4 paired mouse-liver mitochondria calibration on XF24 V7 polystyrene plates, not 101122-100 islet plates)
"25893782", # Pepper 2015 Nat Biotechnol (Two-stage catheter-primed pre-vascularized SC device-less murine islet site; 91% >100 d)
"31527312", # Merola 2019 JCI Insight (PMC6824302, ASCI copyright; B2M+CIITA DKO human ECFC vascular endothelium evades DSA & CD4/CD8 T cells; non-islet guard)
"36591328", # Smink 2023 Transplant Direct (PMC9788983, CC BY-NC-ND 4.0; Fig. 1 rat xenografts in athymic nude mice 65 d [PCL n=3, PDLLCL n=4, KC n=6] & separate 1.5 mm/12-wk Yucatan minipigs)
"41536981", # Jin 2026 iScience (PMC12797044, CC BY-NC-ND 4.0; separate mouse/swine cohorts & n=1 acellular cynomolgus histology with observed p<0.0001 week-4 decline)
"42176117", # Balboni 2026 Biomed Microdevices (PMC13198518, CC BY-NC-ND 4.0; removable 3D-printed NICHE placeholder in insulin-supplemented diabetic F344 rats, 5-wk prevasc, 45-d follow-up)
"17375075", # Sato 2007 Mol Ther (doi:10.1038/mt.sj.6300122; CC BY-NC-ND 4.0 publisher open archive, conservatively redacted; engineered human DTYMK/hTMPK-F105Y/AZT suicide switch; split Jurkat Fig. 3 n=3, primary T-cell early apoptotic index Results 4.0+/-0.3-fold P<0.0001, and Day-14 K562 xenograft Fig. 5 n=5/group P<0.001 vs NT without AZT)
"39696686", # Izadifar 2024 Stem Cell Res Ther (PMC11656860, CC BY 4.0; mouse Cdk1-3'UTR viral HSV-TK/GCV proliferation-gated FailSafe chip precedent; Fig. 3f descriptive mixed-population viability index vs Fig. 4h-i enriched-population short GSIS, separate parental line 2 & untreated edited controls)
"42020736", # Shi 2026 Nature (doi:10.1038/s41586-026-10395-w; abstract-only non-OA Mode-B candidate: QuadPE 1.6-26 kb ~40% at multiple loci and 11x/61x/12x relative gains at 9.5 kb vs PASSIGE/PASTE/CAST; per-panel n, neuron species, and hiPSC rates omitted from abstract)
"42056515", # Liu 2026 Nature (PMC13371498 NIHMS2190808, PMC restricted author-manuscript terms; TwinPE Prime Assembly in HEK293T separating Fig. 4b 11.3 kb single linear DMD PCR donor vs untreated Ctrl [Welch p=0.00065, legend footer mean+/-s.d. n=3-4] from Fig. 3f qualitative gel 3-segment 1.9 kb AAT; ~18 kb Design Y fallback is proposed Stage-0 extension)
"42749803", # Levesque 2026 Nature (PMC13754579, CC BY-NC-ND 4.0; dual-flap PE7 TRAC ssDNA Prime Assembly in primary human CD3+ T cells; Fig. 5g descriptive 3.3% resting vs 2.2% activated contrast across differing culture conditions in n=4 paired donors, SEM, shared TRAC edit on both arms)
"2642862", # Sutherland 1989 Diabetes (doi:10.2337/diab.38.1.s85; publisher standard non-OA abstract-only; 8 graft failures attributed to autoimmune recurrence among a series of 200 pancreas transplants; all 8 in non-/minimal-IS twin (3) or HLA-identical sibling (5) subgroup with subgroup denominator unreported in abstract)
"31427818", # Deuse 2019 Nat Biotechnol (doi:10.1038/s41587-019-0227-7; publisher standard non-OA abstract-only; de novo iPSC chrM mtDNA neoepitopes eliciting host MHC-specific immune responses in mice and humans; local dossier is abstract-only so mouse in vivo vs human in vitro assay breakdowns remain outside local text; motivates >=1,000x NUMT-filtered mtDNA QC)
"35953586", # Rouhani 2022 Nat Genet (PMC9470532, CC BY 4.0, PMC XML full-text dossier; WGS and WES across 696 sequenced hiPSC/subclone samples [555 hiPSCs + 141 derived subclones] in mixed healthy and inherited DNA-repair-disorder cohorts; 21/78 [26.9%] BCOR mutations in Insignia blood hiPSCs, 3/17 HipSci blood cohort, fibroblast UV burden)
"36944335", # Liuyang 2023 Cell Stem Cell (doi:10.1016/j.stem.2023.02.008; publisher standard non-OA abstract-only; chemically defined human somatic-to-CiPSC protocol, 16-day minimum induction & 17-donor reproducibility)
"38843830", # Lin 2024 Cell Stem Cell (PMC11227404 NIHMS author-manuscript terms, PMC XML Fig. 6C-D auto & Fig. 6A-B allo pinned; mapped locally to Ring-1 Tier_2C_NHP_Allogeneic_or_Autologous_NonIslet / NHP_iPSC_Cardiomyocyte_NonIslet [CEG T2_NHP]: descriptive comparison of 2 post-MI autologous rhesus iPSC-CM recipients [Fig. 6C-D 8 & 14 mo; abstract >6 & >12 mo] vs 2 separate non-concurrent allogeneic cardiac NHP recipients at 8 wk [Fig. 6A-B, one non-infarcted: Allo-NHP#1 residual ssTnI+ graft severely infiltrated grade 3R; Allo-NHP#2 no residual graft detectable] without immunosuppression)
"39753706", # Wang 2025 Nat Chem Biol (doi:10.1038/s41589-024-01799-8; publisher standard non-OA abstract-only; accelerated chemical reprogramming: >20-fold 16-day efficiency gain vs prior protocol [eff1], 10-day minimum [eff2], and 100% [15/15] single-arm cohort donor success [eff3])
"41436438", # Fu 2025 Cell Discov (PMC12727690, CC BY 4.0 PMC XML; 14-donor serum-free blood CiPSC induction [eff1] and 22-24 d / 18 d kinetics [eff2], lacking WGS/BCOR/mtDNA release screen)
"41690310", # Uenaka 2026 Cell Stem Cell (doi:10.1016/j.stem.2026.01.007; CC BY-NC 4.0, abstract-only; hPSC safe-harbor silencing screen during neuronal/microglial differentiation motivating CLYBL Stage-7 beta-cell retention gate)
"41764741", # Amdare 2026 J Immunol (PMC13157716, OUP publisher standard / pending PMC release, abstract-only; islet-derived human T-cell line peptide recognition on HLA-C*03:04 motivating H3 mitigation testing)
"41999746", # Cheng 2026 Cell (doi:10.1016/j.cell.2026.03.038; publisher standard non-OA abstract-only; intact p53 safeguards human chemical reprogramming via BTG2 anti-EMT signaling)
"41842939", # Kazmi/Shirwan 2026 JCI Insight (PMC13232006, CC BY 4.0; IIP-0038/DEC-117: curated Tier_4 BALB/c->C57BL/6 intraportal mouse allograft survival for SA-TM + SA-CD47, THBD discovery indexing, Fig. 1 viability/GSIS nulls, and 53/82 d vs 52/108 d date-discrepancy flag)
"37043559", # Hu/Schrepfer 2023 Sci Transl Med (IIP-0039/DEC-118: curated Abstract_Only Tier_2A primary human HIP pseudoislets removing spurious background hypoxia finding, fixing HIP acronym collision, indexing Test 2B autoimmune evasion, and re-filing anti-CD47 antibody clearance as Test 5 safety-switch context rather than Test 3 impairment)
"36593395", # Gravina/Schrepfer 2023 Nat Biotechnol (IIP-0039/DEC-118 corpus-wide HIP acronym collision cleanup: hypoimmune HIP iECs relabeled from CHGA_WE14_Hybrid_Insulin_Peptide_HIP)
"39758817", # Hu/Schrepfer 2025 Cell Stem Cell (IIP-0039/DEC-118 corpus-wide HIP acronym collision cleanup: hypoimmune HIP mouse relabeled from CHGA_WE14_Hybrid_Insulin_Peptide_HIP)
# Wave-5 IIP-0041 (CAND-031 / EXP-028): +8 primary empirical studies
"2139669", # Arend 1990 J Clin Invest (IIP-0041)
"10480601", # Arnush 1999 / M3217 (IIP-0041)
"18023480", # Garcia-Diaz 2008 (IIP-0041)
"28092375", # Dror 2017 Nat Immunol (IIP-0041)
"28494868", # Garcia-Diaz 2017 Cell Rep (IIP-0041)
"34130115", # Gu 2021 J Autoimmun (IIP-0041)
"37643018", # Bhalerao 2023 JCI Insight (IIP-0041)
"42875686", # Rana 2026 Adv Sci (IIP-0041)
# Wave-5 IIP-0042 (CAND-033 / EXP-030): +11 primary empirical studies
"20086230", # Vendrame 2010 Diabetes / M0300 (IIP-0042)
"30012675", # Haller 2018 Diabetes (IIP-0042)
"30569273", # Diggins 2019 (IIP-0042)
"30967424", # Haller 2019 Diabetes Care (IIP-0042)
"31180194", # Herold 2019 NEJM (IIP-0042)
"33632742", # Diggins 2021 (IIP-0042)
"33658358", # Sims 2021 Sci Transl Med (IIP-0042)
"37861217", # Ramos 2023 NEJM PROTECT (IIP-0042)
"39137044", # Jacobsen 2024 (IIP-0042)
"41026533", # Sassi 2025 (IIP-0042)
"41196293", # Sims 2026 (IIP-0042)
# Wave-5 IIP-0043 (CAND-034 / EXP-031): +19 primary empirical studies
"11024144", # Chehadeh 2000 J Virol (IIP-0043)
"17494992", # Dotta 2007 PNAS (IIP-0043)
"19264985", # Nejentsev 2009 Science (IIP-0043)
"26061776", # Marroqui 2015 Diabetes (IIP-0043)
"26258815", # Papas 2015 PLoS One / M0458 (IIP-0043)
"28553952", # Gorman 2017 Nature (IIP-0043)
"29514070", # Almaca 2018 Cell Metab (IIP-0043)
"30578352", # M1054 (IIP-0043)
"35013114", # Yuan 2022 / M0630 (IIP-0043)
"35228745", # M0941 (IIP-0043)
"35883656", # Stabenow 2022 Cells (IIP-0043)
"36094518", # M2123 (IIP-0043)
"36982731", # Perez-Serna 2023 / M0612 (IIP-0043)
"37531253", # Dos Santos 2023 (IIP-0043)
"37854597", # M0085 (IIP-0043)
"38956087", # M0184 (IIP-0043)
"39715591", # Wave-5 Gate 0V empirical study (IIP-0043)
"39915484", # Wave-5 Gate 0V empirical study (IIP-0043)
"41403780", # M0066 (IIP-0043)
}
# Perturbation verbs/nouns required within a window of a gene symbol to classify it as a true perturbation
PERTURBATION_CONTEXT_RE = re.compile(
r"\b(knockout|knock-out|KO\b|-/-|CRISPR|Cas9|Cas12|gRNA|sgRNA|shRNA|siRNA|knockdown|knock-down|"
r"silenc\w+|delet\w+|ablat\w+|loss\s+of\s+function|loss-of-function|LoF\b|deficien\w+|mutan\w+|"
r"mutation|R325W|P1104A|A946T|overexpress\w+|transduc\w+|transfect\w+|"
r"knock-in|knockin|KI\b|inhibit\w+|antagoni\w+|base-edit\w+|prime-edit\w+|"
r"swap\w*|cloak\w*|armored|tether\w+|microgel|autoantigen|autoreactive\s+T|epitope)\b",
flags=re.IGNORECASE,
)