/
plotQuantileHeatmap-methods.R
202 lines (188 loc) · 6.06 KB
/
plotQuantileHeatmap-methods.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
#' @name plotQuantileHeatmap
#' @inherit AcidGenerics::plotQuantileHeatmap
#' @note Updated 2022-03-07.
#'
#' @inheritParams plotHeatmap
#' @inheritParams AcidRoxygen::params
#' @param ... Additional arguments.
#'
#' @param legend `logical(1)`.
#' Show the color legend.
#'
#' @param n `integer(1)`.
#' The number of quantile breaks to create.
#'
#' @examples
#' data(
#' RangedSummarizedExperiment,
#' SingleCellExperiment_splatter,
#' package = "AcidTest"
#' )
#'
#' ## SummarizedExperiment ====
#' object <- RangedSummarizedExperiment
#' plotQuantileHeatmap(object)
#'
#' ## SingleCellExperiment ====
#' object <- SingleCellExperiment_splatter
#' plotQuantileHeatmap(object)
NULL
## Updated 2019-07-23.
.quantileBreaks <- function(object, n = 10L) {
assert(
is.matrix(object),
isInt(n),
isPositive(n)
)
breaks <- quantile(object, probs = seq(0L, 1L, length.out = n))
breaks[!duplicated(breaks)]
}
## Updated 2021-02-08.
`plotQuantileHeatmap,SE` <- # nolint
function(object,
assay = 1L,
interestingGroups = NULL,
n = 10L,
clusterRows = TRUE,
clusterCols = TRUE,
showRownames = isTRUE(nrow(object) <= 30L),
showColnames = TRUE,
treeheightRow = 50L,
treeheightCol = 50L,
color,
legendColor,
legend = FALSE,
borderColor = NULL,
title = NULL,
## Attept to map genes to symbols automatically only when shown.
convertGenesToSymbols = showRownames,
...) {
requireNamespaces("pheatmap")
validObject(object)
assert(
nrow(object) > 1L,
ncol(object) > 1L,
isScalar(assay),
isInt(n),
isFlag(clusterCols),
isFlag(clusterRows),
isFlag(legend),
isString(borderColor, nullOk = TRUE),
isString(title, nullOk = TRUE),
isFlag(convertGenesToSymbols)
)
interestingGroups(object) <-
matchInterestingGroups(object, interestingGroups)
n <- as.integer(n)
if (!isString(borderColor)) {
borderColor <- NA
}
if (!isString(title)) {
title <- NA
}
## Warn and early return if any samples are duplicated.
if (!hasUniqueCols(object)) {
alertWarning("Non-unique samples detected. Skipping plot.")
return(invisible(NULL))
}
## Modify the object to use gene symbols in the row names automatically,
## if possible. We're using `tryCatch()` call here to return the object
## unmodified if gene symbols aren't defined.
if (isTRUE(convertGenesToSymbols)) {
object <- tryCatch(
expr = suppressMessages({
convertGenesToSymbols(object)
}),
error = function(e) {
object
}
)
}
## Ensure we're using a dense matrix.
mat <- as.matrix(assay(object, i = assay))
## Calculate the quantile breaks.
breaks <- .quantileBreaks(mat, n = n)
## Get annotation columns and colors automatically.
x <- .pheatmapAnnotations(object = object, legendColor = legendColor)
assert(
is.list(x),
identical(names(x), c("annotationCol", "annotationColors"))
)
annotationCol <- x[["annotationCol"]]
annotationColors <- x[["annotationColors"]]
## Note the number of breaks here.
color <- .pheatmapColorPalette(color = color, n = length(breaks) - 1L)
## Substitute human-friendly sample names, if defined.
sampleNames <- tryCatch(
expr = sampleNames(object),
error = function(e) {
NULL
}
)
if (hasLength(sampleNames)) {
colnames(mat) <- sampleNames
if (hasLength(annotationCol) && !anyNA(annotationCol)) {
rownames(annotationCol) <- sampleNames
}
}
## Return pretty heatmap with modified defaults.
args <- list(
"mat" = mat,
"annotationCol" = annotationCol,
"annotationColors" = annotationColors,
"borderColor" = borderColor,
"breaks" = breaks,
"clusterCols" = clusterCols,
"clusterRows" = clusterRows,
"color" = color,
"legend" = legend,
"legendBreaks" = breaks,
"legendLabels" = round(breaks, digits = 2L),
"main" = title,
"scale" = "none",
"showColnames" = showColnames,
"showRownames" = showRownames,
"treeheightCol" = treeheightCol,
"treeheightRow" = treeheightRow,
...
)
args <- .pheatmapArgs(args)
## Ignore "partial match of 'just' to 'justification'" warning.
withCallingHandlers(
expr = do.call(what = pheatmap::pheatmap, args = args),
warning = function(w) {
if (isTRUE(grepl(
pattern = "partial match",
x = as.character(w)
))) {
invokeRestart("muffleWarning")
} else {
w
}
}
)
}
formals(`plotQuantileHeatmap,SE`)[c("color", "legendColor")] <- # nolint
.formalsList[c("heatmapQuantileColor", "heatmapLegendColor")]
## Updated 2020-02-19.
`plotQuantileHeatmap,SCE` <- # nolint
function(object, ...) {
plotQuantileHeatmap(
object = aggregateCellsToSamples(object),
...
)
}
#' @rdname plotQuantileHeatmap
#' @export
setMethod(
f = "plotQuantileHeatmap",
signature = signature(object = "SingleCellExperiment"),
definition = `plotQuantileHeatmap,SCE`
)
#' @rdname plotQuantileHeatmap
#' @export
setMethod(
f = "plotQuantileHeatmap",
signature = signature(object = "SummarizedExperiment"),
definition = `plotQuantileHeatmap,SE`
)