OpenIslet

scripts/harvest_corpus.py

OpenIslet.org (Project Islet-1) — Multi-Source Corpus Finder & Harvester (v0.3)

script Release v0.8.0 gen-eb955f8266035aba 60 KB · 5 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/harvest_corpus.py · SHA-256: c61503b386f1f486…

        'OR "survival"[Title/Abstract] OR "apoptosis"[Title/Abstract] OR "islet*"[Title/Abstract]))'
    ),
    "Pillar3B_Amyloid_Enterovirus_Senescence_Medalists": (
        '((("beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] '
        'OR "pancreatic islet*"[Title/Abstract] OR "human islet*"[Title/Abstract] '
        'OR "SC-islet*"[Title/Abstract] OR "islet*"[Title/Abstract] OR "diabetes"[Title/Abstract]) '
        'AND ("CXADR"[Title/Abstract] OR "CAR-SIV"[Title/Abstract] '
        'OR "Coxsackie and adenovirus receptor"[Title/Abstract] '
        'OR ("coxsackie*"[Title/Abstract] AND ("beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] OR "islet*"[Title/Abstract] OR "tropism"[Title/Abstract] OR "persistent"[Title/Abstract] OR "granule*"[Title/Abstract])) '
        'OR ("enterovir*"[Title/Abstract] AND ("CXCL10"[Title/Abstract] OR "CXADR"[Title/Abstract] OR "CAR-SIV"[Title/Abstract] OR "beta-cell failure"[Title/Abstract] OR "secretory granule*"[Title/Abstract])) '
        'OR ("CXCL10"[Title/Abstract] AND "CXCR3"[Title/Abstract] AND ("islet*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] OR "beta cell*"[Title/Abstract])) '
        'OR "senolytic*"[Title/Abstract] OR ("p16INK4a"[Title/Abstract] AND ("islet*"[Title/Abstract] OR "beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract])) '
        'OR ("Medalist*"[Title/Abstract] AND ("Joslin"[Title/Abstract] OR "50 year*"[Title/Abstract] OR "residual insulin"[Title/Abstract])) '
        'OR (("IAPP"[Title/Abstract] OR "islet amyloid polypeptide"[Title/Abstract] OR "islet amyloid"[Title/Abstract] OR "pramlintide"[Title/Abstract]) '
        'AND ("transplant*"[Title/Abstract] OR "graft*"[Title/Abstract] OR "NLRP3"[Title/Abstract] OR "inflammasome"[Title/Abstract] OR "proline"[Title/Abstract])) '
        'OR ("amyloid*"[Title/Abstract] AND "transplanted"[Title/Abstract] AND "islet*"[Title/Abstract]) '
        'OR ("NLRP3"[Title/Abstract] AND "inflammasome"[Title/Abstract] AND ("IAPP"[Title/Abstract] OR "islet*"[Title/Abstract] OR "diabetes"[Title/Abstract])))))'
    ),
    "Pillar3C_HPAP_nPOD_Human_T1D_Islet_Atlas": (
        '(("Human Pancreas Analysis Program"[Title/Abstract] OR "HPAP"[Title/Abstract] '
        'OR "PANC-DB"[Title/Abstract] '
        'OR "Network for Pancreatic Organ Donors with Diabetes"[Title/Abstract] '
        'OR "nPOD"[Title/Abstract]) '
        'AND ("islet*"[Title/Abstract] OR "beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] '
        'OR "pancrea*"[Title/Abstract] OR "type 1 diabetes"[Title/Abstract] OR "autoimmun*"[Title/Abstract]))'
    ),
    "Pillar4A_SC_Islet_and_Allogeneic_Engraftment_Devices": (
        '(("stem cell-derived islet*"[Title/Abstract] OR "stem-cell-derived islet*"[Title/Abstract] '
        'OR "SC-islet*"[Title/Abstract] OR "allogeneic islet*"[Title/Abstract] '
        'OR "islet allograft*"[Title/Abstract] OR "VX-880"[Title/Abstract] '
        'OR "zimislecel"[Title/Abstract] OR "VX-264"[Title/Abstract] '
        'OR "PEC-Direct"[Title/Abstract] OR "PEC-Encap"[Title/Abstract] '
        'OR "VC-01"[Title/Abstract] OR "VC-02"[Title/Abstract] '
        'OR "ViaCyte"[Title/Abstract] OR "UP421"[Title/Abstract] '
        'OR "VCTX210"[Title/Abstract] OR "VCTX211"[Title/Abstract] OR "Cell Pouch"[Title/Abstract]) '
        'AND ("engraftment"[Title/Abstract] OR "vasculariz*"[Title/Abstract] '
        'OR "revasculariz*"[Title/Abstract] OR "inosculation"[Title/Abstract] '
        'OR "encapsulat*"[Title/Abstract] OR "hypoxia"[Title/Abstract] '
        'OR "oxygen*"[Title/Abstract] OR "hydrogel"[Title/Abstract] '
        'OR "scaffold"[Title/Abstract] OR "subcutaneous"[Title/Abstract] '
        'OR "omental"[Title/Abstract] OR "intramuscular"[Title/Abstract] '
        'OR "IBMIR"[Title/Abstract] OR "tissue factor"[Title/Abstract] '
        'OR "FasL"[Title/Abstract] OR "microgel*"[Title/Abstract] '
        'OR "survival"[Title/Abstract] OR "clinical trial"[Title/Abstract] OR "C-peptide"[Title/Abstract]))'
    ),
    "Pillar4B_Islet_Vascularization_Inosculation_Oxygen_IBMIR_SafetySwitch": (
        '(("islet transplant*"[Title/Abstract] OR "transplanted islet*"[Title/Abstract] '
        'OR "islet graft*"[Title/Abstract] OR "islet organoid*"[Title/Abstract] '
        'OR "human islet*"[Title/Abstract] '
        'OR ("islet cells"[Title/Abstract] AND "transplantation"[Title/Abstract])) '
        'AND ("inosculation"[Title/Abstract] OR "prevasculariz*"[Title/Abstract] '
        'OR "pre-vasculariz*"[Title/Abstract] OR "microvascular fragment*"[Title/Abstract] '
        'OR "microvessel*"[Title/Abstract] '
        'OR "functional blood vessel*"[Title/Abstract] OR "endothelial co-cultur*"[Title/Abstract] '
        'OR ("endothelial cell*"[Title/Abstract] AND "pericyte*"[Title/Abstract]) '
        'OR "parathyroid"[Title/Abstract] OR "oxygen-generating"[Title/Abstract] '
        'OR "oxygen-releasing"[Title/Abstract] OR "calcium peroxide"[Title/Abstract] '
        'OR "CaO2"[Title/Abstract] OR "perfluorocarbon"[Title/Abstract] '
        'OR "IBMIR"[Title/Abstract] OR "instant blood-mediated"[Title/Abstract] '
        'OR ("tissue factor"[Title/Abstract] AND ("clotting"[Title/Abstract] OR "thrombo*"[Title/Abstract] OR "coagulation"[Title/Abstract] OR "IBMIR"[Title/Abstract])) '
        'OR "omental pouch"[Title/Abstract] OR "zwitterionic"[Title/Abstract] '
        'OR "iCasp9"[Title/Abstract] OR "inducible caspase"[Title/Abstract] OR "suicide gene"[Title/Abstract]))'
    ),
}

