Erva Ulusoy commited on
Commit
85b27f1
·
1 Parent(s): b42cc3c

color the edge and node titles based on type

Browse files
Files changed (1) hide show
  1. visualize_kg.py +27 -13
visualize_kg.py CHANGED
@@ -277,9 +277,12 @@ def visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):
277
  else:
278
  node_name = name_info['GO_term'][source_str]
279
  url = get_node_url(source_type, source_str)
280
- title = f"{node_name} ({NODE_LABEL_TRANSLATION[source_type] if source_type in NODE_LABEL_TRANSLATION else source_type})"
 
281
  if url:
282
- title = f'<a href="{url}" target="_blank">{title}</a>'
 
 
283
  net.add_node(source_str,
284
  label=source_str,
285
  shape="dot",
@@ -297,9 +300,12 @@ def visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):
297
  else:
298
  node_name = name_info['GO_term'][target_str]
299
  url = get_node_url(target_type, target_str)
300
- title = f"{node_name} ({NODE_LABEL_TRANSLATION[target_type] if target_type in NODE_LABEL_TRANSLATION else target_type})"
 
301
  if url:
302
- title = f'<a href="{url}" target="_blank">{title}</a>'
 
 
303
  net.add_node(target_str,
304
  label=target_str,
305
  shape="dot",
@@ -309,32 +315,36 @@ def visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):
309
  size=15,
310
  mass=1.5)
311
  added_nodes.add(target_str)
 
312
  # Add edge with relationship type and probability as label
313
  edge_label = f"{relation_type}"
314
  if probability is not None:
315
  if probability == 'no_pred':
316
  edge_color = '#219ebc'
317
- edge_label += ' (P=Not generated)'
318
  else:
319
- edge_label += f" (P={probability:.2f})"
320
  edge_color = '#8338ec' if is_ground_truth else '#c1121f'
321
- # if validated prediction purple, if non-validated prediction red, if no prediction (directly from database) blue
 
322
  net.add_edge(source_str, target_str,
323
- label=edge_label,
324
  font={'size': 0},
325
  color=edge_color,
326
- title=edge_label,
327
  length=200,
328
  smooth={'type': 'curvedCW', 'roundness': 0.1})
329
  else:
 
 
330
  net.add_edge(source_str, target_str,
331
- label=edge_label,
332
  font={'size': 0},
333
- color='#666666', # Keep default gray for non-GO edges
334
- title=edge_label,
335
  length=200,
336
  smooth={'type': 'curvedCW', 'roundness': 0.1})
337
-
338
  # LEGEND
339
  legend_html = """
340
  <style>
@@ -535,6 +545,10 @@ def visualize_protein_subgraph(data, protein_id, prediction_df, limit=10):
535
  padding: 5px;
536
  z-index: 1000;
537
  }
 
 
 
 
538
  """
539
 
540
  # Insert the custom CSS in the head
 
277
  else:
278
  node_name = name_info['GO_term'][source_str]
279
  url = get_node_url(source_type, source_str)
280
+ node_color = NODE_TYPE_COLORS[source_type]
281
+ node_type_label = NODE_LABEL_TRANSLATION[source_type] if source_type in NODE_LABEL_TRANSLATION else source_type
282
  if url:
283
+ title = f"<div style='color: {node_color}'><a href='{url}' target='_blank'>{node_name} ({node_type_label})</a></div>"
284
+ else:
285
+ title = f"<div style='color: {node_color}'>{node_name} ({node_type_label})</div>"
286
  net.add_node(source_str,
287
  label=source_str,
288
  shape="dot",
 
300
  else:
301
  node_name = name_info['GO_term'][target_str]
302
  url = get_node_url(target_type, target_str)
303
+ node_color = NODE_TYPE_COLORS[target_type]
304
+ node_type_label = NODE_LABEL_TRANSLATION[target_type] if target_type in NODE_LABEL_TRANSLATION else target_type
305
  if url:
306
+ title = f"<div style='color: {node_color}'><a href='{url}' target='_blank'>{node_name} ({node_type_label})</a></div>"
307
+ else:
308
+ title = f"<div style='color: {node_color}'>{node_name} ({node_type_label})</div>"
309
  net.add_node(target_str,
310
  label=target_str,
311
  shape="dot",
 
315
  size=15,
316
  mass=1.5)
317
  added_nodes.add(target_str)
318
+
319
  # Add edge with relationship type and probability as label
320
  edge_label = f"{relation_type}"
321
  if probability is not None:
322
  if probability == 'no_pred':
323
  edge_color = '#219ebc'
324
+ title_text = f"{relation_type} (P=Not generated)"
325
  else:
326
+ title_text = f"{relation_type} (P={probability:.2f})"
327
  edge_color = '#8338ec' if is_ground_truth else '#c1121f'
328
+ # Add color to edge title
329
+ title_text = f"<div style='color: {edge_color}'>{title_text}</div>"
330
  net.add_edge(source_str, target_str,
331
+ label='', # Empty label
332
  font={'size': 0},
333
  color=edge_color,
334
+ title=title_text,
335
  length=200,
336
  smooth={'type': 'curvedCW', 'roundness': 0.1})
337
  else:
338
+ edge_color = '#666666'
339
+ title_text = f"<div style='color: {edge_color}'>{edge_label}</div>"
340
  net.add_edge(source_str, target_str,
341
+ label='', # Empty label
342
  font={'size': 0},
343
+ color=edge_color,
344
+ title=title_text,
345
  length=200,
346
  smooth={'type': 'curvedCW', 'roundness': 0.1})
347
+
348
  # LEGEND
349
  legend_html = """
350
  <style>
 
545
  padding: 5px;
546
  z-index: 1000;
547
  }
548
+ div.popup a {
549
+ color: inherit;
550
+ text-decoration: underline;
551
+ }
552
  """
553
 
554
  # Insert the custom CSS in the head