Fancy indexing of trajectory

try pytraj online:

use [ ] slicing notation to load all given frames into memory at once

In [1]: import pytraj as pt

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

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

# get 1st frame
In [4]: traj[0]
Out[4]: <Frame with 5293 atoms>

# get last frame
In [5]: traj[-1]
Out[5]: <Frame with 5293 atoms>

# get frame 2 to 8, skip every 2
In [6]: traj[2:8:2]
Out[6]: 
pytraj.Trajectory, 3 frames: 
Size: 0.000355 (GB)
<Topology: 5293 atoms, 1704 residues, 1692 mols, PBC with box type = ortho>
           

# strip all atoms but CA
In [7]: traj['@CA']
Out[7]: 
pytraj.Trajectory, 10 frames: 
Size: 0.000003 (GB)
<Topology: 12 atoms, 12 residues, 12 mols, PBC with box type = ortho>
           

# take coords for 1st residues
In [8]: traj[':1']
Out[8]: 
pytraj.Trajectory, 10 frames: 
Size: 0.000003 (GB)
<Topology: 13 atoms, 1 residues, 1 mols, PBC with box type = ortho>
           

# get frames 1, 3, 7 with only CA atoms
In [9]: traj[[1, 3, 7], '@CA']
Out[9]: 
pytraj.Trajectory, 3 frames: 
Size: 0.000001 (GB)
<Topology: 12 atoms, 12 residues, 12 mols, PBC with box type = ortho>
           

# get frame 2 to 8, skip every 2 and keep only non-H atoms
In [10]: traj[2:8:2, '!@H=']
Out[10]: 
pytraj.Trajectory, 3 frames: 
Size: 0.000121 (GB)
<Topology: 1807 atoms, 1704 residues, 1692 mols, PBC with box type = ortho>
           

# get frame 2 to 8, skip every 2, reverse from last to begin
# strip water
In [11]: traj[8:2:-2, '!:WAT']
Out[11]: 
pytraj.Trajectory, 3 frames: 
Size: 0.000015 (GB)
<Topology: 220 atoms, 13 residues, 1 mols, PBC with box type = ortho>
           

# skip every 2 frames
In [12]: traj[::2]
Out[12]: 
pytraj.Trajectory, 5 frames: 
Size: 0.000592 (GB)
<Topology: 5293 atoms, 1704 residues, 1692 mols, PBC with box type = ortho>

use ( ) notation to create frame iterator, load only a single frame to memory if needed

In [13]: traj(0, 8, 2)
Out[13]: 
<FrameIterator with start=0, stop=8, step=2, n_frames=4, 
frame_indices=None, 
mask=None, autoimage=False, rmsfit=None, copy=False> 

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

# iterating with autoimage
In [15]: for frame in traj(0, 8, 2, autoimage=True): print(frame)
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>

# iterating with rmsfit to first frame
In [16]: for frame in traj(0, 8, 2, rmsfit=0): print(frame)
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>
<Frame with 5293 atoms>

# iterating all frames and keep only CA atoms
In [17]: for frame in traj(mask='@CA'): print(frame)
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>
<Frame with 12 atoms>

# iterating all frames and strip waters
In [18]: for frame in traj(mask='!:WAT'): print(frame)
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>
<Frame with 220 atoms>