nglviewer / nglview

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

More info/docs on coloring or selections? #222

Closed davidlmobley closed 8 years ago

davidlmobley commented 8 years ago

BTW, this is great. Beautiful smooth viewer.

But, now that I have this working on my mixed solvent system (as discussed here: https://github.com/mdtraj/mdtraj/issues/1122#issuecomment-222601126) I run into the issue that I'd like to be able to control the viewer to make, say, phenol molecules be one color and cyclohexane be another.

It is unclear to me how to do this from the info I can find easily. I find code like this in the README.md:

view.add_cartoon("protein", color="residueindex")
view.add_surface("protein", opacity=0.3)

But I have no protein, and it's unclear to me what is acceptable for the first argument. If I try help(view.add_cartoon) it only gives me: func(this, selection='all', **kwargs) method of nglview.NGLWidget instance

which is unhelpful. And, trying view.add_cartoon("phenol", color="residueindex") makes my viewer window disappear without any error message or anything (bug?).

arose commented 8 years ago

what are the group names of your phenol/cyclohexane molecules?

hainm commented 8 years ago

Can you try to replace 'phenol' by its resname?

view.add_surface('ASP', opacity=0.3)

Please see selection language here

http://arose.github.io/ngl/doc/#User_manual/Usage/Selection_language

We are working in writing more doc now, but feel free to ask anything here.

hainm commented 8 years ago

Change color

view.add_surface('1', color='blue')

davidlmobley commented 8 years ago

My phenol has residue name ZTF, it seems (these are actually assigned randomly in this case). However, coloring it seems to fail, i.e. view.add_surface('ZTF', color='blue') results in no viewer window appearing (with no error) and likewise view.add_surface('1', color='blue')

I'll consult the selection language and see if I can do anything useful.

davidlmobley commented 8 years ago

Hmm, so it seems not to be a problem with my selections, in that I don't get any viewer window even if I just do: view.add_surface('all', opacity=0.3) which seems like it ought to work.

(Also, again, I get no info at all - just no viewer appearing, and no error messages or anything).

davidlmobley commented 8 years ago

I have the same issue (viewer window disappears) if I try: view.add_representation('spacefill', selection='all' ) or any variant I've been able to come up with.

davidlmobley commented 8 years ago

@hainm - I also tried the same experiment in the test_mdtraj_nglview environment we created in the other thread, but I have the same issue - any messing with representation/surface/etc at all causes the viewer window to disappear.

I also have two related problems with my other notebook which tries to view a simple trajectory run with OpenMM:

I'm guessing this means I should probably find something else to show in my class (which was supposed to be today but I switched the lecture to Thursday so I'd have time to deal with this) as this seems maybe not quite ready for general usage, unless I'm missing something obvious.

hainm commented 8 years ago

Can you attach a simple notebook so i can look?

davidlmobley commented 8 years ago

The one(s) in teh other thread should work but I'll re-attach.

hainm commented 8 years ago

Btw, in my example 'ASP' is Aspartic residue. :d what I really meant is to replace it with whatever name that mdtraj/openmm give you.

davidlmobley commented 8 years ago

