File size: 2,035 Bytes
0ccfe4a
 
 
 
 
 
 
0569d05
0ccfe4a
 
 
 
 
 
 
 
 
 
 
 
 
 
0569d05
0ccfe4a
 
 
 
 
 
 
0569d05
 
0ccfe4a
 
 
 
 
0569d05
 
 
 
 
 
 
 
0ccfe4a
 
 
 
 
0569d05
 
0ccfe4a
0569d05
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
#!/usr/bin/env python3
"""
Ingest ALS clinical trials from ClinicalTrials.gov v2 API.

Usage:
    uv run python scripts/ingest_trials.py
    uv run python scripts/ingest_trials.py --status RECRUITING
    uv run python scripts/ingest_trials.py --status RECRUITING NOT_YET_RECRUITING COMPLETED
"""
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()

_ALL_STATUSES = ["RECRUITING", "COMPLETED", "ACTIVE_NOT_RECRUITING", "NOT_YET_RECRUITING"]


def main() -> None:
    parser = argparse.ArgumentParser(description="Ingest ALS clinical trials")
    parser.add_argument(
        "--status",
        nargs="+",
        default=["RECRUITING", "NOT_YET_RECRUITING", "ACTIVE_NOT_RECRUITING"],
        choices=_ALL_STATUSES,
        metavar="STATUS",
        help=(
            f"One or more trial statuses to fetch (default: RECRUITING NOT_YET_RECRUITING "
            f"ACTIVE_NOT_RECRUITING). Choices: {_ALL_STATUSES}"
        ),
    )
    args = parser.parse_args()

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

    client = anthropic.Anthropic()

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

    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()