Spaces:
Sleeping
Sleeping
Erva Ulusoy
commited on
Commit
·
297cd76
1
Parent(s):
e0fbc94
edge title fix
Browse files- visualize_kg.py +107 -0
visualize_kg.py
CHANGED
|
@@ -443,10 +443,117 @@ def visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):
|
|
| 443 |
file_path = os.path.join('temp_viz', f'{protein_id}_graph.html')
|
| 444 |
|
| 445 |
net.save_graph(file_path)
|
|
|
|
| 446 |
with open(file_path, 'r', encoding='utf-8') as f:
|
| 447 |
content = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
# Insert the legend before the closing body tag
|
| 449 |
content = content.replace('</body>', f'{legend_html}</body>')
|
|
|
|
| 450 |
with open(file_path, 'w', encoding='utf-8') as f:
|
| 451 |
f.write(content)
|
| 452 |
|
|
|
|
| 443 |
file_path = os.path.join('temp_viz', f'{protein_id}_graph.html')
|
| 444 |
|
| 445 |
net.save_graph(file_path)
|
| 446 |
+
|
| 447 |
with open(file_path, 'r', encoding='utf-8') as f:
|
| 448 |
content = f.read()
|
| 449 |
+
|
| 450 |
+
# Add the custom popup JavaScript code before the return network statement
|
| 451 |
+
custom_popup_code = """
|
| 452 |
+
// make a custom popup
|
| 453 |
+
var popup = document.createElement("div");
|
| 454 |
+
popup.className = 'popup';
|
| 455 |
+
popupTimeout = null;
|
| 456 |
+
popup.addEventListener('mouseover', function () {
|
| 457 |
+
if (popupTimeout !== null) {
|
| 458 |
+
clearTimeout(popupTimeout);
|
| 459 |
+
popupTimeout = null;
|
| 460 |
+
}
|
| 461 |
+
});
|
| 462 |
+
popup.addEventListener('mouseout', function () {
|
| 463 |
+
if (popupTimeout === null) {
|
| 464 |
+
hidePopup();
|
| 465 |
+
}
|
| 466 |
+
});
|
| 467 |
+
container.appendChild(popup);
|
| 468 |
+
|
| 469 |
+
// use the popup event to show
|
| 470 |
+
network.on("showPopup", function (params) {
|
| 471 |
+
showPopup(params);
|
| 472 |
+
});
|
| 473 |
+
|
| 474 |
+
// use the hide event to hide it
|
| 475 |
+
network.on("hidePopup", function (params) {
|
| 476 |
+
hidePopup();
|
| 477 |
+
});
|
| 478 |
+
|
| 479 |
+
// hiding the popup through css
|
| 480 |
+
function hidePopup() {
|
| 481 |
+
popupTimeout = setTimeout(function () { popup.style.display = 'none'; }, 500);
|
| 482 |
+
}
|
| 483 |
+
|
| 484 |
+
// showing the popup
|
| 485 |
+
function showPopup(nodeId) {
|
| 486 |
+
// get the data from the vis.DataSet
|
| 487 |
+
var nodeData = nodes.get(nodeId);
|
| 488 |
+
// get the position of the node
|
| 489 |
+
var posCanvas = network.getPositions([nodeId])[nodeId];
|
| 490 |
+
|
| 491 |
+
if (!nodeData) {
|
| 492 |
+
var edgeData = edges.get(nodeId);
|
| 493 |
+
var poses = network.getPositions([edgeData.from, edgeData.to]);
|
| 494 |
+
var middle_x = (poses[edgeData.to].x - poses[edgeData.from].x) * 0.5;
|
| 495 |
+
var middle_y = (poses[edgeData.to].y - poses[edgeData.from].y) * 0.5;
|
| 496 |
+
posCanvas = poses[edgeData.from];
|
| 497 |
+
posCanvas.x = posCanvas.x + middle_x;
|
| 498 |
+
posCanvas.y = posCanvas.y + middle_y;
|
| 499 |
+
|
| 500 |
+
popup.innerHTML = edgeData.title;
|
| 501 |
+
} else {
|
| 502 |
+
popup.innerHTML = nodeData.title;
|
| 503 |
+
// get the bounding box of the node
|
| 504 |
+
var boundingBox = network.getBoundingBox(nodeId);
|
| 505 |
+
posCanvas.x = posCanvas.x + 0.5 * (boundingBox.right - boundingBox.left);
|
| 506 |
+
posCanvas.y = posCanvas.y + 0.5 * (boundingBox.top - boundingBox.bottom);
|
| 507 |
+
};
|
| 508 |
+
|
| 509 |
+
//position tooltip:
|
| 510 |
+
// convert coordinates to the DOM space
|
| 511 |
+
var posDOM = network.canvasToDOM(posCanvas);
|
| 512 |
+
|
| 513 |
+
// Give it an offset
|
| 514 |
+
posDOM.x += 10;
|
| 515 |
+
posDOM.y -= 20;
|
| 516 |
+
|
| 517 |
+
// show and place the tooltip.
|
| 518 |
+
popup.style.display = 'block';
|
| 519 |
+
popup.style.top = posDOM.y + 'px';
|
| 520 |
+
popup.style.left = posDOM.x + 'px';
|
| 521 |
+
}
|
| 522 |
+
"""
|
| 523 |
+
|
| 524 |
+
# Add the custom popup CSS
|
| 525 |
+
custom_popup_css = """
|
| 526 |
+
/* position absolute is important and the container has to be relative or absolute as well. */
|
| 527 |
+
div.popup {
|
| 528 |
+
position: absolute;
|
| 529 |
+
top: 0px;
|
| 530 |
+
left: 0px;
|
| 531 |
+
display: none;
|
| 532 |
+
background-color: white;
|
| 533 |
+
border-radius: 3px;
|
| 534 |
+
border: 1px solid #ddd;
|
| 535 |
+
box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
|
| 536 |
+
padding: 5px;
|
| 537 |
+
z-index: 1000;
|
| 538 |
+
}
|
| 539 |
+
"""
|
| 540 |
+
|
| 541 |
+
# Insert the custom CSS in the head
|
| 542 |
+
content = content.replace('</style>', f'{custom_popup_css}</style>')
|
| 543 |
+
|
| 544 |
+
# Insert the custom popup code before the "return network;" statement
|
| 545 |
+
content = content.replace('return network;', f'{custom_popup_code}\nreturn network;')
|
| 546 |
+
|
| 547 |
+
# Remove the original tooltip-hiding CSS if it exists
|
| 548 |
+
content = content.replace("""
|
| 549 |
+
/* hide the original tooltip */
|
| 550 |
+
.vis-network-tooltip {
|
| 551 |
+
display:none;
|
| 552 |
+
}""", "")
|
| 553 |
+
|
| 554 |
# Insert the legend before the closing body tag
|
| 555 |
content = content.replace('</body>', f'{legend_html}</body>')
|
| 556 |
+
|
| 557 |
with open(file_path, 'w', encoding='utf-8') as f:
|
| 558 |
f.write(content)
|
| 559 |
|