File size: 1,665 Bytes
d5e0cb0 990dd86 d5e0cb0 990dd86 e202a39 d5e0cb0 990dd86 1e92bb0 |
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 |
# mcp/knowledge_graph.py
from streamlit_agraph import Node, Edge, Config
import re
def build_agraph(papers, umls, drug_safety, umls_relations=None):
nodes, edges = [], []
# 1) concepts
for c in umls:
cui, name = c.get("cui"), c.get("name")
types = c.get("types", [])
if not cui or not name:
continue
label = f"{name}\n({'/'.join(types)})"
size = 20 + int(c.get("score",0)*30)
nodes.append(Node(
id=f"cui_{cui}",
label=label,
size=size,
color="#00b894",
tooltip=c.get("definition","")
))
# 2) concept→concept edges
if umls_relations:
for rels in umls_relations:
for r in rels:
s, t = f"cui_{r['cui']}", f"cui_{r['cui2']}"
if s != t:
edges.append(Edge(source=s, target=t, label=r["label"]))
# 3) papers
for i,p in enumerate(papers):
pid = f"paper_{i}"
nodes.append(Node(
id=pid, label=f"P{i+1}", size=15, color="#0984e3",
tooltip=p.get("title","")
))
txt = (p.get("title","") + " " + p.get("summary","")).lower()
for c in umls:
if c.get("name","").lower() in txt:
edges.append(Edge(source=pid, target=f"cui_{c['cui']}", label="mentions"))
# 4) drugs (as before)…
# nodes.extend(...), edges.extend(...)
cfg = Config(
width="100%", height="600px", directed=True,
nodeHighlightBehavior=True, highlightColor="#f1c40f",
collapsible=True, node={"labelProperty":"label"}
)
return nodes, edges, cfg
|