forked from hhx465453939/OpenClaw-Medical-Skills
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddi_working_example.py
More file actions
310 lines (262 loc) · 11.2 KB
/
ddi_working_example.py
File metadata and controls
310 lines (262 loc) · 11.2 KB
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
#!/usr/bin/env python3
"""
Drug-Drug Interaction Analysis - WORKING EXAMPLE
This script demonstrates correct tool usage for DDI analysis with
proper tool names and parameters (fixed after testing).
Key Fixes:
- RxNorm: Use RxNorm_get_drug_names (not RxNorm_get_drugs_by_name)
- DrugBank: Use 'query' parameter (not 'drug_name_or_drugbank_id')
- DailyMed: Use DailyMed_get_spl_by_setid + DailyMed_parse_drug_interactions
"""
from tooluniverse import ToolUniverse
import json
def analyze_ddi(tu, drug_a, drug_b):
"""
Analyze drug-drug interaction between two drugs.
Args:
tu: ToolUniverse instance
drug_a: First drug name (e.g., "warfarin")
drug_b: Second drug name (e.g., "amoxicillin")
Returns:
dict with DDI analysis results
"""
print(f"\n{'='*80}")
print(f"DDI ANALYSIS: {drug_a.upper()} + {drug_b.upper()}")
print(f"{'='*80}\n")
report = {
'drug_a': drug_a,
'drug_b': drug_b,
'identifiers': {},
'mechanisms': {},
'fda_warnings': {},
'clinical_evidence': {},
'risk_score': 0
}
# ====================================================================
# STEP 1: Drug Identification & Normalization
# ====================================================================
print("STEP 1: Drug Identification")
print("-" * 80)
# Get RxNorm identifiers (CORRECT tool name)
print(f"\n1.1 Looking up {drug_a} in RxNorm...")
try:
result_a = tu.tools.RxNorm_get_drug_names(query=drug_a) # ✅ CORRECT
if result_a.get('status') == 'success':
names_a = result_a.get('data', {}).get('names', [])
if names_a:
report['identifiers'][drug_a] = {
'rxcui': names_a[0].get('rxcui'),
'name': names_a[0].get('name'),
'source': 'RxNorm'
}
print(f"✅ Found: {names_a[0].get('name')} (RxCUI: {names_a[0].get('rxcui')})")
else:
print(f"⚠️ No RxNorm entry found for {drug_a}")
else:
print(f"❌ RxNorm query failed: {result_a.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
print(f"\n1.2 Looking up {drug_b} in RxNorm...")
try:
result_b = tu.tools.RxNorm_get_drug_names(query=drug_b) # ✅ CORRECT
if result_b.get('status') == 'success':
names_b = result_b.get('data', {}).get('names', [])
if names_b:
report['identifiers'][drug_b] = {
'rxcui': names_b[0].get('rxcui'),
'name': names_b[0].get('name'),
'source': 'RxNorm'
}
print(f"✅ Found: {names_b[0].get('name')} (RxCUI: {names_b[0].get('rxcui')})")
else:
print(f"⚠️ No RxNorm entry found for {drug_b}")
else:
print(f"❌ RxNorm query failed: {result_b.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
# ====================================================================
# STEP 2: Mechanism Analysis (DrugBank)
# ====================================================================
print(f"\n\nSTEP 2: Mechanism Analysis (DrugBank)")
print("-" * 80)
# Get drug interactions from DrugBank (CORRECT parameters)
print(f"\n2.1 Querying DrugBank for {drug_a} interactions...")
try:
result = tu.tools.drugbank_get_drug_interactions_by_drug_name_or_id(
query=drug_a, # ✅ CORRECT parameter name
case_sensitive=False, # ✅ Optional
exact_match=False, # ✅ Optional
limit=10 # ✅ Optional
)
if result.get('status') == 'success':
data = result.get('data', {})
interactions = data.get('interactions', [])
print(f"✅ Found {len(interactions)} interactions for {drug_a}")
# Check if drug_b is in the interaction list
for interaction in interactions:
interacting_drug = interaction.get('name', '').lower()
if drug_b.lower() in interacting_drug:
report['mechanisms'][f"{drug_a} → {drug_b}"] = {
'description': interaction.get('description'),
'source': 'DrugBank'
}
print(f"✅ Found interaction: {interaction.get('description')[:100]}...")
break
else:
print(f"⚠️ DrugBank query failed: {result.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
# Bidirectional analysis (B → A)
print(f"\n2.2 Querying DrugBank for {drug_b} interactions...")
try:
result = tu.tools.drugbank_get_drug_interactions_by_drug_name_or_id(
query=drug_b, # ✅ CORRECT
case_sensitive=False,
exact_match=False,
limit=10
)
if result.get('status') == 'success':
data = result.get('data', {})
interactions = data.get('interactions', [])
print(f"✅ Found {len(interactions)} interactions for {drug_b}")
for interaction in interactions:
interacting_drug = interaction.get('name', '').lower()
if drug_a.lower() in interacting_drug:
report['mechanisms'][f"{drug_b} → {drug_a}"] = {
'description': interaction.get('description'),
'source': 'DrugBank'
}
print(f"✅ Found interaction: {interaction.get('description')[:100]}...")
break
else:
print(f"⚠️ DrugBank query failed: {result.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
# ====================================================================
# STEP 3: Get Drug Details (DrugBank)
# ====================================================================
print(f"\n\nSTEP 3: Drug Details")
print("-" * 80)
print(f"\n3.1 Getting basic info for {drug_a}...")
try:
result = tu.tools.drugbank_get_drug_basic_info_by_drug_name_or_id(
query=drug_a, # ✅ CORRECT parameter
case_sensitive=False,
exact_match=False,
limit=1
)
if result.get('status') == 'success':
data = result.get('data', {})
drugs = data.get('drugs', [])
if drugs:
drug_info = drugs[0]
print(f"✅ {drug_info.get('drug_name')}")
print(f" DrugBank ID: {drug_info.get('drugbank_id')}")
print(f" Description: {drug_info.get('description', 'N/A')[:100]}...")
report['identifiers'][drug_a]['drugbank_id'] = drug_info.get('drugbank_id')
else:
print(f"⚠️ Query failed: {result.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
# ====================================================================
# STEP 4: FDA Label Warnings (DailyMed) - Requires SetID
# ====================================================================
print(f"\n\nSTEP 4: FDA Label Warnings (DailyMed)")
print("-" * 80)
print(" (Requires SetID - skipping in this example)")
print(" Use DailyMed_search_spls(query=drug_name) to find SetIDs")
print(" Then DailyMed_parse_drug_interactions(setid=...) for warnings")
# ====================================================================
# STEP 5: Clinical Evidence (FAERS)
# ====================================================================
print(f"\n\nSTEP 5: Post-Market Surveillance (FAERS)")
print("-" * 80)
try:
result = tu.tools.FAERS_count_reactions_by_drug_event(
drug_name=drug_a,
event_name="drug interaction"
)
if result.get('status') == 'success':
data = result.get('data', {})
count = data.get('count', 0)
print(f"✅ Found {count} adverse event reports mentioning '{drug_a}' + 'drug interaction'")
report['clinical_evidence']['faers_count'] = count
else:
print(f"⚠️ FAERS query failed: {result.get('error')}")
except Exception as e:
print(f"❌ Error: {e}")
# ====================================================================
# STEP 6: Risk Scoring
# ====================================================================
print(f"\n\nSTEP 6: Risk Scoring")
print("-" * 80)
# Simple risk score calculation
risk_score = 0
# Add points for mechanisms found
if report['mechanisms']:
risk_score += 40
print("✅ Mechanisms identified: +40 points")
# Add points for FAERS reports
faers_count = report['clinical_evidence'].get('faers_count', 0)
if faers_count > 100:
risk_score += 30
print(f"✅ High FAERS count ({faers_count}): +30 points")
elif faers_count > 10:
risk_score += 15
print(f"✅ Moderate FAERS count ({faers_count}): +15 points")
report['risk_score'] = risk_score
if risk_score >= 70:
severity = "MAJOR"
elif risk_score >= 40:
severity = "MODERATE"
else:
severity = "MINOR"
report['severity'] = severity
print(f"\n📊 Overall Risk Score: {risk_score}/100 ({severity})")
# ====================================================================
# STEP 7: Summary Report
# ====================================================================
print(f"\n\n{'='*80}")
print("DDI ANALYSIS SUMMARY")
print(f"{'='*80}\n")
print(f"Drug Pair: {drug_a.upper()} + {drug_b.upper()}")
print(f"Risk Score: {risk_score}/100")
print(f"Severity: {severity}")
print(f"\nMechanisms Found: {len(report['mechanisms'])}")
for direction, mech in report['mechanisms'].items():
print(f" - {direction}: {mech['description'][:80]}...")
print(f"\nClinical Evidence:")
print(f" - FAERS reports: {report['clinical_evidence'].get('faers_count', 0)}")
print(f"\n{'='*80}\n")
return report
def main():
"""Run DDI analysis examples."""
print("=" * 80)
print("DRUG-DRUG INTERACTION ANALYSIS - WORKING EXAMPLES")
print("=" * 80)
print("\nInitializing ToolUniverse...")
tu = ToolUniverse()
tu.load_tools()
print("✅ ToolUniverse loaded\n")
# Example 1: Warfarin + Antibiotic
print("\n" + "="*80)
print("EXAMPLE 1: Warfarin + Amoxicillin")
print("="*80)
_ = analyze_ddi(tu, "warfarin", "amoxicillin")
# Example 2: Statin + Azole (Major DDI)
print("\n" + "="*80)
print("EXAMPLE 2: Simvastatin + Ketoconazole (Major DDI)")
print("="*80)
_ = analyze_ddi(tu, "simvastatin", "ketoconazole")
print("\n" + "="*80)
print("ALL EXAMPLES COMPLETE")
print("="*80)
print("\n✅ Both DDI analyses completed successfully")
print("✅ All tool calls use correct names and parameters")
print("\n📝 Key Learnings:")
print(" - RxNorm: Use RxNorm_get_drug_names(query=...)")
print(" - DrugBank: Use query parameter, not drug_name_or_drugbank_id")
print(" - FAERS: Works as documented")
print(" - DailyMed: Requires SetID lookup first")
if __name__ == "__main__":
main()