From d9b2e3292d83b5014bc76a7ad3af9a3bf9dc1d42 Mon Sep 17 00:00:00 2001 From: "abel.vertesy" Date: Wed, 11 Oct 2023 12:21:04 +0200 Subject: [PATCH] ... --- Development/Create_the_Seurat.utils_Package.R | 3 +- R/Seurat.Utils.R | 3 +- R/Test.R | 84 +++++++++++++++++++ Seurat.utils.Rproj | 5 ++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 R/Test.R diff --git a/Development/Create_the_Seurat.utils_Package.R b/Development/Create_the_Seurat.utils_Package.R index dc848e0..cb59c9f 100644 --- a/Development/Create_the_Seurat.utils_Package.R +++ b/Development/Create_the_Seurat.utils_Package.R @@ -1,7 +1,7 @@ ###################################################################################################### # Create_the_Seurat.utils_Package.R ###################################################################################################### -# source("/Users/abel.vertesy/GitHub/Packages/Seurat.utils/Development/Create_the_Seurat.utils_Package.v0.1.R") +# source("~/GitHub/Packages/Seurat.utils/Development/Create_the_Seurat.utils_Package.v0.1.R") # rm(list = ls(all.names = TRUE)); try(dev.off(), silent = TRUE) @@ -58,6 +58,7 @@ if ( !dir.exists(RepositoryDir) ) { create(path = RepositoryDir, description = D } + # go and write fun's ------------------------------------------------------------------------ # file.edit(Package_FnP) diff --git a/R/Seurat.Utils.R b/R/Seurat.Utils.R index b46cbd1..0ac394a 100644 --- a/R/Seurat.Utils.R +++ b/R/Seurat.Utils.R @@ -1195,7 +1195,7 @@ recall.parameters <- function(obj = combined.obj, overwrite = FALSE) { obj <- ww.get.1st.Seur.element(obj) if ('p' %in% names(obj@misc)) { - if (!exists('p')) iprint("variable 'p' exits in the global namespace:", head(p)) + if (exists('p')) iprint("variable 'p' exits in the global namespace:", head(p)) if (!exists('p') | (exists('p') & overwrite == TRUE) ) { MarkdownHelpers::ww.assign_to_global(name = "p", value = obj@misc$'p'); print("Overwritten.") @@ -3048,6 +3048,7 @@ multiFeaturePlot.A4 <- function(list.of.genes # Save multiple FeaturePlots, as j , saveGeneList = FALSE , w = wA4, h = hA4, scaling = 1 , format = c('jpg', 'pdf', 'png')[1] + # , solo = MarkdownHelpers::FALSE.unless('b.plot.solo') , raster = MarkdownHelpers::FALSE.unless('b.raster') , raster.dpi = c(512, 512)/4 , ... diff --git a/R/Test.R b/R/Test.R new file mode 100644 index 0000000..411f285 --- /dev/null +++ b/R/Test.R @@ -0,0 +1,84 @@ + + +plot.qUMAPs.in.a.folder <- function(genes, obj = combined.obj, foldername = NULL, intersectionAssay = 'RNA', plot.reduction = 'umap', ...) { + + ParentDir = OutDir + if (is.null(foldername)) foldername = substitute(genes) + create_set_SubDir( paste0(foldername,'-', plot.reduction),'/') + list.of.genes.found = check.genes(list.of.genes = genes, obj = obj, assay.slot = intersectionAssay, makeuppercase = F) + + + for (g in list.of.genes.found) { + qUMAP(g, reduction = plot.reduction, ...) + } + MarkdownReports::create_set_OutDir(... = ParentDir) +} + + + +multiFeaturePlot.A4 <- function(list.of.genes # Save multiple FeaturePlots, as jpeg, on A4 for each gene, which are stored as a list of gene names. + , obj = combined.obj + , foldername = substitute(list.of.genes), plot.reduction='umap' + , intersectionAssay = c('RNA', 'integrated')[1] + , layout = c('tall', 'wide', FALSE )[2] + , colors = c("grey", "red"), nr.Col = 2, nr.Row =4, cex = round(0.1/(nr.Col*nr.Row), digits = 2) + , gene.min.exp = 'q01', gene.max.exp = 'q99', subdir =T + , prefix = NULL , suffix = NULL + , background_col = "white" + , aspect.ratio = c(FALSE, 0.6)[2] + , saveGeneList = FALSE + , w = wA4, h = hA4, scaling = 1 + , format = c('jpg', 'pdf', 'png')[1] + , solo = MarkdownHelpers::FALSE.unless('b.solo.plot') + , raster = MarkdownHelpers::FALSE.unless('b.raster') + , raster.dpi = c(512, 512)/4 + , ... + # , jpeg.res = 225, jpeg.q = 90 +) { + tictoc::tic() + ParentDir = OutDir + if (is.null(foldername)) foldername = "genes" + if (subdir) create_set_SubDir( paste0(foldername,'-', plot.reduction),'/') + list.of.genes.found = check.genes(list.of.genes = list.of.genes, obj = obj, assay.slot = intersectionAssay, makeuppercase = F) + DefaultAssay(obj) <- intersectionAssay + + if (layout == 'tall') { w = wA4 * scaling; h = hA4 * scaling; nr.Col = 2; nr.Row = 4; print('layout active, nr.Col ignored.') } + if (layout == 'wide') { w = hA4 * scaling; h = wA4 * scaling; nr.Col = 2; nr.Row = 2; print('layout active, nr.Col ignored.') } + + lsG = iterBy.over(1:length(list.of.genes.found), by = nr.Row * nr.Col) + for (i in 1:length(lsG)) { + genes = list.of.genes.found[lsG[[i]]] + iprint(i,genes ) + plotname = kpp(c(prefix, plot.reduction,i, genes, suffix, format )) + + if (solo) { + + + } else { + plot.list = Seurat::FeaturePlot(object = obj, features = genes, reduction = plot.reduction, combine = F + , ncol = nr.Col, cols = colors + , min.cutoff = gene.min.exp, max.cutoff = gene.max.exp + , raster = TRUE, raster.dpi = raster.dpi + , pt.size = cex, ...) + + for (i in 1:length(plot.list)) { + plot.list[[i]] <- plot.list[[i]] + NoLegend() + NoAxes() + if (aspect.ratio) plot.list[[i]] <- plot.list[[i]] + ggplot2::coord_fixed(ratio = aspect.ratio) + } + + pltGrid <- cowplot::plot_grid(plotlist = plot.list, ncol = nr.Col, nrow = nr.Row ) + ggsave(filename = plotname, width = w, height = h, bg = background_col, plot = pltGrid) + } + } + + + if (subdir) MarkdownReports::create_set_OutDir(... = ParentDir) + if (saveGeneList) { + if (is.null(obj@misc$gene.lists)) obj@misc$gene.lists <- list() + obj@misc$gene.lists[[substitute(list.of.genes)]] <- list.of.genes.found + print("Genes saved under: obj@misc$gene.lists") + return(obj) + } + tictoc::toc() +}; + diff --git a/Seurat.utils.Rproj b/Seurat.utils.Rproj index aaa62a5..69fafd4 100644 --- a/Seurat.utils.Rproj +++ b/Seurat.utils.Rproj @@ -5,8 +5,13 @@ SaveWorkspace: No AlwaysSaveHistory: Default EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 Encoding: UTF-8 +RnwWeave: Sweave +LaTeX: pdfLaTeX + AutoAppendNewline: Yes StripTrailingWhitespace: Yes LineEndingConversion: Posix