NGLVieweR icon indicating copy to clipboard operation
NGLVieweR copied to clipboard

How to rotate the view?

Open slowkow opened this issue 1 year ago • 6 comments

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 avatar Jun 24 '23 13:06 slowkow

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

slowkow avatar Jun 24 '23 14:06 slowkow

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

nvelden avatar Jun 27 '23 14:06 nvelden

Thank you, Niels!

slowkow avatar Jun 27 '23 14:06 slowkow

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)

nvelden avatar Jul 01 '23 13:07 nvelden

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

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

slowkow avatar Jul 01 '23 20:07 slowkow

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.

nvelden avatar Jul 01 '23 21:07 nvelden