Skip to content

Commit eb03779

Browse files
author
“edo98811”
committed
layout experiments
1 parent bf9c2cf commit eb03779

File tree

1 file changed

+93
-23
lines changed

1 file changed

+93
-23
lines changed

R/enrichment_map_static.R

+93-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#' cluster_walktrap sizes groups delete_vertices as_edgelist membership
2222
#' layout_with_kk
2323
#' @importFrom scales alpha
24-
#' @importFrom BioNAR layoutByCluster
2524
#'
2625
#' @seealso [enrichment_map()] is used to generate the input igraph object.
2726
#' Also, [GeneTonic()] embeds an interactive visualization for the enrichment
@@ -103,8 +102,7 @@ plot_emap_static <- function(emg,
103102
stopifnot(is(emg, "igraph"))
104103

105104
what_attrs_are_in <- vertex_attr_names(emg)
106-
107-
105+
108106
## TODO: We would ideally need to start from here, possibly "just transferring the"
109107
## colors from the "interactive schemes"
110108

@@ -113,12 +111,10 @@ plot_emap_static <- function(emg,
113111

114112
V(emg)$color <- V(emg)$color.highlight
115113

116-
117114
## TODO: this would ideally require a different number of values?
118115
## TODO: in the example, E(emg) is 199 edges, but omm$value is
119116
#### 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)
122118
E(emg)$width_not_scaled <- E(emg)$width * 0.5
123119

124120
# Moved here to highlight the difference between the emapplot static and not emapplot
@@ -141,7 +137,7 @@ plot_emap_static <- function(emg,
141137
# We create a layout dummy object to use artifcially created
142138
emg_layout <- emg
143139
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)
145141
})
146142

147143
# A dataframe with the annotation of each cluster is created
@@ -153,15 +149,49 @@ plot_emap_static <- function(emg,
153149
V(emg)$cluster_label <-
154150
as.factor(cluster_labels[match(V(emg)$membership, names(cluster_labels))])
155151

152+
print(names(igraph::vertex_attr(emg)))
153+
print(names(igraph::edge_attr(emg)))
154+
156155
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)
158156

159157
message("GeneTonicInfo: found ", length(table(mem.df$membership)),
160158
" 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
161175

162-
# lay <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
163176

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,
165195
layout = "manual",
166196
x = lay[, 1],
167197
y = lay[, 2]) +
@@ -177,16 +207,17 @@ plot_emap_static <- function(emg,
177207
aes(.data$x, .data$y,
178208
fill= .data$cluster_label,
179209
## 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+
),
182213
label.fill = scales::alpha("white", 0.1),
183-
concavity = 10,
184-
expand = unit(7, "mm"),
214+
concavity = 1000,
215+
expand = unit(3, "mm"),
185216
alpha = 0.15
186217
) +
187218

188219
## 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
190221
# ggplot2::scale_fill_identity() +
191222
ggplot2::scale_size_continuous(range = c(7, 25)) + # Adjust min and max sizes
192223

@@ -197,27 +228,32 @@ plot_emap_static <- function(emg,
197228

198229
# ggrepel::geom_label_repel(data = cluster_centers,
199230
# aes(x = X1, y = X2, label = cluster_labels),
200-
# size = 5, fontface = "bold",
231+
# size = 2,
201232
# color = "black", fill = alpha("white", .15),
202233
# 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")
205240

206241
return(gp)
207242
}
208243

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) {
210246
if(as.numeric(membership[which(names(membership) == row[1])]) ==
211247
as.numeric(membership[which(names(membership) == row[2])])){
212-
weight <- weigth.within
248+
weight <- weigth_within
213249
}else{
214-
weight <- weight.between
250+
weight <- weight_between
215251
}
216252

217253
return(weight)
218254
}
219255

220-
256+
# To create the names of the clusters
221257
add_cluster_names <- function(emg, gs_communities, n_words = 4) {
222258

223259
stop_words <- c("the", "and", "is", "in", "to", "of",
@@ -259,6 +295,40 @@ add_cluster_names <- function(emg, gs_communities, n_words = 4) {
259295
return(cluster_labels)
260296
}
261297

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+
262332
# emg_layout$layout <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
263333
# emg$layout <- igraph::layout_with_fr(emg_layout,weights=E(emg_layout)$weight)
264334

@@ -286,7 +356,7 @@ add_cluster_names <- function(emg, gs_communities, n_words = 4) {
286356
# cluster_label = cluster_labels[match(colnames(cluster_centers), names(cluster_labels))]
287357
# )
288358
# 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)
290360

291361
# ggplot2::geom_text(
292362
# data = centroid_df, # Add the centroids as a data source

0 commit comments

Comments
 (0)