-
Notifications
You must be signed in to change notification settings - Fork 4
/
notebook_functions.py
131 lines (122 loc) · 4.33 KB
/
notebook_functions.py
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
import requests
import json
def query(message, limit=20, strict=True):
strict_str = "false"
if strict:
strict_str = "true"
endpoint = f"http://robokop.renci.org:6434/query?limit={limit}&strict={strict_str}"
#endpoint = f"http://localhost:6434/query?limit={limit}&strict={strict_str}"
r = requests.post(endpoint, json=message)
result = r.json()
return result
def graphQuestion(question):
data = reasonerGraphToCytoscape(question)
Cytoscape(data=data, visual_style=queryData["style"], layout={"name": "cose", "height": "700px"})
def graphKG():
pass
def reasonerGraphToCytoscape(graph):
csGraph = {}
nodes = []
edges = []
for node in graph["nodes"]:
csNode = {}
node_types = ""
if isinstance(node["type"], str):
node_types = node["type"]
else:
node_types = "\n".join(node["type"])
csNode["data"] = {"id": node["id"], "label": node_types + "\n[" + node.get("curie", "") + "]", "curie": node.get("curie", ""), "type": node_types}
nodes.append(csNode)
for edge in graph["edges"]:
csEdge = {
"data": {
"id": edge["id"],
"source": edge["source_id"],
"target": edge["target_id"],
"label": edge["type"]
}
}
edges.append(csEdge)
csGraph["elements"] = {"nodes": nodes, "edges": edges}
csGraph["style"] = [
{ "selector": 'node', "style": {
'label': 'data(label)',
'color': 'white',
'background-color': '#60f', # #009 looks good too
'shape': 'rectangle',
'text-valign': 'center',
'text-border-style': 'solid',
'text-border-width': 5,
'text-border-color': 'red',
'width': '20em',
'height': '5em',
'text-wrap': 'wrap'
} },
{"selector": "edge", "style": {
"curve-style": "unbundled-bezier",
# "control-point-distances": [20, -20],
# "control-point-weights": [0.250, 0.75],
"control-point-distances": [-20, 20],
"control-point-weights": [0.5],
'content': 'data(label)',
'line-color': '#808080',
'target-arrow-color': '#808080',
'target-arrow-shape': 'triangle',
'target-arrow-fill': 'filled'}
}
]
#print(json.dumps(csGraph, indent=4))
return csGraph
def knowledgeGraphToCytoscape(graph):
csGraph = {}
nodes = []
edges = []
for node in graph["nodes"]:
csNode = {}
node_types = ""
if isinstance(node["type"], str):
node_types = node["type"]
else:
node_types = "\n".join(node["type"])
csNode["data"] = {"id": node["id"], "label": (node["name"] or " ") + "\n[" + node["id"] + "]", "curie": node["id"], "type": node_types}
nodes.append(csNode)
for edge in graph["edges"]:
csEdge = {
"data": {
"id": edge["id"],
"source": edge["source_id"],
"target": edge["target_id"],
"label": edge["type"]
}
}
edges.append(csEdge)
csGraph["elements"] = {"nodes": nodes, "edges": edges}
csGraph["style"] = [
{ "selector": 'node', "style": {
'label': 'data(label)',
'color': 'white',
'background-color': '#60f', # #009 looks good too
'shape': 'rectangle',
'text-valign': 'center',
'text-border-style': 'solid',
'text-border-width': 5,
'text-border-color': 'red',
'width': '20em',
'height': '5em',
'text-wrap': 'wrap'
} },
{"selector": "edge", "style": {
"curve-style": "unbundled-bezier",
# "control-point-distances": [20, -20],
# "control-point-weights": [0.250, 0.75],
"control-point-distances": [-20, 20],
"control-point-weights": [0.5],
'content': 'data(label)',
'line-color': '#808080',
'target-arrow-color': '#808080',
'target-arrow-shape': 'triangle',
'target-arrow-fill': 'filled'}
}
]
#print(json.dumps(csGraph, indent=4))
return csGraph