WIDE_QUERIES = {
    "Wide_All_StemCell_Beta_Islet": (
        '(("stem cell*"[Title/Abstract] OR "iPSC*"[Title/Abstract] '
        'OR "hESC*"[Title/Abstract] OR "pluripotent"[Title/Abstract]) '
        'AND ("beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] '
        'OR "pancreatic islet*"[Title/Abstract] OR "insulin-producing"[Title/Abstract] '
        'OR "SC-islet*"[Title/Abstract]))'
    ),
    "Wide_All_Islet_Transplant_Rejection_Survival_Vascular": (
        '(("islet transplant*"[Title/Abstract] OR "islet graft*"[Title/Abstract] '
        'OR "allogeneic islet*"[Title/Abstract] OR "transplanted islet*"[Title/Abstract]) '
        'AND ("rejection"[Title/Abstract] OR "survival"[Title/Abstract] '
        'OR "tolerance"[Title/Abstract] OR "engraftment"[Title/Abstract] '
        'OR "encapsulat*"[Title/Abstract] OR "hypoxia"[Title/Abstract] '
        'OR "vasculariz*"[Title/Abstract] OR "revasculariz*"[Title/Abstract] '
        'OR "oxygen*"[Title/Abstract] OR "IBMIR"[Title/Abstract] '
        'OR "immunosuppress*"[Title/Abstract]))'
    ),
    "Wide_All_T1D_BetaCell_Gene_Perturbation_Protection": (
        '(("type 1 diabetes"[Title/Abstract] OR "autoimmune diabetes"[Title/Abstract] '
        'OR "LADA"[Title/Abstract]) '
        'AND ("beta cell*"[Title/Abstract] OR "beta-cell*"[Title/Abstract] OR "islet*"[Title/Abstract]) '
        'AND ("protect*"[Title/Abstract] OR "survival"[Title/Abstract] '
        'OR "apoptosis"[Title/Abstract] OR "knockout"[Title/Abstract] '
        'OR "overexpress*"[Title/Abstract] OR "CRISPR"[Title/Abstract] '
        'OR "base edit*"[Title/Abstract] OR "prime edit*"[Title/Abstract] '
        'OR "inhibit*"[Title/Abstract] OR "C-peptide"[Title/Abstract]))'
    ),
}

