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

Usage questions about nglview: Please ask here first [2] #901

Closed
hainm opened this issue May 25, 2020 · 72 comments
Closed

Usage questions about nglview: Please ask here first [2] #901

hainm opened this issue May 25, 2020 · 72 comments
Labels

Comments

@hainm
Copy link
Collaborator

hainm commented May 25, 2020

For any questions relating to how/why/ ... Or anything you don't know where to ask.
For bug, suggestion or anything you want to ask separately, please open seperate issue

PS:

@dprada
Copy link

dprada commented Jun 5, 2020

Hi! Is there a way to work with the residue indices (0-th based), not the ids, in a selection?
I have a homodimer, and thereby two set of residues in the input pdb with the same ids, and a list of scalar values to color all residues with a new '_ColorScheme'. Is there a way to play with the position index of the scalar value being equal to the residue index, in order to avoid the chain and residue ids?

Thanks in advance!!

@hainm
Copy link
Collaborator Author

hainm commented Jun 11, 2020

hi @dprada, I don't know yet on top of my head. Let me think a bit.

@mjw99
Copy link

mjw99 commented Jun 11, 2020

Hello, thank you for all your work on this great tool.

I am trying to calculate and visualize a molecular electrostatic potential, on a specific electron density isosurface, kind of similar to this example with ngl (live example), but using PySCF and nglview, within a Jupyter notebook:

from pyscf import gto
from pyscf import dft
from pyscf.geomopt import berny_solver
from pyscf import __config__
from pyscf.tools import cubegen

import nglview as nv

# Default of 3.0 is too small for 6-31G*
setattr(__config__, 'cubegen_box_margin', 4.0)

h2o = gto.M(atom='      O 0.00000000,  0.000000,  0.119748;\
                        H 0.00000000,  0.761561, -0.478993;\
                        H 0.00000000, -0.761561, -0.478993',\
                        basis='6-31G*')
                        
h2o.build()
h2o.verbose = 0

method = dft.RKS(h2o)
method.grids.prune = dft.gen_grid.treutler_prune
method.grids.atom_grid = {"H": (50, 194), "O": (50, 194)}
method.xc = 'b3lypg'
method.scf()

opt_mol = berny_solver.optimize(method)

cubegen.density(opt_mol, 'water_den.cube', method.make_rdm1(), resolution=(1/6))
cubegen.mep(opt_mol, 'water_pot.cube', method.make_rdm1(), resolution=(1/6))

view = nv.NGLWidget()

view.add_component('water_den.cube', color="red")
view.component_0.add_surface(isolevelType="value", isolevel=0.002)

view.add_component('water_pot.cube')
view.component_1.add_surface(color='red')

view

I think I am close to getting this to work, but I am not sure how to use data from the two components together.

@dprada
Copy link

dprada commented Jun 11, 2020

Dont' worry @hainm, it is something that can be done combining the chain and residues ids. I was wondering if there was a simpler way. But many thanks for your kind reply (as always).

@hainm
Copy link
Collaborator Author

hainm commented Jun 12, 2020

I am trying to calculate and visualize a molecular electrostatic potential, on a specific electron density isosurface, kind of similar to this example with ngl (live example), but using PySCF and nglview, within a Jupyter notebook:

hi @mjw99
Is this what you're looking for? #804

@mjw99
Copy link

mjw99 commented Jun 12, 2020

Dear Hai,
Thank you for the example; with a slightly modified form, and the example data files I can get your example to work:

import nglview as nv

v = nv.NGLWidget()

c0 = v.add_component('esp.mol')
c1 = v.add_component('esp.dx')

c1.clear()

c0.add_surface(
    color_scheme='volume',
      color_volume=c1,      
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],             
      opacity=0.5
)

v

However, with my initial example, a similar approach is still not working:

view = nv.NGLWidget()

c0 = view.add_component('water_den.cube')
c1 = view.add_component('water_pot.cube')
c0.clear()

c0.add_surface(color_scheme='volume', 
      color_volume=c1,
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],      
      opacity=0.5
)

view

Screenshot from 2020-06-12 11-44-59

As a side point, if I load these same CUBE files into VMD (using a protocol similar to the VMD section here ), they do display fine. Any ideas?

@jessevig
Copy link

Thank you for the great tool. Is it possible to set opacity to residues at specific indices or a range of indices? I'm using the cartoon representation. Thanks!

@hainm
Copy link
Collaborator Author

hainm commented Jun 12, 2020

As a side point, if I load these same CUBE files into VMD (using a protocol similar to the VMD section here ), they do display fine. Any ideas?

@mjw99 Can you please try NGL live example first by using your files?

@hainm
Copy link
Collaborator Author

hainm commented Jun 12, 2020

Thank you for the great tool. Is it possible to set opacity to residues at specific indices or a range of indices? I'm using the cartoon representation. Thanks!

