|
|
|
|
|
|
|
|
|
import dash |
|
from dash import dcc, html, Output, Input, callback |
|
import plotly.express as px |
|
import dash_callback_chain |
|
import yaml |
|
import polars as pl |
|
import os |
|
from natsort import natsorted |
|
|
|
|
|
dash.register_page(__name__, location="sidebar") |
|
|
|
dataset = "datasingleron/keratinocytes/singleron_keratinocytes_clusres_scVI" |
|
|
|
|
|
config_fig = { |
|
'toImageButtonOptions': { |
|
'format': 'svg', |
|
'filename': 'custom_image', |
|
'height': 600, |
|
'width': 700, |
|
'scale': 1, |
|
} |
|
} |
|
from adlfs import AzureBlobFileSystem |
|
mountpount=os.environ['AZURE_MOUNT_POINT'], |
|
AZURE_STORAGE_ACCESS_KEY=os.getenv('AZURE_STORAGE_ACCESS_KEY') |
|
AZURE_STORAGE_ACCOUNT=os.getenv('AZURE_STORAGE_ACCOUNT') |
|
|
|
|
|
config_path = "./data/config.yaml" |
|
|
|
|
|
def read_config(filename): |
|
with open(filename, 'r') as yaml_file: |
|
config = yaml.safe_load(yaml_file) |
|
return config |
|
|
|
config = read_config(config_path) |
|
path_parquet = config.get("path_parquet") |
|
col_batch = "batch_renamed" |
|
col_features = config.get("col_features") |
|
col_counts = config.get("col_counts") |
|
col_mt = config.get("col_mt") |
|
|
|
|
|
|
|
storage_options={'account_name': AZURE_STORAGE_ACCOUNT, 'account_key': AZURE_STORAGE_ACCESS_KEY} |
|
|
|
|
|
df = pl.scan_parquet(f"az://{dataset}.parquet", storage_options=storage_options).collect() |
|
|
|
|
|
tab2_content = html.Div([ |
|
html.Div([ |
|
html.Label("S-cycle genes"), |
|
dcc.Dropdown(id='dpdn3', value="MCM5", multi=False, |
|
options=["MCM5","PCNA","TYMS","FEN1","MCM2","MCM4","RRM1","UNG","GINS2","MCM6","CDCA7","DTL", |
|
"PRIM1","UHRF1","HELLS","RFC2","RPA2","NASP","RAD51AP1","GMNN","WDR76","SLBP","CCNE2","UBR7", |
|
"POLD3","MSH2","ATAD2","RAD51","RRM2","CDC45","CDC6","EXO1","TIPIN","DSCC1","BLM","CASP8AP2", |
|
"USP1","CLSPN","POLA1","CHAF1B","BRIP1","E2F8"]), |
|
html.Label("G2M-cycle genes"), |
|
dcc.Dropdown(id='dpdn4', value="TOP2A", multi=False, |
|
options=["HMGB2","CDK1","NUSAP1","UBE2C","BIRC5","TPX2","TOP2A","NDC80","CKS2","NUF2","CKS1B","MKI67", |
|
"TMPO","CENPF","TACC3","SMC4","CCNB2","CKAP2L","CKAP2","AURKB","BUB1","KIF11","ANP32E","TUBB4B", |
|
"GTSE1","KIF20B","HJURP","CDCA3","CDC20","TTK","CDC25C","KIF2C","RANGAP1","NCAPD2","DLGAP5","CDCA2", |
|
"CDCA8","ECT2","KIF23","HMMR","AURKA","PSRC1","ANLN","LBR","CKAP5","CENPE","CTCF","NEK2","G2E3", |
|
"GAS2L3","CBX5","CENPA"]), |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-5', figure={}, className='three columns',config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-6', figure={}, className='three columns',config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-7', figure={}, className='three columns',config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-8', figure={}, className='three columns',config=config_fig) |
|
]), |
|
]) |
|
|
|
|
|
tab3_content = html.Div([ |
|
html.Div([ |
|
html.Label("UMAP condition 1"), |
|
dcc.Dropdown(id='dpdn5', value="batch_renamed", multi=False, |
|
options=df.columns), |
|
html.Label("UMAP condition 2"), |
|
dcc.Dropdown(id='dpdn6', value="AREG", multi=False, |
|
options=df.columns), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-9', figure={}, className='four columns', hoverData=None ,config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-10', figure={}, className='four columns', hoverData=None, config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-11', figure={}, className='four columns',config=config_fig) |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='my-graph_db22', figure={}, clickData=None, hoverData=None, |
|
className='four columns',config=config_fig |
|
) |
|
]), |
|
]), |
|
]) |
|
|
|
tab4_content = html.Div([ |
|
html.Label("Column chosen"), |
|
dcc.Dropdown(id='dpdn2', value="leiden_0.45", multi=False, |
|
options=df.columns), |
|
html.Div([ |
|
html.Label("Multi gene"), |
|
dcc.Dropdown(id='dpdn7', value=["KRT4","VIM","KRT14","KRT15","AREG"], multi=True, |
|
options=df.columns), |
|
]), |
|
html.Div([ |
|
dcc.Graph(id='scatter-plot_db2-12', figure={}, className='row',style={'width': '100vh', 'height': '90vh'}) |
|
]), |
|
]) |
|
|
|
|
|
layout = html.Div([ |
|
html.H1(f'Dataset analysis dashboard: {dataset}'), |
|
dcc.Tabs(id='tabs', style= {'width': 600, |
|
'font-size': '100%', |
|
'height': 50}, value='tab1',children=[ |
|
|
|
|
|
dcc.Tab(label='UMAP visualisation', value='tab3', children=tab3_content), |
|
dcc.Tab(label='Multi dot', value='tab4', children=tab4_content), |
|
dcc.Tab(label='Cell cycle', value='tab2', children=tab2_content), |
|
]), |
|
]) |
|
|
|
@callback( |
|
Output(component_id='scatter-plot_db2-5', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-6', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-7', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-8', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-9', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-10', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-11', component_property='figure'), |
|
Output(component_id='scatter-plot_db2-12', component_property='figure'), |
|
Output(component_id='my-graph_db22', component_property='figure'), |
|
Input(component_id='dpdn2', component_property='value'), |
|
Input(component_id='dpdn3', component_property='value'), |
|
Input(component_id='dpdn4', component_property='value'), |
|
Input(component_id='dpdn5', component_property='value'), |
|
Input(component_id='dpdn6', component_property='value'), |
|
Input(component_id='dpdn7', component_property='value'), |
|
|
|
) |
|
|
|
def update_graph_and_pie_chart(col_chosen, s_chosen, g2m_chosen, condition1_chosen, condition2_chosen, condition3_chosen): |
|
batch_chosen = df[col_chosen].unique().to_list() |
|
dff = df.filter( |
|
(pl.col(col_chosen).cast(str).is_in(batch_chosen)) |
|
) |
|
|
|
if condition1_chosen == "leiden_0.45": |
|
cat_ord= {condition1_chosen: ["1","2","3","4"]} |
|
else: |
|
cat_ord= {condition1_chosen: natsorted(dff[condition1_chosen].unique())} |
|
|
|
|
|
|
|
|
|
|
|
list_conds = condition3_chosen |
|
list_conds += [col_chosen] |
|
dff_pre = dff.select(list_conds) |
|
|
|
|
|
dff_long = dff_pre.melt(id_vars=col_chosen, variable_name="Gene", value_name="Mean expression") |
|
|
|
|
|
expression_means = dff_long.lazy().group_by([col_chosen, "Gene"]).agg(pl.mean("Mean expression")).collect() |
|
|
|
|
|
dff_long1 = dff_pre.melt(id_vars=col_chosen, variable_name="Gene") |
|
count = 1 |
|
dff_long2 = dff_long1.with_columns(pl.lit(count).alias("len")) |
|
dff_long3 = dff_long2.filter(pl.col("value") > 0).group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("len")) |
|
dff_long4 = dff_long2.group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("total")) |
|
dff_5 = dff_long4.join(dff_long3, on=[col_chosen,"Gene"], how="outer") |
|
result = dff_5.select([ |
|
pl.when((pl.col('len').is_not_null()) & (pl.col('total').is_not_null())) |
|
.then(pl.col('len') / pl.col('total')*100) |
|
.otherwise(None).alias("%"), |
|
]) |
|
result = result.with_columns(pl.col("%").fill_null(0)) |
|
dff_5[["percentage"]] = result[["%"]] |
|
dff_5 = dff_5.select(pl.col(col_chosen,"Gene","percentage")) |
|
|
|
|
|
expression_means = expression_means.join(dff_5, on=[col_chosen,"Gene"], how="inner") |
|
|
|
fig_scatter_db2_5 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=s_chosen, |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name=None, title="S-cycle gene:",template="seaborn") |
|
|
|
fig_scatter_db2_6 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=g2m_chosen, |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name='batch_renamed', title="G2M-cycle gene:",template="seaborn") |
|
|
|
fig_scatter_db2_7 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="S_score", |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name='batch_renamed', title="S score:",template="seaborn") |
|
|
|
fig_scatter_db2_8 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="G2M_score", |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name='batch_renamed', title="G2M score:",template="seaborn") |
|
|
|
|
|
dff = dff.sort(condition1_chosen) |
|
|
|
fig_scatter_db2_9 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition1_chosen, |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name=None,hover_data = None, template="seaborn",category_orders=cat_ord) |
|
fig_scatter_db2_9.update_traces(hoverinfo='none', hovertemplate=None) |
|
fig_scatter_db2_9.update_layout(hovermode=False) |
|
|
|
fig_scatter_db2_10 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition2_chosen, |
|
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'}, |
|
hover_name='batch_renamed',template="seaborn") |
|
|
|
fig_scatter_db2_11 = px.scatter(data_frame=dff, x=condition1_chosen, y=condition2_chosen, color=condition1_chosen, |
|
|
|
hover_name='batch_renamed',template="seaborn",category_orders=cat_ord) |
|
|
|
|
|
if col_chosen == "leiden_0.45": |
|
fig_scatter_db2_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression", |
|
size="percentage", size_max = 20, |
|
|
|
hover_name=col_chosen,template="seaborn",category_orders={col_chosen: ["1","2","3","4"],"Gene": condition3_chosen}) |
|
else: |
|
fig_scatter_db2_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression", |
|
size="percentage", size_max = 20, |
|
|
|
hover_name=col_chosen,template="seaborn",category_orders={col_chosen: natsorted(expression_means[col_chosen].unique()),"Gene": condition3_chosen}) |
|
|
|
fig_violin_db22 = px.violin(data_frame=dff, x=condition1_chosen, y=condition2_chosen, box=True, points="all", |
|
color=condition1_chosen, hover_name=condition1_chosen,template="seaborn",category_orders=cat_ord) |
|
|
|
|
|
return fig_scatter_db2_5, fig_scatter_db2_6, fig_scatter_db2_7, fig_scatter_db2_8, fig_scatter_db2_9, fig_scatter_db2_10, fig_scatter_db2_11, fig_scatter_db2_12, fig_violin_db22 |
|
|
|
|
|
|
|
|
|
|
|
|