nvelden / NGLVieweR

Visualize and interact with Protein Data Bank (pdb) and structural files in R and Shiny
https://nvelden.github.io/NGLVieweR/
Other
45 stars 4 forks source link

How to rotate the view? #11

Open slowkow opened 1 year ago

slowkow commented 1 year ago

Hi Niels,

Thank you for sharing this wonderful package. It's a joy to be able to visualize PDB structures with just a few lines of R code.

I'm writing to ask for your help with a simple question: how can we rotate the view of our structure?

Here is the example I am working with:

library(NGLVieweR)
library(magrittr)
my_sele <- "9:A"
NGLVieweR("2bvp") %>%
  stageParameters(backgroundColor = "white", zoomSpeed = 1) %>%
  addRepresentation(
    type = "cartoon"
  ) %>%
  addRepresentation(
    type = "ball+stick",
    param = list(
      sele = my_sele
    )
  ) %>%
  addRepresentation(
    type = "label",
    param = list(
      sele = my_sele,
      labelType = "format",
      labelFormat = "[%(resname)s]%(resno)s", # or enter custom text
      labelGrouping = "residue", # or "atom" (eg. sele = "20:A.CB")
      color = "black",
      fontFamiliy = "sans-serif",
      xOffset = 1,
      yOffset = 0,
      zOffset = 0,
      fixedSize = TRUE,
      radiusType = 1,
      radiusSize = 5.5, # Label size
      showBackground = TRUE
      # backgroundColor="black",
      # backgroundOpacity=0.5
    )
  ) %>%
  zoomMove(
    center = my_sele,
    zoom = my_sele,
    duration = 0, # animation time in ms
    z_offSet = -20
  )

Below, the left panel shows the view that I see. The right panel shows the view that I want, after using the mouse to rotate the view 180 degrees.

In other words, I am seeing the "back" of the structure when I want to see the "front" instead.

Back of the structure (default output) Front of the structure (after rotating 180 degrees with the mouse)

What's the simplest way to rotate the view?

slowkow commented 1 year ago

This issue from the python package nglview looks related to mine. Is there a similar way we can rotate the view in R?

https://github.com/nglviewer/nglview/issues/900

nvelden commented 1 year ago

At the moment this is not possible but I will have a look if I can implements this.

slowkow commented 1 year ago

Thank you, Niels!

nvelden commented 1 year ago

I pushed an update to github that adds setScale, setRotation, and setPosition functions for controlling scale, rotation, and position of representations.

Let me know if this works for you.

library(NGLVieweR)

my_sele <- "9:A"
NGLVieweR("2bvp") %>%
  stageParameters(backgroundColor = "white", zoomSpeed = 1) %>%
  addRepresentation(
    type = "cartoon"
  ) %>%
  addRepresentation(
    type = "ball+stick",
    param = list(
      sele = my_sele
    )
  ) %>%
  addRepresentation(
    type = "label",
    param = list(
      sele = my_sele,
      labelType = "format",
      labelFormat = "[%(resname)s]%(resno)s", # or enter custom text
      labelGrouping = "residue", # or "atom" (eg. sele = "20:A.CB")
      color = "black",
      fontFamiliy = "sans-serif",
      xOffset = 1,
      yOffset = 0,
      zOffset = 0,
      fixedSize = TRUE,
      radiusType = 1,
      radiusSize = 1, # Label size
      showBackground = TRUE
      # backgroundColor="black",
      # backgroundOpacity=0.5
    )
  ) %>%
  setRotation(190,0,200) %>%
  setPosition(5,5,0) %>%
  setScale(1.5)
slowkow commented 1 year ago

Thank you! After installing from github, your example works.

But I didn't notice any difference after deleting this line: setPosition(5,5,0)

nvelden commented 1 year ago

Ah yes sorry. I just put it for demonstration purposes. It allows you to move the whole structure in the x, y, z position. The numbers are in angstroms so a move of 5 is very small. Try to change it to for instance 50 and you should see a change.