hi @jessevig, may be use multiple cartoon representations. Saying you have 5 residues and want to set opacity for some of them.

view.add_cartoon("1,2,5")
view.add_cartoon("3,4", opacity=0.5)

@hainm
Copy link
Collaborator Author

hainm commented Jun 12, 2020

@arose Does NGL support setting opacity for some residues in ONE representation? thanks.

@jessevig
Copy link

Is there a simple example for animating a protein structure to rotate? I found this but wasn't sure how to invoke it from the jupyter notebook: https://github.com/arose/ngl/blob/master/src/controls/animation-controls.ts
Thanks much!

@hainm
Copy link
Collaborator Author

hainm commented Jun 13, 2020

@jessevig
Copy link

I thought I tried that one before, but it didn't show an animation associated with spinning, it just changed the viewing angle. Is it supposed to show the animation? Thanks.

@hainm
Copy link
Collaborator Author

hainm commented Jun 16, 2020

@jessevig I see. Please use view._set_spin for now. I thought two methods are the same. :D

@jessevig
Copy link

Thanks. Do you know if it's possible to display text sideways, i.e. rotated 90 degrees?

@hainm
Copy link
Collaborator Author

hainm commented Jun 18, 2020

rotated 90 degrees?

I am not sure. May be playing with NGL itself to check. I see your open issue in that repo.

@mrjoness
Copy link

Hello,

I've been trying to use ngl to visualize DNA oligos from a .lammpstrj trajectory file and .psf topology. I am able to load the data without any error, but when I visualize a frame I am not able to recover the bonding information contained in the psf file. I believe that some bonds are forming based off proximity (they look like random triangles) but these are not reflective of what I see in vmd for example. Here is the code I am using and the corresponding visualization:

`frame = md.load_frame('../GC-core_321K_vis.lammpstrj', 0, top='../GC-core-ions.psf')

view = nv.show_mdtraj(frame)
view.clear_representations()

strand_len = 29
view.add_representation('licorice', selection=[i for i in range(strand_len)], color='red')
view.add_representation('licorice', selection=[i+strand_len for i in range(strand_len)], color='blue')

view`

Capture

@hainm
Copy link
Collaborator Author

hainm commented Jun 19, 2020

hi @mrjoness, nglview uses mdtraj's save_pdb method to save the topology to pdb format.

frame[0].save_pdb("test.pdb")

Load test.pdb to VMD, do you still see the bond correctly?

@mrjoness
Copy link

When I use test.pdb in vmd I don't see the correct bonds either... I guess there's a problem with the mdtraj method (or my psf file) and not necessarily nglviewer? Is there anything you recommend to fix this?

@hainm
Copy link
Collaborator Author

hainm commented Jun 21, 2020

@mrjoness Either:

  • Ask mdtraj developer about correct way to generate the pdb
  • Try MDAnalysis (I think it supports lammpstrj format).

@luponzo86
Copy link

luponzo86 commented Aug 7, 2020

Hi,
I am trying to draw a distance representation between two components, but it is not clear to me how to select an atom pair between the two components: What is the correct syntax? I tried using the "model" syntax, e.g. ('@1/0', '@99/1'), but it does not work:

view = nv.NGLWidget()

# add components to view
c0 = view.add_component(mol0)
c1 = view.add_component(mol1)

view.add_representation(
    'distance', 
    atomPair=[('@333/0', '@444/1')],  # <- dist, between atom 333 in model (component?) 0 and atom 444 in model 1. Does not work
)

[EDIT] More in general, what is the syntax for selecting a component and its atoms? I have a small molecule loaded as a separate component and I cannot understand how to find out the identifiers assigned to its atoms

@hainm
Copy link
Collaborator Author

hainm commented Aug 7, 2020

hi @luponzo86, I don't think nglview (NGL under the hood) support distance between two components.

cc @arose

@luponzo86
Copy link

Thanks, @hainm

I then tried to combine the two molecules together with OpenBabel:

mol1 += mol2  # both are OBMol objects
comp = view.add_component(nv.OpenbabelStructure(mol1))

and it works, but then I don't know how to tell the two molecules apart during selection. Is there a way to figure out the identifiers of the two molecules? Is there something like comp.getData() that could help understand how the atoms/chains are named?

Thanks!

@hainm
Copy link
Collaborator Author

hainm commented Aug 8, 2020

@luponzo86

Is there something like comp.getData() that could help understand how the atoms/chains are named?

Can you try nv.OpenbabelStructure(mol1).get_structure_string()?

@luponzo86
Copy link

@hainm

Can you try nv.OpenbabelStructure(mol1).get_structure_string()?

Thanks a lot, very useful!

