Skip to content

Commit

Permalink
error correct
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeiferl committed Aug 1, 2024
1 parent fb6ebfe commit 09684a7
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 19 deletions.
73 changes: 65 additions & 8 deletions R/manual_cell_annotation.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ meta_heatmap_ploting <-
mid = "white",
high = "red",
spatial = FALSE,
dimred = dimred,
make_cell_plot = FALSE,
out_dir = NULL) {
cell_metadata <- metadata(sce)[["clustering_enrichment"]]
Expand Down Expand Up @@ -204,25 +205,28 @@ meta_heatmap_ploting <-
metaDT <- metaDT %>%
dplyr::group_by(variable) %>%
dplyr::mutate(zscores_rescaled_per_gene = c(scales::rescale(zscores, to = c(-1, 1))))
# print(head(metaDT))

# Calculate means
testmain <- metaDT %>%
dplyr::group_by(variable, !!!rlang::syms(main_factor)) %>%
dplyr::summarise(mean_value = mean(value))
main_factor <- clustering

testmain <- metaDT %>%
dplyr::group_by(variable, !!!rlang::syms(clustering)) %>%
dplyr::summarise(mean_value = mean(!!!rlang::syms(show_value)))

# Step 2: Define the dfunction
dfunction <- function(d, col_name1, col_name2, value.var) {
d %>%
tidyr::pivot_wider(names_from = {{ col_name2 }}, values_from = {{ value.var }})
}

# Step 3: Apply dfunction to testmain
testmain_matrix <- dfunction(d = testmain, col_name1 = variable, col_name2 = main_factor, value.var = mean_value)
testmain_matrix <- dfunction(d = testmain, col_name1 = variable, col_name2 = clustering, value.var = mean_value)

testmain_mat <- as.matrix(testmain_matrix[, -1])

rownames(testmain_mat) <- testmain_matrix$variable
# for clusters
## this part is ridiculusely redundant...it is just sorting rows and column based on hierarchic clustering!!!!
#
cormatrix <- stats::cor(x = testmain_mat, method = clus_cor_method)
cordist <- stats::as.dist(1 - cormatrix, diag = T, upper = T)
corclus <- stats::hclust(d = cordist, method = clus_cluster_method)
Expand Down Expand Up @@ -252,7 +256,6 @@ meta_heatmap_ploting <-
metaDT <- metaDT %>%
dplyr::mutate(variable = as.character(variable)) # , levels = values_sort_names))
##
# print(head(metaDT))

pl <- ggplot2::ggplot()
pl <- pl + ggplot2::geom_tile(
Expand Down Expand Up @@ -327,7 +330,61 @@ meta_heatmap_ploting <-
anot_plot_out_pdf_file = out_pdf_file,
anot_plot_out_png_file = out_png_file
)


p <- plotReducedDim_mod(
sce = sce,
dimred = dimred,
colour_by = glue::glue("manual_annotation_{clustering}"),
text_by = glue::glue("manual_annotation_{clustering}"),
title = glue::glue("manual_annotation_{clustering}_dimred.pdf"),
use_default_ggplot_palette = TRUE,
legend_title = "Cluster"
)

out_pdf_file <-
fs::path(out_dir,
glue::glue("manual_annotation_{clustering}_dimred.pdf"))
out_png_file <- out_pdf_file
fs::path_ext(out_png_file) <- "png"
pl <- tryCatch({
scdrake::save_pdf(list(p), out_pdf_file, stop_on_error = TRUE)
ggplot2::ggsave(
filename = out_png_file,
plot = p,
device = "png",
dpi = 300
)
pl
},
error = function(e) {
if (stringr::str_detect(e$message, "Viewport has zero dimension")) {
cli_alert_warning(
str_space(
"Error catched: 'Viewport has zero dimension(s)'.",
"There are probably too many levels and the legend doesn't fit into the plot.",
"Removing the legend before saving the plot image."
)
)
p <- p + theme(legend.position = "none")
scdrake::save_pdf(list(p), out_pdf_file)
ggplot2::ggsave(
filename = out_png_file,
plot = p,
device = "png",
dpi = 150
)
p
} else {
cli::cli_abort(e$message)
}
})
dimred_par <- tibble::tibble(
title = as.character(glue::glue("manual_annotation_{clustering}_dimred.pdf")),
anot_plot = list(p),
anot_plot_out_pdf_file = out_pdf_file,
anot_plot_out_png_file = out_png_file
)
par <- rbind(par, dimred_par)
if (spatial) {
man_anot_plot <- visualized_spots(sce,
cell_color = glue::glue("manual_annotation_{clustering}"),
Expand Down
1 change: 1 addition & 0 deletions R/plans_single_sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ get_norm_clustering_subplan <- function(cfg, cfg_pipeline, cfg_main) {
show_value = !!cfg$SHOW_VALUE,
out_dir = !!cfg$NORM_CLUSTERING_OTHER_PLOTS_OUT_DIR,
spatial = !!cfg$SPATIAL,
dimred = !!cfg$HEATMAP_DIMRED,
make_cell_plot = !!cfg$MAKE_CELL_PLOT
)
)
Expand Down
2 changes: 1 addition & 1 deletion R/single_sample_norm_clustering.R
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ sce_norm_hvg_fn <- function(sce_norm,
nfeatures = hvg_selection_value
)
svg_ids <- Seurat::SVFInfo(seu_sce_norm, selection.method = "moransi", )
svg_ids <- rownames(result[result$moransi.spatially.variable == "TRUE", ])
svg_ids <- rownames(svg_ids[svg_ids$moransi.spatially.variable == "TRUE", ])

hvg_ids <- unique(c(hvg_ids, svg_ids))
}
Expand Down
2 changes: 1 addition & 1 deletion R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ dimred_plots_clustering_fn <- function(sce_dimred,
palete <- c(scales::hue_pal()(par$n_clusters))
p_spat <- visualized_spots(sce_add_colData(sce_dimred, cell_data),
cell_color = par$sce_column, color_as_factor = F,
point_shape = border, cell_color_code = palete, show_legend = F
point_shape = "border", cell_color_code = palete, show_legend = F
)
p <- cowplot::plot_grid(p, p_spat, ncol = 2, nrow = 1, rel_widths = c(1, 1.5))
}
Expand Down
11 changes: 6 additions & 5 deletions inst/Rmd/single_sample/02_norm_clustering.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,16 @@ if (isTRUE(cfg$MANUAL_ANNOTATION)) {
}
cat("\n\n")
if (isTRUE(cfg$SPATIAL)) {
scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[2],text = "**Cell annotation plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[3],text = "**Cell annotation plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
cat("\n\n")
scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[3],text = "**Enrichment cells plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
scdrake::create_a_link(href = drake::readd(plot_annotation)$anot_plot_out_pdf_file[4],text = "**Enrichment cells plot**", href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
cat("\n\n")
scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[1],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[1],img_width = "500px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE))
}
scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[1],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[1],img_width = "450px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
}
scdrake::create_img_link(drake::readd(plot_annotation)$anot_plot_out_pdf_file[2],img_src = drake::readd(plot_annotation)$anot_plot_out_png_file[2],img_width = "450px",href_rel_start = fs::path_dir(cfg$NORM_CLUSTERING_REPORT_HTML_FILE),do_cat = TRUE)
```


Expand Down
2 changes: 1 addition & 1 deletion inst/config/single_sample/01_input_qc.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ INPUT_DATA:
target_name: "target_name"

INPUT_QC_REPORT_RMD_FILE: "Rmd/single_sample/01_input_qc.Rmd"
#INPUT_QC_REPORT_RMD_FILE: "Rmd/single_saple/01_input_qc_spatial.Rmd"
#INPUT_QC_REPORT_RMD_FILE: "Rmd/single_sample/01_input_qc_spatial.Rmd"
###############################################################################

### Spatial experiment ########################################################
Expand Down
7 changes: 4 additions & 3 deletions inst/config/single_sample/02_norm_clustering.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ CELL_GROUPINGS: null

### Manual cell annotation signatures #########################################
MANUAL_ANNOTATION: False
ANNOTATION_MARKERS: null
ANNOTATION_MARKERS: "input_data/brain_markers.csv"
SCALE_ANNOTATION: False
OVERLAP: 5
ANNOTATION_CLUSTERING: null
ANNOTATION_CLUSTERING: "cluster_kmeans_k4"
SHOW_VALUE: "value"
MAKE_CELL_PLOT: False
MAKE_CELL_PLOT: True
HEATMAP_DIMRED: "umap"
###############################################################################

### Dimensionality reduction plots ############################################
Expand Down

0 comments on commit 09684a7

Please sign in to comment.