From 66d149d02f3fa03f3bc87d39dd34c9e5e9a7b8bb Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Wed, 24 Aug 2022 09:21:11 -0400 Subject: [PATCH 1/6] Untrack .Rproj files --- .gitignore | 1 + DESCRIPTION | 2 +- JoesFlow.Rproj | 18 ------------------ 3 files changed, 2 insertions(+), 19 deletions(-) delete mode 100644 JoesFlow.Rproj diff --git a/.gitignore b/.gitignore index b1f6033..ebd3271 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .Rhistory .RData .Ruserdata +*.Rproj .DS_Store diff --git a/DESCRIPTION b/DESCRIPTION index dcb3ba8..83e2231 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: JoesFlow Title: Joes Flow simplified analysis for single cell modality data -Version: 0.1.0-4 +Version: 0.1.1-1 Authors@R: c(person('Cooper', 'Devlin', email = 'jcooperdevlin@gmail.com', role = c('cre', 'aut')), person('Randy', 'Johnson', email = 'johnsonra@mail.nih.gov', diff --git a/JoesFlow.Rproj b/JoesFlow.Rproj deleted file mode 100644 index eaa6b81..0000000 --- a/JoesFlow.Rproj +++ /dev/null @@ -1,18 +0,0 @@ -Version: 1.0 - -RestoreWorkspace: Default -SaveWorkspace: Default -AlwaysSaveHistory: Default - -EnableCodeIndexing: Yes -UseSpacesForTab: Yes -NumSpacesForTab: 2 -Encoding: UTF-8 - -RnwWeave: Sweave -LaTeX: pdfLaTeX - -BuildType: Package -PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source -PackageRoxygenize: rd,collate,namespace From 4db2cdb62aa39194bb5b2ab62b01ed416241eb94 Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Thu, 25 Aug 2022 14:33:41 -0400 Subject: [PATCH 2/6] Update `clusterJF` functions * Add ability to specify axis names * Change legend titles to match group (not just "Group") --- R/utils.R | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/R/utils.R b/R/utils.R index ee18934..bc6db74 100644 --- a/R/utils.R +++ b/R/utils.R @@ -7,6 +7,7 @@ #' @param colors Vector of colors for each group #' @param xlab x-axis label #' @param ylab y-axis label +#' @param axis_prefix x-and y-axis prefix (e.g. 'UMAP_') #' @param legend.name Character string for the legend name (default is 'Group') #' @param ... Other objects passed along to functions within clusterJF #' @@ -32,7 +33,7 @@ clusterJF.prcomp <- function(clustered_data, ids, meta, colors, legend.name = 'G # get group labels group_by(.data$SampleID) %>% - mutate(Group = meta[unique(.data$SampleID)]) %>% + mutate(Group = as.character(meta[unique(.data$SampleID)])) %>% ungroup() # proportion of variance @@ -49,7 +50,7 @@ clusterJF.prcomp <- function(clustered_data, ids, meta, colors, legend.name = 'G #' @rdname clusterJF #' @method clusterJF matrix #' @export -clusterJF.matrix <- function(clustered_data, ids, meta, colors, legend.name = 'Group', ...){ +clusterJF.matrix <- function(clustered_data, axis_prefix = 'axis', ids, meta, colors, legend.name = 'Group', ...){ # format data for figure plotter <- tibble(X1 = clustered_data[,1], @@ -58,11 +59,11 @@ clusterJF.matrix <- function(clustered_data, ids, meta, colors, legend.name = 'G # get group labels group_by(.data$SampleID) %>% - mutate(Group = meta[unique(.data$SampleID)]) %>% + mutate(Group = as.character(meta[unique(.data$SampleID)])) %>% ungroup() # render figure - clusterJF(plotter, colors, xlab = "UMAP_1", ylab = "UMAP_2", legend.name, ...) + clusterJF(plotter, colors, xlab = paste(axis_prefix, 1, sep = '_'), ylab = paste(axis_prefix, 2, sep = '_'), legend.name, ...) } # method for tibble object (should normally be called from clusterJF.prcomp or clusterJF.matrix) @@ -90,6 +91,7 @@ clusterJF.tbl <- function(clustered_data, colors, xlab, ylab, legend.name, ...){ #' @param meta Character vector containing metadata labels, corresponding to ids #' @param colors1 Vector of colors for samples #' @param colors2 Vector of colors for clusters +#' @param legend.name Character string for the legend name (default is 'Group') #' #' @return A ggplot object #' @export @@ -97,7 +99,7 @@ clusterJF.tbl <- function(clustered_data, colors, xlab, ylab, legend.name, ...){ #' @import ggplot2 #' @importFrom ggrepel geom_label_repel #' @importFrom rlang .data -sb_clusterJF <- function(clustered_data, ids, meta, colors1, colors2) { +sb_clusterJF <- function(clustered_data, ids, meta, colors1, colors2, legend.name = 'Group') { # format data for figure plotter <- tibble(PC1 = clustered_data$x[,'PC1'], @@ -116,7 +118,7 @@ sb_clusterJF <- function(clustered_data, ids, meta, colors1, colors2) { pp1 <- ggplot(plotter, aes(.data$PC1, .data$PC2, color=.data$Group, label=.data$SampleID)) + geom_point() + theme_bw() + geom_label_repel(size = 6) + - scale_color_manual(values=colors1) + + scale_color_manual(values=colors1, name = legend.name) + guides(color = guide_legend(override.aes = list(label = 'O', size = 3))) + xlab(paste0("PC1 (Explained Variance ", round(PoV[1],4)*100, "%)")) + ylab(paste0("PC2 (Explained Variance ", round(PoV[2],4)*100, "%)")) + @@ -135,7 +137,7 @@ sb_clusterJF <- function(clustered_data, ids, meta, colors1, colors2) { pp2 <- ggplot(plotter, aes(.data$PC1, .data$PC2, color=.data$Label, label=.data$Label)) + geom_point() + theme_bw() + geom_label_repel(size = 6) + - scale_color_manual(values=colors2) + + scale_color_manual(values=colors2, name = 'Cluster') + guides(color = guide_legend(override.aes = list(label = 'O', size = 3))) + theme(axis.text=element_text(color='black', size=14), axis.title=element_text(color='black', size=16)) From 56dde7185a262cbcdcf009eed7841ed059ea9ee0 Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Thu, 25 Aug 2022 14:34:48 -0400 Subject: [PATCH 3/6] Use `clusterJF` for tSNE --- R/app_server.R | 67 +++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index f8be04f..c042496 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -372,47 +372,27 @@ app_server <- function(input, output, session) { tsne_coords<-reactive({ withProgress({ - data_mat2=data_mat()[,-1] - ids=data_mat()[,1] - data_mat2=data.matrix(data_mat2) - mat = Rtsne::Rtsne(data_mat2, initial_dims=15, pca=TRUE, theta=1)$Y + + mat <- data_mat()[,-1] %>% + Rtsne::Rtsne(initial_dims=15, pca=TRUE, theta=1) %>% + .[['Y']] + colnames(mat)=c("tSNE_1", "tSNE_2") + mat }, message="Calculating tSNE") }) output$tsne_plot = renderPlot({ - data_mat2=data_mat()[,-1] - ids=data_mat()[,1] - data_mat2=data.matrix(data_mat2) - - tsne_df=tsne_coords() - plotter=data.frame(tSNE_1=tsne_df[,1], tSNE_2=tsne_df[,2], SampleID=ids) - - if(length(input$meta_val)>0){ - grouper=meta_mat()[,input$meta_val] - - plotter$Group=as.character(plotter$SampleID) - samps=as.character(unique(plotter$SampleID)) - for(jj in 1:length(samps)){ - grouper=dplyr::filter(meta_mat(), .data$ID==samps[jj]) - grouper=as.character(grouper[,input$meta_val][1]) - - plotter$Group[plotter$SampleID==samps[jj]]<-grouper - } - } else { - plotter$Group=plotter$SampleID - } - - colors_samples2=colors_samples - - gg=ggplot(sample(plotter), aes(.data$tSNE_1, .data$tSNE_2, color=.data$Group)) + - geom_point() + theme_bw() + - scale_color_manual(values=colors_samples2) + - theme(axis.text=element_text(color='black', size=14), - axis.title=element_text(color='black', size=16)) + gg <- tsne_coords() %>% + clusterJF(axis_prefix = 'tSNE', + ids = data_mat()[,1], + meta = meta_mat()[,input$meta_val], + colors = colors_samples, + legend.name = input$meta_val) + vals$tsne_samps<-gg print(gg) @@ -420,20 +400,13 @@ app_server <- function(input, output, session) { }) output$tsne_k_plot = renderPlot({ - data_mat2=data_mat()[,-1] - ids=data_mat()[,1] - data_mat2=data.matrix(data_mat2) - - tsne_df=tsne_coords() - plotter=data.frame(tSNE_1=tsne_df[,1], tSNE_2=tsne_df[,2], SampleID=ids) - - plotter$Kmeans=as.character(kmeaner()) - - gg=ggplot(sample(plotter), aes(.data$tSNE_1, .data$tSNE_2, color=.data$Kmeans)) + - geom_point() + theme_bw() + - scale_color_manual(values=colors_clusters()) + - theme(axis.text=element_text(color='black', size=14), - axis.title=element_text(color='black', size=16)) + + gg <- tsne_coords() %>% + clusterJF(axis_prefix = 'tSNE', + ids = 1:nrow(data_mat()), + meta = kmeaner(), + colors = colors_clusters(), + legend.name = 'Cluster') vals$tsne_kmeans<-gg From 1ed0bc897b96ac8debb8931d2b415e5f933106b9 Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Thu, 25 Aug 2022 14:36:47 -0400 Subject: [PATCH 4/6] Fix group choice and labels * Noticed a bug that stemmed from not using `input$meta_val` when we should have. * Fixed labels on axes and legends in several figures --- R/app_server.R | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index c042496..49306c2 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -271,8 +271,9 @@ app_server <- function(input, output, session) { gg <- pca_coords() %>% clusterJF(ids = data_mat()[,1], - meta = meta_mat()[,2], - colors = colors_samples) + meta = meta_mat()[,input$meta_val], + colors = colors_samples, + legend.name = input$meta_val) vals$pca_samps<-gg @@ -287,7 +288,7 @@ app_server <- function(input, output, session) { clusterJF(ids = 1:nrow(data_mat()), meta = kmeaner(), colors = colors_clusters(), - legend.name = 'Kmeans') + legend.name = 'Cluster') vals$pca_kmeans<-gg @@ -315,7 +316,8 @@ app_server <- function(input, output, session) { ids = rownames(sb_pca()$groups_table), meta = as.character(meta_mat()[,input$meta_val]), colors1 = colors_samples, - colors2 = colors_clusters()) + colors2 = colors_clusters(), + legend.name = input$meta_val) }) output$samp_p_pca <- renderPlot({ @@ -345,9 +347,11 @@ app_server <- function(input, output, session) { output$umap_plot = renderPlot({ gg <- umap_coords() %>% - clusterJF(ids = data_mat()[,1], - meta = meta_mat()[,2], - colors = colors_samples) + clusterJF(axis_prefix = 'UMAP', + ids = data_mat()[,1], + meta = meta_mat()[,input$meta_val], + colors = colors_samples, + legend.name = input$meta_val) vals$umap_samps<-gg @@ -358,10 +362,11 @@ app_server <- function(input, output, session) { output$umap_k_plot = renderPlot({ gg <- umap_coords() %>% - clusterJF(ids = 1:nrow(data_mat()), + clusterJF(axis_prefix = 'UMAP', + ids = 1:nrow(data_mat()), meta = kmeaner(), colors = colors_clusters(), - legend.name = 'Kmeans') + legend.name = 'Cluster') vals$umap_kmeans<-gg From 68ccf652f89290454ed74c997c58217562c0055a Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Thu, 25 Aug 2022 14:37:31 -0400 Subject: [PATCH 5/6] Update number of clusters to `numericInput` --- R/app_server.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index 49306c2..3d20d34 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -140,11 +140,10 @@ app_server <- function(input, output, session) { # Visualize::number of clusters output$cluster_setting<-renderUI({ - sliderInput("kmean", + numericInput("kmean", "Number of clusters:", value = 5, - min = 1, - max = 20) + min = 2) }) From aea12609e35951efe85dccf5a38714566fd08cad Mon Sep 17 00:00:00 2001 From: Randy Johnson Date: Fri, 26 Aug 2022 13:17:40 -0400 Subject: [PATCH 6/6] documentation update --- man/clusterJF.Rd | 12 +++++++++++- man/sb_clusterJF.Rd | 11 ++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/man/clusterJF.Rd b/man/clusterJF.Rd index 8da8229..8004886 100644 --- a/man/clusterJF.Rd +++ b/man/clusterJF.Rd @@ -11,7 +11,15 @@ clusterJF(clustered_data, ...) \method{clusterJF}{prcomp}(clustered_data, ids, meta, colors, legend.name = "Group", ...) -\method{clusterJF}{matrix}(clustered_data, ids, meta, colors, legend.name = "Group", ...) +\method{clusterJF}{matrix}( + clustered_data, + axis_prefix = "axis", + ids, + meta, + colors, + legend.name = "Group", + ... +) \method{clusterJF}{tbl}(clustered_data, colors, xlab, ylab, legend.name, ...) } @@ -28,6 +36,8 @@ clusterJF(clustered_data, ...) \item{legend.name}{Character string for the legend name (default is 'Group')} +\item{axis_prefix}{x-and y-axis prefix (e.g. 'UMAP_')} + \item{xlab}{x-axis label} \item{ylab}{y-axis label} diff --git a/man/sb_clusterJF.Rd b/man/sb_clusterJF.Rd index 9563d33..4650b46 100644 --- a/man/sb_clusterJF.Rd +++ b/man/sb_clusterJF.Rd @@ -4,7 +4,14 @@ \alias{sb_clusterJF} \title{Sample-based PCA for JoesFlow} \usage{ -sb_clusterJF(clustered_data, ids, meta, colors1, colors2) +sb_clusterJF( + clustered_data, + ids, + meta, + colors1, + colors2, + legend.name = "Group" +) } \arguments{ \item{clustered_data}{Object containing clustered data (expects output from `prcomp` or `umap`)} @@ -16,6 +23,8 @@ sb_clusterJF(clustered_data, ids, meta, colors1, colors2) \item{colors1}{Vector of colors for samples} \item{colors2}{Vector of colors for clusters} + +\item{legend.name}{Character string for the legend name (default is 'Group')} } \value{ A ggplot object