-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_functions.R
219 lines (179 loc) · 5.78 KB
/
plot_functions.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
normalize_feature_values <- function(feature_values) {
unique_values <- sort(unique(feature_values))
if (length(unique_values) == 1) {
return(rep(1, length(feature_values)))
}
if (is.numeric(unique_values)) {
low <- min(unique_values)
high <- max(unique_values)
return(unlist(lapply(
feature_values,
function(x) {
(x - low) / (high - low)
})))
} else if (is.character(unique_values)) {
return(unlist(lapply(
feature_values,
function(x) {
(which(unique_values == x) - 1) / (length(unique_values) - 1)
})))
}
}
generate_sample_hsv_map <- function(
pheno_features,
seed = 10,
hue_shift = .0) {
pf <- pheno_features[!duplicated(pheno_features), , drop = FALSE]
normalized_pheno_features <- apply(pf[, -4], 2, normalize_feature_values)
if (!is.matrix(normalized_pheno_features)) {
normalized_pheno_features <- t(as.matrix(normalized_pheno_features))
}
sample_colors <- setNames(
apply(normalized_pheno_features, 1, function(x){
hsv(x[1], x[2], x[3], 1)
}),
pf[, 4])
return(sample_colors[sort(names(sample_colors))])
}
get_color_matrix <- function(
cell_colors,
expressionSet,
cell_clusters,
read_counts) {
if (!identical(cell_colors, "none")) {
cell_colors_matrix <- do.call(cbind.data.frame, lapply(
cell_colors,
function(x) {
if (x %in% colnames(pData(expressionSet)))
return(as.factor(pData(expressionSet)[, x]))
if (x %in% cell_clusters$names())
return(as.factor(cell_clusters$get(x)$cell_ids))
if (x %in% rownames(read_counts))
return(as.double(read_counts[x, ]))
return(rep(NA, nrow(expressionSet)))
}))
colnames(cell_colors_matrix) <- cell_colors
rownames(cell_colors_matrix) <- colnames(expressionSet)
cell_colors_matrix <- cell_colors_matrix[, colnames(cell_colors_matrix), drop = FALSE]
} else {
cell_colors_matrix <- NA
}
return(cell_colors_matrix)
}
colorize <- function(
label,
values,
color_maps,
outlier_quantile_t = NA) {
if (label %in% names(color_maps)) {
if (label %in% names(color_maps) & is.double(values)) {
if (identical(outlier_quantile_t, NA)) {
values <- values / max(values)
} else {
q <- quantile(x = value, outlier_quantile_t)
values <- values / q
values[which(values > 1)] <- 1
}
values <- as.integer(1+(length(color_maps[[label]])-1)*values)
}
cols <- color_maps[[label]][values]
} else {
# use same default colors as pheatmap
cols <- scales::dscale(
factor(1:length(unique(values))),
scales::hue_pal(l = 75))[values]
}
setNames(
cols,
values)
}
plot_color_graph <- function(
gf,
lay,
colors = NA,
label = NA,
shape = "circle",
vertex.size = 8,
rescale = TRUE,
vertex.frame.color = 'black',
edge.width = 1,
edge.color = 'grey',
shuffle = FALSE,
vertex.plot.order = NA,
...) {
require(igraph)
V(gf)$color=colors
labels=NA
if(!is.na(label[1])){
labels=label
}
if(length(shape)==1){
shape <- rep(shape, vcount(gf))
}
if(length(vertex.frame.color)==1){
vertex.frame.color <- rep(vertex.frame.color, vcount(gf))
}
if(length(vertex.size)==1){
vertex.size <- rep(vertex.size, vcount(gf))
}
# no rescaling for the graph, so the coordinates must belong to [-1, 1]**2
lay <- t(apply(lay, 1, function(r){r - colMeans(lay)}))
lay <- lay / max(abs(lay))
# Change vertex z-order if needed
if(!identical(vertex.plot.order, NA) | shuffle){
if(shuffle)
new_order = sample(vcount(gf))
if(!identical(vertex.plot.order, NA))
new_order = vertex.plot.order
adj_mat = igraph::as_adjacency_matrix(gf, type="both", attr="weight", names=TRUE, sparse=TRUE)
adj_mat <- adj_mat[new_order, new_order]
gf <- igraph::graph_from_adjacency_matrix(adj_mat, mode="undirected", weighted="weight")
lay <- lay[new_order, , drop=F]
colors <- colors[new_order]
vertex.size <- vertex.size[new_order]
label <- label[new_order]
shape <- shape[new_order]
vertex.frame.color <- vertex.frame.color[new_order]
}
igraph::plot.igraph(
gf,
layout = lay,
vertex.color = colors,
vertex.size = vertex.size,
vertex.label = label,
# vertex.label.dist = .0,
# vertex.label.color = 'grey',
# vertex.label.cex = .2,
vertex.shape = shape,
vertex.frame.color = vertex.frame.color,
edge.width = edge.width,
edge.color = edge.color,
rescale = rescale,
...)
}
#' Build and name color palette vectors
#'
#' @name brew_more_colors
#' @param ids character or integer vector. The names that will be associated with the colors.
#' @param palette_name character string. The name of the RColorBrewer palette that will be extrapolated if needed. See \code{RColorBrewer::brewer.pal.info} for list of available colors.
#' @export brew_more_colors
brew_more_colors <- function(
ids,
palette_name = rownames(
RColorBrewer::brewer.pal.info)) {
palette_name <- match.arg(palette_name)
palette <- RColorBrewer::brewer.pal(
RColorBrewer::brewer.pal.info[palette_name, "maxcolors"],
palette_name)
n <- length(ids)
if (n <= length(palette) &
RColorBrewer::brewer.pal.info[palette_name, "category"] == "qual") {
return(setNames(
palette[seq(n)],
ids))
} else {
return(setNames(
colorRampPalette(palette)(n),
ids))
}
}