Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualization of an MDAnalysis Universe resets its translation #1081

Open
daandtu opened this issue Nov 9, 2023 · 1 comment
Open

Visualization of an MDAnalysis Universe resets its translation #1081

daandtu opened this issue Nov 9, 2023 · 1 comment

Comments

@daandtu
Copy link

daandtu commented Nov 9, 2023

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

@daandtu daandtu changed the title Visualization resets molecule translation Visualization of an MDAnalysis Universe resets its translation Nov 10, 2023
@hainm
Copy link
Collaborator

hainm commented Nov 13, 2023

thanks @daandtu for your report. I don't know why on top of my head. The code is super simple:

nglview/nglview/adaptor.py

Lines 412 to 460 in d9e7740

class MDAnalysisTrajectory(Trajectory, Structure):
'''MDAnalysis adaptor.
Can take either a Universe or AtomGroup.
Example
-------
>>> import nglview as nv
>>> import MDAnalysis as mda
>>> u = mda.Universe(nv.datafiles.GRO, nv.datafiles.XTC)
>>> prot = u.select_atoms('protein')
>>> t = nv.MDAnalysisTrajectory(prot)
>>> w = nv.NGLWidget(t)
>>> w
'''
def __init__(self, atomgroup):
self.atomgroup = atomgroup
self.ext = "pdb"
self.params = {}
self.id = str(uuid.uuid4())
def get_coordinates(self, index):
self.atomgroup.universe.trajectory[index]
xyz = self.atomgroup.atoms.positions
return xyz
@property
def n_frames(self):
return self.atomgroup.universe.trajectory.n_frames
def get_structure_string(self):
try:
import MDAnalysis as mda
except ImportError:
raise ImportError(
"'MDAnalysisTrajectory' requires the 'MDAnalysis' package")
u = self.atomgroup.universe
u.trajectory[0]
f = mda.lib.util.NamedStream(StringIO(), 'tmp.pdb')
atoms = self.atomgroup.atoms
# add PDB output to the named stream
with mda.Writer(f, atoms.n_atoms, multiframe=False) as w:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
w.write(atoms)
# extract from the stream
pdb_string = f.read()
return pdb_string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants