Arts-of-coding commited on
Commit
3d886b4
·
verified ·
1 Parent(s): c85b5fd

Update dash_plotly_QC_scRNA.py

Browse files
Files changed (1) hide show
  1. dash_plotly_QC_scRNA.py +21 -21
dash_plotly_QC_scRNA.py CHANGED
@@ -77,8 +77,8 @@ max_value_3 = round(max_value_3, 1)
77
  # Add Sliders for three QC params: N genes by counts, total amount of reads and pct MT reads
78
 
79
  tab1_content = html.Div([
80
- #dcc.Dropdown(id='dpdn2', value=conditions, multi=True,
81
- # options=conditions),
82
  html.Label("N Genes by Counts"),
83
  dcc.RangeSlider(
84
  id='range-slider-1',
@@ -287,7 +287,7 @@ def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
287
  Output(component_id='scatter-plot-11', component_property='figure'),
288
  Output(component_id='scatter-plot-12', component_property='figure'),
289
  Output(component_id='my-graph2', component_property='figure'),
290
- #Input(component_id='dpdn2', component_property='value'),
291
  Input(component_id='dpdn3', component_property='value'),
292
  Input(component_id='dpdn4', component_property='value'),
293
  Input(component_id='dpdn5', component_property='value'),
@@ -298,10 +298,10 @@ def update_slider_values(min_1, max_1, min_2, max_2, min_3, max_3):
298
  Input(component_id='range-slider-3', component_property='value')
299
  )
300
 
301
- def update_graph_and_pie_chart(s_chosen, g2m_chosen, condition1_chosen, condition2_chosen, condition3_chosen, range_value_1, range_value_2, range_value_3): #batch_chosen,
302
- batch_chosen = df[condition1_chosen].unique().to_list()
303
  dff = df.filter(
304
- (pl.col(condition1_chosen).cast(str).is_in(batch_chosen)) &
305
  (pl.col(col_features) >= range_value_1[0]) &
306
  (pl.col(col_features) <= range_value_1[1]) &
307
  (pl.col(col_counts) >= range_value_2[0]) &
@@ -311,19 +311,19 @@ def update_graph_and_pie_chart(s_chosen, g2m_chosen, condition1_chosen, conditio
311
  )
312
 
313
  #Drop categories that are not in the filtered data
314
- dff = dff.with_columns(dff[condition1_chosen].cast(pl.Categorical))
315
 
316
  # Plot figures
317
- fig_violin = px.violin(data_frame=dff, x=condition1_chosen, y=col_features, box=True, points="all",
318
- color=condition1_chosen, hover_name=condition1_chosen,template="seaborn")
319
 
320
  # Cache commonly used subexpressions
321
  total_count = pl.lit(len(dff))
322
- category_counts = dff.group_by(condition1_chosen).agg(pl.col(condition1_chosen).count().alias("count"))
323
  category_counts = category_counts.with_columns(((pl.col("count") / total_count * 100).round(decimals=2)).alias("normalized_count"))
324
 
325
  # Display the result
326
- labels = category_counts[condition1_chosen].to_list()
327
  values = category_counts["normalized_count"].to_list()
328
 
329
  total_cells = total_count # Calculate total number of cells
@@ -334,22 +334,22 @@ def update_graph_and_pie_chart(s_chosen, g2m_chosen, condition1_chosen, conditio
334
  # Melt wide format DataFrame into long format
335
  # Specify batch column as string type and gene columns as float type
336
  list_conds = condition3_chosen
337
- list_conds += [condition1_chosen]
338
  dff_pre = dff.select(list_conds)
339
 
340
  # Melt wide format DataFrame into long format
341
- dff_long = dff_pre.melt(id_vars=condition1_chosen, variable_name="Gene", value_name="Mean expression")
342
 
343
  # Calculate the mean expression levels for each gene in each region
344
- expression_means = dff_long.lazy().group_by([condition1_chosen, "Gene"]).agg(pl.mean("Mean expression")).collect()
345
 
346
  # Calculate the percentage total expressed
347
- dff_long1 = dff_pre.melt(id_vars=condition1_chosen, variable_name="Gene")#.group_by(pl.all()).agg(pl.len())
348
  count = 1
349
  dff_long2 = dff_long1.with_columns(pl.lit(count).alias("len"))
350
- dff_long3 = dff_long2.filter(pl.col("value") > 0).group_by([condition1_chosen, "Gene"]).agg(pl.sum("len").alias("len"))
351
- dff_long4 = dff_long2.group_by([condition1_chosen, "Gene"]).agg(pl.sum("len").alias("total"))
352
- dff_5 = dff_long4.join(dff_long3, on=[condition1_chosen,"Gene"], how="outer")
353
  result = dff_5.select([
354
  pl.when((pl.col('len').is_not_null()) & (pl.col('total').is_not_null()))
355
  .then(pl.col('len') / pl.col('total')*100)
@@ -357,11 +357,11 @@ def update_graph_and_pie_chart(s_chosen, g2m_chosen, condition1_chosen, conditio
357
  ])
358
  result = result.with_columns(pl.col("%").fill_null(100))
359
  dff_5[["percentage"]] = result[["%"]]
360
- dff_5 = dff_5.select(pl.col(condition1_chosen,"Gene","percentage"))
361
 
362
  # Final part to join the percentage expressed and mean expression levels
363
  # TO DO
364
- expression_means = expression_means.join(dff_5, on=[condition1_chosen,"Gene"], how="inner")
365
 
366
  #expression_means = expression_means.select(["batch", "Gene", "Expression"] + condition3_chosen)
367
 
@@ -418,7 +418,7 @@ def update_graph_and_pie_chart(s_chosen, g2m_chosen, condition1_chosen, conditio
418
  #labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
419
  hover_name='batch',template="seaborn")
420
 
421
- fig_violin2 = px.violin(data_frame=dff, x=condition1_chosen, y=condition2_chosen, box=True, points="all",
422
  color=condition1_chosen, hover_name=condition1_chosen,template="seaborn")
423
 
424
 
 
77
  # Add Sliders for three QC params: N genes by counts, total amount of reads and pct MT reads
78
 
79
  tab1_content = html.Div([
80
+ dcc.Dropdown(id='dpdn2', value="batch", multi=False,
81
+ options=df.columns),
82
  html.Label("N Genes by Counts"),
83
  dcc.RangeSlider(
84
  id='range-slider-1',
 
287
  Output(component_id='scatter-plot-11', component_property='figure'),
288
  Output(component_id='scatter-plot-12', component_property='figure'),
289
  Output(component_id='my-graph2', component_property='figure'),
290
+ Input(component_id='dpdn2', component_property='value'),
291
  Input(component_id='dpdn3', component_property='value'),
292
  Input(component_id='dpdn4', component_property='value'),
293
  Input(component_id='dpdn5', component_property='value'),
 
298
  Input(component_id='range-slider-3', component_property='value')
299
  )
300
 
301
+ 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,
302
+ batch_chosen = df[col_chosen].unique().to_list()
303
  dff = df.filter(
304
+ (pl.col(col_chosen).cast(str).is_in(batch_chosen)) &
305
  (pl.col(col_features) >= range_value_1[0]) &
306
  (pl.col(col_features) <= range_value_1[1]) &
307
  (pl.col(col_counts) >= range_value_2[0]) &
 
311
  )
312
 
313
  #Drop categories that are not in the filtered data
314
+ dff = dff.with_columns(dff[col_chosen].cast(pl.Categorical))
315
 
316
  # Plot figures
317
+ fig_violin = px.violin(data_frame=dff, x=col_chosen, y=col_features, box=True, points="all",
318
+ color=col_chosen, hover_name=col_chosen,template="seaborn")
319
 
320
  # Cache commonly used subexpressions
321
  total_count = pl.lit(len(dff))
322
+ category_counts = dff.group_by(col_chosen).agg(pl.col(col_chosen).count().alias("count"))
323
  category_counts = category_counts.with_columns(((pl.col("count") / total_count * 100).round(decimals=2)).alias("normalized_count"))
324
 
325
  # Display the result
326
+ labels = category_counts[col_chosen].to_list()
327
  values = category_counts["normalized_count"].to_list()
328
 
329
  total_cells = total_count # Calculate total number of cells
 
334
  # Melt wide format DataFrame into long format
335
  # Specify batch column as string type and gene columns as float type
336
  list_conds = condition3_chosen
337
+ list_conds += [col_chosen]
338
  dff_pre = dff.select(list_conds)
339
 
340
  # Melt wide format DataFrame into long format
341
+ dff_long = dff_pre.melt(id_vars=col_chosen, variable_name="Gene", value_name="Mean expression")
342
 
343
  # Calculate the mean expression levels for each gene in each region
344
+ expression_means = dff_long.lazy().group_by([col_chosen, "Gene"]).agg(pl.mean("Mean expression")).collect()
345
 
346
  # Calculate the percentage total expressed
347
+ dff_long1 = dff_pre.melt(id_vars=col_chosen, variable_name="Gene")#.group_by(pl.all()).agg(pl.len())
348
  count = 1
349
  dff_long2 = dff_long1.with_columns(pl.lit(count).alias("len"))
350
+ dff_long3 = dff_long2.filter(pl.col("value") > 0).group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("len"))
351
+ dff_long4 = dff_long2.group_by([col_chosen, "Gene"]).agg(pl.sum("len").alias("total"))
352
+ dff_5 = dff_long4.join(dff_long3, on=[col_chosen,"Gene"], how="outer")
353
  result = dff_5.select([
354
  pl.when((pl.col('len').is_not_null()) & (pl.col('total').is_not_null()))
355
  .then(pl.col('len') / pl.col('total')*100)
 
357
  ])
358
  result = result.with_columns(pl.col("%").fill_null(100))
359
  dff_5[["percentage"]] = result[["%"]]
360
+ dff_5 = dff_5.select(pl.col(col_chosen,"Gene","percentage"))
361
 
362
  # Final part to join the percentage expressed and mean expression levels
363
  # TO DO
364
+ expression_means = expression_means.join(dff_5, on=[col_chosen,"Gene"], how="inner")
365
 
366
  #expression_means = expression_means.select(["batch", "Gene", "Expression"] + condition3_chosen)
367
 
 
418
  #labels={'X_umap-0': 'umap1' , 'X_umap-1': 'umap2'},
419
  hover_name='batch',template="seaborn")
420
 
421
+ fig_violin2 = px.violin(data_frame=dff, x=col_chosen, y=condition2_chosen, box=True, points="all",
422
  color=condition1_chosen, hover_name=condition1_chosen,template="seaborn")
423
 
424