Interface with mdanalysisΒΆ

try pytraj online:

http://mybinder.org/badge.svg

interface_to_mdanalysis
In [1]:
# load MDAnalysis
from MDAnalysis import Universe
from MDAnalysisTests import datafiles
In [9]:
# create Universe in MDanalysis
universe = Universe(datafiles.GRO, datafiles.XTC)
universe.trajectory
Out[9]:
< XTCReader 'MDAnalysisTests/data/adk_oplsaa.xtc' with 10 frames of 47681 atoms>
In [10]:
# need to save a pdb file which will be used as Topology for pytraj
# you don't need to do this step if you already have a pdb file
all_atoms = universe.select_atoms('all')
all_atoms.write('pdb_0.pdb')
In [3]:
# load pytraj
import pytraj as pt
In [19]:
# iterate Universe to get coordinates
# convert to numpy with dtype=f8 (double precision)
import numpy as np
coords = np.asarray([all_atoms.coordinates() for frame in universe.trajectory], dtype='f8')
coords.shape, coords.dtype
Out[19]:
((10, 47681, 3), dtype('float64'))
In [24]:
# create pytraj's Trajectory
traj = pt.Trajectory(xyz=coords, top='pdb_0.pdb')
traj, traj.xyz.shape, traj.xyz.dtype
Out[24]:
(<pytraj.Trajectory, 10 frames, include:
 <Topology: 47681 atoms, 11302 residues, 0 mols, PBC with box type = nonortho>>
            , (10, 47681, 3), dtype('float64'))
In [22]:
# perform some calculation
pt.center_of_geometry(traj)
Out[22]:
array([[ 59.95022684,  39.88316017,  28.29267956],
       [ 59.9966342 ,  39.84912084,  28.32422609],
       [ 60.04153786,  39.90619703,  28.25798692],
       ..., 
       [ 60.03552057,  39.91483842,  28.27997638],
       [ 60.03982541,  39.82686919,  28.3703214 ],
       [ 60.10060834,  39.81491474,  28.34947451]])