I was finally able to combine the two molecules (mol1 and mol2) together and draw the distance between two atoms that were originally in the two separate molecules. The only thing to be careful about is to remap the atom indices of the second molecule, since after appending it to the first one its indices are shifted by len(mol1)

@hainm
Copy link
Collaborator Author

hainm commented Aug 8, 2020

@luponzo86 fantastic.

@markperri
Copy link

markperri commented Aug 28, 2020

Is there a way to display a view from inside a function in a Jupyter Notebook:

cell 1:

import nglview as nv
import pytraj as pt
import numpy as np

def loadTrajectory(job):
    traj = pt.load(f'{job}.dcd', top=f'{job}.psf')
    print(set(residue.name for residue in traj.top.residues))
    view1 = nv.show_pytraj(traj)
    view1

cell 2:
loadTrajectory(182076)

output:
{'TIP3'}

The trajectory isn't displayed, but it works fine when I don't use a function. Is there a view1.show() type function I should use instead?

@hainm
Copy link
Collaborator Author

hainm commented Aug 28, 2020

hi @markperri, just return view1 or

from IPython.display import display
display(view1)

@markperri
Copy link

@hainm thanks!

@hainm
Copy link
Collaborator Author

hainm commented Dec 28, 2020

hi @gmacari: sorry for the delay. There is no direct way to do that, however, you can use view.shape.add_cylinder to create the lines: https://github.com/hainm/nglview/blob/1edd80dde2f45bcc72e498a4d3603a5b57542610/nglview/shape.py#L24

@jochym
Copy link
Contributor

jochym commented Feb 2, 2021

I have a problem with the size of the ngl view in the embedded viewer. The _remote_call trick works fine in the notebook but when I save widget to html it always comes in default height. Seems to adopt the width of the surrounding div but not the height. The only way to resize the view is resizing by hand. Is there any example how we can do the equivalent of _remote_call inside the html with embedded widget? I am not an expert in JS - thus it is not easy find the correct object to call the setSize method on. Some minimal example would be great but any lead will help.

@hainm
Copy link
Collaborator Author

hainm commented Feb 2, 2021

@jochym I don't know for now, let me think a bit.

@hainm
Copy link
Collaborator Author

hainm commented Feb 2, 2021

@jochym The best way currently is to wrap the view in an ipywidgets's Box (or GridBox):
https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Styling.html

Screen Shot 2021-02-02 at 12 10 10 PM

@jochym
Copy link
Contributor

jochym commented Feb 2, 2021

This is absolutely fine for my needs - probably not for everyone, but it is a perfectly acceptable solution. Thanks!

@jochym
Copy link
Contributor

jochym commented Feb 5, 2021

Added to FAQ (#945)

@atygesen
Copy link

atygesen commented Feb 9, 2021

Hi,

Is there a way to update the atomic species in the NGLViewer widget when changing frames? Currently it only updates the positions of the species, however I'm working on trajectories which has varying species as well.

As a an example (from ASE):

import nglview
from ase.build import bulk
images = [bulk('Au'), bulk('Ag')]
viewer = nglview.show_asetraj(images)
viewer

Here, changing the frames does not actually update the atomic species.
Thanks!

@hainm
Copy link
Collaborator Author

hainm commented Feb 9, 2021

however I'm working on trajectories which has varying species as well.

hi @atygesen No, nglview does not support multiple species in a single trajectory.

@ssharlin
Copy link

Hi, does anyone know how to get the periodic boundary conditions to show in the visuals? this is what my molecules look like:
image

I want it to look something like this:

image

@jochym
Copy link
Contributor

jochym commented Feb 11, 2021

You need to have the periodic boundary conditions turned on in the structure (at least in the ase structures it works that way) and you need to add_unitcell to your visualisation.

@ssharlin
Copy link

You need to have the periodic boundary conditions turned on in the structure (at least in the ase structures it works that way) and you need to add_unitcell to your visualisation.

thank you so much!
image

@jaimergp
Copy link
Contributor

@hainm Can you label this as Q&A as the other two so they appear in this search query? Thanks!

@hainm hainm added the Q&A label Feb 11, 2021
@hainm
Copy link
Collaborator Author

hainm commented Feb 11, 2021

Can you label this as Q&A

Done. Thanks @jaimergp

@ssharlin
Copy link

You need to have the periodic boundary conditions turned on in the structure (at least in the ase structures it works that way) and you need to add_unitcell to your visualisation.

also, do you know if there is a way to add color to the unit cell's background?

@hainm
Copy link
Collaborator Author

hainm commented Feb 12, 2021

also, do you know if there is a way to add color to the unit cell's background?

No. And you can not even change the color of the unitcell.

@hainm
Copy link
Collaborator Author

hainm commented Feb 15, 2021

Open new thread: #948

@hainm hainm closed this as completed Feb 15, 2021
@jcerezochem
Copy link

Dear Hai, Thank you for the example; with a slightly modified form, and the example data files I can get your example to work:

import nglview as nv

v = nv.NGLWidget()

c0 = v.add_component('esp.mol')
c1 = v.add_component('esp.dx')

c1.clear()

c0.add_surface(
    color_scheme='volume',
      color_volume=c1,      
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],             
      opacity=0.5
)

