Arts-of-coding commited on
Commit
6de0a86
·
verified ·
1 Parent(s): 350f0cc

Update pages/Cornea_v1_integrated_scVI.py

Browse files
Files changed (1) hide show
  1. pages/Cornea_v1_integrated_scVI.py +144 -141
pages/Cornea_v1_integrated_scVI.py CHANGED
@@ -86,18 +86,18 @@ df = pl.read_parquet(f"./data/{dataset}.parquet")
86
  # return
87
 
88
  #df = pl.read_parquet(filepath, storage_options=storage_options)
89
- min_value = df[col_features].min()
90
- max_value = df[col_features].max()
91
 
92
- min_value_2 = df[col_counts].min()
93
- min_value_2 = round(min_value_2)
94
- max_value_2 = df[col_counts].max()
95
- max_value_2 = round(max_value_2)
96
 
97
- min_value_3 = df[col_mt].min()
98
- min_value_3 = round(min_value_3, 1)
99
- max_value_3 = df[col_mt].max()
100
- max_value_3 = round(max_value_3, 1)
101
 
102
  # Loads in the conditions specified in the yaml file
103
 
@@ -107,55 +107,55 @@ max_value_3 = round(max_value_3, 1)
107
  # Create the first tab content
108
  # Add Sliders for three QC params: N genes by counts, total amount of reads and pct MT reads
109
 
110
- tab1_content = html.Div([
111
- html.Label("Column chosen"),
112
- dcc.Dropdown(id='dpdn2', value="batch", multi=False,
113
- options=df.columns),
114
- html.Label("N Genes by Counts"),
115
- dcc.RangeSlider(
116
- id='range-slider_db2-1',
117
- step=250,
118
- value=[min_value, max_value],
119
- marks={i: str(i) for i in range(min_value, max_value + 1, 250)},
120
- ),
121
- dcc.Input(id='min-slider_db2-1', type='number', value=min_value, debounce=True),
122
- dcc.Input(id='max-slider_db2-1', type='number', value=max_value, debounce=True),
123
- html.Label("Total Counts"),
124
- dcc.RangeSlider(
125
- id='range-slider_db2-2',
126
- step=7500,
127
- value=[min_value_2, max_value_2],
128
- marks={i: str(i) for i in range(min_value_2, max_value_2 + 1, 7500)},
129
- ),
130
- dcc.Input(id='min-slider_db2-2', type='number', value=min_value_2, debounce=True),
131
- dcc.Input(id='max-slider_db2-2', type='number', value=max_value_2, debounce=True),
132
- html.Label("Percent Mitochondrial Genes"),
133
- dcc.RangeSlider(
134
- id='range-slider_db2-3',
135
- step=5,
136
- min=0,
137
- max=100,
138
- value=[min_value_3, max_value_3],
139
- ),
140
- dcc.Input(id='min-slider_db2-3', type='number', value=min_value_3, debounce=True),
141
- dcc.Input(id='max-slider_db2-3', type='number', value=max_value_3, debounce=True),
142
- html.Div([
143
- dcc.Graph(id='pie-graph_db2', figure={}, className='four columns',config=config_fig),
144
- dcc.Graph(id='my-graph_db2', figure={}, clickData=None, hoverData=None,
145
- className='four columns',config=config_fig
146
- ),
147
- dcc.Graph(id='scatter-plot_db2', figure={}, className='four columns',config=config_fig)
148
- ]),
149
- html.Div([
150
- dcc.Graph(id='scatter-plot_db2-2', figure={}, className='four columns',config=config_fig)
151
- ]),
152
- html.Div([
153
- dcc.Graph(id='scatter-plot_db2-3', figure={}, className='four columns',config=config_fig)
154
- ]),
155
- html.Div([
156
- dcc.Graph(id='scatter-plot_db2-4', figure={}, className='four columns',config=config_fig)
157
- ]),
158
- ])
159
 
160
  # Create the second tab content with scatter-plot_db2-5 and scatter-plot_db2-6