# Europe PMC Supplementary Discovery Queries (Full-Text Body Targets & Preprints)
EUROPEPMC_SUPPLEMENTARY_QUERIES = {
    "EPMC_FullText_SC_Islet_26_Target_Genes": (
        'TITLE_ABS:("SC-islet*" OR "stem cell-derived islet*" OR "stem-cell-derived islet*" '
        'OR "SC-beta" OR "iPSC-derived beta" OR "hESC-derived beta" OR "EndoC-betaH1") '
        'AND ("TYK2" OR "RNLS" OR "IFIH1" OR "CXADR" OR "NLRC5" OR "SLC30A8" OR "G6PC2" '
        'OR "PTPRN" OR "GAD2" OR "IAPP" OR "CHGA" OR "TGM2" OR "SERPINB9" OR "TNFAIP3" '
        'OR "PTPN2" OR "TXNIP" OR "MANF" OR "GLIS3" OR "B2M" OR "CIITA" OR "CD47" '
        'OR "CD274" OR "CD55" OR "CD59" OR "ENTPD1" OR "CXCL10") AND HAS_FT:y'
    ),
    "EPMC_Preprints_SC_Islet_and_Hypoimmune": (
        'SRC:PPR AND ("SC-islet" OR "SC-islets" OR "stem cell-derived islet" '
        'OR "stem-cell-derived islet" OR "stem cell-derived islets" '
        'OR ("hypoimmune" AND "islet") OR ("hypoimmunogenic" AND "islet") '
        'OR ("B2M" AND "islet") OR ("CD47" AND "islet") OR ("HLA-E" AND "islet") '
        'OR ("VX-880" OR "zimislecel" OR "VX-264" OR "UP421"))'
    ),
}

# Europe PMC Patent Discovery Queries (SRC:PAT)
EUROPEPMC_PATENT_QUERIES = {
    "Patent_SC_Islet_and_Hypoimmune_Engineering": (
        'SRC:PAT AND (islet AND (pluripotent OR iPSC OR hESC OR stem '
        'OR hypoimmunogenic OR hypoimmune OR B2M OR CIITA OR CD47 '
        'OR HLA OR encapsulation OR vascularization))'
    ),
}

# NCBI GEO (GDS) Functional Genomics & Single-Cell Dataset Queries
GEO_GDS_QUERIES = {
    "GEO_SC_Islet_Differentiation_and_MultiOmics": (
        '("SC-islet*" OR "stem cell-derived islet*" OR "stem-cell-derived islet*" '
        'OR "SC-beta" OR "iPSC-derived beta" OR "hESC-derived beta" '
        'OR "iPSC-derived islet*" OR "hESC-derived islet*") AND "gse"[ETYP]'
    ),
    "GEO_BetaCell_CRISPR_Screens_and_Gene_Perturbation": (
        '(("CRISPR" OR "Perturb-seq" OR "Cas9" OR "Cas12a" OR "genetic screen" OR "genome-wide screen") '
        'AND ("beta cell*" OR "beta-cell*" OR "pancreatic islet*" OR "human islet*" '
        'OR "EndoC-betaH1" OR "SC-islet*" OR "NIT-1" OR "MIN6")) AND "gse"[ETYP]'
    ),
    "GEO_HPAP_and_nPOD_Human_T1D_Islet_Omics": (
        '(("Human Pancreas Analysis Program" OR "HPAP" OR "nPOD" '
        'OR "Network for Pancreatic Organ Donors") '
        'AND ("islet*" OR "pancrea*" OR "beta cell*" OR "beta-cell*")) AND "gse"[ETYP]'
    ),
    "GEO_Hypoimmune_and_Target_Gene_Islet_Studies": (
        '(("hypoimmune" OR "hypoimmunogenic" OR "B2M" OR "CIITA" OR "CD47" '
        'OR "TYK2" OR "RNLS" OR "SLC30A8" OR "CXADR" OR "TET2" OR "MANF" OR "TNFAIP3") '
        'AND ("pancreatic islet*" OR "human islet*" OR "SC-islet*" OR "beta-cell*" OR "EndoC-betaH1")) AND "gse"[ETYP]'
    ),
}

NON_REVIEW_FILTER = ' NOT ("Review"[Publication Type] OR "Editorial"[Publication Type] OR "Comment"[Publication Type])'


