diff --git a/global.R b/global.R index 5cea813..e7a9964 100644 --- a/global.R +++ b/global.R @@ -1,5 +1,6 @@ library(readr) library(dplyr) +library(tidyr) library(Matrix) library(plotly) library(magrittr) @@ -81,3 +82,35 @@ plot_geneExpr <- function(gene_of_interest, gene_name, ggtitle(gene_name) ggplotly() } + +#' Draw geneExpr boxplot by cluster +plot_geneExprGeneCluster <- function(gene_of_interest, gene_name){ + the_genes <- setNames(gene_name, gene_of_interest) + + gene_expr <- + expression[names(the_genes),] %>% + as.matrix() %>% + t() %>% + as.data.frame() %>% + tibble::rownames_to_column("barcode") + tsne1 <- + left_join(tsne, gene_expr, by="barcode") %>% + gather(gene, expr, starts_with("ENSG")) %>% + mutate( + gene = as.factor(gene), + gene = plyr::revalue(gene, the_genes) + ) + tsne1 %>% + ggplot(aes(x=id, y=expr)) + + geom_boxplot() + + facet_wrap(~gene) + + xlab("Cluster") + + ylab("Normalized expression") + + theme_classic() + + theme( + axis.text.x = element_text(angle=90, hjust=1, vjust=0.5), + strip.background = element_rect(fill = NA, colour = NA) + ) + ggplotly() +} + diff --git a/server.R b/server.R index 301c27e..fe9260d 100644 --- a/server.R +++ b/server.R @@ -190,7 +190,7 @@ shinyServer(function(input, output, session) { } else{ gene_idx = input$difGeneTable_rows_selected } - + dg_mat <- c() for ( n in gene_idx ) { gene_idx <- which(genes$Symbol == diff_genes[n]) @@ -216,8 +216,8 @@ shinyServer(function(input, output, session) { arrange(panel) dg_mat$gene <- factor(dg_mat$gene, levels = dg_mat$gene) - ggplot(dg_mat, aes(x=group, y=expr, fill=group)) + geom_boxplot() + - facet_wrap(~gene, scales="free_x", nrow=2, ncol=gene_cnt) + + ggplot(dg_mat, aes(x=group, y=expr, fill=group)) + geom_boxplot() + + facet_wrap(~gene, scales="free_x", nrow=2, ncol=gene_cnt) + theme(panel.margin = unit(1, "lines"), panel.background = element_rect(fill = "white"), strip.background = element_rect(fill = "white"), @@ -365,5 +365,12 @@ shinyServer(function(input, output, session) { }) } + output$geneExprGeneCluster <- renderPlotly({ + gene_of_interest <- parse_gene_input(geneExpr_genes()) + gene_name <- parse_gene_input(geneExpr_genes(), get="name") + plot_geneExprGeneCluster(gene_of_interest, gene_name) + }) + + } ) diff --git a/ui.R b/ui.R index b18567e..a9052ac 100644 --- a/ui.R +++ b/ui.R @@ -58,7 +58,7 @@ shinyUI( ) ), - column(8, uiOutput("geneExprPlot")) + column(8, uiOutput("geneExprPlot"), plotlyOutput("geneExprGeneCluster")) ), # exploration of selection tabPanel( 'Compare',