Graph visualization

Toru Maruyama

2023-12-08

library(CellInteractomeR)
library(neo2R)
library(kableExtra)
library(tidyverse)
library(visNetwork)
library(igraph)
library(ggraph)
library(tidygraph)
## ℹ Loading CellInteractomeR
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::%--%()      masks CellInteractomeR::%--%()
## ✖ dplyr::as_data_frame() masks tibble::as_data_frame(), CellInteractomeR::as_data_frame()
## ✖ purrr::compose()       masks CellInteractomeR::compose()
## ✖ tidyr::crossing()      masks CellInteractomeR::crossing()
## ✖ dplyr::filter()        masks CellInteractomeR::filter(), stats::filter()
## ✖ dplyr::group_rows()    masks kableExtra::group_rows()
## ✖ dplyr::lag()           masks stats::lag()
## ✖ lubridate::show()      masks CellInteractomeR::show(), methods::show()
## ✖ purrr::simplify()      masks CellInteractomeR::simplify()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'igraph'
## 
## 
## The following objects are masked from 'package:lubridate':
## 
##     %--%, union
## 
## 
## The following objects are masked from 'package:dplyr':
## 
##     as_data_frame, groups, union
## 
## 
## The following objects are masked from 'package:purrr':
## 
##     compose, simplify
## 
## 
## The following object is masked from 'package:tidyr':
## 
##     crossing
## 
## 
## The following object is masked from 'package:tibble':
## 
##     as_data_frame
## 
## 
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## 
## 
## The following object is masked from 'package:base':
## 
##     union
## 
## 
## 
## Attaching package: 'tidygraph'
## 
## 
## The following object is masked from 'package:igraph':
## 
##     groups
## 
## 
## The following objects are masked from 'package:CellInteractomeR':
## 
##     create_tree, groups
## 
## 
## The following object is masked from 'package:stats':
## 
##     filter

Connect database

  • url: http://localhost:7474 is the default URL set in Neo4j
  • please use your username and password instead
con = connect_database(
  url = "http://localhost:7474",
  username = "neo4j",
  password = "yourpassword"
)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## ℹ The deprecated feature was likely used in the CellInteractomeR package.
##   Please report the issue to the authors.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Create network object

CellInteractomeR has functions to query the database and return objects which can be used in graph visualization libraries such as visNetwork, igraph and ggraph. The following functions are currently prepared.

  • network_single_entity(): Get network from single entity
  • network_multi_entity(): Get network among selected entities

network_single_entity()

Visualize w/ visNetwork

n = network_single_entity(
  paths = c("Cell - CORRELATE_WITH - Microbe", "Cell - SPECIFICALLY_EXPRESS - Gene - RECEPTOR - Metabolite"),
  target_entity_class = "Cell",
  target_entity_name = "BEST4+ epithelial",
  states = list(Disease = c("UC")),
  con,
  config,
  threshold,
  output = "visNetwork"
)

visNetwork(n$nodes, n$edges) %>%
  visIgraphLayout(layout="layout_with_fr") %>%
  visPhysics(enabled = F) %>%
  visOptions(
    highlightNearest = list(enabled = T, degree = 1, algorithm = "hierarchical"),
    nodesIdSelection = list(enabled = T, style = "width: 0px; height: 0px")
  )

Visualize w/ igraph/ggraph

g = network_single_entity(
  paths = c("Cell - CORRELATE_WITH - Microbe", "Cell - SPECIFICALLY_EXPRESS - Gene - RECEPTOR - Metabolite"),
  target_entity_class = "Cell",
  target_entity_name = "BEST4+ epithelial",
  states = list(Disease = c("UC")),
  con,
  config,
  threshold,
  output = "igraph"
)

plot(g)

# draw graph with ggraph
ggraph(g, layout="igraph", algorithm="kk") + 
  geom_edge_link(aes(color=type), alpha=0.5) +
  geom_node_point(aes(color=type)) +
  geom_node_text(aes(label=name), size=1.5, nudge_y=0.1) + 
  scale_edge_color_manual(values=config$relation_path_color) +
  theme_void()
## Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

network_multi_entity()

Visualize w/ visNetwork

cells = get_entity_properties(con, "Cell", "id") %>% na.omit()
metabolites = c("butyrate", "propionate", "cholate")

n = network_multi_entity(
  paths = c("Cell - SPECIFICALLY_EXPRESS - Gene - RECEPTOR - Metabolite", "Cell - CORRELATE_WITH - Metabolite"),
  target_entities = list(Cell = cells, 
                         Metabolite = metabolites),
  states = list(Disease = c("UC")),
  con,
  config,
  threshold,
  output = "visNetwork"
)

visNetwork(n$nodes, n$edges) %>%
  visIgraphLayout(layout="layout_with_fr") %>%
  visPhysics(enabled = F) %>%
  visOptions(
    highlightNearest = list(enabled = T, degree = 1, algorithm = "hierarchical"),
    nodesIdSelection = list(enabled = T, style = "width: 0px; height: 0px")
  )

Visualize w/ igraph/ggraph

g = network_multi_entity(
  paths = c("Cell - SPECIFICALLY_EXPRESS - Gene - RECEPTOR - Metabolite", "Cell - CORRELATE_WITH - Metabolite"),
  target_entities = list(Cell = cells, 
                         Metabolite = metabolites),
  states = list(Disease = c("UC")),
  con,
  config,
  threshold,
  output = "igraph"
)

plot(g)

# draw graph with ggraph
ggraph(g, layout="igraph", algorithm="kk") + 
  geom_edge_link(aes(color=type), alpha=0.5) +
  geom_node_point(aes(color=type)) +
  geom_node_text(aes(label=name), size=1.5, nudge_y=0.1) + 
  scale_edge_color_manual(values=config$relation_path_color) +
  theme_void()