v

However, with my initial example, a similar approach is still not working:

view = nv.NGLWidget()

c0 = view.add_component('water_den.cube')
c1 = view.add_component('water_pot.cube')
c0.clear()

c0.add_surface(color_scheme='volume', 
      color_volume=c1,
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],      
      opacity=0.5
)

view

Screenshot from 2020-06-12 11-44-59

As a side point, if I load these same CUBE files into VMD (using a protocol similar to the VMD section here ), they do display fine. Any ideas?

In case this may still interest anyone. The example actually works if the isolevel of the surface is specified:

 c0.add_surface(color_scheme='volume', 
       isolevelType="value",
       isolevel = 0.001,
       color_volume=c1,
       colorScale='rwb',
       colorDomain=[ -0.05, 0.05 ],      
       opacity=0.5
 )

@hainm
Copy link
Collaborator Author

hainm commented Oct 21, 2021

Thanks @jcerezochem

@Haoyuwang98
Copy link

hi
when i am using the nglview, i load the traj file and then want to show the traj with nglview, i meet the question like this
截屏2021-10-30 上午11 42 01
:

@Haoyuwang98
Copy link

hi when i am using the nglview, i load the traj file and then want to show the traj with nglview, i meet the question like this 截屏2021-10-30 上午11 42 01 :

i can't get the rendered picture ;
there is a picture which is can't load : (

@hainm
Copy link
Collaborator Author

hainm commented Oct 30, 2021 via email

@Haoyuwang98
Copy link

Haoyuwang98 commented Oct 31, 2021 via email

@samuelflores
Copy link

Hello Hai,

I am super interested in the Shape method. I would like to draw a cylinder connecting two atoms on two different macromolecule residues. How can I retrieve the coordinates of these atoms? Once I have the atom index I can do that, but I want to be able to retrieve given residue numbers and chain IDs which is the data I have.

Thanks!

Sam

hi @gmacari: sorry for the delay. There is no direct way to do that, however, you can use view.shape.add_cylinder to create the lines: https://github.com/hainm/nglview/blob/1edd80dde2f45bcc72e498a4d3603a5b57542610/nglview/shape.py#L24

@mjw99
Copy link

mjw99 commented Jun 1, 2022

Dear Hai, Thank you for the example; with a slightly modified form, and the example data files I can get your example to work:

import nglview as nv

v = nv.NGLWidget()

c0 = v.add_component('esp.mol')
c1 = v.add_component('esp.dx')

c1.clear()

c0.add_surface(
    color_scheme='volume',
      color_volume=c1,      
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],             
      opacity=0.5
)

v

However, with my initial example, a similar approach is still not working:

view = nv.NGLWidget()

c0 = view.add_component('water_den.cube')
c1 = view.add_component('water_pot.cube')
c0.clear()

c0.add_surface(color_scheme='volume', 
      color_volume=c1,
      colorScale='rwb',
      colorDomain=[ -0.05, 0.05 ],      
      opacity=0.5
)

view

Screenshot from 2020-06-12 11-44-59
As a side point, if I load these same CUBE files into VMD (using a protocol similar to the VMD section here ), they do display fine. Any ideas?

In case this may still interest anyone. The example actually works if the isolevel of the surface is specified:

 c0.add_surface(color_scheme='volume', 
       isolevelType="value",
       isolevel = 0.001,
       color_volume=c1,
       colorScale='rwb',
       colorDomain=[ -0.05, 0.05 ],      
       opacity=0.5
 )

For any one interested; there is a demo of this here: https://github.com/mjw99/pyscf_mep

@hainm
Copy link
Collaborator Author

hainm commented Jun 1, 2022

Thanks @mjw99

@GregorySchwing
Copy link

I have a working installation of nglview using jupyter lab. My problem is i'd like to be able to "Restart Kernel and Run all Cells" and see the nglview as the cells are run, however, the views aren't displayed until after all the kernels finish or I stop the kernel. Is there anyway to get my desired behavior?

Nglview (Version 1.1.7, from Bioconda)

Conda environment yml

name: biobb_amber
channels:

  • anaconda
  • bioconda
  • conda-forge
  • plotly
    dependencies:
  • biobb_io
  • biobb_model
  • biobb_chemistry
  • biobb_analysis
  • biobb_structure_utils
  • jupyter
  • plotly
  • simpletraj
  • bioconda::nglview
  • ipywidgets==7.7.2
  • biobb_amber
  • gfortran
  • libgfortran5

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

No branches or pull requests