File size: 2,287 Bytes
0ccfe4a
 
 
6e92226
 
0ccfe4a
 
 
6e92226
0ccfe4a
 
 
 
 
 
 
 
 
 
 
 
 
 
0569d05
0ccfe4a
 
 
 
 
 
 
 
 
e53bbe5
0ccfe4a
 
 
 
0569d05
 
6e92226
 
0ccfe4a
 
e53bbe5
 
 
 
 
 
 
 
 
 
 
 
 
 
0ccfe4a
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
"""
Ingest ALS clinical trials from ClinicalTrials.gov v2 API.
Fetches ALL statuses (recruiting, completed, terminated, withdrawn, etc.)
so physicians can see the full trial landscape including failed trials.

Usage:
    uv run python scripts/ingest_trials.py
    uv run python scripts/ingest_trials.py --upsert
"""
from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from dotenv import load_dotenv

load_dotenv()

import anthropic
from rich.console import Console

from config import TRIALS_PATH
from ingestion.clinicaltrials import fetch_als_trials

console = Console()

def main() -> None:
    parser = argparse.ArgumentParser(description="Ingest ALS clinical trials")
    parser.add_argument("--upsert", action="store_true", help="Merge fetched trials into existing trials.jsonl by nct_id")
    args = parser.parse_args()

    TRIALS_PATH.parent.mkdir(parents=True, exist_ok=True)

    client = anthropic.Anthropic()

    console.print("[cyan]Fetching all ALS interventional trials (no status filter)...[/cyan]")
    trials = fetch_als_trials(client=client)
    console.print(f"[green]Fetched {len(trials)} trials[/green]")

    if args.upsert and TRIALS_PATH.exists():
        existing: dict[str, dict] = {}
        with open(TRIALS_PATH, encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    t = json.loads(line)
                    existing[t["nct_id"]] = t
        before = len(existing)
        for trial in trials:
            existing[trial["nct_id"]] = trial
        trials = list(existing.values())
        console.print(f"[cyan]Upsert: {before} existing + {len(trials) - before} new/updated → {len(trials)} total[/cyan]")

    with open(TRIALS_PATH, "w", encoding="utf-8") as f:
        for trial in trials:
            f.write(json.dumps(trial) + "\n")

    with_targets = sum(1 for t in trials if t.get("target_entities"))
    console.print(f"\n[bold green]Done![/bold green] Written to {TRIALS_PATH}")
    console.print(f"  Trials:               {len(trials)}")
    console.print(f"  With entity targets:  {with_targets}")


if __name__ == "__main__":
    main()