Skip to content

Commit

Permalink
Small UI update and bug fixes
Browse files Browse the repository at this point in the history
* Changed the number of clusters from slider to numeric input
* Fixed some labeling bugs in a few of the figures
* More background changes to make code easier to read and maintain.
  • Loading branch information
johnsonra committed Aug 26, 2022
2 parents c5f4bc0 + aea1260 commit 3ef0b01
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 87 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.Rhistory
.RData
.Ruserdata
*.Rproj

.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: JoesFlow
Title: Joes Flow simplified analysis for single cell modality data
Version: 0.1.1
Version: 0.1.1-1
Authors@R: c(person('Cooper', 'Devlin', email = '[email protected]',
role = c('cre', 'aut')),
person('Randy', 'Johnson', email = '[email protected]',
Expand Down
18 changes: 0 additions & 18 deletions JoesFlow.Rproj

This file was deleted.

95 changes: 36 additions & 59 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})


Expand Down Expand Up @@ -271,8 +270,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

Expand All @@ -287,7 +287,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

Expand Down Expand Up @@ -315,7 +315,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({
Expand Down Expand Up @@ -345,9 +346,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

Expand All @@ -358,10 +361,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

Expand All @@ -372,68 +376,41 @@ 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)

})

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

Expand Down
16 changes: 9 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -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)
Expand Down Expand Up @@ -90,14 +91,15 @@ 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
#' @import dplyr
#' @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'],
Expand All @@ -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, "%)")) +
Expand All @@ -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))
Expand Down
12 changes: 11 additions & 1 deletion man/clusterJF.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion man/sb_clusterJF.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3ef0b01

Please sign in to comment.