Spaces:
Running
Running
File size: 13,267 Bytes
3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 4e2ccbf 3fcd8e7 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# Phase 10 Implementation Spec: ClinicalTrials.gov Integration
**Goal**: Add clinical trial search for drug repurposing evidence.
**Philosophy**: "Clinical trials are the bridge from hypothesis to therapy."
**Prerequisite**: Phase 9 complete (DuckDuckGo removed)
**Estimated Time**: 2-3 hours
---
## 1. Why ClinicalTrials.gov?
### Scientific Value
| Feature | Value for Drug Repurposing |
|---------|---------------------------|
| **400,000+ studies** | Massive evidence base |
| **Trial phase data** | Phase I/II/III = evidence strength |
| **Intervention details** | Exact drug + dosing |
| **Outcome measures** | What was measured |
| **Status tracking** | Completed vs recruiting |
| **Free API** | No cost, no key required |
### Example Query Response
Query: "metformin Alzheimer's"
```json
{
"studies": [
{
"nctId": "NCT04098666",
"briefTitle": "Metformin in Alzheimer's Dementia Prevention",
"phase": "Phase 2",
"status": "Recruiting",
"conditions": ["Alzheimer Disease"],
"interventions": ["Drug: Metformin"]
}
]
}
```
**This is GOLD for drug repurposing** - actual trials testing the hypothesis!
---
## 2. API Specification
### Endpoint
```
Base URL: https://clinicaltrials.gov/api/v2/studies
```
### Key Parameters
| Parameter | Description | Example |
|-----------|-------------|---------|
| `query.cond` | Condition/disease | `Alzheimer` |
| `query.intr` | Intervention/drug | `Metformin` |
| `query.term` | General search | `metformin alzheimer` |
| `pageSize` | Results per page | `20` |
| `fields` | Fields to return | See below |
### Fields We Need
```
NCTId, BriefTitle, Phase, OverallStatus, Condition,
InterventionName, StartDate, CompletionDate, BriefSummary
```
### Rate Limits
- ~50 requests/minute per IP
- No authentication required
- Paginated (100 results max per call)
### Documentation
- [API v2 Docs](https://clinicaltrials.gov/data-api/api)
- [Migration Guide](https://www.nlm.nih.gov/pubs/techbull/ma24/ma24_clinicaltrials_api.html)
---
## 3. Data Model
### 3.1 Update Citation Source Type (`src/utils/models.py`)
```python
# BEFORE
source: Literal["pubmed", "web"]
# AFTER
source: Literal["pubmed", "clinicaltrials", "biorxiv"]
```
### 3.2 Evidence from Clinical Trials
Clinical trial data maps to our existing `Evidence` model:
```python
Evidence(
content=f"{brief_summary}. Phase: {phase}. Status: {status}.",
citation=Citation(
source="clinicaltrials",
title=brief_title,
url=f"https://clinicaltrials.gov/study/{nct_id}",
date=start_date or "Unknown",
authors=[] # Trials don't have authors in the same way
),
relevance=0.8 # Trials are highly relevant for repurposing
)
```
---
## 4. Implementation
### 4.0 Important: HTTP Client Selection
**ClinicalTrials.gov's WAF blocks `httpx`'s TLS fingerprint.** Use `requests` instead.
| Library | Status | Notes |
|---------|--------|-------|
| `httpx` | β 403 Blocked | TLS/JA3 fingerprint flagged |
| `httpx[http2]` | β 403 Blocked | HTTP/2 doesn't help |
| `requests` | β
Works | Industry standard, not blocked |
| `urllib` | β
Works | Stdlib alternative |
We use `requests` wrapped in `asyncio.to_thread()` for async compatibility.
### 4.1 ClinicalTrials Tool (`src/tools/clinicaltrials.py`)
```python
"""ClinicalTrials.gov search tool using API v2."""
import asyncio
from typing import Any, ClassVar
import requests
from tenacity import retry, stop_after_attempt, wait_exponential
from src.utils.exceptions import SearchError
from src.utils.models import Citation, Evidence
class ClinicalTrialsTool:
"""Search tool for ClinicalTrials.gov.
Note: Uses `requests` library instead of `httpx` because ClinicalTrials.gov's
WAF blocks httpx's TLS fingerprint. The `requests` library is not blocked.
"""
BASE_URL = "https://clinicaltrials.gov/api/v2/studies"
FIELDS: ClassVar[list[str]] = [
"NCTId",
"BriefTitle",
"Phase",
"OverallStatus",
"Condition",
"InterventionName",
"StartDate",
"BriefSummary",
]
@property
def name(self) -> str:
return "clinicaltrials"
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
reraise=True,
)
async def search(self, query: str, max_results: int = 10) -> list[Evidence]:
"""Search ClinicalTrials.gov for studies."""
params = {
"query.term": query,
"pageSize": min(max_results, 100),
"fields": "|".join(self.FIELDS),
}
try:
# Run blocking requests.get in a separate thread for async compatibility
response = await asyncio.to_thread(
requests.get,
self.BASE_URL,
params=params,
headers={"User-Agent": "DeepCritical-Research-Agent/1.0"},
timeout=30,
)
response.raise_for_status()
data = response.json()
studies = data.get("studies", [])
return [self._study_to_evidence(study) for study in studies[:max_results]]
except requests.HTTPError as e:
raise SearchError(f"ClinicalTrials.gov API error: {e}") from e
except requests.RequestException as e:
raise SearchError(f"ClinicalTrials.gov request failed: {e}") from e
def _study_to_evidence(self, study: dict) -> Evidence:
"""Convert a clinical trial study to Evidence."""
# Navigate nested structure
protocol = study.get("protocolSection", {})
id_module = protocol.get("identificationModule", {})
status_module = protocol.get("statusModule", {})
desc_module = protocol.get("descriptionModule", {})
design_module = protocol.get("designModule", {})
conditions_module = protocol.get("conditionsModule", {})
arms_module = protocol.get("armsInterventionsModule", {})
nct_id = id_module.get("nctId", "Unknown")
title = id_module.get("briefTitle", "Untitled Study")
status = status_module.get("overallStatus", "Unknown")
start_date = status_module.get("startDateStruct", {}).get("date", "Unknown")
# Get phase (might be a list)
phases = design_module.get("phases", [])
phase = phases[0] if phases else "Not Applicable"
# Get conditions
conditions = conditions_module.get("conditions", [])
conditions_str = ", ".join(conditions[:3]) if conditions else "Unknown"
# Get interventions
interventions = arms_module.get("interventions", [])
intervention_names = [i.get("name", "") for i in interventions[:3]]
interventions_str = ", ".join(intervention_names) if intervention_names else "Unknown"
# Get summary
summary = desc_module.get("briefSummary", "No summary available.")
# Build content with key trial info
content = (
f"{summary[:500]}... "
f"Trial Phase: {phase}. "
f"Status: {status}. "
f"Conditions: {conditions_str}. "
f"Interventions: {interventions_str}."
)
return Evidence(
content=content[:2000],
citation=Citation(
source="clinicaltrials",
title=title[:500],
url=f"https://clinicaltrials.gov/study/{nct_id}",
date=start_date,
authors=[], # Trials don't have traditional authors
),
relevance=0.85, # Trials are highly relevant for repurposing
)
```
---
## 5. TDD Test Suite
### 5.1 Unit Tests (`tests/unit/tools/test_clinicaltrials.py`)
Uses `unittest.mock.patch` to mock `requests.get` (not `respx` since we're not using `httpx`).
```python
"""Unit tests for ClinicalTrials.gov tool."""
from unittest.mock import MagicMock, patch
import pytest
import requests
from src.tools.clinicaltrials import ClinicalTrialsTool
from src.utils.exceptions import SearchError
from src.utils.models import Evidence
@pytest.fixture
def mock_clinicaltrials_response() -> dict:
"""Mock ClinicalTrials.gov API response."""
return {
"studies": [
{
"protocolSection": {
"identificationModule": {
"nctId": "NCT04098666",
"briefTitle": "Metformin in Alzheimer's Dementia Prevention",
},
"statusModule": {
"overallStatus": "Recruiting",
"startDateStruct": {"date": "2020-01-15"},
},
"descriptionModule": {
"briefSummary": "This study evaluates metformin for Alzheimer's prevention."
},
"designModule": {"phases": ["PHASE2"]},
"conditionsModule": {"conditions": ["Alzheimer Disease", "Dementia"]},
"armsInterventionsModule": {
"interventions": [{"name": "Metformin", "type": "Drug"}]
},
}
}
]
}
class TestClinicalTrialsTool:
"""Tests for ClinicalTrialsTool."""
def test_tool_name(self) -> None:
"""Tool should have correct name."""
tool = ClinicalTrialsTool()
assert tool.name == "clinicaltrials"
@pytest.mark.asyncio
async def test_search_returns_evidence(
self, mock_clinicaltrials_response: dict
) -> None:
"""Search should return Evidence objects."""
with patch("src.tools.clinicaltrials.requests.get") as mock_get:
mock_response = MagicMock()
mock_response.json.return_value = mock_clinicaltrials_response
mock_response.raise_for_status = MagicMock()
mock_get.return_value = mock_response
tool = ClinicalTrialsTool()
results = await tool.search("metformin alzheimer", max_results=5)
assert len(results) == 1
assert isinstance(results[0], Evidence)
assert results[0].citation.source == "clinicaltrials"
assert "NCT04098666" in results[0].citation.url
assert "Metformin" in results[0].citation.title
@pytest.mark.asyncio
async def test_search_api_error(self) -> None:
"""Search should raise SearchError on API failure."""
with patch("src.tools.clinicaltrials.requests.get") as mock_get:
mock_response = MagicMock()
mock_response.raise_for_status.side_effect = requests.HTTPError(
"500 Server Error"
)
mock_get.return_value = mock_response
tool = ClinicalTrialsTool()
with pytest.raises(SearchError):
await tool.search("metformin alzheimer")
class TestClinicalTrialsIntegration:
"""Integration tests (marked for separate run)."""
@pytest.mark.integration
@pytest.mark.asyncio
async def test_real_api_call(self) -> None:
"""Test actual API call (requires network)."""
tool = ClinicalTrialsTool()
results = await tool.search("metformin diabetes", max_results=3)
assert len(results) > 0
assert all(isinstance(r, Evidence) for r in results)
assert all(r.citation.source == "clinicaltrials" for r in results)
```
---
## 6. Integration with SearchHandler
### 6.1 Update Example Files
```python
# examples/search_demo/run_search.py
from src.tools.clinicaltrials import ClinicalTrialsTool
from src.tools.pubmed import PubMedTool
from src.tools.search_handler import SearchHandler
search_handler = SearchHandler(
tools=[PubMedTool(), ClinicalTrialsTool()],
timeout=30.0
)
```
### 6.2 Update SearchResult Type
```python
# src/utils/models.py
sources_searched: list[Literal["pubmed", "clinicaltrials"]]
```
---
## 7. Definition of Done
Phase 10 is **COMPLETE** when:
- [ ] `src/tools/clinicaltrials.py` implemented
- [ ] Unit tests in `tests/unit/tools/test_clinicaltrials.py`
- [ ] Integration test marked with `@pytest.mark.integration`
- [ ] SearchHandler updated to include ClinicalTrialsTool
- [ ] Type definitions updated in models.py
- [ ] Example files updated
- [ ] All unit tests pass
- [ ] Lints pass
- [ ] Manual verification with real API
---
## 8. Verification Commands
```bash
# 1. Run unit tests
uv run pytest tests/unit/tools/test_clinicaltrials.py -v
# 2. Run integration test (requires network)
uv run pytest tests/unit/tools/test_clinicaltrials.py -v -m integration
# 3. Run full test suite
uv run pytest tests/unit/ -v
# 4. Run example
source .env && uv run python examples/search_demo/run_search.py "metformin alzheimer"
# Should show results from BOTH PubMed AND ClinicalTrials.gov
```
---
## 9. Value Delivered
| Before | After |
|--------|-------|
| Papers only | Papers + Clinical Trials |
| "Drug X might help" | "Drug X is in Phase II trial" |
| No trial status | Recruiting/Completed/Terminated |
| No phase info | Phase I/II/III evidence strength |
**Demo pitch addition**:
> "DeepCritical searches PubMed for peer-reviewed evidence AND ClinicalTrials.gov for 400,000+ clinical trials."
|