Intro Thoughts
Some really naive chem X ggraph experiments
library(tidyverse)
#CH₃COCH₃
# edgelist_el
acetone_el <- tribble(~from, ~to, ~num_bonds,
"C1", "H1", 1,
"C1", "H2", 1,
"C1", "H3", 1,
"C1", "C2", 1,
"C2", "O1", 2,
"C2", "C3", 1,
"C3", "H4", 1,
"C3", "H5", 1,
"C3", "H6", 1)
library(ggraph)
acetone_el |>
ggraph(layout = "kk") +
geom_edge_link() +
geom_node_label(aes(label = name |> str_remove("\\d")))

acetone_el |>
filter(to |> str_detect("H", negate = T)) |>
ggraph(layout = "kk") +
geom_edge_link() +
geom_node_point(size = 8) +
geom_node_label(aes(label = name |> str_remove("\\d")))

acetone_el |>
filter(to |> str_detect("H", negate = T)) |>
tidygraph::as_tbl_graph() |>
mutate(deg = tidygraph::centrality_degree()) |>
ggraph(layout = "kk") +
geom_edge_link(aes(edge_width = num_bonds)) +
geom_node_point(size = 9, color = "grey92", aes(alpha = deg!=2)) +
geom_node_text(aes(label = name |> str_remove("\\d") |> str_remove("C")))

Looking at ggchemplot
# Install ggchemplot from GitHub
# remotes::install_github("JPFQueiroz/ggchemplot")
library(ggchemplot)
# Path to the example file
sdf_s_adenosyl_methionine <- system.file("extdata", "SAM.sdf",
package = "ggchemplot")
# Parse SDF file and generate the initial data object
p1 <- ggchemplot1(sdf_file = sdf_s_adenosyl_methionine,
collapse_hydrogens = TRUE,
paint_it_black = TRUE,
atom_size = 6,
label_size = 14,
normalize = TRUE,
target_bond_length = 1.5
)
# readLines(file_path, warn = FALSE)
layer by layer (with codehover :-))
# Plot the structure
p2 <- p1 %>%
ggchemplot2() +
labs(title = expression(italic(S)*"-adenosyl-"*scriptstyle(L)*"-methionine"))
p2

