a b/R/NewGgraph.R
1
#' Generate a graphical representation of pathway gene maps
2
#'
3
#' This function merges multiple gene-pathway related dataframes, processes them
4
#' for graph creation, and visualizes the relationships in a dendrogram layout using
5
#' the provided node and edge gathering functions from the 'ggraph' package.
6
#'
7
#' @param BP_dataframe Dataframe for Biological Process.
8
#' @param BP_ids IDs for Biological Process.
9
#' @param KEGG_dataframe Dataframe for KEGG pathways.
10
#' @param KEGG_ids IDs for KEGG pathways.
11
#' @param MF_dataframe Dataframe for Molecular Function.
12
#' @param MF_ids IDs for Molecular Function.
13
#' @param REACTOME_dataframe Dataframe for REACTOME pathways.
14
#' @param REACTOME_ids IDs for REACTOME pathways.
15
#' @param CC_dataframe Dataframe for Cellular Component.
16
#' @param CC_ids IDs for Cellular Component.
17
#' @param DO_dataframe Dataframe for Disease Ontology.
18
#' @param DO_ids IDs for Disease Ontology.
19
#' @importFrom tidygraph tbl_graph
20
#' @importFrom ggraph ggraph geom_edge_diagonal geom_node_point geom_node_text scale_edge_colour_brewer node_angle
21
#' @importFrom ggplot2 theme element_rect scale_size scale_color_brewer coord_cartesian
22
#' @return A 'ggraph' object representing the pathway gene map visualization.
23
#' @export
24
#' 
25
new_ggraph <- function(BP_dataframe, BP_ids, KEGG_dataframe, KEGG_ids,
26
                       MF_dataframe, MF_ids, REACTOME_dataframe, REACTOME_ids,
27
                       CC_dataframe, CC_ids, DO_dataframe, DO_ids) {
28
29
  new_dataframe <- gene_map_pathway(BP_dataframe, BP_ids, KEGG_dataframe, KEGG_ids,
30
                                    MF_dataframe, MF_ids, REACTOME_dataframe, REACTOME_ids,
31
                                    CC_dataframe, CC_ids, DO_dataframe, DO_ids)
32
33
  # Prepare the data for graph creation using 'ggraph'
34
  index_ggraph <- c("type", "pathway", "gene")  # columns other than the lowest level
35
  nodes_ggraph <- gather_graph_node(new_dataframe, index = index_ggraph, root = "combination")
36
  edges_ggraph <- gather_graph_edge(new_dataframe, index = index_ggraph, root = "combination")
37
38
  # Create and plot the graph using 'tidygraph' and 'ggraph'
39
  graph_ggraph <- tidygraph::tbl_graph(nodes = nodes_ggraph, edges = edges_ggraph)
40
41
  plot <- ggraph::ggraph(graph_ggraph, layout = 'dendrogram', circular = TRUE) +
42
    ggraph::geom_edge_diagonal(aes(color = .data$node1.node.branch, filter = .data$node1.node.level != "combination", alpha = .data$node1.node.level), edge_width = 1) +
43
    ggraph::geom_node_point(aes(size = .data$node.size, color = .data$node.branch, filter = .data$node.level != "combination"), alpha = 0.45) +
44
    ggplot2::scale_size(range = c(15, 90)) +
45
    ggplot2::theme(legend.position = "none") +
46
    ggraph::scale_edge_colour_brewer(palette= "Dark2") +
47
    ggplot2::scale_color_brewer(palette = "Dark2") +
48
    ggraph::geom_node_text(aes(x = 1.058 * .data$x, y = 1.058 * .data$y, label = .data$node.short_name, angle = -((-ggraph::node_angle(.data$x, .data$y) + 90) %% 180) + 60, filter = .data$leaf, color = .data$node.branch), size = 4, hjust = 'outward') +
49
    ggraph::geom_node_text(aes(label = .data$node.short_name, filter = !.data$leaf & (.data$node.level == "type"), color = .data$node.branch), fontface = "bold", size = 8, family = "sans") +
50
    ggraph::geom_node_text(aes(label = .data$node.short_name, filter = !.data$leaf & (.data$node.level == "pathway"), color = .data$node.branch, angle = -((-ggraph::node_angle(.data$x, .data$y) + 90) %% 180) + 36), fontface = "bold", size = 4.5, family = "sans") +
51
    ggplot2::theme(panel.background = ggplot2::element_rect(fill = NA)) +
52
    ggplot2::coord_cartesian(xlim = c(-1.3, 1.3), ylim = c(-1.3, 1.3))
53
54
  return(plot)
55
}