Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions scSNViz/R/individual_snv_plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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()
Expand Down
73 changes: 46 additions & 27 deletions scSNViz/R/plot_snv_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion scSNViz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading