From 94c5909604325b4aebdbed88e94e277e60d3d1ee Mon Sep 17 00:00:00 2001 From: Luke-Johnson-5 Date: Tue, 20 May 2025 15:48:48 -0400 Subject: [PATCH] multi-core update --- scSNViz/R/individual_snv_plots.R | 29 +++++++++---- scSNViz/R/plot_snv_data.R | 73 ++++++++++++++++++++------------ scSNViz/README.md | 2 +- 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/scSNViz/R/individual_snv_plots.R b/scSNViz/R/individual_snv_plots.R index 7d693cb..31b8a21 100644 --- a/scSNViz/R/individual_snv_plots.R +++ b/scSNViz/R/individual_snv_plots.R @@ -43,6 +43,18 @@ individual_snv_plots <- function(seurat_object, processed_snv, output_dir = NULL, slingshot = T, dimensionality_reduction = "UMAP", dynamic_cell_size = F, save_each_plot = F, enable_integrated = F) { + + #OS checking + sys <- Sys.info()[['sysname']] + if (sys == "Linux" || sys == "Darwin"){ + cores <- readline("How many cores would you like to use?: ") + cores <- as.numeric(cores) + if (cores > detectCores()){ + stop(paste0("Invalid amount of cores, you have "), detectCores(), " cores.") + } + } else { + cores <- 1 + } cat("\nGenerating individual SNV plots...\n") valid_reductions <- c("umap", "pca", "tsne") @@ -279,7 +291,7 @@ individual_snv_plots <- function(seurat_object, processed_snv, output_dir = NULL counter = 0 # function to save plots' json - plots_json <- lapply(snv_options, function(snv) { + plots_json <- mclapply(snv_options, function(snv) { plots <- generate_snv_plots(snv, title_color = 'blue') list( VAF = list( @@ -294,7 +306,7 @@ individual_snv_plots <- function(seurat_object, processed_snv, output_dir = NULL id = paste0("plot_N_REF_", gsub(":", "_", snv)), json = plotly::plotly_json(plots[['N_REF']], jsonedit = F) ) - )} + )}, mc.cores = cores ) plots_json <- unlist(plots_json, recursive = F) @@ -320,12 +332,13 @@ individual_snv_plots <- function(seurat_object, processed_snv, output_dir = NULL saveWidget(as_widget(plot), file = file_path, selfcontained = F, libdir = "lib") } - for (snv in snv_options) { - plots <- generate_snv_plots(snv, title_color = "black") - save_snv_plot(plots[["VAF"]], snv, "VAF") - save_snv_plot(plots[['N_VAR']], snv, "N_VAR") - save_snv_plot(plots[['N_REF']], snv, "N_REF") - } + mclapply(snv_options, function(snv) { + plots <- generate_snv_plots(snv, title_color = "black") + save_snv_plot(plots[["VAF"]], snv, "VAF") + save_snv_plot(plots[["N_VAR"]], snv, "N_VAR") + save_snv_plot(plots[["N_REF"]], snv, "N_REF") + }, mc.cores = cores) + } ind_snv_out <- list() diff --git a/scSNViz/R/plot_snv_data.R b/scSNViz/R/plot_snv_data.R index 9f1714e..42aa4c0 100644 --- a/scSNViz/R/plot_snv_data.R +++ b/scSNViz/R/plot_snv_data.R @@ -82,6 +82,17 @@ plot_snv_data <- function(seurat_object, processed_snv, aggregated_snv, plot_dat if (slingshot==TRUE){print('Turning off the slingshot option - this is not available for integrated objects')} if (include_copykat==TRUE){print('Turning off the copykat option - this is not available for integrated objects')} } + #os checking + sys <- Sys.info()[['sysname']] + if (sys == "Linux" || sys == "Darwin"){ + cores <- readline("How many cores would you like to use?") + cores <- as.numeric(cores) + if (cores > detectCores()){ + stop(paste0("Invalid amount of cores, you have "), detectCores(), " cores.") + } + } else { + cores <- 1 + } valid_reductions <- c("umap", "pca", "tsne") dimensionality_reduction <- tolower(dimensionality_reduction) @@ -278,35 +289,43 @@ plot_snv_data <- function(seurat_object, processed_snv, aggregated_snv, plot_dat #plots <- list() - for (metric in names(plot_titles)) { - if (!metric %in% colnames(plot_data)) - next - - plot <- generate_plot( - metric = metric, - plot_data = plot_data, - dim_plotting = dim_plotting, - color_scale = color_scale, - reversescale_option = reversescale_option, - cell_border = cell_border, - color_undetected = color_undetected, - curves = curves, - disable_3d_axis = disable_3d_axis, - title = plot_titles[[metric]] #(duplicate title fix) - ) +generate_and_save_plot <- function(metric) { + if (!metric %in% colnames(plot_data)) + return(NULL) + + plot <- generate_plot( + metric = metric, + plot_data = plot_data, + dim_plotting = dim_plotting, + color_scale = color_scale, + reversescale_option = reversescale_option, + cell_border = cell_border, + color_undetected = color_undetected, + curves = curves, + disable_3d_axis = disable_3d_axis, + title = plot_titles[[metric]] + ) + + plot_name <- plot_titles[[metric]] + file_title <- gsub("[^a-zA-Z0-9]", "_", plot_name) - plot_name <- plot_titles[[metric]] - plots[[plot_name]] <- plot - #plots[[metric]] <- plot (changed naming of the plots in the output for easier matching in generate_report) + if (save_each_plot && !is.null(output_dir)) { + saveWidget(as_widget(plot), file = file.path( + output_dir, "SNV_data_plots", paste0(file_title, ".html")), + selfcontained = FALSE, libdir = "lib") + } - file_title <- gsub("[^a-zA-Z0-9]", "_", plot_titles[[metric]]) + return(list(name = plot_name, plot = plot)) +} - if (save_each_plot && !is.null(output_dir)) { - saveWidget(as_widget(plot), file = file.path( - output_dir, "SNV_data_plots", paste0(file_title, ".html")), - selfcontained = F, libdir = "lib") - } - } +# Parallel here +results <- mclapply(names(plot_titles), generate_and_save_plot, mc.cores = cores) +filtered_results <- Filter(Negate(is.null), results) +new_plots <- setNames( + lapply(filtered_results, `[[`, "plot"), + sapply(filtered_results, `[[`, "name") +) +plots <- c(plots, new_plots) # INTEGRATED ORIG IDENT PLOT @@ -544,7 +563,7 @@ plot_snv_data <- function(seurat_object, processed_snv, aggregated_snv, plot_dat options(bitmapType = "cairo") cat("Running CopyKat. This may take a while...\n") cts <- GetAssayData(seurat_object, layer = "counts", assay = "SCT") - ckt <- copykat(cts, sam.name = "sample_name") + ckt <- copykat(cts, sam.name = "sample_name", n.cores = cores) options(bitmapType = "C_X11") seurat_object[["karyotype"]] <- "Unknown" seurat_object[["karyotype"]][rownames(ckt$pred), ] <- ckt$pred[, 2] diff --git a/scSNViz/README.md b/scSNViz/README.md index f82eb25..1ef00a5 100644 --- a/scSNViz/README.md +++ b/scSNViz/README.md @@ -28,7 +28,7 @@ install_github("HorvathLab/NGS", ref = "scSNViz_R_v1.0.0", subdir = "scSNViz") The input files are located in the input folder on github. The snv file is an output from SCReadCounts. The user may provide a .tsv file that is not from SCReadCounts as long as it is also a .tsv and contains the following columns: CHROM, POS, REF, ALT, ReadGroup, SNVCount, RefCount. ``` load.lib<-c("scSNViz","SingleCellExperiment", "stringr", "HGNChelper", "Matrix", "umap", "Rtsne", "Seurat", "sctransform", "ggplot2", "readr", - "dplyr", "plotly", "htmlwidgets", "htmltools", "jsonlite", "glmGamPoi", "slingshot", "listviewer","openxlsx","randomcoloR") # the installation of ("glmGamPoi") is highly recommended + "dplyr", "plotly", "htmlwidgets", "htmltools", "jsonlite", "glmGamPoi", "slingshot", "listviewer","openxlsx","randomcoloR", "parallel") # the installation of ("glmGamPoi") is highly recommended install.lib <- load.lib[!load.lib %in% installed.packages()] for(lib in install.lib) install.packages(lib,dependencies=TRUE)