Arts-of-coding commited on
Commit
034fe1f
·
verified ·
1 Parent(s): 3c9e1e0

Create DLC_epi.py

Browse files
Files changed (1) hide show
  1. pages/DLC_epi.py +258 -0
pages/DLC_epi.py ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = "corg/DLC_epi_nopluri_clusres_scVI_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
+ interesting_genes = ["LIN28A", "KRT8", "ABCG2", "S100A9", "COL1A2", "AQP1", "LUM", "TEK", "PAX6", "PMEL"]
50
+
51
+ #filepath = f"az://{path_parquet}"
52
+
53
+ storage_options={'account_name': AZURE_STORAGE_ACCOUNT, 'account_key': AZURE_STORAGE_ACCESS_KEY} #,'anon': False
54
+ #azfs = AzureBlobFileSystem(**storage_options )
55
+
56
+ # Load in multiple dataframes
57
+ df = pl.scan_parquet(f"az://{dataset}.parquet", storage_options=storage_options).collect()
58
+
59
+ # Create the second tab content with scatter-plot_db30-5 and scatter-plot_db30-6
60
+ tab2_content = html.Div([
61
+ html.Div([
62
+ html.Label("S-cycle genes"),
63
+ dcc.Dropdown(id='dpdn3', value="MCM5", multi=False,
64
+ options=["MCM5","PCNA","TYMS","FEN1","MCM2","MCM4","RRM1","UNG","GINS2","MCM6","CDCA7","DTL",
65
+ "PRIM1","UHRF1","HELLS","RFC2","RPA2","NASP","RAD51AP1","GMNN","WDR76","SLBP","CCNE2","UBR7",
66
+ "POLD3","MSH2","ATAD2","RAD51","RRM2","CDC45","CDC6","EXO1","TIPIN","DSCC1","BLM","CASP8AP2",
67
+ "USP1","CLSPN","POLA1","CHAF1B","BRIP1","E2F8"]),
68
+ html.Label("G2M-cycle genes"),
69
+ dcc.Dropdown(id='dpdn4', value="TOP2A", multi=False,
70
+ options=["HMGB2","CDK1","NUSAP1","UBE2C","BIRC5","TPX2","TOP2A","NDC80","CKS2","NUF2","CKS1B","MKI67",
71
+ "TMPO","CENPF","TACC3","SMC4","CCNB2","CKAP2L","CKAP2","AURKB","BUB1","KIF11","ANP32E","TUBB4B",
72
+ "GTSE1","KIF20B","HJURP","CDCA3","CDC20","TTK","CDC25C","KIF2C","RANGAP1","NCAPD2","DLGAP5","CDCA2",
73
+ "CDCA8","ECT2","KIF23","HMMR","AURKA","PSRC1","ANLN","LBR","CKAP5","CENPE","CTCF","NEK2","G2E3",
74
+ "GAS2L3","CBX5","CENPA"]),
75
+ ]),
76
+ html.Div([
77
+ dcc.Graph(id='scatter-plot_db30-5', figure={}, className='three columns',config=config_fig)
78
+ ]),
79
+ html.Div([
80
+ dcc.Graph(id='scatter-plot_db30-6', figure={}, className='three columns',config=config_fig)
81
+ ]),
82
+ html.Div([
83
+ dcc.Graph(id='scatter-plot_db30-7', figure={}, className='three columns',config=config_fig)
84
+ ]),
85
+ html.Div([
86
+ dcc.Graph(id='scatter-plot_db30-8', figure={}, className='three columns',config=config_fig)
87
+ ]),
88
+ ])
89
+
90
+ # Create the second tab content with scatter-plot_db30-5 and scatter-plot_db30-6
91
+ tab3_content = html.Div([
92
+ html.Div([
93
+ html.Label("UMAP condition 1"),
94
+ dcc.Dropdown(id='dpdn5', value="sample", multi=False,
95
+ options=df.columns),
96
+ html.Label("UMAP condition 2"),
97
+ dcc.Dropdown(id='dpdn6', value="PAX6", multi=False,
98
+ options=df.columns),
99
+ html.Div([
100
+ dcc.Graph(id='scatter-plot_db30-9', figure={}, className='four columns', hoverData=None ,config=config_fig)
101
+ ]),
102
+ html.Div([
103
+ dcc.Graph(id='scatter-plot_db30-10', figure={}, className='four columns', hoverData=None, config=config_fig)
104
+ ]),
105
+ html.Div([
106
+ dcc.Graph(id='scatter-plot_db30-11', figure={}, className='four columns',config=config_fig)
107
+ ]),
108
+ html.Div([
109
+ dcc.Graph(id='my-graph_db302', figure={}, clickData=None, hoverData=None,
110
+ className='four columns',config=config_fig
111
+ )
112
+ ]),
113
+ ]),
114
+ ])
115
+
116
+ tab4_content = html.Div([
117
+ html.Label("Column chosen"),
118
+ dcc.Dropdown(id='dpdn2', value="leiden_res_0.95_r3", multi=False,
119
+ options=df.columns),
120
+ html.Div([
121
+ html.Label("Multi gene"),
122
+ dcc.Dropdown(id='dpdn7', value=interesting_genes, multi=True,
123
+ options=df.columns),
124
+ ]),
125
+ html.Div([
126
+ dcc.Graph(id='scatter-plot_db30-12', figure={}, className='row',style={'width': '100vh', 'height': '90vh'})
127
+ ]),
128
+ ])
129
+
130
+ # Define the tabs layout
131
+ layout = html.Div([
132
+ html.H1(f'Dataset analysis dashboard: {dataset}'),
133
+ dcc.Tabs(id='tabs', style= {'width': 600,
134
+ 'font-size': '100%',
135
+ 'height': 50}, value='tab1',children=[
136
+ #dcc.Tab(label='Dataset', value='tab0', children=tab0_content),
137
+ #dcc.Tab(label='QC', value='tab1', children=tab1_content),
138
+ dcc.Tab(label='UMAP visualisation', value='tab3', children=tab3_content),
139
+ dcc.Tab(label='Multi dot', value='tab4', children=tab4_content),
140
+ dcc.Tab(label='Cell cycle', value='tab2', children=tab2_content),
141
+ ]),
142
+ ])
143
+
144
+ @callback(
145
+ Output(component_id='scatter-plot_db30-5', component_property='figure'),
146
+ Output(component_id='scatter-plot_db30-6', component_property='figure'),
147
+ Output(component_id='scatter-plot_db30-7', component_property='figure'),
148
+ Output(component_id='scatter-plot_db30-8', component_property='figure'),
149
+ Output(component_id='scatter-plot_db30-9', component_property='figure'),
150
+ Output(component_id='scatter-plot_db30-10', component_property='figure'),
151
+ Output(component_id='scatter-plot_db30-11', component_property='figure'),
152
+ Output(component_id='scatter-plot_db30-12', component_property='figure'),
153
+ Output(component_id='my-graph_db302', component_property='figure'),
154
+ Input(component_id='dpdn2', component_property='value'),
155
+ Input(component_id='dpdn3', component_property='value'),
156
+ Input(component_id='dpdn4', component_property='value'),
157
+ Input(component_id='dpdn5', component_property='value'),
158
+ Input(component_id='dpdn6', component_property='value'),
159
+ Input(component_id='dpdn7', component_property='value'),
160
+
161
+ )
162
+
163
+ 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,
164
+ batch_chosen = df[col_chosen].unique().to_list()
165
+ dff = df.filter(
166
+ (pl.col(col_chosen).cast(str).is_in(batch_chosen)) #&
167
+ )
168
+ # Select ordering of plots
169
+ if condition1_chosen == "integrated_cell_states":
170
+ cat_ord= {condition1_chosen: natsorted(dff[condition1_chosen].unique())}
171
+ else:
172
+ cat_ord= {condition1_chosen: natsorted(dff[condition1_chosen].unique())}
173
+
174
+ # Calculate the mean expression
175
+
176
+ # Melt wide format DataFrame into long format
177
+ # Specify batch column as string type and gene columns as float type
178
+ list_conds = condition3_chosen
179
+ list_conds += [col_chosen]
180
+ dff_pre = dff.select(list_conds)
181
+
182
+ # Melt wide format DataFrame into long format
183
+ dff_long = dff_pre.melt(id_vars=col_chosen, variable_name="Gene", value_name="Mean expression")
184
+
185
+ # Calculate the mean expression levels for each gene in each region
186
+ expression_means = dff_long.lazy().group_by([col_chosen, "Gene"]).agg(pl.mean("Mean expression")).collect() #
187
+
188
+ # Calculate the percentage total expressed
189
+ dff_long1 = dff_pre.melt(id_vars=col_chosen, variable_name="Gene")#.group_by(pl.all()).agg(pl.len())
190
+ count = 1
191
+ dff_long2 = dff_long1.with_columns(pl.lit(count).alias("len"))
192
+ dff_long3 = dff_long2.filter(pl.col("value") > 0).group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("len"))
193
+ dff_long4 = dff_long2.group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("total"))
194
+ dff_5 = dff_long4.join(dff_long3, on=[col_chosen,"Gene"], how="outer")
195
+ result = dff_5.select([
196
+ pl.when((pl.col('len').is_not_null()) & (pl.col('total').is_not_null()))
197
+ .then(pl.col('len') / pl.col('total')*100)
198
+ .otherwise(None).alias("%"),
199
+ ])
200
+ result = result.with_columns(pl.col("%").fill_null(0))
201
+ dff_5[["percentage"]] = result[["%"]]
202
+ dff_5 = dff_5.select(pl.col(col_chosen,"Gene","percentage"))
203
+
204
+ # Final part to join the percentage expressed and mean expression levels
205
+ expression_means = expression_means.join(dff_5, on=[col_chosen,"Gene"], how="inner")
206
+
207
+ fig_scatter_db30_5 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=s_chosen,
208
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
209
+ hover_name=None, title="S-cycle gene:",template="seaborn")
210
+
211
+ fig_scatter_db30_6 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=g2m_chosen,
212
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
213
+ hover_name='sample', title="G2M-cycle gene:",template="seaborn")
214
+
215
+ fig_scatter_db30_7 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="S_score",
216
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
217
+ hover_name='sample', title="S score:",template="seaborn")
218
+
219
+ fig_scatter_db30_8 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color="G2M_score",
220
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
221
+ hover_name='sample', title="G2M score:",template="seaborn")
222
+
223
+ # Sort values of custom in-between
224
+ dff = dff.sort(condition1_chosen)
225
+
226
+ fig_scatter_db30_9 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition1_chosen,
227
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
228
+ hover_name=None,hover_data = None, template="seaborn",category_orders=cat_ord)
229
+ fig_scatter_db30_9.update_traces(hoverinfo='none', hovertemplate=None)
230
+ fig_scatter_db30_9.update_layout(hovermode=False)
231
+
232
+ fig_scatter_db30_10 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=condition2_chosen,
233
+ labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
234
+ hover_name='sample',template="seaborn")
235
+
236
+ fig_scatter_db30_11 = px.scatter(data_frame=dff, x=condition1_chosen, y=condition2_chosen, color=condition1_chosen,
237
+ #labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
238
+ hover_name='sample',template="seaborn",category_orders=cat_ord)
239
+
240
+ # Reorder categories on natural sorting or on the integrated cell state order of the paper
241
+ if col_chosen == "integrated_cell_states":
242
+ fig_scatter_db30_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression",
243
+ size="percentage", size_max = 20,
244
+ #labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
245
+ hover_name=col_chosen,template="seaborn",category_orders={col_chosen: natsorted(expression_means[col_chosen].unique()),"Gene": condition3_chosen})
246
+ else:
247
+ fig_scatter_db30_12 = px.scatter(data_frame=expression_means, x="Gene", y=col_chosen, color="Mean expression",
248
+ size="percentage", size_max = 20,
249
+ #labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
250
+ hover_name=col_chosen,template="seaborn",category_orders={col_chosen: natsorted(expression_means[col_chosen].unique()),"Gene": condition3_chosen})
251
+
252
+ fig_violin_db302 = px.violin(data_frame=dff, x=condition1_chosen, y=condition2_chosen, box=True, points="all",
253
+ color=condition1_chosen, hover_name=condition1_chosen,template="seaborn",category_orders=cat_ord)
254
+
255
+
256
+ return fig_scatter_db30_5, fig_scatter_db30_6, fig_scatter_db30_7, fig_scatter_db30_8, fig_scatter_db30_9, fig_scatter_db30_10, fig_scatter_db30_11, fig_scatter_db30_12, fig_violin_db302 #fig_violin_db30, fig_pie_db30, fig_scatter_db30, fig_scatter_db30_2, fig_scatter_db30_3, fig_scatter_db30_4,
257
+
258
+ # Set http://localhost:5000/ in web browser