Update mcp/knowledge_graph.py
Browse files- mcp/knowledge_graph.py +46 -31
mcp/knowledge_graph.py
CHANGED
@@ -1,37 +1,52 @@
|
|
1 |
# mcp/knowledge_graph.py
|
2 |
from streamlit_agraph import Node, Edge, Config
|
|
|
3 |
|
4 |
-
def build_agraph(
|
5 |
nodes, edges = [], []
|
6 |
-
# add each paper as a node
|
7 |
-
for i,p in enumerate(res["papers"]):
|
8 |
-
nid = f"paper_{i}"
|
9 |
-
nodes.append(Node(id=nid, label=p["title"], size=20, color="#0984e3"))
|
10 |
-
# connect to AI summary?
|
11 |
-
# add UMLS concepts
|
12 |
-
for u in res["umls"]:
|
13 |
-
cid = f"cui_{u['cui']}"
|
14 |
-
label = f"{u['name']} ({u['cui']})"
|
15 |
-
nodes.append(Node(id=cid, label=label, size=25, color="#00b894"))
|
16 |
-
# connect concept → first paper
|
17 |
-
edges.append(Edge(source=cid, target="paper_0", label="mentioned_in"))
|
18 |
-
# genes
|
19 |
-
g = res.get("gene",{})
|
20 |
-
if g:
|
21 |
-
gid = "gene_node"
|
22 |
-
nodes.append(Node(id=gid, label=g.get("symbol",g.get("name","gene")), color="#d63031"))
|
23 |
-
edges.append(Edge(source=gid, target="cui_"+res["umls"][0]["cui"], label="related"))
|
24 |
-
# variants
|
25 |
-
for v in res["variants"]:
|
26 |
-
vid = f"var_{v['mutationId']}"
|
27 |
-
nodes.append(Node(id=vid, label=v["mutationId"], color="#fdcb6e", size=15))
|
28 |
-
edges.append(Edge(source=vid, target=gid, label="affects"))
|
29 |
-
# trials
|
30 |
-
for t in res["trials"]:
|
31 |
-
tid = t["NCTId"][0]
|
32 |
-
nodes.append(Node(id=tid, label=tid, color="#6c5ce7"))
|
33 |
-
edges.append(Edge(source=tid, target=gid, label="studies"))
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
return nodes, edges, cfg
|
|
|
1 |
# mcp/knowledge_graph.py
|
2 |
from streamlit_agraph import Node, Edge, Config
|
3 |
+
import re
|
4 |
|
5 |
+
def build_agraph(papers, umls, drug_safety, umls_relations=None):
|
6 |
nodes, edges = [], []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# 1) concepts
|
9 |
+
for c in umls:
|
10 |
+
cui, name = c.get("cui"), c.get("name")
|
11 |
+
types = c.get("types", [])
|
12 |
+
if not cui or not name:
|
13 |
+
continue
|
14 |
+
label = f"{name}\n({'/'.join(types)})"
|
15 |
+
size = 20 + int(c.get("score",0)*30)
|
16 |
+
nodes.append(Node(
|
17 |
+
id=f"cui_{cui}",
|
18 |
+
label=label,
|
19 |
+
size=size,
|
20 |
+
color="#00b894",
|
21 |
+
tooltip=c.get("definition","")
|
22 |
+
))
|
23 |
+
|
24 |
+
# 2) concept→concept edges
|
25 |
+
if umls_relations:
|
26 |
+
for rels in umls_relations:
|
27 |
+
for r in rels:
|
28 |
+
s, t = f"cui_{r['cui']}", f"cui_{r['cui2']}"
|
29 |
+
if s != t:
|
30 |
+
edges.append(Edge(source=s, target=t, label=r["label"]))
|
31 |
+
|
32 |
+
# 3) papers
|
33 |
+
for i,p in enumerate(papers):
|
34 |
+
pid = f"paper_{i}"
|
35 |
+
nodes.append(Node(
|
36 |
+
id=pid, label=f"P{i+1}", size=15, color="#0984e3",
|
37 |
+
tooltip=p.get("title","")
|
38 |
+
))
|
39 |
+
txt = (p.get("title","") + " " + p.get("summary","")).lower()
|
40 |
+
for c in umls:
|
41 |
+
if c.get("name","").lower() in txt:
|
42 |
+
edges.append(Edge(source=pid, target=f"cui_{c['cui']}", label="mentions"))
|
43 |
+
|
44 |
+
# 4) drugs (as before)…
|
45 |
+
# nodes.extend(...), edges.extend(...)
|
46 |
+
|
47 |
+
cfg = Config(
|
48 |
+
width="100%", height="600px", directed=True,
|
49 |
+
nodeHighlightBehavior=True, highlightColor="#f1c40f",
|
50 |
+
collapsible=True, node={"labelProperty":"label"}
|
51 |
+
)
|
52 |
return nodes, edges, cfg
|