|
|
|
import warnings |
|
warnings.filterwarnings("ignore") |
|
import io |
|
import os |
|
import time |
|
import warnings |
|
warnings.simplefilter(action='ignore', category=FutureWarning) |
|
warnings.simplefilter(action='ignore', category=RuntimeWarning) |
|
import pandas as pd |
|
import csv |
|
import ast |
|
from tqdm import tqdm |
|
from operator import itemgetter |
|
import numpy as np |
|
import re |
|
import datetime |
|
import html |
|
from joblib import Parallel, delayed |
|
import matplotlib.pyplot as plt |
|
import matplotlib.dates as mdates |
|
|
|
import holoviews as hv |
|
from holoviews import opts, dim |
|
from bokeh.sampledata.les_mis import data |
|
from bokeh.io import show |
|
from bokeh.sampledata.les_mis import data |
|
import panel as pn |
|
|
|
|
|
import bokeh |
|
from bokeh.resources import INLINE |
|
from holoviews.operation.timeseries import rolling, rolling_outlier_std |
|
hv.extension('bokeh') |
|
|
|
|
|
|
|
data = './data' |
|
|
|
|
|
def read_freq_map(filename): |
|
df = pd.read_csv(os.path.join(data,filename), sep=' ') |
|
|
|
if 'Unnamed: 0' in df.columns: |
|
df = df.drop('Unnamed: 0', axis=1) |
|
column_0 = df.columns[0] |
|
column_1 = df.columns[1] |
|
freqmap = dict(zip(df[column_0], df[column_1])) |
|
return freqmap |
|
|
|
|
|
def read_ont_freq_dataframe(filename): |
|
df = pd.read_csv(os.path.join(data,filename), sep=' ') |
|
|
|
if 'Unnamed: 0' in df.columns: |
|
df = df.drop('Unnamed: 0', axis=1) |
|
column_0 = df.columns[0] |
|
column_1 = df.columns[1] |
|
freqmap = dict(zip(df[column_0], df[column_1])) |
|
return freqmap |
|
|
|
|
|
entityTypesFreqMap = read_freq_map('entityTypes.tsv') |
|
relationTypesFreqMap = read_freq_map('relationTypes.tsv') |
|
topDrugEntities = read_freq_map('topDrugs.tsv') |
|
|
|
topConditionEntities = read_freq_map('topConditions.tsv') |
|
topDrugOnts_df = pd.read_csv(os.path.join(data,'topDrugOntologies.tsv'), sep='\t') |
|
topConditionOnts_df = pd.read_csv(os.path.join(data,'topConditionOntologies.tsv'), sep='\t') |
|
|
|
|
|
grouping_filtered = pd.read_csv(os.path.join(data, 'drugReviewsCausal_relations.tsv'), sep=" ") |
|
|
|
|
|
|
|
|
|
def create_type_bar_charts(entRelsButton, **kwargs): |
|
if entRelsButton=='Entity': |
|
dictionary = entityTypesFreqMap |
|
return hv.Bars(dictionary, hv.Dimension('Entity Types'), 'Frequency').opts( framewise=True, xrotation=45,width=1200, height=600) |
|
elif entRelsButton=='Relation': |
|
dictionary = relationTypesFreqMap |
|
return hv.Bars(dictionary, hv.Dimension('Relation Types'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600) |
|
|
|
|
|
def create_ent_bar_charts(ents, **kwargs): |
|
|
|
drug_buttons = [] |
|
condition_buttons = [] |
|
for i, drg in enumerate(list(topDrugEntities.keys())): |
|
button = pn.widgets.Button(name=drg, width=150) |
|
|
|
button.js_on_click(code=f'window.open("https://api-vast.jrc.service.ec.europa.eu/describe/?url=http://causaldrugskg.org/causaldrugskg/resource/{drg}", "_blank");') |
|
drug_buttons.append(button) |
|
for i, cnd in enumerate(list(topConditionEntities.keys())): |
|
button = pn.widgets.Button(name=cnd, width=150) |
|
|
|
button.js_on_click( |
|
code=f'window.open("https://api-vast.jrc.service.ec.europa.eu/describe/?url=http://causaldrugskg.org/causaldrugskg/resource/{cnd}", "_blank");') |
|
condition_buttons.append(button) |
|
|
|
|
|
drug_button_column = pn.Column(*drug_buttons, sizing_mode='stretch_width') |
|
condition_button_column = pn.Column(*condition_buttons, sizing_mode='stretch_width') |
|
|
|
|
|
if ents=='Drug': |
|
dictionary = topDrugEntities |
|
bars = hv.Bars(dictionary, hv.Dimension('Drug Entities'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600, fontsize={'xticks': 18, 'xlabel':18, 'ylabel':16}) |
|
|
|
layout = pn.Row(bars, drug_button_column) |
|
return layout |
|
elif ents=='Condition': |
|
dictionary = topConditionEntities |
|
bars = hv.Bars(dictionary, hv.Dimension('Condition Entities'), 'Frequency').opts(framewise=True, xrotation=45,width=1200, height=600, fontsize={'xticks': 18, 'xlabel':18, 'ylabel':16}) |
|
layout = pn.Row(bars, condition_button_column) |
|
return layout |
|
|
|
|
|
def create_ontology_bar_charts(ents, **kwargs): |
|
if ents=='Drug': |
|
df = pd.DataFrame({ |
|
'Drug_Ontologies': [ont.split('/')[-1] for ont in topDrugOnts_df['ontology']], |
|
'Frequency': list(topDrugOnts_df['count']), |
|
'url': list(topDrugOnts_df['ontology_url']) |
|
}) |
|
drug_ontolgy_buttons = [] |
|
for i,row in df.iterrows(): |
|
button = pn.widgets.Button(name=row['Drug_Ontologies'], width=150) |
|
|
|
url = row["url"] |
|
button.js_on_click( |
|
code=f'window.open("{url}", "_blank");') |
|
drug_ontolgy_buttons.append(button) |
|
drug_ontology_column = pn.Column(*drug_ontolgy_buttons, sizing_mode='stretch_width') |
|
|
|
|
|
bars = hv.Bars(df, kdims=['Drug_Ontologies'], vdims=['Frequency']) |
|
bars.opts( |
|
framewise=True, |
|
tools=['hover'], |
|
width=1200, |
|
height=600, |
|
show_legend=True, |
|
xrotation=45, |
|
xlabel='Drug_Ontologies', |
|
ylabel='Frequency', |
|
hover_tooltips=[ |
|
("Drug_Ontologies", "@Drug_Ontologies"), |
|
("Frequency", "@Frequency") |
|
] |
|
) |
|
|
|
layout = pn.Row(bars, drug_ontology_column) |
|
return layout |
|
elif ents=='Condition': |
|
df = pd.DataFrame({ |
|
'Condition_Ontologies': [ont.split('/')[-1] for ont in topConditionOnts_df['ontology']], |
|
'Frequency': list(topConditionOnts_df['count']), |
|
'url': list(topConditionOnts_df['ontology_url']) |
|
}) |
|
condition_ontolgy_buttons = [] |
|
for i, row in df.iterrows(): |
|
button = pn.widgets.Button(name=row['Condition_Ontologies'], width=150) |
|
|
|
url = row["url"] |
|
button.js_on_click( |
|
code=f'window.open("{url}", "_blank");') |
|
condition_ontolgy_buttons.append(button) |
|
condition_ontology_column = pn.Column(*condition_ontolgy_buttons, sizing_mode='stretch_width') |
|
|
|
bars = hv.Bars(df, kdims=['Condition_Ontologies'], vdims=['Frequency']) |
|
bars.opts( |
|
framewise=True, |
|
tools=['hover'], |
|
width=1200, |
|
height=600, |
|
show_legend=True, |
|
xrotation=45, |
|
xlabel='Condition_Ontologies', |
|
ylabel='Frequency', |
|
hover_tooltips=[ |
|
("Condition Ontologies", "@Condition Ontologies"), |
|
("Frequency", "@Frequency") ]) |
|
|
|
layout = pn.Row(bars, condition_ontology_column) |
|
return layout |
|
|
|
|
|
|
|
def filter_data0(df, min_value): |
|
filtered_df = df[df['value'] >= min_value] |
|
return filtered_df |
|
|
|
|
|
def plot_chord_new(df,min_value): |
|
filtered_df = filter_data0(df, min_value) |
|
|
|
nodes = hv.Dataset(filtered_df, 'index') |
|
nodes.data.head() |
|
chord = hv.Chord(filtered_df, ['source', 'target'], ['value']) |
|
|
|
|
|
|
|
num_nodes = len(nodes.data) |
|
angles = np.linspace(0, 2 * np.pi, num_nodes, endpoint=False) |
|
radius = 1.05 |
|
|
|
label_data = [] |
|
for i, row in nodes.data.iterrows(): |
|
angle = angles[i] |
|
x = radius * np.cos(angle) |
|
y = radius * np.sin(angle) |
|
label_data.append((x, y, row['index'])) |
|
|
|
labels = hv.Labels(label_data, ['x', 'y'], 'text') |
|
|
|
layout = chord.opts( |
|
opts.Chord( |
|
labels=None, |
|
node_color=hv.dim('index').str(), |
|
edge_color=hv.dim('source').str(), |
|
cmap='Category20', |
|
edge_cmap='Category20', |
|
label_text_color='black', |
|
width=800, |
|
height=800, |
|
tools=['hover'] |
|
) |
|
) * labels.opts( |
|
text_font_size='9pt', |
|
text_align='center', |
|
text_baseline='middle' |
|
) |
|
return layout |
|
|
|
|
|
def plot_chord(df,min_value): |
|
filtered_df = filter_data0(df, min_value) |
|
|
|
nodes = hv.Dataset(filtered_df, 'index') |
|
nodes.data.head() |
|
chord = hv.Chord(filtered_df, ['source', 'target'], ['value']) |
|
return chord.opts(opts.Chord(cmap='Category20', edge_cmap='Category20', label_text_color="white", node_color = hv.dim('index').str(), edge_color = hv.dim('source').str(), labels = 'index', label_text_font_size="12pt", width=800, height=800)) |
|
|
|
|
|
|
|
def chordify_triples(rel_grouping, min_val): |
|
|
|
min_value_range = rel_grouping['value'].unique() |
|
min_value_range.sort() |
|
min_value_range = min_value_range[min_value_range > min_val] |
|
|
|
|
|
|
|
holomap = hv.HoloMap({min_value: plot_chord(rel_grouping, min_value) |
|
for min_value in min_value_range}, |
|
kdims=['Show triples with support greater than'] |
|
) |
|
return holomap |
|
|
|
|
|
|
|
button1 = pn.widgets.Button(name="Introduction", button_type="warning", icon="file-info", styles={"width": "100%"}) |
|
button2 = pn.widgets.Button(name="Top Key Entities", button_type="warning", icon="chart-bar", styles={"width": "100%"}) |
|
button3 = pn.widgets.Button(name="Entity/Relation Types:", button_type="warning", icon="chart-histogram", styles={"width": "100%"}) |
|
button4 = pn.widgets.Button(name="Ontology Coverage", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
|
|
|
|
markdown_button_style = """ |
|
<div style="background-color: #f0f0f0; /* Matches 'warning' button type */ |
|
color: white; |
|
font-weight: bold; |
|
padding: 8px 12px; |
|
border-radius: 4px; |
|
text-align: center; |
|
width: 100%; |
|
"> |
|
Causal Relation Chord Diagrams |
|
</div> |
|
""" |
|
|
|
|
|
button5 = pn.pane.Markdown("<div style='background-color:#f7c045; color: black; padding:8px 12px; font-weight: bold; border:1px solid #ccc; " "border-radius:8px; text-align:center; width:100%; white-space: nowrap;'>Causal Relation Chord Diagrams</div>", width=225, height=30, margin=(9, 9)) |
|
|
|
|
|
|
|
child_button_1 = pn.widgets.Button(name="Cause", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
child_button_2 = pn.widgets.Button(name="Enable", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
child_button_3 = pn.widgets.Button(name="Prevent", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
child_button_4 = pn.widgets.Button(name="Hinder", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
child_button_5 = pn.widgets.Button(name="Other", button_type="warning", icon="chart-dots-filled", styles={"width": "100%"}) |
|
|
|
tree_layout = pn.Column( |
|
button5, |
|
pn.Row(pn.Spacer(), |
|
pn.Column(child_button_1, child_button_2,child_button_3,child_button_4, child_button_5)), |
|
sizing_mode='stretch_width' |
|
) |
|
|
|
entRelsButton = pn.widgets.RadioButtonGroup(name='### Select', options=['Entity','Relation'], value = 'Entity' ) |
|
|
|
entTypeButton = pn.widgets.RadioButtonGroup(name='### Select Entity Type', options=list(entityTypesFreqMap.keys()), value='Drug') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def show_page(page_key): |
|
main_area.clear() |
|
main_area.append(mapping[page_key]) |
|
|
|
button1.on_click(lambda event: show_page("Page1")) |
|
button2.on_click(lambda event: show_page("Page2")) |
|
button3.on_click(lambda event: show_page("Page3")) |
|
button4.on_click(lambda event: show_page("Page4")) |
|
|
|
child_button_1.on_click(lambda event: show_page("Page5a")) |
|
child_button_2.on_click(lambda event: show_page("Page5b")) |
|
child_button_3.on_click(lambda event: show_page("Page5c")) |
|
child_button_4.on_click(lambda event: show_page("Page5d")) |
|
child_button_5.on_click(lambda event: show_page("Page5e")) |
|
|
|
|
|
|
|
|
|
def CreatePage1(): |
|
return pn.Column(pn.pane.Markdown(""" |
|
|
|
This is a dashboard for exploring a causal relation knowledge graph automatically extracted from a collection of drug reviews. The source data consists of around 19200 reviews from the **Drug Reviews (Druglib.com)** dataset (https://archive.ics.uci.edu/dataset/461/drug+review+dataset+druglib+com) containing patient reviews on specific drugs along with related conditions, crawled from online pharmaceutical review sites. |
|
The causal relations represented in the KG are defined by the **MIMICause** schema (https://huggingface.co/datasets/pensieves/mimicause). The underlying CausalDrugsKG graph is available in Turtle and RDF serialization format in the European Data portal: https://data.jrc.ec.europa.eu/dataset/acebeb4e-9789-4b5c-97ec-292ce14e75d0. |
|
|
|
|
|
--------------------------- |
|
|
|
## Top Key Entities |
|
Bar plots representing the occurence counts of the top 30 Drug and Condition entities in the KG, where occurrence means the entity is either the Subject or Object of an extracted triple in the KG. |
|
Clicking on the entity name in the right legend redirects to the corresponding entry in the Virtuoso Faceted Browser endpoint of the KG |
|
|
|
|
|
## Entities/Relation Types |
|
Bar plots of the Entity and Relation type counts. |
|
|
|
|
|
## Ontology Coverage |
|
Bar plots representing the linking of KG entities to standard Biomedical ontologies. Bar heights indicate the number of Drug/Condition entities linked to the corresponding ontology. |
|
Linking is performed using the Bioportal API (https://bioportal.bioontology.org/) |
|
Clicking on the ontology name on the right legend redirects to the ontology entry page. |
|
|
|
|
|
## Causal Relations Chord Diagrams |
|
Entity Chord Diagrams represent the most frequently connected entity pairs within the KG through chord illustrations, serving as both Subjects and Objects of predicative triples. The size of the chords corresponds to the support of the depicted relations. |
|
""", width=800), align="center") |
|
|
|
|
|
|
|
def CreatePage2(): |
|
return pn.Column( |
|
pn.pane.Markdown("## Top 30 Entities "), |
|
entTypeButton, |
|
pn.bind(create_ent_bar_charts, entTypeButton), |
|
align="center", ) |
|
|
|
|
|
def CreatePage3(): |
|
return pn.Column( |
|
pn.pane.Markdown("## Entity/Relation Types "), |
|
entRelsButton, |
|
pn.bind(create_type_bar_charts, entRelsButton), |
|
align="center", |
|
) |
|
|
|
def CreatePage4(): |
|
return pn.Column( |
|
pn.pane.Markdown("## Bio-Medical Ontology Coverage "), |
|
entTypeButton, |
|
pn.bind(create_ontology_bar_charts, entTypeButton), |
|
align="center", ) |
|
def CreatePage5(): |
|
return pn.Column( |
|
pn.pane.Markdown("## Causal Relation Chord Diagrams"), |
|
chordify_triples(grouping_filtered), |
|
align="center", ) |
|
|
|
def CreatePage5a(): |
|
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Cause'] |
|
return pn.Column( |
|
pn.pane.Markdown("## Relation Chord Diagram: Cause"), |
|
chordify_triples(rel_grouping,10), |
|
align="center", ) |
|
|
|
def CreatePage5b(): |
|
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Enable'] |
|
return pn.Column( |
|
pn.pane.Markdown("## Relation Chord Diagram: Enable"), |
|
chordify_triples(rel_grouping,4), |
|
align="center", ) |
|
|
|
def CreatePage5c(): |
|
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Prevent'] |
|
return pn.Column( |
|
pn.pane.Markdown("## Relation Chord Diagram: Prevent"), |
|
chordify_triples(rel_grouping,50), |
|
align="center", ) |
|
|
|
def CreatePage5d(): |
|
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Hinder'] |
|
return pn.Column( |
|
pn.pane.Markdown("## Relation Chord Diagram: Hinder"), |
|
chordify_triples(rel_grouping,10), |
|
align="center", ) |
|
|
|
def CreatePage5e(): |
|
rel_grouping = grouping_filtered[grouping_filtered['causal_relation'] == 'Other'] |
|
return pn.Column( |
|
pn.pane.Markdown("## Relation Chord Diagram: Other"), |
|
chordify_triples(rel_grouping,10), |
|
align="center", ) |
|
mapping = { |
|
"Page1": CreatePage1(), |
|
"Page2": CreatePage2(), |
|
"Page3": CreatePage3(), |
|
"Page4": CreatePage4(), |
|
|
|
"Page5a": CreatePage5a(), |
|
"Page5b": CreatePage5b(), |
|
"Page5c": CreatePage5c(), |
|
"Page5d": CreatePage5d(), |
|
"Page5e": CreatePage5e(), |
|
} |
|
|
|
|
|
sidebar = pn.Column(pn.pane.Markdown("## Panels"), button1,button2,button3, |
|
button4,tree_layout, |
|
styles={"width": "100%", "padding": "15px"}) |
|
|
|
|
|
main_area = pn.Column(mapping["Page1"], styles={"width":"100%"}) |
|
|
|
|
|
template = pn.template.BootstrapTemplate( |
|
title=" CausalDrugsKG_Dashboard ", |
|
sidebar=[sidebar], |
|
main=[main_area], |
|
|
|
|
|
|
|
sidebar_width=270, |
|
busy_indicator=pn.indicators.BooleanStatus(value=True), |
|
) |
|
|
|
|
|
|
|
|
|
template.servable() |