An R wrapper around 3Dmol.js — a WebGL-powered molecular viewer
Visualize proteins, DNA, and small molecules
Works in RStudio, R Markdown, Quarto, Shiny
No javascript or python needed — stay in R
On CRAN since 2021 but development stopped in 2023
install.packages("r3dmol")# or dev version:devtools::install_github("swsoyee/r3dmol")
Visualizing a Small Protein
library(r3dmol)pdb_id <-"1CRN"pdb_url <-paste0("https://files.rcsb.org/download/", pdb_id, ".pdb")pdb_data <-paste(readLines(url(pdb_url)), collapse ="\n")r3dmol(viewer_spec =m_viewer_spec(cartoonQuality =10)) %>%m_add_model(data = pdb_data, format ="pdb") %>%m_zoom_to() %>%# zoom to fitm_spin() # protein structures are 3D
The Result
Targeting Specific Regions
set a base style first, then override selections
r3dmol(viewer_spec =m_viewer_spec(cartoonQuality =10)) %>%m_add_model(data = pdb_data, format ="pdb") %>%m_zoom_to() %>%# Step 1: base style — everything greym_set_style(style =m_style_cartoon(color ="grey")) %>%# Step 2: color beta sheetsm_set_style(sel =m_sel(ss ="s"), # ss = "s" for beta sheetsstyle =m_style_cartoon(color ="blue")) %>%# Step 3: color alpha helices separatelym_set_style(sel =m_sel(ss ="h"), # ss = "h" for alpha helixstyle =m_style_cartoon(color ="red",arrows =TRUE)) %>%m_spin()
The Result
A Nuclear Pore Protein: Nup62•Nup58•Nup54
5C3L — the transport channel heterotrimer that gates the nuclear pore
pdb_id <-"5C3L"pdb_url <-paste0("https://files.rcsb.org/download/", pdb_id, ".pdb")pdb_data <-paste(readLines(url(pdb_url)), collapse ="\n")# default sticks — raw PDB with no stylingr3dmol(viewer_spec =m_viewer_spec(cartoonQuality =10)) %>%m_add_model(data = pdb_data, format ="pdb") %>%m_zoom_to() %>%m_spin()
The Result
Styling: Amino Acid Colors
colorScheme = "amino" colors each residue type distinctly