161
  tab2_content = html.Div([
@@ -259,6 +259,9 @@ tab3_content = html.Div([
259
 
260
 
261
  tab4_content = html.Div([
 
 
 
262
  html.Div([
263
  html.Label("Multi gene"),
264
  dcc.Dropdown(id='dpdn7', value=["PAX6","TP63","S100A9","KRT5","KRT14","KRT10"], multi=True,
@@ -276,54 +279,54 @@ layout = html.Div([
276
  'font-size': '100%',
277
  'height': 50}, value='tab1',children=[
278
  #dcc.Tab(label='Dataset', value='tab0', children=tab0_content),
279
- dcc.Tab(label='QC', value='tab1', children=tab1_content),
 
280
  dcc.Tab(label='Cell cycle', value='tab2', children=tab2_content),
281
  dcc.Tab(label='Custom', value='tab3', children=tab3_content),
282
- dcc.Tab(label='Multi dot', value='tab4', children=tab4_content),
283
  ]),
284
  ])
285
 
286
  # Define the circular callback
287
- @callback(
288
- Output("min-slider_db2-1", "value"),
289
- Output("max-slider_db2-1", "value"),
290
- Output("min-slider_db2-2", "value"),
291
- Output("max-slider_db2-2", "value"),
292
- Output("min-slider_db2-3", "value"),
293
- Output("max-slider_db2-3", "value"),
294
- Input("min-slider_db2-1", "value"),
295
- Input("max-slider_db2-1", "value"),
296
- Input("min-slider_db2-2", "value"),
297
- Input("max-slider_db2-2", "value"),
298
- Input("min-slider_db2-3", "value"),
299
- Input("max-slider_db2-3", "value"),
300
 
301
- )
302
- def circular_callback(min_1, max_1, min_2, max_2, min_3, max_3):
303
- return min_1, max_1, min_2, max_2, min_3, max_3
304
-
305
- @callback(
306
- Output('range-slider_db2-1', 'value'),
307
- Output('range-slider_db2-2', 'value'),
308
- Output('range-slider_db2-3', 'value'),
309
- Input('min-slider_db2-1', 'value'),
310
- Input('max-slider_db2-1', 'value'),
311
- Input('min-slider_db2-2', 'value'),
312
- Input('max-slider_db2-2', 'value'),
313
- Input('min-slider_db2-3', 'value'),
314
- Input('max-slider_db2-3', 'value'),
315
 
316
- )
317
- def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
318
- return [min_1, max_1], [min_2, max_2], [min_3, max_3]
319
 
320
  @callback(
321
- Output(component_id='my-graph_db2', component_property='figure'),
322
- Output(component_id='pie-graph_db2', component_property='figure'),
323
- Output(component_id='scatter-plot_db2', component_property='figure'),
324
- Output(component_id='scatter-plot_db2-2', component_property='figure'),
325
- Output(component_id='scatter-plot_db2-3', component_property='figure'),
326
- Output(component_id='scatter-plot_db2-4', component_property='figure'), # Add this new scatter plot
327
  Output(component_id='scatter-plot_db2-5', component_property='figure'),
328
  Output(component_id='scatter-plot_db2-6', component_property='figure'),
329
  Output(component_id='scatter-plot_db2-7', component_property='figure'),
@@ -339,44 +342,44 @@ def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
339
  Input(component_id='dpdn5', component_property='value'),
340
  Input(component_id='dpdn6', component_property='value'),
341
  Input(component_id='dpdn7', component_property='value'),
342
- Input(component_id='range-slider_db2-1', component_property='value'),
343
- Input(component_id='range-slider_db2-2', component_property='value'),
344
- Input(component_id='range-slider_db2-3', component_property='value'),
345
 
346
  )
347
 
348
- 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,
349
  batch_chosen = df[col_chosen].unique().to_list()
350
  dff = df.filter(
351
- (pl.col(col_chosen).cast(str).is_in(batch_chosen)) &
352
- (pl.col(col_features) >= range_value_1[0]) &
353
- (pl.col(col_features) <= range_value_1[1]) &
354
- (pl.col(col_counts) >= range_value_2[0]) &
355
- (pl.col(col_counts) <= range_value_2[1]) &
356
- (pl.col(col_mt) >= range_value_3[0]) &
357
- (pl.col(col_mt) <= range_value_3[1])
358
  )
359
 
360
- #Drop categories that are not in the filtered data
361
- dff = dff.with_columns(dff[col_chosen].cast(pl.Categorical))
362
 
363
- dff = dff.sort(col_chosen)
364
 
365
- # Plot figures
366
- fig_violin_db2 = px.violin(data_frame=dff, x=col_chosen, y=col_features, box=True, points="all",
367
- color=col_chosen, hover_name=col_chosen,template="seaborn")
368
 
369
- # Cache commonly used subexpressions
370
- total_count = pl.lit(len(dff))
371
- category_counts = dff.group_by(col_chosen).agg(pl.col(col_chosen).count().alias("count"))
372
- category_counts = category_counts.with_columns(((pl.col("count") / total_count * 100).round(decimals=2)).alias("normalized_count"))
373
 
374
- # Sort the dataframe
375
- #category_counts = category_counts.sort(col_chosen) does not work check if the names are different ...
376
 
377
- # Display the result
378
- total_cells = total_count # Calculate total number of cells
379
- pie_title = f'Percentage of Total Cells: {total_cells}' # Include total cells in the title
380
 
381
  # Calculate the mean expression
382
 
@@ -416,30 +419,30 @@ def update_graph_and_pie_chart(col_chosen, s_chosen, g2m_chosen, condition1_chos
416
  expression_means = expression_means.sort(col_chosen, descending=True)
417
 
418
  #expression_means = expression_means.select(["batch", "Gene", "Expression"] + condition3_chosen)
419
- category_counts = category_counts.sort(col_chosen)
420
 
421
- fig_pie_db2 = px.pie(category_counts, values="normalized_count", names=col_chosen, labels=col_chosen, hole=.3, title=pie_title, template="seaborn")
422
 
423
- #labels = category_counts[col_chosen].to_list()
424
- #values = category_counts["normalized_count"].to_list()
425
 
426
- # Create the scatter plots
427
- fig_scatter_db2 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_chosen,
428
- labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
429
- hover_name='batch',template="seaborn")
430
 
431
- fig_scatter_db2_2 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_mt,
432
- labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
433
- hover_name='batch',template="seaborn")
434
 
435
- fig_scatter_db2_3 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_features,
436
- labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
437
- hover_name='batch',template="seaborn")
438
 
439
 
440
- fig_scatter_db2_4 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_counts,
441
- labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
442
- hover_name='batch',template="seaborn")
443
 
444
  fig_scatter_db2_5 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=s_chosen,
445
  labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
@@ -481,7 +484,7 @@ def update_graph_and_pie_chart(col_chosen, s_chosen, g2m_chosen, condition1_chos
481
  color=condition1_chosen, hover_name=condition1_chosen,template="seaborn")
482
 
483
 
484
- return fig_violin_db2, fig_pie_db2, fig_scatter_db2, fig_scatter_db2_2, fig_scatter_db2_3, fig_scatter_db2_4, 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
485
 
486
  # Set http://localhost:5000/ in web browser
487
  # Now create your regular FASTAPI application
 
86
  # return
87
 
88
  #df = pl.read_parquet(filepath, storage_options=storage_options)
89
+ #min_value = df[col_features].min()
90
+ #max_value = df[col_features].max()
91
 
92
+ #min_value_2 = df[col_counts].min()
93
+ #min_value_2 = round(min_value_2)
94
+ #max_value_2 = df[col_counts].max()
95
+ #max_value_2 = round(max_value_2)
96
 
97
+ #min_value_3 = df[col_mt].min()
98
+ #min_value_3 = round(min_value_3, 1)
99
+ #max_value_3 = df[col_mt].max()
100
+ #max_value_3 = round(max_value_3, 1)
101
 
102
  # Loads in the conditions specified in the yaml file
103
 
 
107
  # Create the first tab content
108
  # Add Sliders for three QC params: N genes by counts, total amount of reads and pct MT reads
109
 
110
+ # tab1_content = html.Div([
111
+ # html.Label("Column chosen"),
112
+ # dcc.Dropdown(id='dpdn2', value="batch", multi=False,
113
+ # options=df.columns),
114
+ # html.Label("N Genes by Counts"),
115
+ # dcc.RangeSlider(
116
+ # id='range-slider_db2-1',
117
+ # step=250,
118
+ # value=[min_value, max_value],
119
+ # marks={i: str(i) for i in range(min_value, max_value + 1, 250)},
120
+ # ),
121
+ # dcc.Input(id='min-slider_db2-1', type='number', value=min_value, debounce=True),
122
+ # dcc.Input(id='max-slider_db2-1', type='number', value=max_value, debounce=True),
123
+ # html.Label("Total Counts"),
124
+ # dcc.RangeSlider(
125
+ # id='range-slider_db2-2',
126
+ # step=7500,
127
+ # value=[min_value_2, max_value_2],
128
+ # marks={i: str(i) for i in range(min_value_2, max_value_2 + 1, 7500)},
129
+ # ),
130
+ # dcc.Input(id='min-slider_db2-2', type='number', value=min_value_2, debounce=True),
131
+ # dcc.Input(id='max-slider_db2-2', type='number', value=max_value_2, debounce=True),
132
+ # html.Label("Percent Mitochondrial Genes"),
133
+ # dcc.RangeSlider(
134
+ # id='range-slider_db2-3',
135
+ # step=5,
136
+ # min=0,
137
+ # max=100,
138
+ # value=[min_value_3, max_value_3],
139
+ # ),
140
+ # dcc.Input(id='min-slider_db2-3', type='number', value=min_value_3, debounce=True),
141
+ # dcc.Input(id='max-slider_db2-3', type='number', value=max_value_3, debounce=True),
142
+ # html.Div([
143
+ # dcc.Graph(id='pie-graph_db2', figure={}, className='four columns',config=config_fig),
144
+ # dcc.Graph(id='my-graph_db2', figure={}, clickData=None, hoverData=None,
145
+ # className='four columns',config=config_fig
146
+ # ),
147
+ # dcc.Graph(id='scatter-plot_db2', figure={}, className='four columns',config=config_fig)
148
+ # ]),
149
+ # html.Div([
150
+ # dcc.Graph(id='scatter-plot_db2-2', figure={}, className='four columns',config=config_fig)
151
+ # ]),
152
+ # html.Div([
153
+ # dcc.Graph(id='scatter-plot_db2-3', figure={}, className='four columns',config=config_fig)
154
+ # ]),
155
+ # html.Div([
156
+ # dcc.Graph(id='scatter-plot_db2-4', figure={}, className='four columns',config=config_fig)
157
+ # ]),
158
+ # ])
159
 
160
  # Create the second tab content with scatter-plot_db2-5 and scatter-plot_db2-6
161
  tab2_content = html.Div([
 
259
 
260
 
261
  tab4_content = html.Div([
262
+ html.Label("Column chosen"),
263
+ dcc.Dropdown(id='dpdn2', value="batch", multi=False,
264
+ options=df.columns),
265
  html.Div([
266
  html.Label("Multi gene"),
267
  dcc.Dropdown(id='dpdn7', value=["PAX6","TP63","S100A9","KRT5","KRT14","KRT10"], multi=True,
 
279
  'font-size': '100%',
280
  'height': 50}, value='tab1',children=[
281
  #dcc.Tab(label='Dataset', value='tab0', children=tab0_content),
282
+ #dcc.Tab(label='QC', value='tab1', children=tab1_content),
283
+ dcc.Tab(label='Multi dot', value='tab4', children=tab4_content),
284
  dcc.Tab(label='Cell cycle', value='tab2', children=tab2_content),
285
  dcc.Tab(label='Custom', value='tab3', children=tab3_content),
 
286
  ]),
287
  ])
288
 
289
  # Define the circular callback
290
+ #@callback(
291
+ #Output("min-slider_db2-1", "value"),
292
+ #Output("max-slider_db2-1", "value"),
293
+ #Output("min-slider_db2-2", "value"),
294
+ #Output("max-slider_db2-2", "value"),
295
+ #Output("min-slider_db2-3", "value"),
296
+ #Output("max-slider_db2-3", "value"),
297
+ #Input("min-slider_db2-1", "value"),
298
+ #Input("max-slider_db2-1", "value"),
299
+ #Input("min-slider_db2-2", "value"),
300
+ #Input("max-slider_db2-2", "value"),
301
+ #Input("min-slider_db2-3", "value"),
302
+ #Input("max-slider_db2-3", "value"),
303
 
304
+ #)
305
+ # def circular_callback(min_1, max_1, min_2, max_2, min_3, max_3):
306
+ # return min_1, max_1, min_2, max_2, min_3, max_3
307
+
308
+ # @callback(
309
+ # Output('range-slider_db2-1', 'value'),
310
+ # Output('range-slider_db2-2', 'value'),
311
+ # Output('range-slider_db2-3', 'value'),
312
+ # Input('min-slider_db2-1', 'value'),
313
+ # Input('max-slider_db2-1', 'value'),
314
+ # Input('min-slider_db2-2', 'value'),
315
+ # Input('max-slider_db2-2', 'value'),
316
+ # Input('min-slider_db2-3', 'value'),
317
+ # Input('max-slider_db2-3', 'value'),
318
 
319
+ # )
320
+ # def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
321
+ # return [min_1, max_1], [min_2, max_2], [min_3, max_3]
322
 
323
  @callback(
324
+ #Output(component_id='my-graph_db2', component_property='figure'),
325
+ #Output(component_id='pie-graph_db2', component_property='figure'),
326
+ #Output(component_id='scatter-plot_db2', component_property='figure'),
327
+ #Output(component_id='scatter-plot_db2-2', component_property='figure'),
328
+ #Output(component_id='scatter-plot_db2-3', component_property='figure'),
329
+ #Output(component_id='scatter-plot_db2-4', component_property='figure'), # Add this new scatter plot
330
  Output(component_id='scatter-plot_db2-5', component_property='figure'),
331
  Output(component_id='scatter-plot_db2-6', component_property='figure'),
332
  Output(component_id='scatter-plot_db2-7', component_property='figure'),
 
342
  Input(component_id='dpdn5', component_property='value'),
343
  Input(component_id='dpdn6', component_property='value'),
344
  Input(component_id='dpdn7', component_property='value'),
345
+ #Input(component_id='range-slider_db2-1', component_property='value'),
346
+ #Input(component_id='range-slider_db2-2', component_property='value'),
347
+ #Input(component_id='range-slider_db2-3', component_property='value'),
348
 
349
  )
350
 
351
+ 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,
352
  batch_chosen = df[col_chosen].unique().to_list()
353
  dff = df.filter(
354
+ (pl.col(col_chosen).cast(str).is_in(batch_chosen)) #&
355
+ #(pl.col(col_features) >= range_value_1[0]) &
356
+ #(pl.col(col_features) <= range_value_1[1]) &
357
+ #(pl.col(col_counts) >= range_value_2[0]) &
358
+ #(pl.col(col_counts) <= range_value_2[1]) &
359
+ #(pl.col(col_mt) >= range_value_3[0]) &
360
+ #(pl.col(col_mt) <= range_value_3[1])
361
  )
362
 
363
+ # #Drop categories that are not in the filtered data
364
+ # dff = dff.with_columns(dff[col_chosen].cast(pl.Categorical))
365
 
366
+ # dff = dff.sort(col_chosen)
367
 
368
+ # # Plot figures
369
+ # fig_violin_db2 = px.violin(data_frame=dff, x=col_chosen, y=col_features, box=True, points="all",
370
+ # color=col_chosen, hover_name=col_chosen,template="seaborn")
371
 
372
+ # # Cache commonly used subexpressions
373
+ # total_count = pl.lit(len(dff))
374
+ # category_counts = dff.group_by(col_chosen).agg(pl.col(col_chosen).count().alias("count"))
375
+ # category_counts = category_counts.with_columns(((pl.col("count") / total_count * 100).round(decimals=2)).alias("normalized_count"))
376
 
377
+ # # Sort the dataframe
378
+ # #category_counts = category_counts.sort(col_chosen) does not work check if the names are different ...
379
 
380
+ # # Display the result
381
+ # total_cells = total_count # Calculate total number of cells
382
+ # pie_title = f'Percentage of Total Cells: {total_cells}' # Include total cells in the title
383
 
384
  # Calculate the mean expression
385
 
 
419
  expression_means = expression_means.sort(col_chosen, descending=True)
420
 
421
  #expression_means = expression_means.select(["batch", "Gene", "Expression"] + condition3_chosen)
422
+ # category_counts = category_counts.sort(col_chosen)
423
 
424
+ # fig_pie_db2 = px.pie(category_counts, values="normalized_count", names=col_chosen, labels=col_chosen, hole=.3, title=pie_title, template="seaborn")
425
 
426
+ # #labels = category_counts[col_chosen].to_list()
427
+ # #values = category_counts["normalized_count"].to_list()
428
 
429
+ # # Create the scatter plots
430
+ # fig_scatter_db2 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_chosen,
431
+ # labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
432
+ # hover_name='batch',template="seaborn")
433
 
434
+ # fig_scatter_db2_2 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_mt,
435
+ # labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
436
+ # hover_name='batch',template="seaborn")
437
 
438
+ # fig_scatter_db2_3 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_features,
439
+ # labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
440
+ # hover_name='batch',template="seaborn")
441
 
442
 
443
+ # fig_scatter_db2_4 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=col_counts,
444
+ # labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
445
+ # hover_name='batch',template="seaborn")
446
 
447
  fig_scatter_db2_5 = px.scatter(data_frame=dff, x='X_umap-0', y='X_umap-1', color=s_chosen,
448
  labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
 
484
  color=condition1_chosen, hover_name=condition1_chosen,template="seaborn")
485
 
486
 
487
+ 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 #fig_violin_db2, fig_pie_db2, fig_scatter_db2, fig_scatter_db2_2, fig_scatter_db2_3, fig_scatter_db2_4,
488
 
489
  # Set http://localhost:5000/ in web browser
490
  # Now create your regular FASTAPI application