Update mcp/drugbank.py
Browse files- mcp/drugbank.py +41 -14
mcp/drugbank.py
CHANGED
@@ -1,28 +1,55 @@
|
|
1 |
-
|
2 |
-
"""
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
"""
|
|
|
6 |
|
7 |
import csv
|
|
|
8 |
from pathlib import Path
|
9 |
from typing import Dict, Optional
|
10 |
|
11 |
-
_DATA = Path(__file__).parent / "data" / "drugbank_open_structured_drug_links.tsv"
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
13 |
def _load_index() -> Dict[str, Dict]:
|
14 |
-
index = {}
|
15 |
if not _DATA.exists():
|
16 |
-
raise FileNotFoundError(
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
for row in reader:
|
20 |
-
|
21 |
-
index[
|
22 |
return index
|
23 |
|
24 |
-
|
|
|
|
|
25 |
|
26 |
def lookup_drug(name: str) -> Optional[Dict]:
|
27 |
-
"""Return DrugBank row dict or None
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""MedGenesis – lightweight **DrugBank Open Data** TSV helper.
|
3 |
+
|
4 |
+
* Loads the DrugBank TSV once into an in‑memory dict (lazy, cached).
|
5 |
+
* Case‑insensitive lookup via `lookup_drug(name)`.
|
6 |
+
* Raises actionable `FileNotFoundError` if the TSV is missing and
|
7 |
+
provides official download URL in the error message.
|
8 |
+
* TSV expected at `<repo_root>/mcp/data/drugbank_open_structured_drug_links.tsv`.
|
9 |
"""
|
10 |
+
from __future__ import annotations
|
11 |
|
12 |
import csv
|
13 |
+
from functools import lru_cache
|
14 |
from pathlib import Path
|
15 |
from typing import Dict, Optional
|
16 |
|
17 |
+
_DATA = Path(__file__).with_suffix("").parent / "data" / "drugbank_open_structured_drug_links.tsv"
|
18 |
+
_DOWNLOAD_URL = "https://go.drugbank.com/releases/latest#open-data"
|
19 |
+
|
20 |
+
# ---------------------------------------------------------------------
|
21 |
+
# Lazy index loader
|
22 |
+
# ---------------------------------------------------------------------
|
23 |
|
24 |
+
@lru_cache(maxsize=1)
|
25 |
def _load_index() -> Dict[str, Dict]:
|
|
|
26 |
if not _DATA.exists():
|
27 |
+
raise FileNotFoundError(
|
28 |
+
f"DrugBank TSV not found at {_DATA}. Download the open-data TSV from\n"
|
29 |
+
f"{_DOWNLOAD_URL} and place it in the same path."
|
30 |
+
)
|
31 |
+
|
32 |
+
index: Dict[str, Dict] = {}
|
33 |
+
with _DATA.open(newline="", encoding="utf-8") as fh:
|
34 |
+
reader = csv.DictReader(fh, delimiter="\t")
|
35 |
for row in reader:
|
36 |
+
# Primary key is lower‑cased `Name`; keep original row otherwise untouched.
|
37 |
+
index[row["Name"].lower()] = row
|
38 |
return index
|
39 |
|
40 |
+
# ---------------------------------------------------------------------
|
41 |
+
# Public lookup
|
42 |
+
# ---------------------------------------------------------------------
|
43 |
|
44 |
def lookup_drug(name: str) -> Optional[Dict]:
|
45 |
+
"""Return DrugBank row dict for *name* (case‑insensitive) or `None`."""
|
46 |
+
idx = _load_index()
|
47 |
+
return idx.get(name.lower())
|
48 |
+
|
49 |
+
|
50 |
+
# ---------------------------------------------------------------------
|
51 |
+
# CLI demo
|
52 |
+
# ---------------------------------------------------------------------
|
53 |
+
if __name__ == "__main__":
|
54 |
+
hit = lookup_drug("Temozolomide")
|
55 |
+
print(hit or "Drug not found – ensure TSV downloaded.")
|