"linked_geo_datasets": geo_list, "has_parsed_fulltext_xml": fulltext_block is not None, "fulltext_data_sections": fulltext_block, } # --force requests reacquisition, not destruction of scientific source data # when a stripped manifest or a transient network failure supplies no text. for key, value in existing.items(): if value not in (None, "", [], {}) and dossier.get(key) in (None, "", [], {}): dossier[key] = value if fulltext_block is None and existing.get("fulltext_data_sections"): dossier["fulltext_data_sections"] = existing["fulltext_data_sections"] dossier["has_parsed_fulltext_xml"] = bool(existing.get("has_parsed_fulltext_xml")) DOSSIER_DIR.mkdir(parents=True, exist_ok=True) with tempfile.NamedTemporaryFile("w", dir=DOSSIER_DIR, encoding="utf-8", delete=False) as fout: tmp_name = fout.name json.dump(dossier, fout, indent=2, ensure_ascii=False) fout.write("\n") os.replace(tmp_name, out_path) return pmid, bool(dossier.get("has_parsed_fulltext_xml")), 1 def main() -> None: parser = argparse.ArgumentParser(description="Build Study Data Dossiers for Project Islet-1 (OpenIslet.org).") parser.add_argument("--pilot-5", action="store_true", help="Build dossiers for the 5 Calibration Pilot studies") parser.add_argument("--ring1", action="store_true", help="Build dossiers for all 532 Ring 1 Platinum studies") parser.add_argument("--pmids", nargs="*", help="Specific PMIDs to build dossiers for") parser.add_argument("--workers", type=int, default=12, help="Concurrent Europe PMC download threads") parser.add_argument("--force", action="store_true", help="Rebuild existing dossiers") args = parser.parse_args() if args.ring1: with open(RAW_DIR / "ring1_platinum_queue.json", "r", encoding="utf-8") as f: r1 = json.load(f) target_pmids = [str(s["pmid"]) for s in r1["studies"]] elif args.pilot_5: target_pmids = PILOT_5_PMIDS else: target_pmids = args.pmids or PILOT_5_PMIDS if args.workers < 1: parser.error("--workers must be positive") for pmid in target_pmids: validate_pmid(pmid) if len(target_pmids) != len(set(target_pmids)): raise ValueError("Requested dossier PMIDs must be unique") target_set = set(target_pmids) # Load metadata index meta_by_pmid: dict[str, dict] = {} manifest_file = RAW_DIR / "corpus_manifest_all_sources.jsonl" with open(manifest_file, "r", encoding="utf-8") as f: for line in f: if line.strip(): rec = json.loads(line) if str(rec.get("pmid")) in target_set: if str(rec["pmid"]) in meta_by_pmid: raise ValueError("Duplicate requested PMID in metadata manifest") meta_by_pmid[str(rec["pmid"])] = rec # Load GEO datasets pmid_to_geo: dict[str, list[dict]] = {} geo_file = RAW_DIR / "functional_genomics_geo_datasets.json" if geo_file.exists(): with open(geo_file, "r", encoding="utf-8") as f: gdata = json.load(f) for ds in gdata.get("datasets", []): for pid in ds.get("pubmed_ids", []): pmid_to_geo.setdefault(str(pid), []).append(ds) print(f"[DOSSIER BUILDER] Building {len(target_pmids)} Study Data Dossiers with {args.workers} workers...") fulltext_count = 0 abstract_only_count = 0 newly_built = 0 with ThreadPoolExecutor(max_workers=args.workers) as pool: futs = { pool.submit( build_one_dossier, pmid, meta_by_pmid.get(pmid, {"pmid": pmid}), pmid_to_geo.get(pmid, []), args.force, ): pmid for pmid in target_pmids } for idx, fut in enumerate(as_completed(futs), 1): pmid, has_ft, is_new = fut.result() if has_ft: fulltext_count += 1 else: abstract_only_count += 1 newly_built += is_new if idx % 50 == 0 or idx == len(target_pmids): print( f" Progress: {idx}/{len(target_pmids)} dossiers ready " f"(FullText XML parsed: {fulltext_count}, Abstract+Metadata: {abstract_only_count}, Newly built: {newly_built})" ) print( f"[COMPLETE] {len(target_pmids)} dossiers in {DOSSIER_DIR} | " f"FullText XML parsed: {fulltext_count} | Abstract+GEO/MeSH: {abstract_only_count}" ) if __name__ == "__main__": main()