{ggplot() +
p2@layers[[1]] +
p2@layers[[2]] +
p2@layers[[3]] +
p2@layers[[4]] +
p2@layers[[5]] +
p2@layers[[6]] +
p2@layers[[7]] +
p2@layers[[8]]} |>
codehover::ch_hover()
| ggplot() + |
| p2@layers[[1]] + |
| p2@layers[[2]] + |
| p2@layers[[3]] + |
| p2@layers[[4]] + |
| p2@layers[[5]] + |
| p2@layers[[6]] + |
| p2@layers[[7]] + |
| p2@layers[[8]] |
p2@layers
## $geom_segment
## mapping: x = ~.data$x1s, y = ~.data$y1s, xend = ~.data$x2s, yend = ~.data$y2s
## geom_segment: na.rm = FALSE, arrow = NULL, arrow.fill = NULL, lineend = butt, linejoin = round
## stat_identity: na.rm = FALSE
## position_identity
##
## $geom_wedge
## mapping: x1 = ~.data$x1s, y1 = ~.data$y1s, x2 = ~.data$x2s, y2 = ~.data$y2s, wedgetype = ~.data$bond_type, width = ~.data$wedge_w, n_hashes = ~.data$n_hashes_adapt, wedge_thickness = ~.data$hash_thick
## geom_wedge: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
##
## $geom_segment...3
## mapping: x = ~.data$x1s, y = ~.data$y1s, xend = ~.data$x2s, yend = ~.data$y2s
## geom_segment: na.rm = FALSE, arrow = NULL, arrow.fill = NULL, lineend = butt, linejoin = round
## stat_identity: na.rm = FALSE
## position_identity
##
## $geom_segment...4
## mapping: x = ~.data$ox1, y = ~.data$oy1, xend = ~.data$ox2, yend = ~.data$oy2
## geom_segment: na.rm = FALSE, arrow = NULL, arrow.fill = NULL, lineend = butt, linejoin = round
## stat_identity: na.rm = FALSE
## position_identity
##
## $geom_point
## mapping: x = ~.data$x, y = ~.data$y
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
##
## $geom_text
## mapping: x = ~.data$x, y = ~.data$y, label = ~.data$label_draw
## geom_text: na.rm = FALSE, parse = TRUE, check_overlap = FALSE, size.unit = pt
## stat_identity: na.rm = FALSE
## position_nudge
##
## $geom_text...7
## mapping: x = ~.data$hx, y = ~.data$hy, label = H, colour = ~.data$color
## geom_text: na.rm = FALSE, parse = FALSE, check_overlap = FALSE, size.unit = pt
## stat_identity: na.rm = FALSE
## position_nudge
##
## $geom_text...8
## mapping: x = ~.data$hx, y = ~.data$hy, label = ~.data$lab, colour = ~.data$color
## geom_text: na.rm = FALSE, parse = FALSE, check_overlap = FALSE, size.unit = pt
## stat_identity: na.rm = FALSE
## position_nudge
exploratory experiments… 🙃🤷♀️
library(tidyverse)
# first bit of ggchemplot1
sdf_file_to_atoms_df <- function (sdf_file, use_sdf_stereo = TRUE, title = NULL, collapse_hydrogens = FALSE,
rotation = 0, flip_horizontal = FALSE, flip_vertical = FALSE,
label_padding = NULL, show_atom_circles = TRUE, hide_carbon_circles = TRUE,
circle_stroke = 0, show_atom_labels = TRUE, hide_carbon_labels = TRUE,
bond_width = NULL, atom_size = NULL, label_size = NULL, double_bond_offset = NULL,
custom_atom_colors = NULL, paint_it_black = FALSE, bond_color = "black",
atom_circle_color = "transparent", H_offset = NULL, label_fontface = "plain",
normalize = TRUE, target_bond_length = 1)
{
ggchemplot1(sdf_file,
collapse_hydrogens = TRUE,
paint_it_black = TRUE,
atom_size = 6,
label_size = 14,
normalize = TRUE,
target_bond_length = 1.5
)$atoms
}
sdf_s_adenosyl_methionine |>
sdf_file_to_atoms_df() |>
ggplot() +
aes(x = x, y = y, label = symbol, color = I(color)) +
geom_text()

sdf_file_to_bonds_df <- function(sdf_file, use_sdf_stereo = TRUE, title = NULL, collapse_hydrogens = FALSE,
rotation = 0, flip_horizontal = FALSE, flip_vertical = FALSE,
label_padding = NULL, show_atom_circles = TRUE, hide_carbon_circles = TRUE,
circle_stroke = 0, show_atom_labels = TRUE, hide_carbon_labels = TRUE,
bond_width = NULL, atom_size = NULL, label_size = NULL, double_bond_offset = NULL,
custom_atom_colors = NULL, paint_it_black = FALSE, bond_color = "black",
atom_circle_color = "transparent", H_offset = NULL, label_fontface = "plain",
normalize = TRUE, target_bond_length = 1){
ggchemplot1(sdf_file,
collapse_hydrogens = TRUE,
paint_it_black = TRUE,
atom_size = 6,
label_size = 14,
normalize = TRUE,
target_bond_length = 1.5
)$bond_coords
}
sdf_s_adenosyl_methionine |>
sdf_file_to_bonds_df() |>
ggplot() +
aes(x = x1, y = y1, xend = x2, yend = y2) +
geom_segment()

ggchem <- function(file_path){
file_path |>
sdf_file_to_atoms_df() |>
mutate(.file_path = file_path) |> # sneak in the file path name
ggplot()
}
geom_bonds <- function(){
geom_segment(data = . %>% .[1,".file_path"] %>% sdf_file_to_bonds_df(), # pretty nonstandard move here... 😬
mapping = aes(x = x1, y = y1, xend = x2, yend = y2),
inherit.aes = FALSE
)
}
geom_atoms <- function(){
geom_text(mapping = aes(x = x, y = y, label = symbol, color = I(color)))
}
{sdf_s_adenosyl_methionine |>
ggchem() +
geom_bonds() + # maybe inherit.aes false here, and then
geom_atoms() +
theme_void()} |>
codehover::ch_hover()
| ggchem(sdf_s_adenosyl_methionine) + |
| geom_bonds() + |
| geom_atoms() + |
| theme_void() |
Next: look at ggchemplot2 layer creation!