(About "ASP", I mistyped my code. I'd tried to select "ZTF" but it failed).

hainm commented 8 years ago

Ok, i will check your nb in about 1-2 hours later.

davidlmobley commented 8 years ago

debugging.zip

OK, please try this. Maybe try Lec17_mixtures.ipynb first. Note the box at the top should be run to import stuff, but will fail with an exception because of the existing files I've included in 'data'. AMBER prmtop and .crd files are there in the tleap directory. I've also tried loading GROMACS ones but I can't get that to work for some reason.

davidlmobley commented 8 years ago

So, er, to get it to work, try making the top two lines:

traj = md.load( 'data/tleap/phenol_cyclohexane_3_100.inpcrd', top = 'data/tleap/phenol_cyclohexane_3_100.prmtop' )
#traj = md.load( 'data/gromacs/phenol_cyclohexane_3_100.gro', top = 'data/gromacs/phenol_cyclohexane_3_100.gro' )

and then removing the line view.add_representation('1', opacity=0.3)

If you add that last line back in, the viewer will disappear again (at least if it works for you the way it does for me).

hainm commented 8 years ago

@davidlmobley The first thing I realize is that you never display view to make it work

In first cell

view = nglview.show_mdtraj(traj)
#view = nglview.show_pytraj(traj_pt)
view # you need to call this to display

then in 2nd cell

view.add_surface(...)
view.add_licorice(...)

I will look other issues.

davidlmobley commented 8 years ago

The code in my notebook has:

view = nglview.show_mdtraj(traj)
#view = nglview.show_pytraj(traj_pt)
view
davidlmobley commented 8 years ago

Elaborating, the relevant box has (stripping out commented stuff):


import mdtraj as md
traj = md.load( 'data/tleap/phenol_cyclohexane_3_100.inpcrd', top = 'data/tleap/phenol_cyclohexane_3_100.prmtop' )

import nglview
import pytraj as pt

view = nglview.show_mdtraj(traj)
view

#view.add_representation('1', opacity=0.3)

If I leave it as is, I get a viewer with useful info in it. If I comment out the last line, I get nothing (no view window, no error, nothing).

hainm commented 8 years ago

yes, that is my point. To display in notebook, it need to call view in the last command.

hainm commented 8 years ago

You need to add view.add_representation('1', opacity=0.3) in another notebook cell.

hainm commented 8 years ago

fireshot capture 13 - lec17_mixtures-_ - http___localhost_8888_notebooks_lec17_mixtures-copy1 ipynb

davidlmobley commented 8 years ago

Ah, so you're saying I just have to move the change of representation up.

Well, that wasn't clear at all from the docs or examples. But yes, that seems to work for this.

OK, so that solves the problem with my mixed solvent notebook. However, I still have the issue that my OpenMM simulation brings up an empty viewer.

davidlmobley commented 8 years ago

(Thanks for that example you just sent though! Extremely helpful. So nwo I can use this for mixed solvents. Now if only I could view trajectories...)

hainm commented 8 years ago

OK, so that solves the problem with my mixed solvent notebook. However, I still have the issue that my OpenMM simulation brings up an empty viewer.

can you make a list of bullet points in your 1st post so I can easily check?

hainm commented 8 years ago

this is your residue names

print("residue set: %s" % set(residue.name for residue in traj.topology.residues))
# residue set: {'pheno', 'cyclo', 'solut'}

so, you can use

view.add_surface('pheno', opacity=0.3)
davidlmobley commented 8 years ago

I'm going to have to disappear for a couple of hours momentarily. Bullet points on what?

Here's the code

import os
import mdtraj
import mdtraj.reporters
from simtk import unit
import simtk.openmm as mm
from simtk.openmm import app
import mdtraj.testing
#pdb = mdtraj.load(mdtraj.testing.get_fn('native.pdb'))
pdb = mdtraj.load_pdb('http://www.rcsb.org/pdb/files/1LE1.pdb')
topology = pdb.topology.to_openmm()
forcefield = app.ForceField('amber99sbildn.xml', 'amber99_obc.xml')
system = forcefield.createSystem(topology, nonbondedMethod=app.CutoffNonPeriodic)
integrator = mm.LangevinIntegrator(330*unit.kelvin, 1.0/unit.picoseconds, 2.0*unit.femtoseconds)
simulation = app.Simulation(topology, system, integrator)
simulation.context.setPositions(pdb.xyz[0])
simulation.context.setVelocitiesToTemperature(330*unit.kelvin)
if not os.path.exists('test.h5'):
    simulation.reporters.append(mdtraj.reporters.HDF5Reporter('test.h5', 10))
    simulation.step(100)
print("Done")

from __future__ import print_function
import nglview
from IPython import display

traj = mdtraj.load('test.h5')
print(traj)

view = nglview.show_mdtraj(traj)
view

and it yields just an empty view window.

I'm not totally sure I'm doing the OpenMM simulation right, but this DID work a year ago with TrajectoryView, so one would think it should work. Really, I'm just after a minimal example of visualizing ANY OpenMM simulation, so if you don't like this one I'm open to anything else.

hainm commented 8 years ago

Bullet points on what?

never mind. (I just want you make like this)

- [ ] phenol selection ...
- [ ] openmm issue ...
hainm commented 8 years ago

Per openmm issue, it's likely due to openmm and mdtraj themself. If I ran you script in my notebook with py35. I got

/Users/haichit/miniconda/lib/python3.5/site-packages/tables/file.py in open_file(filename, mode, title, root_uep, filters, **kwargs)
    302                 raise ValueError(
    303                     "The file '%s' is already opened, but "
--> 304                     "not in read-only mode (as requested)." % filename)
    305             # 'a' and 'r+' are compatible with everything except 'r'
    306             elif mode in ('a', 'r+') and omode == 'r':

ValueError: The file 'test.h5' is already opened, but not in read-only mode (as requested).
hainm commented 8 years ago

After running openmm in one notebook, then open another one to reload test.h5 file, it's ok too me.

fireshot capture 14 - untitled - http___localhost_8890_notebooks_untitled ipynb_kernel_name python3

hainm commented 8 years ago

more fun stuff

fireshot capture 15 - untitled - http___localhost_8890_notebooks_untitled ipynb_kernel_name python3

davidlmobley commented 8 years ago

In Python 2.7, it doesn't work for me even if I just try to load the trajectory; after the usual import stuff at the top (cut off) I get this:

mdtraj
davidlmobley commented 8 years ago

Maybe send me your traj.h5 and I can check if it's a problem with that?

davidlmobley commented 8 years ago

I could also go back to trying the minimal test environment you created earlier.

I had a couple minutes before I run out the door, hence the responses. But I'll (shortly) be delayed by a while.

hainm commented 8 years ago

sorry, just had dinner. This test.h5 works for me. test.h5.zip

hainm commented 8 years ago

also try files from nglview trr

this is also a trajectory

davidlmobley commented 8 years ago

Well, yup, your test.h5 works for me. So something is wrong with mine.

That narrows things down by about, well, 50%. :) Any advice? Otherwise I'll go work on my openmm knowledge and bug those guys. Thanks for your help!

