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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Dash app to visualize scRNA-seq data quality control metrics from scanpy objects
|
2 |
+
# Shoutout to Coding-with-Adam for the initial template of the project:
|
3 |
+
# https://github.com/Coding-with-Adam/Dash-by-Plotly/blob/master/Dash%20Components/Graph/dash-graph.py
|
4 |
+
|
5 |
+
import dash
|
6 |
+
from dash import dcc, html, Output, Input, callback
|
7 |
+
import plotly.express as px
|
8 |
+
import dash_callback_chain
|
9 |
+
import yaml
|
10 |
+
import polars as pl
|
11 |
+
import os
|
12 |
+
from natsort import natsorted
|
13 |
+
#pl.enable_string_cache(False)
|
14 |
+
|
15 |
+
dash.register_page(__name__, location="sidebar")
|
16 |
+
|
17 |
+
dataset = "data10xflex/corg/WT2_polars"
|
18 |
+
|
19 |
+
# Set custom resolution for plots:
|
20 |
+
config_fig = {
|
21 |
+
'toImageButtonOptions': {
|
22 |
+
'format': 'svg',
|
23 |
+
'filename': 'custom_image',
|
24 |
+
'height': 600,
|
25 |
+
'width': 700,
|
26 |
+
'scale': 1,
|
27 |
+
}
|
28 |
+
}
|
29 |
+
from adlfs import AzureBlobFileSystem
|
30 |
+
mountpount=os.environ['AZURE_MOUNT_POINT'],
|
31 |
+
AZURE_STORAGE_ACCESS_KEY=os.getenv('AZURE_STORAGE_ACCESS_KEY')
|
32 |
+
AZURE_STORAGE_ACCOUNT=os.getenv('AZURE_STORAGE_ACCOUNT')
|
33 |
+
|
34 |
+
# Load in config file
|
35 |
+
config_path = "./data/config.yaml"
|
36 |
+
|
37 |
+
# Add the read-in data from the yaml file
|
38 |
+
def read_config(filename):
|
39 |
+
with open(filename, 'r') as yaml_file:
|
40 |
+
config = yaml.safe_load(yaml_file)
|
41 |
+
return config
|
42 |
+
|
43 |
+
config = read_config(config_path)
|
44 |
+
path_parquet = config.get("path_parquet")
|
45 |
+
col_batch = config.get("col_batch")
|
46 |
+
col_features = config.get("col_features")
|
47 |
+
col_counts = config.get("col_counts")
|
48 |
+
col_mt = config.get("col_mt")
|
49 |
+
|
50 |
+
#filepath = f"az://{path_parquet}"
|
51 |
+
|
52 |
+
storage_options={'account_name': AZURE_STORAGE_ACCOUNT, 'account_key': AZURE_STORAGE_ACCESS_KEY} #,'anon': False
|
53 |
+
#azfs = AzureBlobFileSystem(**storage_options )
|
54 |
+
|
55 |
+
# Load in multiple dataframes
|
56 |
+
df = pl.read_parquet(f"az://{dataset}.parquet", storage_options=storage_options)
|
57 |
+
|
58 |
+
# Create the second tab content with scatter-plot_db20-5 and scatter-plot_db20-6
|
59 |
+
tab2_content = html.Div([
|
60 |
+
html.Div([
|
61 |
+
html.Label("S-cycle genes"),
|
62 |
+
dcc.Dropdown(id='dpdn3', value="MCM5", multi=False,
|
63 |
+
options=["MCM5","PCNA","TYMS","FEN1","MCM2","MCM4","RRM1","UNG","GINS2","MCM6","CDCA7","DTL",
|
64 |
+
"PRIM1","UHRF1","HELLS","RFC2","RPA2","NASP","RAD51AP1","GMNN","WDR76","SLBP","CCNE2","UBR7",
|
65 |
+
"POLD3","MSH2","ATAD2","RAD51","RRM2","CDC45","CDC6","EXO1","TIPIN","DSCC1","BLM","CASP8AP2",
|
66 |
+
"USP1","CLSPN","POLA1","CHAF1B","BRIP1","E2F8"]),
|
67 |
+
html.Label("G2M-cycle genes"),
|
68 |
+
dcc.Dropdown(id='dpdn4', value="TOP2A", multi=False,
|
69 |
+
options=["HMGB2","CDK1","NUSAP1","UBE2C","BIRC5","TPX2","TOP2A","NDC80","CKS2","NUF2","CKS1B","MKI67",
|
70 |
+
"TMPO","CENPF","TACC3","SMC4","CCNB2","CKAP2L","CKAP2","AURKB","BUB1","KIF11","ANP32E","TUBB4B",
|
71 |
+
"GTSE1","KIF20B","HJURP","CDCA3","CDC20","TTK","CDC25C","KIF2C","RANGAP1","NCAPD2","DLGAP5","CDCA2",
|
72 |
+
"CDCA8","ECT2","KIF23","HMMR","AURKA","PSRC1","ANLN","LBR","CKAP5","CENPE","CTCF","NEK2","G2E3",
|
73 |
+
"GAS2L3","CBX5","CENPA"]),
|
74 |
+
]),
|
75 |
+
html.Div([
|
76 |
+
dcc.Graph(id='scatter-plot_db20-5', figure={}, className='three columns',config=config_fig)
|
77 |
+
]),
|
78 |
+
html.Div([
|
79 |
+
dcc.Graph(id='scatter-plot_db20-6', figure={}, className='three columns',config=config_fig)
|
80 |
+
]),
|
81 |
+
html.Div([
|
82 |
+
dcc.Graph(id='scatter-plot_db20-7', figure={}, className='three columns',config=config_fig)
|
83 |
+
]),
|
84 |
+
html.Div([
|
85 |
+
dcc.Graph(id='scatter-plot_db20-8', figure={}, className='three columns',config=config_fig)
|
86 |
+
]),
|
87 |
+
])
|
88 |
+
|
89 |
+
# Create the second tab content with scatter-plot_db20-5 and scatter-plot_db20-6
|
90 |
+
tab3_content = html.Div([
|
91 |
+
html.Div([
|
92 |
+
html.Label("UMAP condition 1"),
|
93 |
+
dcc.Dropdown(id='dpdn5', value="sample", multi=False,
|
94 |
+
options=df.columns),
|
95 |
+
html.Label("UMAP condition 2"),
|
96 |
+
dcc.Dropdown(id='dpdn6', value="PAX6", multi=False,
|
97 |
+
options=df.columns),
|
98 |
+
html.Div([
|
99 |
+
dcc.Graph(id='scatter-plot_db20-9', figure={}, className='four columns', hoverData=None ,config=config_fig)
|
100 |
+
]),
|
101 |
+
html.Div([
|
102 |
+
dcc.Graph(id='scatter-plot_db20-10', figure={}, className='four columns', hoverData=None, config=config_fig)
|
103 |
+
]),
|
104 |
+
html.Div([
|
105 |
+
dcc.Graph(id='scatter-plot_db20-11', figure={}, className='four columns',config=config_fig)
|
106 |
+
]),
|
107 |
+
html.Div([
|
108 |
+
dcc.Graph(id='my-graph_db202', figure={}, clickData=None, hoverData=None,
|
109 |
+
className='four columns',config=config_fig
|
110 |
+
)
|
111 |
+
]),
|
112 |
+
]),
|
113 |
+
])
|
114 |
+
|
115 |
+
tab4_content = html.Div([
|
116 |
+
html.Label("Column chosen"),
|
117 |
+
dcc.Dropdown(id='dpdn2', value="leiden_res_1.35", multi=False,
|
118 |
+
options=df.columns),
|
119 |
+
html.Div([
|
120 |
+
html.Label("Multi gene"),
|
121 |
+
dcc.Dropdown(id='dpdn7', value=['PAX6', 'TP63', 'OTX2', 'SIX3', 'LHX2', 'SIX6', 'SOX2', 'PMEL',
|
122 |
+
'RAX', 'LIN28A', 'ABCG2', 'KRT8', 'KRT7',
|
123 |
+
'KRT19', 'COL1A2', 'AQP1', 'LUM', 'TFAP2A', 'HAND1', 'S100A9',
|
124 |
+
'SPP1', 'TEK', 'FOXC2', 'PECAM1', 'SOX9'], multi=True,
|
125 |
+
options=df.columns),
|
126 |
+
]),
|
127 |
+
html.Div([
|
128 |
+
dcc.Graph(id='scatter-plot_db20-12', figure={}, className='row',style={'width': '100vh', 'height': '90vh'})
|
129 |
+
]),
|
130 |
+
])
|
131 |
+
|
132 |
+
# Define the tabs layout
|
133 |
+
layout = html.Div([
|
134 |
+
html.H1(f'Dataset analysis dashboard: {dataset}'),
|
135 |
+
dcc.Tabs(id='tabs', style= {'width': 600,
|
136 |
+
'font-size': '100%',
|
137 |
+
'height': 50}, value='tab1',children=[
|
138 |
+
#dcc.Tab(label='Dataset', value='tab0', children=tab0_content),
|
139 |
+
#dcc.Tab(label='QC', value='tab1', children=tab1_content),
|
140 |
+
dcc.Tab(label='UMAP visualisation', value='tab3', children=tab3_content),
|
141 |
+
dcc.Tab(label='Multi dot', value='tab4', children=tab4_content),
|
142 |
+
dcc.Tab(label='Cell cycle', value='tab2', children=tab2_content),
|
143 |
+
]),
|
144 |
+
])
|
145 |
+
|
146 |
+
@callback(
|
147 |
+
Output(component_id='scatter-plot_db20-5', component_property='figure'),
|
148 |
+
Output(component_id='scatter-plot_db20-6', component_property='figure'),
|
149 |
+
Output(component_id='scatter-plot_db20-7', component_property='figure'),
|
150 |
+
Output(component_id='scatter-plot_db20-8', component_property='figure'),
|
151 |
+
Output(component_id='scatter-plot_db20-9', component_property='figure'),
|
152 |
+
Output(component_id='scatter-plot_db20-10', component_property='figure'),
|
153 |
+
Output(component_id='scatter-plot_db20-11', component_property='figure'),
|
154 |
+
Output(component_id='scatter-plot_db20-12', component_property='figure'),
|
155 |
+
Output(component_id='my-graph_db202', component_property='figure'),
|
156 |
+
Input(component_id='dpdn2', component_property='value'),
|
157 |
+
Input(component_id='dpdn3', component_property='value'),
|
158 |
+
Input(component_id='dpdn4', component_property='value'),
|
159 |
+
Input(component_id='dpdn5', component_property='value'),
|
160 |
+
Input(component_id='dpdn6', component_property='value'),
|
161 |
+
Input(component_id='dpdn7', component_property='value'),
|
162 |
+
|
163 |
+
)
|
164 |
+
|
165 |
+
def update_graph_and_pie_chart(col_chosen, s_chosen, g2m_chosen, condition1_chosen, condition2_chosen, condition3_chosen): #, range_value_1, range_value_2, range_value_3 batch_chosen,
|
166 |
+
batch_chosen = df[col_chosen].unique().to_list()
|
167 |
+
dff = df.filter(
|
168 |
+
(pl.col(col_chosen).cast(str).is_in(batch_chosen)) #&
|
169 |
+
)
|
170 |
+
# Select ordering of plots
|
171 |
+
if condition1_chosen == "integrated_cell_states":
|
172 |
+
cat_ord= {condition1_chosen: natsorted(dff[condition1_chosen].unique())}
|
173 |
+
else:
|
174 |
+
cat_ord= {condition1_chosen: natsorted(dff[condition1_chosen].unique())}
|
175 |
+
|
176 |
+
# Calculate the mean expression
|
177 |
+
|
178 |
+
# Melt wide format DataFrame into long format
|
179 |
+
# Specify batch column as string type and gene columns as float type
|
180 |
+
list_conds = condition3_chosen
|
181 |
+
list_conds += [col_chosen]
|
182 |
+
dff_pre = dff.select(list_conds)
|
183 |
+
|
184 |
+
# Melt wide format DataFrame into long format
|
185 |
+
dff_long = dff_pre.melt(id_vars=col_chosen, variable_name="Gene", value_name="Mean expression")
|
186 |
+
|
187 |
+
# Calculate the mean expression levels for each gene in each region
|
188 |
+
expression_means = dff_long.lazy().group_by([col_chosen, "Gene"]).agg(pl.mean("Mean expression")).collect() #
|
189 |
+
|
190 |
+
# Calculate the percentage total expressed
|
191 |
+
dff_long1 = dff_pre.melt(id_vars=col_chosen, variable_name="Gene")#.group_by(pl.all()).agg(pl.len())
|
192 |
+
count = 1
|
193 |
+
dff_long2 = dff_long1.with_columns(pl.lit(count).alias("len"))
|
194 |
+
dff_long3 = dff_long2.filter(pl.col("value") > 0).group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("len"))
|
195 |
+
dff_long4 = dff_long2.group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("total"))
|
196 |
+
dff_5 = dff_long4.join(dff_long3, on=[col_chosen,"Gene"], how="outer")
|
197 |
+
result = dff_5.select([
|
198 |
+
pl.when((pl.col('len').is_not_null()) & (pl.col('total').is_not_null()))
|
199 |
+
.then(pl.col('len') / pl.col('total')*100)
|
200 |
+
.otherwise(None).alias("%"),
|
201 |
+
])
|
202 |
+
result = result.with_columns(pl.col("%").fill_null(0))
|
203 |
+
dff_5[["percentage"]] = result[["%"]]
|
204 |
+
dff_5 = dff_5.select(pl.col(col_chosen,"Gene","percentage"))
|
205 |
+
|
206 |
+
# Final part to join the percentage expressed and mean expression levels
|
207 |
+
expression_means = expression_means.join(dff_5, on=[col_chosen,"Gene"], how="inner")
|
208 |
+
|
209 |
+
fig_scatter_db20_5 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=s_chosen,
|
210 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
211 |
+
hover_name=None, title="S-cycle gene:",template="seaborn")
|
212 |
+
|
213 |
+
fig_scatter_db20_6 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=g2m_chosen,
|
214 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
215 |
+
hover_name='sample', title="G2M-cycle gene:",template="seaborn")
|
216 |
+
|
217 |
+
fig_scatter_db20_7 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="S_score",
|
218 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
219 |
+
hover_name='sample', title="S score:",template="seaborn")
|
220 |
+
|
221 |
+
fig_scatter_db20_8 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="G2M_score",
|
222 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
223 |
+
hover_name='sample', title="G2M score:",template="seaborn")
|
224 |
+
|
225 |
+
# Sort values of custom in-between
|
226 |
+
dff = dff.sort(condition1_chosen)
|
227 |
+
|
228 |
+
fig_scatter_db20_9 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition1_chosen,
|
229 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
230 |
+
hover_name=None,hover_data = None, template="seaborn",category_orders=cat_ord)
|
231 |
+
fig_scatter_db20_9.update_traces(hoverinfo='none', hovertemplate=None)
|
232 |
+
fig_scatter_db20_9.update_layout(hovermode=False)
|
233 |
+
|
234 |
+
fig_scatter_db20_10 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition2_chosen,
|
235 |
+
labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
236 |
+
hover_name='sample',template="seaborn")
|
237 |
+
|
238 |
+
fig_scatter_db20_11 = px.scatter(data_frame=dff, x=condition1_chosen, y=condition2_chosen, color=condition1_chosen,
|
239 |
+
#labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
240 |
+
hover_name='sample',template="seaborn",category_orders=cat_ord)
|
241 |
+
|
242 |
+
# Reorder categories on natural sorting or on the integrated cell state order of the paper
|
243 |
+
if col_chosen == "integrated_cell_states":
|
244 |
+
fig_scatter_db20_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression",
|
245 |
+
size="percentage", size_max = 20,
|
246 |
+
#labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
247 |
+
hover_name=col_chosen,template="seaborn",category_orders={col_chosen: natsorted(expression_means[col_chosen].unique()),"Gene": condition3_chosen})
|
248 |
+
else:
|
249 |
+
fig_scatter_db20_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression",
|
250 |
+
size="percentage", size_max = 20,
|
251 |
+
#labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
|
252 |
+
hover_name=col_chosen,template="seaborn",category_orders={col_chosen: natsorted(expression_means[col_chosen].unique()),"Gene": condition3_chosen})
|
253 |
+
|
254 |
+
fig_violin_db202 = px.violin(data_frame=dff, x=condition1_chosen, y=condition2_chosen, box=True, points="all",
|
255 |
+
color=condition1_chosen, hover_name=condition1_chosen,template="seaborn",category_orders=cat_ord)
|
256 |
+
|
257 |
+
|
258 |
+
return fig_scatter_db20_5, fig_scatter_db20_6, fig_scatter_db20_7, fig_scatter_db20_8, fig_scatter_db20_9, fig_scatter_db20_10, fig_scatter_db20_11, fig_scatter_db20_12, fig_violin_db202 #fig_violin_db20, fig_pie_db20, fig_scatter_db20, fig_scatter_db20_2, fig_scatter_db20_3, fig_scatter_db20_4,
|
259 |
+
|
260 |
+
# Set http://localhost:5000/ in web browser
|