zifeng/ctgov
#2
by zifeng-ai - opened
- .gitattributes +2 -0
- README.md +16 -0
- documents/NCT02267317/Prot_SAP_000.pdf +3 -0
- documents/NCT03254108/Prot_000.pdf +3 -0
- documents/NCT03254108/SAP_001.pdf +3 -0
- documents/NCT03345004/Prot_000.pdf +3 -0
- documents/NCT03442868/Prot_SAP_000.pdf +3 -0
- documents/NCT03521102/Prot_SAP_001.pdf +3 -0
- documents/NCT03531632/Prot_000.pdf +3 -0
- documents/NCT03531632/SAP_001.pdf +3 -0
- documents/NCT03834168/Prot_SAP_000.pdf +3 -0
- documents/NCT04493892/Prot_SAP_000.pdf +3 -0
- download_ctgov_prot_sap.py +212 -0
- raw_data/20260521_clinical_trials_ctgov/provided_documents.txt +3 -0
.gitattributes
CHANGED
|
@@ -5697,3 +5697,5 @@ documents/10.1056_nejmoa1710030/protocol.pdf filter=lfs diff=lfs merge=lfs -text
|
|
| 5697 |
documents/10.1056_nejmoa1900906/sap.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5698 |
documents/10.1056_nejmoa1900906/protocol.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5699 |
documents/10.1001_jama.2018.6496/sap.pdf filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 5697 |
documents/10.1056_nejmoa1900906/sap.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5698 |
documents/10.1056_nejmoa1900906/protocol.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5699 |
documents/10.1001_jama.2018.6496/sap.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5700 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
| 5701 |
+
raw_data/**/*.txt filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -11,3 +11,19 @@ configs:
|
|
| 11 |
# TrialDesignBench Source
|
| 12 |
|
| 13 |
CSV index of clinical trials with linked protocol and SAP PDFs under `documents/<row_id>/`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# TrialDesignBench Source
|
| 12 |
|
| 13 |
CSV index of clinical trials with linked protocol and SAP PDFs under `documents/<row_id>/`.
|
| 14 |
+
|
| 15 |
+
## Bulk download from AACT
|
| 16 |
+
|
| 17 |
+
`download_ctgov_prot_sap.py` downloads protocol and SAP PDFs in bulk from an [AACT](https://aact.ctti-clinicaltrials.org/) static dataset dump. It reads `<aact_dir>/provided_documents.txt` and saves each PDF to `documents/<nct_id>/<original-filename>.pdf`.
|
| 18 |
+
|
| 19 |
+
```bash
|
| 20 |
+
pip install pandas requests tqdm
|
| 21 |
+
python download_ctgov_prot_sap.py /path/to/aact_pipe_delimited_dump
|
| 22 |
+
```
|
| 23 |
+
|
| 24 |
+
Useful flags: `--output-dir`, `--limit N`, `--full` (re-download existing), `--include-icf` (also fetch ICF-only rows).
|
| 25 |
+
|
| 26 |
+
Example output (already in `documents/`):
|
| 27 |
+
|
| 28 |
+
- `documents/NCT03254108/Prot_000.pdf`, `documents/NCT03254108/SAP_001.pdf` — separate protocol and SAP
|
| 29 |
+
- `documents/NCT03521102/Prot_SAP_001.pdf` — combined protocol + SAP
|
documents/NCT02267317/Prot_SAP_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:349d4d0886372ee6119efafb136d2da59a1ab80a9a95838c826722f5dcf13df5
|
| 3 |
+
size 1772156
|
documents/NCT03254108/Prot_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ad137c6bd5a4adf9704ed1c85533e717e8abbf6cfc346c462dd53c5a96cd990
|
| 3 |
+
size 1800675
|
documents/NCT03254108/SAP_001.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:72dd6b64c16937aa39ca95717cc5852481168c3a985b34727f2887f92d177923
|
| 3 |
+
size 383474
|
documents/NCT03345004/Prot_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3b5b652bc823b10f8379ba932fc73e25b2fe95e5f2084c45f6f3854e913f91b8
|
| 3 |
+
size 1952713
|
documents/NCT03442868/Prot_SAP_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:803988d6e31c805accbabe47c36b3974b08fa3dd1be34a98c3050802a9b0362d
|
| 3 |
+
size 725875
|
documents/NCT03521102/Prot_SAP_001.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:73bf099f89e3ff654d4b20311399e611012be96aa234ec672ec623519ef5f519
|
| 3 |
+
size 366996
|
documents/NCT03531632/Prot_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9e50087b762eb91cfe91c38c807d9b229b683a1ea63ea48e1c2b7ff16d27304f
|
| 3 |
+
size 4507356
|
documents/NCT03531632/SAP_001.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7987bc2610dafd2eb32940fb5664b17668cf8bc0a742d279b77700b4a67e7918
|
| 3 |
+
size 961049
|
documents/NCT03834168/Prot_SAP_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e1682af36e92c0301e4d962ef74eacd05438fc81721bb9d5aead49273048a07a
|
| 3 |
+
size 192900
|
documents/NCT04493892/Prot_SAP_000.pdf
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:51ad4a48016cfd435de6d1d4f7a07b743dcb9c25e43e0591a26396d0b9d0cd68
|
| 3 |
+
size 376349
|
download_ctgov_prot_sap.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Download protocol/SAP PDFs listed in an AACT provided_documents.txt dump.
|
| 3 |
+
|
| 4 |
+
Reads <aact_dir>/provided_documents.txt (pipe-delimited) and saves each PDF to
|
| 5 |
+
<output_dir>/<nct_id>/<basename>.pdf, preserving the URL's filename.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python download_ctgov_prot_sap.py <aact_dir>
|
| 9 |
+
python download_ctgov_prot_sap.py <aact_dir> --output-dir ./documents
|
| 10 |
+
python download_ctgov_prot_sap.py <aact_dir> --limit 100
|
| 11 |
+
python download_ctgov_prot_sap.py <aact_dir> --include-icf
|
| 12 |
+
python download_ctgov_prot_sap.py <aact_dir> --full # re-download existing
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import logging
|
| 19 |
+
import os
|
| 20 |
+
import re
|
| 21 |
+
import sys
|
| 22 |
+
import time
|
| 23 |
+
from pathlib import Path
|
| 24 |
+
from urllib.parse import unquote, urlparse
|
| 25 |
+
|
| 26 |
+
import pandas as pd
|
| 27 |
+
import requests
|
| 28 |
+
|
| 29 |
+
LOG = logging.getLogger("download_ctgov_prot_sap")
|
| 30 |
+
|
| 31 |
+
SCRIPT_DIR = Path(__file__).resolve().parent
|
| 32 |
+
DEFAULT_OUTPUT_DIR = SCRIPT_DIR / "documents"
|
| 33 |
+
DEFAULT_DELAY = 0.5
|
| 34 |
+
REQUEST_TIMEOUT = 120
|
| 35 |
+
|
| 36 |
+
HEADERS = {
|
| 37 |
+
"User-Agent": (
|
| 38 |
+
"Mozilla/5.0 (compatible; ClinicalTrialCrawler/1.0; "
|
| 39 |
+
"+https://github.com/clinical-trial-crawler)"
|
| 40 |
+
),
|
| 41 |
+
"Accept": "application/pdf,*/*",
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def safe_basename(url: str) -> str:
|
| 46 |
+
"""Filesystem-safe basename from a URL, defaulting to document.pdf."""
|
| 47 |
+
name = os.path.basename(unquote(urlparse(url).path)) or "document.pdf"
|
| 48 |
+
name = re.sub(r'[<>:"|?*\\\x00-\x1f]', "_", name).strip(".") or "document.pdf"
|
| 49 |
+
return name[:200]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def _norm_url(url: str) -> str:
|
| 53 |
+
u = (url or "").strip()
|
| 54 |
+
if u.startswith("http://"):
|
| 55 |
+
u = "https://" + u[7:]
|
| 56 |
+
return u
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def load_rows(aact_dir: Path, include_icf: bool) -> pd.DataFrame:
|
| 60 |
+
"""Load (nct_id, url) pairs for protocol/SAP rows from provided_documents.txt."""
|
| 61 |
+
path = aact_dir / "provided_documents.txt"
|
| 62 |
+
if not path.exists():
|
| 63 |
+
raise FileNotFoundError(f"provided_documents.txt not found in {aact_dir}")
|
| 64 |
+
|
| 65 |
+
df = pd.read_csv(path, sep="|", dtype=str, low_memory=False)
|
| 66 |
+
for col in ("nct_id", "url"):
|
| 67 |
+
if col not in df.columns:
|
| 68 |
+
raise ValueError(f"provided_documents.txt missing column: {col}")
|
| 69 |
+
|
| 70 |
+
df = df.dropna(subset=["nct_id", "url"]).copy()
|
| 71 |
+
df["nct_id"] = df["nct_id"].str.strip()
|
| 72 |
+
df["url"] = df["url"].map(_norm_url)
|
| 73 |
+
df = df[(df["nct_id"] != "") & (df["url"] != "")]
|
| 74 |
+
|
| 75 |
+
if not include_icf:
|
| 76 |
+
# Keep rows that have a protocol or SAP; drop ICF-only rows.
|
| 77 |
+
has_prot = df.get("has_protocol", "f").fillna("f").str.lower().eq("t")
|
| 78 |
+
has_sap = df.get("has_sap", "f").fillna("f").str.lower().eq("t")
|
| 79 |
+
df = df[has_prot | has_sap]
|
| 80 |
+
|
| 81 |
+
return df.drop_duplicates(subset=["nct_id", "url"]).reset_index(drop=True)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def download_one(session: requests.Session, url: str, dest: Path) -> bool:
|
| 85 |
+
"""Download url to dest. Returns True on success."""
|
| 86 |
+
try:
|
| 87 |
+
resp = session.get(url, timeout=REQUEST_TIMEOUT)
|
| 88 |
+
resp.raise_for_status()
|
| 89 |
+
dest.parent.mkdir(parents=True, exist_ok=True)
|
| 90 |
+
dest.write_bytes(resp.content)
|
| 91 |
+
return True
|
| 92 |
+
except requests.RequestException as e:
|
| 93 |
+
LOG.debug("Download failed %s: %s", url, e)
|
| 94 |
+
return False
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def run(
|
| 98 |
+
aact_dir: Path,
|
| 99 |
+
output_dir: Path,
|
| 100 |
+
delay: float = DEFAULT_DELAY,
|
| 101 |
+
limit: int = 0,
|
| 102 |
+
full: bool = False,
|
| 103 |
+
include_icf: bool = False,
|
| 104 |
+
) -> dict[str, int]:
|
| 105 |
+
df = load_rows(aact_dir, include_icf=include_icf)
|
| 106 |
+
if limit > 0:
|
| 107 |
+
df = df.head(limit)
|
| 108 |
+
LOG.info(
|
| 109 |
+
"Loaded %d (nct_id, url) pairs from %s -> writing to %s",
|
| 110 |
+
len(df), aact_dir, output_dir,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
session = requests.Session()
|
| 114 |
+
session.headers.update(HEADERS)
|
| 115 |
+
|
| 116 |
+
stats = {"downloaded": 0, "skipped": 0, "failed": 0}
|
| 117 |
+
try:
|
| 118 |
+
from tqdm import tqdm
|
| 119 |
+
it = tqdm(df.itertuples(index=False), total=len(df), desc="Download", unit="pdf")
|
| 120 |
+
except ImportError:
|
| 121 |
+
it = df.itertuples(index=False)
|
| 122 |
+
|
| 123 |
+
for row in it:
|
| 124 |
+
nct_id = row.nct_id
|
| 125 |
+
url = row.url
|
| 126 |
+
dest = output_dir / nct_id / safe_basename(url)
|
| 127 |
+
|
| 128 |
+
if not full and dest.exists() and dest.stat().st_size > 0:
|
| 129 |
+
stats["skipped"] += 1
|
| 130 |
+
continue
|
| 131 |
+
|
| 132 |
+
time.sleep(delay)
|
| 133 |
+
if download_one(session, url, dest):
|
| 134 |
+
stats["downloaded"] += 1
|
| 135 |
+
else:
|
| 136 |
+
stats["failed"] += 1
|
| 137 |
+
|
| 138 |
+
LOG.info(
|
| 139 |
+
"Done: %d downloaded, %d skipped, %d failed",
|
| 140 |
+
stats["downloaded"], stats["skipped"], stats["failed"],
|
| 141 |
+
)
|
| 142 |
+
return stats
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def main() -> int:
|
| 146 |
+
logging.basicConfig(
|
| 147 |
+
level=logging.INFO,
|
| 148 |
+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
| 149 |
+
)
|
| 150 |
+
parser = argparse.ArgumentParser(
|
| 151 |
+
description="Download CTGOV protocol/SAP PDFs from an AACT provided_documents.txt dump.",
|
| 152 |
+
)
|
| 153 |
+
parser.add_argument(
|
| 154 |
+
"aact_dir",
|
| 155 |
+
type=Path,
|
| 156 |
+
help="Path to AACT data directory containing provided_documents.txt",
|
| 157 |
+
)
|
| 158 |
+
parser.add_argument(
|
| 159 |
+
"--output-dir",
|
| 160 |
+
type=Path,
|
| 161 |
+
default=DEFAULT_OUTPUT_DIR,
|
| 162 |
+
help=f"Output base directory (default: {DEFAULT_OUTPUT_DIR})",
|
| 163 |
+
)
|
| 164 |
+
parser.add_argument(
|
| 165 |
+
"--delay",
|
| 166 |
+
type=float,
|
| 167 |
+
default=DEFAULT_DELAY,
|
| 168 |
+
help=f"Seconds between requests (default: {DEFAULT_DELAY})",
|
| 169 |
+
)
|
| 170 |
+
parser.add_argument(
|
| 171 |
+
"--limit",
|
| 172 |
+
type=int,
|
| 173 |
+
default=0,
|
| 174 |
+
help="Max rows to process (0 = all)",
|
| 175 |
+
)
|
| 176 |
+
parser.add_argument(
|
| 177 |
+
"--full",
|
| 178 |
+
action="store_true",
|
| 179 |
+
help="Re-download even if the destination file already exists",
|
| 180 |
+
)
|
| 181 |
+
parser.add_argument(
|
| 182 |
+
"--include-icf",
|
| 183 |
+
action="store_true",
|
| 184 |
+
help="Also download ICF-only rows (default: protocol/SAP only)",
|
| 185 |
+
)
|
| 186 |
+
args = parser.parse_args()
|
| 187 |
+
|
| 188 |
+
try:
|
| 189 |
+
stats = run(
|
| 190 |
+
aact_dir=args.aact_dir.resolve(),
|
| 191 |
+
output_dir=args.output_dir.resolve(),
|
| 192 |
+
delay=args.delay,
|
| 193 |
+
limit=args.limit,
|
| 194 |
+
full=args.full,
|
| 195 |
+
include_icf=args.include_icf,
|
| 196 |
+
)
|
| 197 |
+
print(
|
| 198 |
+
f"Downloaded: {stats['downloaded']} | "
|
| 199 |
+
f"Skipped: {stats['skipped']} | "
|
| 200 |
+
f"Failed: {stats['failed']}"
|
| 201 |
+
)
|
| 202 |
+
return 0
|
| 203 |
+
except (FileNotFoundError, ValueError) as e:
|
| 204 |
+
LOG.error("%s", e)
|
| 205 |
+
return 1
|
| 206 |
+
except Exception as e:
|
| 207 |
+
LOG.exception("%s", e)
|
| 208 |
+
return 1
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
if __name__ == "__main__":
|
| 212 |
+
sys.exit(main())
|
raw_data/20260521_clinical_trials_ctgov/provided_documents.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:faffa0944014c5b8499cf042ce338b54c2779f65b146ed7b58ad6bfe5672c18d
|
| 3 |
+
size 11189443
|