hainm commented 8 years ago

Otherwise I'll go work on my openmm knowledge and bug those guys. Thanks for your help!

this definitely should go to openmm issue, out of our league since we (me and @arose) are not using openmm.

hainm commented 8 years ago

btw, I just install openmm and mdtraj from omnia

conda install openmm -c omnia --force
conda install mdtraj -c omnia --force
hainm commented 8 years ago

I just update a bit about coloring and atom selection: https://github.com/arose/nglview/commit/d90f57a3575e222babfacfa443ebd0923300c402

Due to our limited time for this (nglview) side project, doc is never done thoroughly. But please ask anything you need, we might have it in nglview but hidden ( :D ).

For example, you can even add more mdtraj.Trajectory as you like

# 1st cell
view = nglview.show_mdtraj(traj0)
view

# 2nd cell
# wrap mdtraj.Trajectory in nglview object
view.add_trajectory(nglview.MDTrajTrajectory(traj2))

# you can even mix pytraj and mdtraj too
view.add_trajectory(nglview.PyTrajTrajectory(pytraj_object))

# or even rdkit :D 
view.add_trajectory(nglview.RdkitStructure(its_object))

Of course, you need to take care by your own trajectory superimpose in mdtraj.

davidlmobley commented 8 years ago

Thanks for all the help! Turns out I have my issues resolved now. It appears the main problem was that I was just using an old .h5 file that wasn't being overwritten when I ran new ones because of the if statement. :) So now everything works. Nice viewer - good work! I'll definitely use this again in the future.

hainm commented 8 years ago

cool.

davidlmobley commented 8 years ago

So, @hainm - if I DO try:

view.add_surface('pheno', opacity=0.3)

for some reason I get a surface which seems to correspond to the surface of my box rather than the surface of the individual phenol molecule. Insights?

hainm commented 8 years ago

can you post screenshot?

davidlmobley commented 8 years ago

Also, relatedly, if I clear all representations and then do view.add_licorice('solut', color="blue") or view.add_licorice('pheno', color="blue")

I end up with everything colored blue...

This is with my residue set as residue set: set(['pheno', 'solut', 'cyclo']).

I'd really like to be able to color things separately and visualize just certain tings. cursor_and_lec17_mixtures

davidlmobley commented 8 years ago

The surface issue. Zoomed in: cursor_and_lec17_mixtures

Zoomed out: cursor_and_lec17_mixtures

Sorry, I'm sure all of this is just me doing something wrong. But, maybe it will help you in terms of knowing the minimum you have to explain to a novice to get them going (i.e. the "view command has to be last" was a key piece of info which was non-obvious).

hainm commented 8 years ago

you need to set opacity=0.3 (or others). The default is 1.0.

hainm commented 8 years ago

(i.e. the "view command has to be last" was a key piece of info which was non-obvious).

this is my English issue.

davidlmobley commented 8 years ago

No, no. The problem is that I should be selecting only one molecule! i.e., "solut" specifies a single residue/moelcule.

davidlmobley commented 8 years ago

In other words, what I should be seeing is a single molecule represented as a surface and everything else should be licorice.

hainm commented 8 years ago

hold on. Let me check myself.

davidlmobley commented 8 years ago

Ah, OK. It seems as though it's the selection by residue name which is not working. If I use residue numbers I get the expected result:

cursor_and_lec17_mixtures