forked from jokergoo/ComplexHeatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorMapping-class.R
executable file
·332 lines (300 loc) · 9.77 KB
/
ColorMapping-class.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
#####################################
# class and methods to map values to colors
#
# == title
# Class for Color Mapping
#
# == details
# The `ColorMapping-class` handles color mapping for discrete values and continuous values.
# Discrete values are mapped by setting a vector of colors and continuous values are mapped by setting
# a color mapping function.
#
# == methods
# The `ColorMapping-class` provides following methods:
#
# - `ColorMapping`: contructor methods.
# - `map_to_colors,ColorMapping-method`: mapping values to colors.
# - `color_mapping_legend,ColorMapping-method`: draw legend or get legend as an object.
#
# == author
# Zuguang Gu <[email protected]>
#
ColorMapping = setClass("ColorMapping",
slots = list(
colors = "character", # a list of colors
levels = "ANY", # levels which colors correspond to
col_fun = "function", # function to map values to colors
type = "character", # continuous or discrete
name = "character", # used to map to the dataset and taken as the title of the legend
na_col = "character"
)
)
# == title
# Constructor Method for ColorMapping Class
#
# == param
# -name Name for this color mapping. The name is automatically generated if it is not specified.
# -colors Discrete colors.
# -levels Levels that correspond to ``colors``. If ``colors`` is name indexed,
# ``levels`` can be ignored.
# -col_fun Color mapping function that maps continuous values to colors.
# -breaks Breaks for the continuous color mapping. If ``col_fun`` is
# generated by `circlize::colorRamp2`, ``breaks`` is automatically inferred from the color mapping function.
# -na_col Colors for ``NA`` values.
#
# == detail
# ``colors`` and ``levels`` are used for discrete color mapping, ``col_fun`` and
# ``breaks`` are used for continuous color mapping.
#
# == value
# A `ColorMapping-class` object.
#
# == author
# Zuguang Gu <[email protected]>
#
# == examples
# cm = ColorMapping(colors = c("A" = "red", "B" = "black"))
# cm
# require(circlize)
# col_fun = colorRamp2(c(0, 1), c("white", "red"))
# cm = ColorMapping(col_fun = col_fun)
ColorMapping = function(name, colors = NULL, levels = NULL,
col_fun = NULL, breaks = NULL, na_col = "#FFFFFF") {
.Object = new("ColorMapping")
if(missing(name)) {
increase_color_mapping_index()
name = paste0("color_mapping_", get_color_mapping_index())
}
if(!is.null(colors)) {
if(is.null(levels)) {
if(is.null(names(colors))) {
stop("either provide `levels` or provide named `colors`.\n")
}
levels = names(colors)
}
if(length(colors) != length(levels)) {
stop("length of colors and length of levels should be the same.\n")
}
colors = t(col2rgb(colors, alpha = TRUE))
colors = rgb(colors[, 1:3, drop = FALSE], alpha = colors[, 4], maxColorValue = 255)
.Object@colors = colors
if(is.numeric(levels)) {
.Object@levels = as.character(levels)
#attr(.Object@levels, "numeric") = TRUE
} else {
.Object@levels = levels
}
names(.Object@colors) = levels
.Object@type = "discrete"
} else if(!is.null(col_fun)) {
if(is.null(breaks)) {
breaks = attr(col_fun, "breaks")
if(is.null(breaks)) {
stop("You should provide breaks.\n")
}
le1 = grid.pretty(range(breaks))
le2 = pretty(breaks, n = 3)
if(abs(length(le1) - 5) < abs(length(le2) - 5)) {
le = le1
} else {
le = le2
}
} else {
le = breaks
}
.Object@colors = col_fun(le)
.Object@levels = le
.Object@col_fun = col_fun
.Object@type = "continuous"
} else {
stop("initialization failed. Either specify `colors` + `levels` or `col_fun` + `breaks`\n")
}
.Object@name = name
na_col = t(col2rgb(na_col, alpha = TRUE))
na_col = rgb(na_col[, 1:3, drop = FALSE], alpha = na_col[, 4], maxColorValue = 255)
.Object@na_col = na_col[1]
return(.Object)
}
# == title
# Print the ColorMapping Object
#
# == param
# -object A `ColorMapping-class` object.
#
# == value
# This function returns no value.
#
# == author
# Zuguang Gu <[email protected]>
#
setMethod(f = "show",
signature = "ColorMapping",
definition = function(object) {
if(object@type == "discrete") {
cat("Discrete color mapping:\n")
cat("name:", object@name, "\n")
cat("levels:\n")
print(object@levels)
cat("\n")
cat("colors:\n")
col = object@colors; names(col) = NULL
print(col)
cat("\n")
} else if(object@type == "continuous") {
cat("Continuous color mapping:\n")
cat("name:", object@name, "\n")
cat("default breaks:\n")
print(object@levels)
cat("\n")
cat("colors:\n")
col = object@colors; names(col) = NULL
print(col)
cat("\n")
}
})
# == title
# Map Values to Colors
#
# == param
# -object A `ColorMapping-class` object.
# -x Input values.
#
# == details
# It maps a vector of values to a vector of colors.
#
# This function provides a uniform way for discrete and continuous color mapping.
#
# == value
# A vector of colors.
#
# == author
# Zuguang Gu <[email protected]>
#
# == example
# cm = ColorMapping(colors = c("A" = "red", "B" = "black"))
# map_to_colors(cm, sample(c("A", "B"), 10, replace = TRUE))
# require(circlize)
# col_fun = colorRamp2(c(0, 1), c("white", "red"))
# cm = ColorMapping(col_fun = col_fun)
# map_to_colors(cm, runif(10))
setMethod(f = "map_to_colors",
signature = "ColorMapping",
definition = function(object, x) {
if(is.factor(x)) x = as.vector(x)
original_attr = attributes(x)
x2 = vector(length = length(x))
if(object@type == "discrete") {
x[grepl("^\\s*$", x)] = NA
lna = is.na(x)
if(is.numeric(x)) x = as.character(x)
if(any(! x[!lna] %in% object@levels)) {
msg = paste0(object@name, ": cannot map colors to some of the levels:\n", paste(setdiff(x[!lna], object@levels), sep = ", ", collapse = ", "))
stop(msg)
}
x2[lna] = object@na_col
x2[!lna] = object@colors[ x[!lna] ]
} else {
lna = is.na(x)
x2[lna] = object@na_col
x2[!lna] = object@col_fun(x[!lna])
}
# keep original attributes, such as dimension
attributes(x2) = original_attr
return(x2)
})
# == title
# Draw Legend Based on Color Mapping
#
# == param
# -object A `ColorMapping-class` object.
# -plot Whether to plot or just return the legend object?
# -... Pass to `draw,Legends-method`.
# -color_bar "continous" or "discrete". It controls whether to show the discrete legend for the continuous color mapping.
# -title Title of the legend, by default it is the name of the legend.
# -title_gp Graphical parameters for legend title.
# -title_position Position of the title. See `Legend` for all possible values.
# -grid_height Height of each legend grid. Pass to `Legend`.
# -grid_width Width of each legend grid. Pass to `Legend`.
# -border Color for legend grid borders. Pass to `Legend`.
# -at Break values of the legend. By default it is the levels in the `ColorMapping-class` object.
# -labels Labels corresponding to break values.
# -labels_gp Graphcial parameters for legend labels.
# -labels_rot Rotation of labels.
# -nrow Pass to `Legend`. It controls the layout of legend grids if they are arranged in multiple rows or columns.
# -ncol Pass to `Legend`. It controls the layout of legend grids if they are arranged in multiple rows or columns.
# -by_row Pass to `Legend`. It controls the order of legend grids if they are arranged in multiple rows or columns.
# -legend_height Height of the legend body. It only works when ``color_bar`` is ``continuous`` and ``direction`` is ``vertical``. Pass to `Legend`.
# -legend_width Width of the legend body. It only works when ``color_bar`` is ``continuous`` and ``direction`` is ``horizontal``. Pass to `Legend`.
# -legend_direction When ``color_bar`` is ``continuous``, whether the legend is vertical or horizontal? Pass to `Legend`.
# -param All the legend-related parameters can be specified as a single list.
#
# == details
# The legend is constructed by `Legend`.
#
# == value
# A `Legends-class` object.
#
# == author
# Zuguang Gu <[email protected]>
#
setMethod(f = "color_mapping_legend",
signature = "ColorMapping",
definition = function(object,
plot = TRUE, ...,
color_bar = object@type,
title = object@name,
title_gp = gpar(fontsize = 10, fontface = "bold"),
title_position = "topleft",
grid_height = unit(4, "mm"),
grid_width = unit(4, "mm"),
border = NULL,
at = object@levels,
labels = at,
labels_gp = gpar(fontsize = 10),
labels_rot = 0,
nrow = NULL,
ncol = 1,
by_row = FALSE,
legend_height = NULL,
legend_width = NULL,
legend_direction = c("vertical", "horizontal"),
param = NULL) {
e = environment()
if(!is.null(param)) {
for(nm in names(param)) {
assign(nm, param[[nm]], envir = e)
}
}
title_gp = check_gp(title_gp)
labels_gp = check_gp(labels_gp)
if(object@type == "discrete" && color_bar == "continuous") {
stop("'color_bar' can only be set to 'discrete' only if the color mapping is discrete")
}
# get labels
if(length(at) != length(labels)) {
stop("Length of 'at' should be same as length of 'labels'.")
}
# if it is character color mapping, remove items in `at` which are not in the available optinos
if(color_bar == "discrete" && is.character(at)) {
l = which(at %in% object@levels)
at = at[l]
labels = labels[l]
}
if(color_bar == "discrete") {
if(object@type == "continuous") {
at = rev(at)
labels = rev(labels)
}
gf = Legend(at = at, labels = labels, title = title, title_gp = title_gp, grid_height = grid_height,
grid_width = grid_width, border = border, labels_gp = labels_gp, direction = legend_direction, nrow = nrow, ncol = ncol,
legend_gp = gpar(fill = map_to_colors(object, at)), title_position = title_position, by_row = by_row)
} else {
gf = Legend(at = at, labels = labels, col_fun = object@col_fun, title = title, title_gp = title_gp, grid_height = grid_height,
grid_width = grid_width, border = border, labels_gp = labels_gp, labels_rot = labels_rot, direction = legend_direction,
legend_width = legend_width, legend_height = legend_height, title_position = title_position, by_row = by_row)
}
if(plot) {
draw(gf, ...)
}
return(invisible(gf))
})