Spaces:
Running
Running
Arts-of-coding
commited on
Update dash_plotly_QC_scRNA.py
Browse files- 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 |
-
|
81 |
-
|
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 |
-
|
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[
|
303 |
dff = df.filter(
|
304 |
-
(pl.col(
|
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[
|
315 |
|
316 |
# Plot figures
|
317 |
-
fig_violin = px.violin(data_frame=dff, x=
|
318 |
-
color=
|
319 |
|
320 |
# Cache commonly used subexpressions
|
321 |
total_count = pl.lit(len(dff))
|
322 |
-
category_counts = dff.group_by(
|
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[
|
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 += [
|
338 |
dff_pre = dff.select(list_conds)
|
339 |
|
340 |
# Melt wide format DataFrame into long format
|
341 |
-
dff_long = dff_pre.melt(id_vars=
|
342 |
|
343 |
# Calculate the mean expression levels for each gene in each region
|
344 |
-
expression_means = dff_long.lazy().group_by([
|
345 |
|
346 |
# Calculate the percentage total expressed
|
347 |
-
dff_long1 = dff_pre.melt(id_vars=
|
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([
|
351 |
-
dff_long4 = dff_long2.group_by([
|
352 |
-
dff_5 = dff_long4.join(dff_long3, on=[
|
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(
|
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=[
|
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=
|
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 |
|