def esearch_pmids(query: str, retmax: int = 10000) -> list[str]:
    if type(retmax) is not int or retmax <= 0:
        raise ValueError("PubMed page size must be a positive integer")
    pmids = []
    seen = set()
    retstart = 0
    expected_total = None
    while True:
        params = {
            "db": "pubmed",
            "term": query,
            "retmode": "json",
            "retstart": str(retstart),
            "retmax": str(min(retmax, 5000)),
        }
        api_key = os.environ.get("NCBI_API_KEY")
        if api_key:
            params["api_key"] = api_key
        url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?" + urllib.parse.urlencode(params)
        with urllib.request.urlopen(url, timeout=30) as resp:
            data = json.loads(resp.read().decode("utf-8"))
        res = data["esearchresult"]
        batch = res.get("idlist", [])
        raw_total = res.get("count")
        if not isinstance(raw_total, str) or not raw_total.isdigit():
            raise ValueError("PubMed response has an invalid result count")
        total = int(raw_total)
        if expected_total is None:
            expected_total = total
        elif total != expected_total:
            raise ValueError("PubMed result count changed during pagination; retry a stable query snapshot")
        if not isinstance(batch, list) or any(not isinstance(pid, str) or not pid.isdigit() or int(pid) <= 0 for pid in batch):
            raise ValueError("PubMed response has invalid PMID identifiers")
        if len(set(batch)) != len(batch) or seen.intersection(batch):
            raise ValueError("PubMed pagination repeated PMID identifiers; query snapshot is incomplete")
        if len(batch) > min(retmax, 5000) or retstart + len(batch) > expected_total:
            raise ValueError("PubMed response exceeds its declared result count or page size")
        if not batch and retstart < total:
            raise ValueError(f"PubMed pagination truncated at {retstart} of {total}; partition the query before claiming complete retrieval")
        pmids.extend(batch)
        seen.update(batch)
        retstart += len(batch)
        time.sleep(0.35)
        if retstart >= total:
            break
    return pmids


def _validated_europepmc_page(data: dict, total, fetched: int, seen_cursors: set[str], seen_records: set[tuple[str, str]]):
    """Distinguish a complete result set from an exhausted or repeated cursor."""
    if not isinstance(data, dict) or not isinstance(data.get("resultList"), dict):
        raise ValueError("Europe PMC response has invalid result container")
    count = data.get("hitCount")
    results = data.get("resultList", {}).get("result", [])
    if (type(count) is not int or count < 0 or not isinstance(results, list)
            or any(not isinstance(record, dict) for record in results)):
        raise ValueError("Europe PMC response has invalid result count or records")
    if total is not None and count != total:
        raise ValueError("Europe PMC result count changed during pagination")
    for record in results:
        pmid = record.get("pmid")
        if pmid not in (None, "") and (not isinstance(pmid, str) or not pmid.isascii()
                or not pmid.isdigit() or not 1 <= len(pmid) <= 9 or pmid[0] == "0"):
            raise ValueError("Europe PMC result has invalid PMID identity")
        source = record.get("source", "MED" if record.get("pmid") else "")
        identifier = record.get("id", record.get("pmid"))
        if not isinstance(source, str) or not source or not isinstance(identifier, str) or not identifier:
            raise ValueError("Europe PMC result lacks a valid source identity")
        identity = (source, identifier)
        if identity in seen_records:
            raise ValueError("Europe PMC pagination repeated a record identity")
        seen_records.add(identity)
    fetched += len(results)
    if fetched > count:
        raise ValueError("Europe PMC response exceeds its declared result count")
    if fetched == count:
        return results, count, fetched, None
    next_cursor = data.get("nextCursorMark")
    if not results or not isinstance(next_cursor, str) or not next_cursor or next_cursor in seen_cursors:
        raise ValueError("Europe PMC pagination ended before retrieval completion")
    seen_cursors.add(next_cursor)
    return results, count, fetched, next_cursor


def europepmc_search_ids(query: str, max_pages: int = 10) -> tuple[set[str], list[dict]]:
    """Queries Europe PMC REST API for additional PMIDs and Preprint (PPR/DOI) records."""
    if type(max_pages) is not int or max_pages <= 0:
        raise ValueError("Europe PMC page budget must be a positive integer")
    pmids: set[str] = set()
    preprints: list[dict] = []
    cursor = "*"
    total, fetched, seen_cursors = None, 0, {cursor}
    seen_records: set[tuple[str, str]] = set()
    for _ in range(max_pages):
        params = {
            "query": query,
            "format": "json",
            "pageSize": "1000",
            "resultType": "lite",
            "cursorMark": cursor,