21
21
# ' cluster_walktrap sizes groups delete_vertices as_edgelist membership
22
22
# ' layout_with_kk
23
23
# ' @importFrom scales alpha
24
- # ' @importFrom BioNAR layoutByCluster
25
24
# '
26
25
# ' @seealso [enrichment_map()] is used to generate the input igraph object.
27
26
# ' Also, [GeneTonic()] embeds an interactive visualization for the enrichment
@@ -103,8 +102,7 @@ plot_emap_static <- function(emg,
103
102
stopifnot(is(emg , " igraph" ))
104
103
105
104
what_attrs_are_in <- vertex_attr_names(emg )
106
-
107
-
105
+
108
106
# # TODO: We would ideally need to start from here, possibly "just transferring the"
109
107
# # colors from the "interactive schemes"
110
108
@@ -113,12 +111,10 @@ plot_emap_static <- function(emg,
113
111
114
112
V(emg )$ color <- V(emg )$ color.highlight
115
113
116
-
117
114
# # TODO: this would ideally require a different number of values?
118
115
# # TODO: in the example, E(emg) is 199 edges, but omm$value is
119
116
# ### E(emg)$width_not_scaled <- omm$value * scale_edges_width
120
- edge_attrs <- igraph :: edge_attr_names(emg )
121
-
117
+ # edge_attrs <- igraph::edge_attr_names(emg)
122
118
E(emg )$ width_not_scaled <- E(emg )$ width * 0.5
123
119
124
120
# Moved here to highlight the difference between the emapplot static and not emapplot
@@ -141,7 +137,7 @@ plot_emap_static <- function(emg,
141
137
# We create a layout dummy object to use artifcially created
142
138
emg_layout <- emg
143
139
E(emg_layout )$ weight <- apply(igraph :: as_edgelist(emg_layout ), 1 , function (row ) {
144
- weight.community (as.character(row ), igraph :: membership(gs_communities ), 3 , 1 )
140
+ weight_community (as.character(row ), igraph :: membership(gs_communities ), 5 , 2 )
145
141
})
146
142
147
143
# A dataframe with the annotation of each cluster is created
@@ -153,15 +149,49 @@ plot_emap_static <- function(emg,
153
149
V(emg )$ cluster_label <-
154
150
as.factor(cluster_labels [match(V(emg )$ membership , names(cluster_labels ))])
155
151
152
+ print(names(igraph :: vertex_attr(emg )))
153
+ print(names(igraph :: edge_attr(emg )))
154
+
156
155
mem.df <- data.frame (names = V(emg )$ name ,membership = as.numeric(V(emg )$ membership ))
157
- lay <- BioNAR :: layoutByCluster(emg_layout , mem.df , layout = igraph :: layout_with_kk )
158
156
159
157
message(" GeneTonicInfo: found " , length(table(mem.df $ membership )),
160
158
" clusters of genesets" )
159
+
160
+
161
+ # ### Layout experiments
162
+ # browser()
163
+ E(emg_layout )$ weigth_to_use_for_layout <- log(E(emg_layout )$ width ) * E(emg_layout )$ weight
164
+
165
+ # hist((E(emg_layout)$weigth_to_use))
166
+ # lay <- layout_by_cluster(emg_layout, mem.df, layout = igraph::layout_with_fr)
167
+ lay <- layout_by_cluster(emg_layout , mem.df , layout = igraph :: layout_with_kk )
168
+ # lay <- layout_by_cluster(emg_layout, mem.df, layout = igraph::layout_with_graphopt)
169
+ # lay <- layout_by_cluster(emg, mem.df, layout = igraph::layout_with_graphopt)
170
+ # lay <- igraph::layout_with_fr(emg_layout) # ,weights=E(emg_layout)$weight)
171
+ # lay <- igraph::layout_with_graphopt(emg_layout)
172
+ # lay <- igraph::layout_with_kk(emg)
173
+ # lay <- igraph::component_wise(lay)
174
+ # lay <- igraph::layout_(emg, igraph::layout_with_kk(), igraph::component_wise()) # Source? https://igraph.org/r/html/1.3.0/layout_.html
161
175
162
- # lay <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
163
176
164
- gp <- ggraph :: ggraph(emg ,
177
+ # ### Artifical labels for the clusters
178
+ # cluster_centers <- data.frame(t(sapply(igraph::groups(gs_communities), function(nodes) {
179
+ # node_indices <- match(nodes, V(emg_layout)$name)
180
+ # centroid <- colMeans(lay[node_indices, , drop = FALSE])
181
+ # return(centroid)
182
+ # })))
183
+
184
+ # cluster_annotation <- merge(cluster_centers, as.data.frame(cluster_labels), by.x = "row.names", by.y = "row.names")
185
+ # igraph::vertex_attr_names(emg)
186
+ # community_colors <- rainbow(length(igraph::groups(gs_communities)))
187
+ # centroid_df <- data.frame(
188
+ # x = cluster_centers[1, ],
189
+ # y = cluster_centers[2, ],
190
+ # cluster_label = cluster_labels[match(colnames(cluster_centers), names(cluster_labels))]
191
+ # )
192
+
193
+ gp <-
194
+ ggraph :: ggraph(emg ,
165
195
layout = " manual" ,
166
196
x = lay [, 1 ],
167
197
y = lay [, 2 ]) +
@@ -177,16 +207,17 @@ plot_emap_static <- function(emg,
177
207
aes(.data $ x , .data $ y ,
178
208
fill = .data $ cluster_label ,
179
209
# # why not having the border of the hull too "colored in sync"
180
- color = .data $ cluster_label ,
181
- label = .data $ cluster_label ),
210
+ color = .data $ cluster_label ,
211
+ label = .data $ cluster_label
212
+ ),
182
213
label.fill = scales :: alpha(" white" , 0.1 ),
183
- concavity = 10 ,
184
- expand = unit(7 , " mm" ),
214
+ concavity = 1000 ,
215
+ expand = unit(3 , " mm" ),
185
216
alpha = 0.15
186
217
) +
187
218
188
219
# # handling the individual nodes at the end
189
- ggraph :: geom_node_point(aes(size = .data $ size , fill = I(V(emg )$ color )), shape = 21 , color = " black" ) + # I(V(emg_for_gggraph)$ # Adjust border thickness) + # Use `I()` to prevent scaling
220
+ ggraph :: geom_node_point(aes(size = .data $ original_size , fill = I(V(emg )$ color )), shape = 21 , color = " black" ) + # I(V(emg_for_gggraph)$ # Adjust border thickness) + # Use `I()` to prevent scaling
190
221
# ggplot2::scale_fill_identity() +
191
222
ggplot2 :: scale_size_continuous(range = c(7 , 25 )) + # Adjust min and max sizes
192
223
@@ -197,27 +228,32 @@ plot_emap_static <- function(emg,
197
228
198
229
# ggrepel::geom_label_repel(data = cluster_centers,
199
230
# aes(x = X1, y = X2, label = cluster_labels),
200
- # size = 5, fontface = "bold" ,
231
+ # size = 2 ,
201
232
# color = "black", fill = alpha("white", .15),
202
233
# label.size = 0.5, # Border thickness
203
- # label.padding = unit(0.2, "lines"))+
204
-
234
+ # label.padding = unit(0.2, "lines"))
235
+ # ggplot2::geom_text(data = cluster_centers,
236
+ # aes(x = X1, y = X2, label = cluster_labels),
237
+ # size = 3,
238
+ # color = "black",
239
+ # fontface = "bold")
205
240
206
241
return (gp )
207
242
}
208
243
209
- weight.community <- function (row , membership , weigth.within , weight.between ) {
244
+ # To create the artificial weights
245
+ weight_community <- function (row , membership , weigth_within , weight_between ) {
210
246
if (as.numeric(membership [which(names(membership ) == row [1 ])]) ==
211
247
as.numeric(membership [which(names(membership ) == row [2 ])])){
212
- weight <- weigth.within
248
+ weight <- weigth_within
213
249
}else {
214
- weight <- weight.between
250
+ weight <- weight_between
215
251
}
216
252
217
253
return (weight )
218
254
}
219
255
220
-
256
+ # To create the names of the clusters
221
257
add_cluster_names <- function (emg , gs_communities , n_words = 4 ) {
222
258
223
259
stop_words <- c(" the" , " and" , " is" , " in" , " to" , " of" ,
@@ -259,6 +295,40 @@ add_cluster_names <- function(emg, gs_communities, n_words = 4) {
259
295
return (cluster_labels )
260
296
}
261
297
298
+ # Layout by cluster (copied from BioNAR)
299
+ # For this also component_wise from igraph seems to be useful? but i could not make it work.https://igraph.org/r/html/1.3.0/component_wise.html
300
+ layout_by_cluster <- function (gg , mem , layout_to_use = igraph :: layout_with_kk )
301
+ {
302
+
303
+ get_cluster_subgraph_by_id <- function (clID , gg , mem )
304
+ {
305
+ idx <- which(mem == clID )
306
+ sg <- igraph :: induced_subgraph(gg , V(gg )[idx ], impl = " auto" )
307
+ return (sg )
308
+ }
309
+
310
+
311
+ Cn <- table(mem $ membership )
312
+
313
+ sgraphs <- lapply(names(Cn ), get_cluster_subgraph_by_id ,
314
+ gg = gg ,
315
+ mem = mem $ membership )
316
+
317
+ layouts <- lapply(sgraphs , function (sg ) {
318
+ if ((identical(layout_to_use , igraph :: layout_with_fr ) || identical(layout_to_use , igraph :: layout_with_kk )) && " weight_to_use_for_layout" %in% E(sg )) {
319
+ layout_to_use(sg , weights = E(sg )$ weigth_to_use_for_layout )
320
+ } else {
321
+ layout_to_use(sg )
322
+ }
323
+ })
324
+
325
+ lay <- igraph :: merge_coords(sgraphs , layouts )
326
+ ug <- igraph :: disjoint_union(sgraphs )
327
+ idx <- match(V(gg )$ name , V(ug )$ name )
328
+ lay <- lay [idx , ]
329
+ return (lay )
330
+ }
331
+
262
332
# emg_layout$layout <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
263
333
# emg$layout <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
264
334
@@ -286,7 +356,7 @@ add_cluster_names <- function(emg, gs_communities, n_words = 4) {
286
356
# cluster_label = cluster_labels[match(colnames(cluster_centers), names(cluster_labels))]
287
357
# )
288
358
# Remove clusters with 2 or fewer nodes
289
- # lay <-BioNAR::layoutByCluster (emg, mem.df, layout = igraph::layout_with_kk)
359
+ # lay <-BioNAR::layout_by_cluster (emg, mem.df, layout = igraph::layout_with_kk)
290
360
291
361
# ggplot2::geom_text(
292
362
# data = centroid_df, # Add the centroids as a data source
0 commit comments