Trajectory

Try pytraj online:

http://mybinder.org/assets/images/logo.svg

Overview

There are two types of trajecoties in pytraj.

This class offers out-of-core data store with easiness to load data to memory.

  • a mutable pytraj.Trajectory. This class hold in-memory data. It is suitable for loading a chunk of snapshots.

Try it

In [1]: import pytraj as pt

In [2]: traj = pt.iterload('data/tz2.ortho.nc', 'data/tz2.ortho.parm7')

In [3]: traj
Out[3]: 
pytraj.TrajectoryIterator, 10 frames: 
Size: 0.001183 (GB)
<Topology: 5293 atoms, 1704 residues, 1692 mols, PBC with box type = ortho>
           

In [4]: pt.rmsd(traj, ref=0, mask='@CA')
Out[4]: 
array([ 0.    ,  0.2641,  0.386 ,  0.2688,  0.2619,  0.4576,  0.4565,
        0.4443,  0.4363,  0.4475])

pytraj is able to detect single file (mol2, pdb) to load as TrajectoryIterator.

>>> pt.iterload('my_pdb.pdb')
>>> pt.iterload('your_mol2.mol2')

TrajectoryIterator is something like range(start, stop[, step]) in python3 or xrange(start, stop[, step]) in python2

In [5]: for i in range(0, 8, 2): print(i)
0
2
4
6

In [6]: for f in traj(0, 8, 2): print(f)
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>

However, TrajectoryIterator is much more than that, you can slice atoms:

In [7]: for f in traj(0, 8, 2, mask='@CA'): print(f, f.xyz[0])
<Frame with 12 atoms> [ 14.8788  27.639   18.088 ]
<Frame with 12 atoms> [ 14.4703  27.9171  18.2456]
<Frame with 12 atoms> [ 14.5707  27.8402  17.4244]
<Frame with 12 atoms> [ 14.7721  28.067   16.9504]

To load all frames to memory at once, use [] notation:

In [8]: traj[0:8:2, '@CA']
Out[8]: 
pytraj.Trajectory, 4 frames: 
Size: 0.000001 (GB)
<Topology: 12 atoms, 12 residues, 12 mols, PBC with box type = ortho>

How to perform analysis with TrajectoryIterator? It’s very simple. For example, if you want to calculate rmsd to 3rd frame (index starts from 0) for all atoms, just:

In [9]: pt.rmsd(traj, ref=3)
Out[9]: 
array([ 11.2707,  10.0133,   8.3305,   0.    ,   8.288 ,  10.0907,
        11.2475,  11.9937,  12.0499,  12.3873])

You have TB of data and want to speed up your calculation, just add more cpus

In [10]: pt.pmap(pt.rmsd, traj, ref=3, n_cores=4)
Out[10]: 
OrderedDict([('RMSD_00001',
              array([ 11.2707,  10.0133,   8.3305,   0.    ,   8.288 ,  10.0907,
                      11.2475,  11.9937,  12.0499,  12.3873]))])

How to get raw coordinates?

>>> traj.xyz
>>> traj[[1, 3, 5]].xyz