Skip to content

Commit f235d20

Browse files
authored
Merge pull request #12 from Core-Bioinformatics/display_improvements
Display improvements
2 parents 32445e3 + 4af8554 commit f235d20

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

R/DEsummaryFuns.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#' @inheritParams generateShinyApp
44
#' @param top.annotation.ids a vector of column indices denoting which columns
55
#' of the metadata should become heatmap annotations
6+
#' @param show.columns.names whether to show the column names below the heatmap;
7+
#' default is TRUE
68
#' @return The heatmap as detailed in the ComplexHeatmap package.
79
#' @export
810
#' @examples
@@ -20,12 +22,14 @@ expression_heatmap <- function(
2022
expression.matrix.subset,
2123
top.annotation.ids = NULL,
2224
metadata,
23-
type = c('Z-score', 'Log2 Expression', 'Expression')
25+
type = c('Z-score', 'Log2 Expression', 'Expression'),
26+
show.column.names = TRUE
2427
){
2528
heatmat <- as.matrix(expression.matrix.subset)
2629

2730
type <- type[1]
2831
heatmat <- rescale_matrix(heatmat, type)
32+
if(!show.column.names){colnames(heatmat <- NULL)}
2933

3034
if(!is.null(top.annotation.ids)){
3135
qual.col.pals = dplyr::filter(RColorBrewer::brewer.pal.info, .data$category == 'qual')
@@ -53,16 +57,13 @@ expression_heatmap <- function(
5357
top.annotation <- NULL
5458
}
5559
if (type != 'Z-score'){
56-
breaks <- seq(min(heatmat),
57-
max(heatmat),
58-
(max(heatmat) - min(heatmat))/9)
59-
colours = c("#FFFFFF",RColorBrewer::brewer.pal(n = 9, name = "YlOrRd"))
60-
}
61-
else {
62-
breaks <- seq(-3,3,6/9)
60+
breaks <- seq(min(heatmat), max(heatmat), (max(heatmat) - min(heatmat)) / 9)
61+
colours = c("#FFFFFF", RColorBrewer::brewer.pal(n = 9, name = "YlOrRd"))
62+
}else{
63+
breaks <- seq(-3, 3, 6 / 9)
6364
colours = rev(RColorBrewer::brewer.pal(n = 10, name = "RdBu"))
64-
heatmat[heatmat>3]<-3
65-
heatmat[heatmat<(-3)]<- (-3)
65+
heatmat[heatmat > 3] <- 3
66+
heatmat[heatmat < (-3)] <- (-3)
6667
}
6768
ComplexHeatmap::Heatmap(
6869
matrix = heatmat,

R/DEsummaryPanel.R

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ DEsummaryPanelUI <- function(id, metadata){
2828
onStatus = FALSE
2929
),
3030
checkboxInput(ns("pca.show.labels"), label = "Show sample labels", value = FALSE),
31-
checkboxInput(ns('pca.show.ellipses'),label = "Show ellipses around groups",value=TRUE),
32-
textInput(ns('plotPCAFileName'), 'File name for PCA plot download', value ='PCAPlotDE.png'),
31+
checkboxInput(ns('pca.show.ellipses'), label = "Show ellipses around groups", value = TRUE),
32+
textInput(ns('plotPCAFileName'), 'File name for PCA plot download', value = 'PCAPlotDE.png'),
3333
downloadButton(ns('downloadPCAPlot'), 'Download PCA Plot'),
3434

3535
status = "info",
@@ -42,12 +42,10 @@ DEsummaryPanelUI <- function(id, metadata){
4242
radioButtons(ns('heatmap.processing'), label = "Heatmap values",
4343
choices = c('Expression','Log2 Expression','Z-score'),
4444
selected = 'Z-score'),
45-
shinyjqui::orderInput(ns('heatmap.annotations'), label = "Show annotations",
46-
items = colnames(metadata)[c(ncol(metadata), seq_len(ncol(metadata) - 1))][-2]),
45+
shinyjqui::orderInput(ns('heatmap.annotations'), label = "Show annotations", items = colnames(metadata)),
4746
selectInput(ns("geneName"), "Additional genes to include:", multiple = TRUE, choices = character(0)),
4847
div("\nIf no genes are selected in the DE panel or here then the top 50 DE genes are chosen.\n"),
4948
div(style="margin-bottom:10px"),
50-
actionButton(ns('goHeatmap'), label = 'Create heatmap'),
5149
textInput(ns('plotHeatmapFileName'), 'File name for heatmap plot download', value ='HeatmapPlot.png'),
5250
downloadButton(ns('downloadHeatmapPlot'), 'Download Heatmap Plot'),
5351

@@ -99,6 +97,16 @@ DEsummaryPanelServer <- function(id, expression.matrix, metadata, DEresults, ann
9997
})
10098
output[['pca']] <- renderPlot(pca.plot())
10199

100+
observe({
101+
items <- colnames(metadata())
102+
include.exclude <- apply(metadata(), 2, function(x){
103+
l <- length(unique(x))
104+
(l > 1) & (l < length(x))
105+
})
106+
items <- colnames(metadata())[include.exclude]
107+
items <- items[c(length(items), seq_len(length(items) - 1))]
108+
shinyjqui::updateOrderInput(session, "heatmap.annotations", items = items)
109+
})
102110
heatmap.plot <- reactive({
103111
selectedGenes = DEresults()$selectedGenes()
104112
if(length(selectedGenes)){
@@ -119,14 +127,14 @@ DEsummaryPanelServer <- function(id, expression.matrix, metadata, DEresults, ann
119127
dplyr::arrange(dplyr::across(input[['heatmap.annotations']]))
120128

121129
myplot <- expression_heatmap(
122-
expression.matrix = subsetExpression[, meta[, 1]],
130+
expression.matrix = subsetExpression[, meta[, 1], drop = FALSE],
123131
top.annotation.ids = match(input[['heatmap.annotations']], colnames(meta)),
124132
metadata = meta,
125-
type = input[["heatmap.processing"]]
133+
type = input[["heatmap.processing"]],
134+
show.column.names = (nrow(meta) <= 20)
126135
)
127136
return(myplot)
128-
}) %>%
129-
bindEvent(DEresults(), input[['goHeatmap']])
137+
})
130138
output[['heatmap']] <- renderPlot(heatmap.plot(), height = 800)
131139

132140
output[['downloadHeatmapPlot']] <- downloadHandler(

R/QCpanel.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ QCpanelUI <- function(id, metadata){
1515
'Quality checks',
1616
tags$h1("Jaccard Similarity Index Heatmap"),
1717
shinyWidgets::dropdownButton(
18-
shinyjqui::orderInput(ns('jaccard.annotations'), label = "Show annotations",
19-
items = colnames(metadata)[c(ncol(metadata), seq_len(ncol(metadata) - 1))][-2]),
18+
shinyjqui::orderInput(ns('jaccard.annotations'), label = "Show annotations", items = colnames(metadata)),
2019
sliderInput(ns('jaccard.n.abundant'), label = '# of (most abundant) genes',
2120
min = 50, value = 500, max = 5000, step = 50, ticks = FALSE),
2221
checkboxInput(ns("jaccard.show.values"), label = "Show JSI values", value = FALSE),
@@ -73,6 +72,16 @@ QCpanelServer <- function(id, expression.matrix, metadata, anno){
7372
})
7473

7574
moduleServer(id, function(input, output, session){
75+
observe({
76+
items <- colnames(metadata())
77+
include.exclude <- apply(metadata(), 2, function(x){
78+
l <- length(unique(x))
79+
(l > 1) & (l < length(x))
80+
})
81+
items <- colnames(metadata())[include.exclude]
82+
items <- items[c(length(items), seq_len(length(items) - 1))]
83+
shinyjqui::updateOrderInput(session, "jaccard.annotations", items = items)
84+
})
7685
jaccard.plot <- reactive({
7786
meta <- lapply(metadata(), function(x) factor(x, levels = unique(x))) %>%
7887
as.data.frame() %>%
@@ -83,7 +92,8 @@ QCpanelServer <- function(id, expression.matrix, metadata, anno){
8392
metadata = meta,
8493
top.annotation.ids = match(input[['jaccard.annotations']], colnames(meta)),
8594
n.abundant = input[['jaccard.n.abundant']],
86-
show.values = input[["jaccard.show.values"]]
95+
show.values = input[["jaccard.show.values"]],
96+
show.row.columns.names = (nrow(meta) <= 20)
8797
)
8898
myplot
8999
})

R/QCplotFuns.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ jaccard_index <- function(a, b){
2424
#' of the metadata should become heatmap annotations
2525
#' @param n.abundant number of most abundant genes to use for the JSI calculation
2626
#' @param show.values whether to show the JSI values within the heatmap squares
27+
#' @param show.row.columns.names whether to show the row and column names below
28+
#' the heatmap; default is TRUE
2729
#' @return The JSI heatmap as detailed in the ComplexHeatmap package.
2830
#' @export
2931
#' @examples
@@ -42,7 +44,8 @@ jaccard_heatmap <- function(
4244
metadata,
4345
top.annotation.ids = NULL,
4446
n.abundant = NULL,
45-
show.values = TRUE
47+
show.values = TRUE,
48+
show.row.columns.names = TRUE
4649
){
4750
n.abundant <- min(n.abundant, nrow(expression.matrix))
4851
n.samples <- ncol(expression.matrix)
@@ -54,7 +57,9 @@ jaccard_heatmap <- function(
5457
heatmat[i, j] <- heatmat[j, i] <- jaccard_index(i.gene.indices, j.gene.indices)
5558
}
5659
}
57-
rownames(heatmat) <- colnames(heatmat) <- colnames(expression.matrix)
60+
if(show.row.columns.names){
61+
rownames(heatmat) <- colnames(heatmat) <- colnames(expression.matrix)
62+
}
5863

5964
if(!is.null(top.annotation.ids)){
6065
qual.col.pals = dplyr::filter(RColorBrewer::brewer.pal.info, .data$category == 'qual')
@@ -71,6 +76,7 @@ jaccard_heatmap <- function(
7176
vec <- c(vec, col.vector[colind])
7277
names(vec)[i] <- values[i]
7378
colind <- colind + 1
79+
if(colind > length(col.vector)){colind <- colind %% length(col.vector)}
7480
}
7581
top.annotation.colour.list[[colnames(metadata)[top.annotation.ids[annos]]]] <- vec
7682
}

0 commit comments

Comments
 (0)