-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotAgglomerativeClusteringPlot.R
414 lines (335 loc) · 17.9 KB
/
plotAgglomerativeClusteringPlot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#'
#' Plot module for variance plot
#'
library(shiny)
library(ggplot2)
library(dendextend)
library(ctc)
library(gplots)
source("widgetVisualsEditor.R")
source("widgetDownloadablePlot.R")
source("widgetGradientEditor.R")
source("defaultParameters.R")
plotAgglomerativeClusteringPlotUI.dist.methodsSelection <- c(
"Euclidean" = "euclidean",
"Maximum" = "maximum",
"Manhattan" = "manhattan",
"Canberra" = "canberra",
"Binary" = "binary",
"Minkowski" = "minkowski"
)
plotAgglomerativeClusteringPlotUI.hclust.methodsSelection <- c(
"Average" = "average",
"McQuitty" = "mcquitty",
"Median" = "median",
"Centroid" = "centroid",
"Ward D" = "ward.D",
"Ward D2" = "ward.D2",
"Single linkage" = "single",
"Complete linkage" = "complete"
)
plotAgglomerativeClusteringPlotUI.plotTypes <- c(
"Dendrogram (horizontal)" = "dendrogram.horizontal",
"Dendrogram (vertical)" = "dendrogram.vertical",
"Heatmap Pearson correlation" = "cor.pearson",
"Heatmap Spearman correlation" = "cor.spearman"
)
plotAgglomerativeClusteringPlotUI.dendrogramPositions <- c(
"Row + Column" = "both",
"Row only" = "row",
"Column only" = "column",
"No dendrograms" = "none"
)
plotAgglomerativeClusteringPlotUI.heatmapDensityInfo <- c(
"No density info" = "none",
"Histogram" = "histogram",
"Density plot" = "density"
)
plotAgglomerativeClusteringPlotUI.heatmapTrace <- c(
"No trace" = "none",
"Row + Column" = "both",
"Row only" = "row",
"Column only" = "column"
)
plotAgglomerativeClusteringPlot.defaultGradient <- importGradientSample("Gradients/HeatmapRdBu.csv", list())
plotAgglomerativeClusteringPlotUI <- function(id) {
ns <- NS(id)
export.tree.buttons <- tagList(
downloadButton(ns("export.newick"), "as *.newick")
)
return(downloadablePlotOutput(ns("plot"),
custom.header.items = dropdownButton(ns("export.tree"),
"Export tree",
icon = icon("download"),
export.tree.buttons)))
}
plotAgglomerativeClusteringPlotSettingsUI <- function(id) {
ns <- NS(id)
return(bsCollapse(
bsCollapsePanel(recommendedDataText("Plot type"),
value = "plottype",
selectizeInput(ns("plottype"), "Plot type", choices = plotAgglomerativeClusteringPlotUI.plotTypes),
conditionalPanel(paste(conditionalPanel.equals(ns("plottype"), "'cor.pearson'"), "||", conditionalPanel.equals(ns("plottype"), "'cor.spearman'")),
selectizeInput(ns("heatmap.dendrograms"), "Visible dendrograms", choices = plotAgglomerativeClusteringPlotUI.dendrogramPositions),
selectizeInput(ns("heatmap.density.info"), "Density info", choices = plotAgglomerativeClusteringPlotUI.heatmapDensityInfo),
selectizeInput(ns("heatmap.trace"), "Trace line", choices = plotAgglomerativeClusteringPlotUI.heatmapTrace),
colourInput(ns("heatmap.tracecol"), "Trace line color", value = "cyan", allowTransparent = T))
),
bsCollapsePanel(recommendedDataText("Hierarchical clustering"),
value = "hierarchical.clustering",
selectizeInput(ns("method.dist"), "Distance method", choices = plotAgglomerativeClusteringPlotUI.dist.methodsSelection),
selectizeInput(ns("method.hclust"), "Clustering method", choices = plotAgglomerativeClusteringPlotUI.hclust.methodsSelection)),
bsCollapsePanel(optionalDataText("Visualization"),
value = "visualization",
conditionalPanel(paste(conditionalPanel.equals(ns("plottype"), "'cor.pearson'"), "||", conditionalPanel.equals(ns("plottype"), "'cor.spearman'")), gradientEditorUI(ns("heatmap.color"))),
conditionalPanel(paste(conditionalPanel.equals(ns("plottype"), "'dendrogram.horizontal'"), "||", conditionalPanel.equals(ns("plottype"), "'dendrogram.vertical'")), visualsEditorUI(ns("visuals")))
),
bsCollapsePanel(optionalDataText("General settings"),
value = "generalsettings",
generalPlotSettingsInput(ns("plot.settings"),
legend.color = F,
legend.shape = F))
))
}
plotAgglomerativeClusteringPlot.save <- function(readcounts,
plot.settings,
sample.visuals,
format,
filename,
method.distance = "euclidian",
method.cluster = "average",
plottype = "dendrogram.horizontal",
heatmap.dendrograms = "both",
heatmap.density.info = "none",
heatmap.trace = "none",
heatmap.tracecol = "cyan",
heatmap.color = plotAgglomerativeClusteringPlot.defaultGradient){
validate(need(is.matrix(readcounts()) || is.SummarizedExperiment(readcounts()), "No data to plot!"),
need(sample.visuals(), "No sample visuals available!"))
plot.settings <- plotSettingsSetNA(plot.settings,
PlotSettings(width = default.plot.width,
height = default.plot.height,
dpi = default.plot.dpi,
scale = 1,
title = "Hierarchical Clustering",
subtitle = ""))
if(plottype == "cor.pearson") {
plot.settings <- plotSettingsSetNA(plot.settings,
PlotSettings(legend.color = "Pearson correlation"))
}
else if(plottype == "cor.spearman") {
plot.settings <- plotSettingsSetNA(plot.settings,
PlotSettings(legend.color = "Spearman correlation"))
}
width <- plot.settings@width
height <- plot.settings@height
dpi <- plot.settings@dpi
scale <- plot.settings@scale
title <- plot.settings@title
subtitle <- plot.settings@subtitle
legend.color <- [email protected]
palette.colors <- sample.visuals()$palette.colors
palette.shapes <- sample.visuals()$palette.shapes
saveRPlot(width, height, dpi, scale, filename, format, expr = function() {
X <- if(is.matrix(readcounts())) readcounts() else assay(readcounts())
if(plottype == "dendrogram.horizontal") {
# Agglomerative clustering plot
dend <- t(X) %>%
dist(method = method.distance) %>%
hclust(method = method.cluster) %>%
as.dendrogram
dend.samples <- labels(dend)
dend.factors <- sample.visuals()$factors[dend.samples,]
dend <- dend %>% dendextend::set("leaves_pch", palette.shapes[as.numeric(dend.factors$shape)]) %>%
dendextend::set("leaves_col", palette.colors[as.numeric(dend.factors$color)])
par(mar = c(5,4,4,10))
dend %>% plot(main = title, sub = subtitle, horiz = T, cex = 0.6)
# par(mar = c(5,4,4,10))
# dend <- hclust(dist(t(X), method = method.distance), method = method.cluster)
# plot(dend, main = title, sub = subtitle)
}
else if(plottype == "dendrogram.vertical") {
# Agglomerative clustering plot
dend <- t(X) %>%
dist(method = method.distance) %>%
hclust(method = method.cluster) %>%
as.dendrogram
dend.samples <- labels(dend)
dend.factors <- sample.visuals()$factors[dend.samples,]
dend <- dend %>% dendextend::set("leaves_pch", palette.shapes[as.numeric(dend.factors$shape)]) %>%
dendextend::set("leaves_col", palette.colors[as.numeric(dend.factors$color)])
par(mar = c(5,4,4,10))
dend %>% plot(main = title, sub = subtitle, horiz = F, cex = 0.6)
# par(mar = c(5,4,4,10))
# dend <- hclust(dist(t(X), method = method.distance), method = method.cluster)
# plot(dend, main = title, sub = subtitle)
}
else {
# Heatmap plots
validate(need(nrow(heatmap.color) > 1, "No valid gradient map defined!"))
cor.method <- "pearson"
if(plottype == "cor.kendall") {
cor.method <- "kendall"
}
else if(plottype == "cor.spearman") {
cor.method <- "spearman"
}
palette <- colorRampPalette(heatmap.color[, "color"])
color.breaks <- c()
for(i in 1:(nrow(heatmap.color) - 1)) {
color.breaks <- c(color.breaks, seq(heatmap.color[i, "value"], heatmap.color[i + 1, "value"], length.out = 100))
}
color.breaks <- unique(color.breaks)
heatmap.2(cor(X, method = cor.method),
hclustfun = function(x) hclust(x, method = method.cluster),
distfun = function(x) dist(x, method = method.distance),
symm = T,
main = title,
sub = subtitle,
margins = c(12, 9),
dendrogram = heatmap.dendrograms,
density.info = heatmap.density.info,
trace = heatmap.trace,
tracecol = heatmap.tracecol,
col = palette,
breaks = color.breaks,
key.xlab = legend.color)
}
})
return(plot.settings)
}
plotAgglomerativeClusteringPlot.saveNewick <- function(readcounts,
filename,
method.distance = "euclidian",
method.cluster = "average") {
validate(need(is.matrix(readcounts()) || is.SummarizedExperiment(readcounts()), "No data to cluster!"))
X <- if(is.matrix(readcounts())) readcounts() else assay(readcounts())
clust <- t(X) %>%
dist(method = method.distance) %>%
hclust(method = method.cluster)
write(ctc::hc2Newick(clust), file = filename)
}
plotAgglomerativeClusteringPlot_ <- function(input,
output,
session,
conditions,
readcounts,
default.title,
xauto) {
plot.settings <- generalPlotSettings("plot.settings")
visuals.conditions <- visualsEditorValue("visuals", reactive({colnames(conditions())}))
visuals.sample <- reactive({ calculateSampleVisuals(colnames(readcounts()), conditions(), visuals.conditions()) })
heatmap.color <- gradientEditorValue("heatmap.color", default.gradient = plotAgglomerativeClusteringPlot.defaultGradient)
# Provide plot height that scales with sample count
plot.settings.dynamic <- reactive({
validate(need(is.matrix(readcounts()) || is.SummarizedExperiment(readcounts()), "No data to build plot settings from!"))
settings <- plotSettingsSetNA(plot.settings(),
PlotSettings(dpi = default.plot.dpi,
scale = 1))
# Calculate the plot size based on the count of samples
if(input$plottype == "dendrogram.horizontal") {
height <- (1 + 0.4 * ncol(readcounts())) * settings@dpi * settings@scale
settings <- plotSettingsSetNA(plot.settings(),
PlotSettings(height = height))
}
else if(input$plottype == "dendrogram.vertical") {
width <- (1 + 0.4 * ncol(readcounts())) * settings@dpi * settings@scale
settings <- plotSettingsSetNA(plot.settings(),
PlotSettings(width = width))
}
else {
height <- (1 + 0.2 * ncol(readcounts())) * settings@dpi * settings@scale
settings <- plotSettingsSetNA(plot.settings(),
PlotSettings(height = height))
}
return(settings)
})
# Plot
downloadablePlot("plot",
plot.settings = plot.settings.dynamic,
exprplot = function(plot.settings, format, filename)
{
distance.methods <- plotAgglomerativeClusteringPlotUI.dist.methodsSelection
clustering.methods <- plotAgglomerativeClusteringPlotUI.hclust.methodsSelection
distance.method.name <- names(distance.methods)[distance.methods == input$method.dist]
clustering.method.name <- names(clustering.methods)[clustering.methods == input$method.hclust]
plot.settings <- plotSettingsSetNA(plot.settings,
PlotSettings(subtitle = paste0(distance.method.name, " distance, ", clustering.method.name),
title = default.title()))
return(plotAgglomerativeClusteringPlot.save(readcounts,
plot.settings,
format,
filename,
sample.visuals = visuals.sample,
method.distance = input$method.dist,
method.cluster = input$method.hclust,
plottype = input$plottype,
heatmap.dendrograms = input$heatmap.dendrograms,
heatmap.density.info = input$heatmap.density.info,
heatmap.trace = input$heatmap.trace,
heatmap.tracecol = input$heatmap.tracecol,
heatmap.color = heatmap.color()))
})
# Download tree as NEWICK
output$export.newick <- downloadHandler("clustering.newick", function(file) {
plotAgglomerativeClusteringPlot.saveNewick(readcounts, file, input$method.dist, input$method.hclust)
})
# xauto exporter that allows triggering of exporting data from code
xautovars <- reactiveValues(xautocounter = 1)
if(!is.null(xauto)) {
observeEvent(xauto(), {
if(!is.null(xauto()$filename.newick)) {
plotAgglomerativeClusteringPlot.saveNewick(readcounts, xauto()$filename.newick, input$method.dist, input$method.hclust)
}
if(!is.null(xauto()$filename.svg)) {
filename <- xauto()$filename.svg
distance.methods <- plotAgglomerativeClusteringPlotUI.dist.methodsSelection
clustering.methods <- plotAgglomerativeClusteringPlotUI.hclust.methodsSelection
distance.method.name <- names(distance.methods)[distance.methods == input$method.dist]
clustering.method.name <- names(clustering.methods)[clustering.methods == input$method.hclust]
plot.settings <- plotSettingsSetNA(plot.settings(),
PlotSettings(subtitle = paste0(distance.method.name, " distance, ", clustering.method.name),
title = default.title()))
return(plotAgglomerativeClusteringPlot.save(readcounts,
plot.settings,
"svg",
filename,
sample.visuals = visuals.sample,
method.distance = input$method.dist,
method.cluster = input$method.hclust,
plottype = input$plottype,
heatmap.dendrograms = input$heatmap.dendrograms,
heatmap.density.info = input$heatmap.density.info,
heatmap.trace = input$heatmap.trace,
heatmap.tracecol = input$heatmap.tracecol,
heatmap.color = heatmap.color()))
}
xautovars$xautocounter <- xautovars$xautocounter + 1
})
}
return(reactive({ xautovars$xautocounter }))
}
#' Agglomerative clustering plot
#'
#' @param id
#' @param conditions
#' @param readcounts
#' @param default.title
#' @param xauto If not null returns a function that returns list(filename.svg = <>, filename.newick = <>)
#'
#' @return
#' @export
#'
#' @examples
plotAgglomerativeClusteringPlot <- function(id,
conditions,
readcounts,
default.title = reactive({"Hierarchical clustering"}),
xauto = NULL) {
return(callModule(plotAgglomerativeClusteringPlot_,
id,
conditions = conditions,
readcounts = readcounts,
default.title = default.title,
xauto = xauto))
}