Update mcp/knowledge_graph.py
Browse files- mcp/knowledge_graph.py +48 -30
mcp/knowledge_graph.py
CHANGED
@@ -1,43 +1,61 @@
|
|
1 |
# mcp/knowledge_graph.py
|
2 |
|
3 |
-
from streamlit_agraph import
|
|
|
4 |
|
5 |
-
def build_agraph(papers, umls,
|
6 |
"""
|
7 |
-
Build nodes and edges
|
|
|
8 |
"""
|
|
|
9 |
nodes, edges = [], []
|
10 |
|
11 |
-
#
|
12 |
for c in umls:
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
for c in umls:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
config = Config(
|
39 |
width="100%", height="600", directed=False,
|
40 |
-
nodeHighlightBehavior=True, highlightColor="#
|
41 |
-
collapsible=True
|
|
|
42 |
)
|
43 |
return nodes, edges, config
|
|
|
1 |
# mcp/knowledge_graph.py
|
2 |
|
3 |
+
from streamlit_agraph import Node, Edge, Config
|
4 |
+
import re
|
5 |
|
6 |
+
def build_agraph(papers, umls, drug_safety):
|
7 |
"""
|
8 |
+
Build interactive agraph nodes and edges.
|
9 |
+
Handles drug_safety entries that may be dict or list.
|
10 |
"""
|
11 |
+
|
12 |
nodes, edges = [], []
|
13 |
|
14 |
+
# Add UMLS concept nodes
|
15 |
for c in umls:
|
16 |
+
cui = c.get("cui")
|
17 |
+
name = c.get("name", "")
|
18 |
+
if cui and name:
|
19 |
+
nid = f"concept_{cui}"
|
20 |
+
nodes.append(Node(id=nid, label=name, size=25, color="#00b894"))
|
21 |
+
|
22 |
+
# Add drug nodes, handling list or dict
|
23 |
+
drug_names = []
|
24 |
+
for i, dr in enumerate(drug_safety):
|
25 |
+
if not dr:
|
26 |
+
continue
|
27 |
+
|
28 |
+
# Normalize to single dict
|
29 |
+
recs = dr if isinstance(dr, list) else [dr]
|
30 |
+
for j, rec in enumerate(recs):
|
31 |
+
# Attempt to extract a drug name
|
32 |
+
dn = rec.get("drug_name") or rec.get("patient", {}).get("drug", "") or rec.get("medicinalproduct", "")
|
33 |
+
dn = dn or f"drug_{i}_{j}"
|
34 |
+
did = f"drug_{i}_{j}"
|
35 |
+
drug_names.append((did, dn))
|
36 |
+
nodes.append(Node(id=did, label=dn, size=25, color="#d35400"))
|
37 |
+
|
38 |
+
# Add paper nodes and link to concepts & drugs
|
39 |
+
for pi, p in enumerate(papers):
|
40 |
+
pid = f"paper_{pi}"
|
41 |
+
nodes.append(Node(id=pid, label=f"P{pi+1}", tooltip=p["title"], size=15, color="#0984e3"))
|
42 |
+
|
43 |
+
text = f"{p.get('title','')} {p.get('summary','')}".lower()
|
44 |
+
# Link to concepts
|
45 |
for c in umls:
|
46 |
+
cname = c.get("name", "")
|
47 |
+
cui = c.get("cui")
|
48 |
+
if cname and cui and cname.lower() in text:
|
49 |
+
edges.append(Edge(source=pid, target=f"concept_{cui}", label="mentions"))
|
50 |
+
# Link to drugs
|
51 |
+
for did, dn in drug_names:
|
52 |
+
if dn.lower() in text:
|
53 |
+
edges.append(Edge(source=pid, target=did, label="mentions"))
|
54 |
+
|
55 |
config = Config(
|
56 |
width="100%", height="600", directed=False,
|
57 |
+
nodeHighlightBehavior=True, highlightColor="#f1c40f",
|
58 |
+
collapsible=True,
|
59 |
+
node={"labelProperty": "label"}
|
60 |
)
|
61 |
return nodes, edges, config
|