nglviewer / nglview

Jupyter widget to interactively view molecular structures and trajectories
http://nglviewer.org/nglview/latest/
Other
788 stars 132 forks source link

Visualization of an MDAnalysis Universe resets its translation #1081

Open daandtu opened 7 months ago

daandtu commented 7 months ago

Setup:

  1. I load a molecule with MDAnalysis from a PDB file.
  2. Then I translate the molecule to a new position.
  3. I visualize the molecule via nglview.show_mdanalysis

Problem: During the visualization, the translation is reset, i.e. after the creation of the NGLView-Widget the coordinates of the molecule are the same again as in the PDB file.

Example:

import MDAnalysis as mda
from MDAnalysis.transformations.translate import translate
import numpy as np
import nglview as nv

pdb_path = 'temp.pdb'
structure1 = mda.Universe(pdb_path)
print('Before translation:')
for atom in structure1.atoms:
    print(atom.position)
translate(np.array([10,10,10]))(structure1.atoms)
print('After translation:')
for atom in structure1.atoms:
    print(atom.position)
nv_widget_1 = nv.show_mdanalysis(structure1)
print('After visualization:')
for atom in structure1.atoms:
    print(atom.position)
nv_widget_1

The output is:

Before translation:
[1. 0. 0.]
[ 0.  0. -1.]
[1. 0. 1.]
After translation:
[11. 10. 10.]
[10. 10.  9.]
[11. 10. 11.]
After visualization:
[1. 0. 0.]
[ 0.  0. -1.]
[1. 0. 1.]

I used this PDB file:

ATOM      1  N1  1MIMX   1       1.000   0.000   0.000  1.00  0.00      1M
ATOM      2  N2  1MIMX   1       0.000   0.000  -1.000  1.00  0.00      1M    
ATOM      3  R3  1MIMX   1       1.000   0.000   1.000  1.00  0.00      1M    
ENDMDL
CONECT    1    2    3
CONECT    2    1    3
CONECT    3    1    2
END

Version report nglview version: 3.0.8 ipywidgets version: 8.1.1

hainm commented 7 months ago

thanks @daandtu for your report. I don't know why on top of my head. The code is super simple: https://github.com/nglviewer/nglview/blob/d9e7740100b586b1e73cc51d247d16b6dc8d0914/nglview/adaptor.py#L412-L460