r3Dmol

Aiden Hewitt

What is r3Dmol?

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 fit
  m_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 grey
  m_set_style(
    style = m_style_cartoon(color = "grey")) %>%

  # Step 2: color beta sheets
  m_set_style(
    sel   = m_sel(ss = "s"), # ss = "s" for beta sheets
    style = m_style_cartoon(color = "blue")) %>%

  # Step 3: color alpha helices separately
  m_set_style(
    sel   = m_sel(ss = "h"), # ss = "h" for alpha helix
    style = 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 styling
r3dmol(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

r3dmol(viewer_spec = m_viewer_spec(cartoonQuality = 10)) %>%
  m_add_model(data = pdb_data, format = "pdb") %>%
  m_zoom_to() %>%
  m_set_style(style = m_style_stick(colorScheme = "amino")) %>%
  m_spin()

Styling: Amino Acid Colors

Styling: CPK Element Colors

m_style_sphere() defaults to CPK — coloring by element

r3dmol(viewer_spec = m_viewer_spec(cartoonQuality = 10)) %>%
  m_add_model(data = pdb_data, format = "pdb") %>%
  m_zoom_to() %>%
  m_set_style(style = m_style_sphere()) %>%
  m_spin()
  • Gray = Carbon
  • Red = Oxygen
  • Blue = Nitrogen
  • Yellow = Sulfur

Styling: CPK Element Colors

Styling: Secondary Structure

m_style_cartoon() reveals alpha helices and beta sheets

r3dmol(viewer_spec = m_viewer_spec(cartoonQuality = 10)) %>%
  m_add_model(data = pdb_data, format = "pdb") %>%
  m_zoom_to() %>%
  m_set_style(style = m_style_cartoon(color = "grey")) %>%
  m_set_style(sel = m_sel(ss = "h"),
              style = m_style_cartoon(color = "red")) %>%
  m_set_style(sel = m_sel(ss = "s"),
              style = m_style_cartoon(color = "blue")) %>%
  m_spin()

Styling: Secondary Structure

Styling: Layering Representations

m_add_style() layers on top of existing styles

r3dmol(viewer_spec = m_viewer_spec(cartoonQuality = 10)) %>%
  m_add_model(data = pdb_data, format = "pdb") %>%
  m_zoom_to() %>%
  m_set_style(style = m_style_cartoon()) %>%
  m_add_style(style = m_style_sphere(opacity = 0.4)) %>%
  m_spin()

Styling: Layering Representations

Styling: Coloring by Chain

colorScheme = "chain" distinguishes Nup62, Nup58, and Nup54

r3dmol(viewer_spec = m_viewer_spec(cartoonQuality = 10)) %>%
  m_add_model(data = pdb_data, format = "pdb") %>%
  m_zoom_to() %>%
  m_set_style(sel = m_sel(chain = "A"), style = m_style_cartoon(color = "#4E9AF1")) %>%
  m_set_style(sel = m_sel(chain = "B"), style = m_style_cartoon(color = "#E05C5C")) %>%
  m_set_style(sel = m_sel(chain = "C"), style = m_style_cartoon(color = "#50C878")) %>%
  m_add_style(style = m_style_sphere(colorScheme = "chain", opacity = 0.4)) %>%
  m_spin()

Styling: Coloring by Chain

A Full R Pipeline

  • randomly mutate a protein of interest’s DNA
  • Put that mutant DNA inside an organism
  • Sequence their DNA at that protein
  • Read in DNA sequence data
  • Align sequences, calculate differences
  • Convert DNA sequences into protein sequences
  • Determine highly conserved regions
  • Display conserved regions in 3D structure
  • Save as .html files for integration into quarto or websites