mgbam commited on
Commit
955107f
·
verified ·
1 Parent(s): 5eb3323

Create trials.py

Browse files
Files changed (1) hide show
  1. genesis/trials.py +60 -0
genesis/trials.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # genesis/trials.py
2
+ import requests
3
+ from typing import List, Dict
4
+
5
+ CLINICAL_TRIALS_API = "https://clinicaltrials.gov/api/query/study_fields"
6
+
7
+ def search_clinical_trials(query: str, max_results: int = 5) -> List[Dict]:
8
+ """
9
+ Search ClinicalTrials.gov for trials matching the query.
10
+
11
+ Returns a list of dictionaries with:
12
+ - title
13
+ - status
14
+ - nct_id
15
+ - locations
16
+ - url
17
+ """
18
+ try:
19
+ params = {
20
+ "expr": query,
21
+ "fields": "NCTId,BriefTitle,OverallStatus,LocationCity,LocationCountry",
22
+ "min_rnk": 1,
23
+ "max_rnk": max_results,
24
+ "fmt": "json"
25
+ }
26
+ r = requests.get(CLINICAL_TRIALS_API, params=params, timeout=15)
27
+ r.raise_for_status()
28
+ data = r.json()
29
+
30
+ studies = data.get("StudyFieldsResponse", {}).get("StudyFields", [])
31
+ results = []
32
+ for s in studies:
33
+ nct_id = s.get("NCTId", [""])[0]
34
+ title = s.get("BriefTitle", [""])[0]
35
+ status = s.get("OverallStatus", [""])[0]
36
+ cities = s.get("LocationCity", [])
37
+ countries = s.get("LocationCountry", [])
38
+ locations = [f"{c}, {countries[i]}" if i < len(countries) else c
39
+ for i, c in enumerate(cities)]
40
+ results.append({
41
+ "title": title,
42
+ "status": status,
43
+ "nct_id": nct_id,
44
+ "locations": locations,
45
+ "url": f"https://clinicaltrials.gov/study/{nct_id}"
46
+ })
47
+ return results
48
+ except Exception as e:
49
+ print(f"[ClinicalTrials] Failed: {e}")
50
+ return []
51
+
52
+ def format_trials_markdown(trials: List[Dict]) -> str:
53
+ """Convert trials list into markdown table."""
54
+ if not trials:
55
+ return "No clinical trials found."
56
+ md = "| Title | Status | NCT ID | Locations |\n|-------|--------|--------|-----------|\n"
57
+ for t in trials:
58
+ loc_str = ", ".join(t["locations"]) if t["locations"] else "N/A"
59
+ md += f"| [{t['title']}]({t['url']}) | {t['status']} | [{t['nct_id']}]({t['url']}) | {loc_str} |\n"
60
+ return md