AMBER Tutorial

Real time visualization of MD simulation

By Hai Nguyen (BSD 2-clause)

In [1]:
from __future__ import print_function
from time import gmtime, strftime
print('updated: ', strftime("%Y-%m-%d", gmtime()))

from IPython.display import Image
Image("images/real_time_md.png")
updated:  2016-06-12
Out[1]:

Targeted audience: Expert

Or any one that wants to use this notebook for teaching or demonstration

Require

How to use this notebook?

amber.jupyter notebook notebook.ipynb # notebook.ipynb is the name of this notebook
# you can make your own notebook and copy commands in this notebook

Download notebook

Prepare topology and starting file

The peptide used in this demonstration is CLN025, a variant of chignolin

This tutorial uses GBNeck2 + ff14SBonlysc

You can use any text editor but we can create/edit files in this notebook by %%file command

In [2]:
%%file leap.in
source leaprc.protein.ff14SBonlysc
x = sequence {TYR TYR ASP PRO GLU THR GLY THR TRP TYR}
set default PBradii mbondi3 
saveamberparm x peptide.top peptide.crd
savepdb x peptide.pdb 
quit
Overwriting leap.in
In [3]:
!$AMBERHOME/bin/tleap -f leap.in > leap.log
In [4]:
!ls peptide.*
peptide.crd peptide.pdb peptide.top

Create md input and run minimization

In [5]:
%%file min.in
Stage 1 - minimisation
&cntrl
  imin=1, maxcyc=1000, ncyc=500,
  cut=999.,
  igb=8,
  ntb=0,
  ntpr=100
/
Overwriting min.in
In [6]:
%%file md.in
md.in, 100 ps
&cntrl
       imin = 0, nstlim = 50000, dt = 0.002,
       ntx = 1, irest = 0, ig = -1,
       ntt=3, tempi = 300.0, temp0 = 300.0, gamma_ln = 1.0,
       ntc = 2, ntf = 2,
       ntwx = 25000, ntwe = 0, ntwr = 100, ntpr = 25000,
       cut = 999.0,
       ntb = 0,
       igb = 8, gbsa = 0,
       nscm = 25000,
       ioutfm = 1,
/
Overwriting md.in
In [7]:
!$AMBERHOME/bin/sander -O -i min.in -p peptide.top -c peptide.crd -r min.rst7
In [8]:
!ls min.rst7
min.rst7

Real time visualizing simulation

In [9]:
# use pytraj for reading coordinates from rst7 file
import pytraj as pt

# use nglview for visualizing this notebook
import nglview as nv
In [10]:
from time import time, sleep

def get_coordinates():
    # return coordinates in restart file
    # we reuse topology from reference structure to avoid reload
    traj = pt.load('md.rst', top=ref.top)
    
    # perform superpose to min.rst7 to remove translation and rotation
    # superpose is similiar to cpptraj's rms
    traj.superpose(mask='@CA', ref=ref)
    return traj.xyz[0]

def update_view(view, delay=1, timeout=300):
    # update coordinates every `delay` second, default 1 second
    
    from time import sleep
    coordinates = get_coordinates()
    view.coordinates_dict = {0: coordinates}
    sleep(delay)
    
def run_view(view, delay=1., timeout=300):
    # stop viewing after `timeout` seconds, default 300 (5 minutes)
    start = time()
    stop = start
    
    while stop - start < timeout:
        update_view(view, delay=delay)
        stop = time()
    
def run_md():
    # run md simulation via sander
    import os
    import subprocess
    
    # you can use sander.MPI, pmemd, pmemd.MPI, pmemd.cuda too
    # need to provide absolute directory for sander
    sander = os.getenv('AMBERHOME') + '/bin/sander'
    cm = "{sander} -O -i md.in -o md.out -p peptide.top -c min.rst7 -r md.rst -x md.nc".format(sander=sander)
    
    # we will run sander in background so we can keep using this notebook
    print('running {}'.format(cm))
    return subprocess.Popen(cm.split())
In [11]:
# load minimized struture and use it as reference for superposing
ref = pt.load('min.rst7', top='peptide.top')

# view starting structure (min.rst7)
view = nv.show_pytraj(ref)
view
In [12]:
view.clear_representations()

# add more representation here
view.add_ball_and_stick()
view.add_cartoon()

view.parameters = dict(camera_type='perpective',
                       background_color='black',
                       fog_near=60,
                       clip_dist=0)
In [13]:
# run MD simulation in background so we can keep using this notebook
process = run_md()
running /Users/haichit/amber16/bin/sander -O -i md.in -o md.out -p peptide.top -c min.rst7 -r md.rst -x md.nc
In [14]:
# If you want to kill sander job, please use choose Kernel --> Interupt (top page)

# If you want to run again (fresh MD), run previous step first (process = run_md()), then run this cell
# change the timeout if needed
# I am using 20 seconds, but you can make it run for few days too

run_view(view, timeout=20)

Note

  • Real time visualization does not slow down the speed of your simulation if your simulation and this notebook are running in different CPU.

  • You can also run pmemd.cuda on GPU and use this notebook, just need to update run_md method

  • It's not necessarily to run MD simulation in this notebook, pytraj/nglview only load and visualize your restart file

  • See also: pytraj and nglview tutorials