pytraj.Trajectory

class pytraj.trajectory.trajectory.Trajectory(filename=None, top=None, xyz=None, velocity=None, force=None)

Bases: pytraj.trajectory.shared_trajectory.SharedTrajectory

Simple in-memory Trajectory. It has only information about 3D coordinates and unitcells (no time, no velocity, no force, ...)

Parameters:

filename: str, trajectory filename

top : Topology

xyz: 3D-array

if filename is not given, pytraj will construct Trajectory from given Topology and given xyz array.

Examples

>>> import pytraj as pt
>>> from pytraj.testing import get_fn
>>> fn, tn = get_fn('ala3')
>>> # load from filename and topology name
>>> traj = pt.Trajectory(fn, tn)
>>> # load from a list of filenames
>>> traj = pt.Trajectory([fn, fn], tn)
>>> # load from 3D array-like
>>> xyz = traj.xyz
>>> traj_1 = pt.Trajectory(xyz=xyz, top=traj.top)
>>> # get new Trajectory with only CA atoms
>>> traj['@CA'].xyz[:, :, 0]
array([[  3.970048 ,   7.6400076,  10.1610562]])
>>> # iterate
>>> for frame in traj: pass

Attributes

n_atoms
n_frames
shape (n_frames, n_atoms, 3)
top Topology (having info about atom, residue, ...)
topology Topology (having info about atom, residue, ...)
unitcells return 2D ndarray, shape=(n_frames, 6)
xyz Trajectory’s coordinates, shape=(n_frames, n_frames, 3), dtype=’f8’

Methods

__call__(\*args, \*\*kwd) shortcut of iterframe method
align_principal_axis([command]) align principal axis
append(other) other: xyz, Frame, Trajectory, ...
append_xyz(xyz) append 3D numpy array
autoimage([command]) perform autoimage
center(\*args, \*\*kwargs) do centering
copy() return a deep copy of trajectory
from_iterable(iterables[, top]) create a new Trajectory from iterable (produce Frame)
iterframe([start, stop, step, mask, ...]) iterate trajectory with given frame_indices or given (start, stop, step)
load([filename]) load file or files. This is for internal use. User should always use
rmsfit(\*args, \*\*kwd) do the fitting to reference Frame by rotation and translation
rotate([command]) do rotation
save([filename, overwrite])
scale([command]) do scaling
strip(mask) strip atoms with given mask
superpose([mask, ref, ref_mask, ...]) do the fitting to reference Frame by rotation and translation
transform(commands[, frame_indices]) apply a series of cpptraj commands to trajectory
translate([command]) do translation
view(\*args, \*\*kwargs)
visualize(\*args, \*\*kwargs) require NGLView
align_principal_axis(command='')

align principal axis

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('ala3')[:]
>>> traj = traj.align_principal_axis()
append(other)

other: xyz, Frame, Trajectory, ...

Notes

  • Can not append TrajectoryIterator object since we use Trajectory in TrajectoryIterator class
  • This method is not well optimized for speed.

Examples

>>> import pytraj as pt
>>> import numpy as np
>>> traj = pt.load_sample_data('tz2')[:]
>>> t0 = pt.Trajectory(top=traj.top)
>>> t0.n_frames
0
>>> f0 = traj[0]
>>> t0.append(f0)
>>> t0.n_frames
1
>>> t0.append(np.array([traj[3].xyz,]))
>>> t0.n_frames
2
>>> t0.append(traj)
>>> t0.n_frames
12
>>> t0.append(traj())
>>> t0.n_frames
22
>>> t1 = pt.Trajectory(top=traj.top)
>>> t1.append(traj)
>>> t2 = pt.Trajectory(top=traj.top)
>>> t2.append(traj.xyz)
append_xyz(xyz)

append 3D numpy array

Notes

This method is not well optimized for speed.

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> t0 = pt.Trajectory(top=traj.top)
>>> t0.append_xyz(traj.xyz)
>>> t0.n_frames
10
>>> t0.append_xyz(traj.xyz)
>>> t0.n_frames
20
autoimage(command='')

perform autoimage

Returns:self

Examples

>>> import pytraj as pt; from pytraj.testing import get_fn
>>> t0 = pt.load(*get_fn('tz2'))
>>> t0.top.has_box()
True
>>> t0 = t0.autoimage()
center(*args, **kwargs)

do centering

Returns:self

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('ala3')[:]
>>> traj = traj.center('@CA origin')
copy()

return a deep copy of trajectory

Examples

>>> import pytraj as pt
>>> t0 = pt.datafiles.load_rna()[:]
>>> isinstance(t0.copy(), pt.Trajectory)
True
classmethod from_iterable(iterables, top=None)

create a new Trajectory from iterable (produce Frame)

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')
>>> t0 = pt.Trajectory.from_iterable(traj(3, 8, 2))
>>> from pytraj import pipe
>>> fi = pipe(traj, ['autoimage', 'rms'])
>>> t0 = pt.Trajectory.from_iterable(fi, top=traj.top)
>>> t0.n_frames
10
>>> pt.radgyr(t0)
array([ 18.90953437,  18.93564662,  18.85415458,  18.90994856,
        18.85884218,  18.88551081,  18.9364612 ,  18.89353463,
        18.91772124,  18.87070283])
iterframe(start=0, stop=None, step=1, mask=None, autoimage=False, frame_indices=None, rmsfit=None)

iterate trajectory with given frame_indices or given (start, stop, step)

Parameters:

start : int, default 0

stop : {None, int}, default None

if None, iterate to final frame

step : int, default 1

mask : {None, str}, default None

if None, use all atoms. If not None, use given mask

autoimage : bool, default False

if True, perform autoimage for each frame

rmsfit : {None, int, tuple}, default None

if not None, perform superpose each Frame to to reference.

frame_indices : {None, array-like}

if not None, iterate trajectory for given indices. If frame_indices is given, (start, stop, step) will be ignored.

Examples

>>> import pytraj as pt
>>> from pytraj.testing import get_fn
>>> traj = pt.load(*get_fn('tz2'))
>>> for frame in traj.iterframe(0, 8, 2): pass
>>> for frame in traj.iterframe(0, 8, 2, autoimage=True): pass
>>> # use negative index
>>> traj.n_frames
10
>>> fi = traj.iterframe(0, -1, 2, autoimage=True)
>>> fi.n_frames
5
>>> # mask is atom indices
>>> fi = traj.iterframe(0, -1, 2, mask=range(100), autoimage=True)
>>> fi.n_atoms
100
load(filename='')

load file or files. This is for internal use. User should always use pytraj.load (or iterload) method

Notes

It’s better to use pytraj.load method

>>> traj = pt.load(fname, tname)
>>> traj.n_atoms
5293

Examples

>>> import pytraj as pt
>>> from pytraj.testing import get_fn
>>> fname, tname = get_fn('tz2')
>>> traj = pt.Trajectory()
>>> traj.top = pt.load_topology(tname)
>>> traj.load(fname)
>>> traj.n_atoms
5293
>>> # from list/tuple
>>> traj.load([fname, fname])
>>> traj.load((fname, fname))
n_atoms
n_frames
rmsfit(*args, **kwd)

do the fitting to reference Frame by rotation and translation

Parameters:

ref : {Frame, int}, default=None (first Frame)

Reference

mask : str or AtomMask object, default=’*’ (fit all atoms)

Returns:

self

Notes

this is alias of superpose

Examples

>>> traj.rmsfit(0) # fit to 1st frame 
>>> traj.rmsfit(-1, '@CA') # fit to last frame using @CA atoms 
rotate(command='')

do rotation

Returns:self

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('ala3')[:]
>>> traj = traj.rotate('@CA x 20')
save(filename='', overwrite=False, **kwd)
scale(command='')

do scaling

Returns:self

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('ala3')[:]
>>> traj = traj.scale('@CA x 1.2')
shape

(n_frames, n_atoms, 3)

strip(mask)

strip atoms with given mask

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data()[:]
>>> traj.n_atoms
34
>>> t0 = traj.strip('!@CA') # keep only CA atoms
>>> isinstance(t0, pt.Trajectory)
True
>>> t0.n_atoms
3
superpose(mask='*', ref=None, ref_mask='', frame_indices=None, mass=False)

do the fitting to reference Frame by rotation and translation

Parameters:

mask : str or AtomMask object, default=’*’ (fit all atoms)

ref : {Frame object, int, str}, default=None

Reference

ref_mask : str, default ‘’

if not given, use mask if given, use it

mass : bool, default False

if True, use mass-weighted

frame_indices : array-like, default None, optional

if not None, only do fitting for specific frames

Returns:

self

Examples

>>> import pytraj as pt
>>> from pytraj.testing import get_fn
>>> traj = pt.load(*get_fn('tz2'))
>>> traj = traj.superpose() # fit to 1st frame
>>> traj = traj.superpose(ref=0) # fit to 1st frame, explitly specify
>>> traj = traj.superpose(ref=-1, mask='@CA') # fit to last frame using @CA atoms
top

Topology (having info about atom, residue, ...)

topology

Topology (having info about atom, residue, ...)

transform(commands, frame_indices=None)

apply a series of cpptraj commands to trajectory

Returns:self

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()[:]
>>> traj.xyz[0, 0]
array([ 15.55458927,  28.54844856,  17.18908691])
>>> traj = traj.transform(['autoimage', 'center @CA origin', 'translate x 1.2'])
>>> traj.xyz[0, 0]
array([-1.19438073,  8.75046229, -1.82742397])
>>> # which is similiar to below:
>>> traj2 = pt.datafiles.load_tz2_ortho()[:]
>>> traj2.xyz[0, 0] # before transforming
array([ 15.55458927,  28.54844856,  17.18908691])
>>> traj = traj2.autoimage().center('@CA origin').translate('x 1.2')
>>> traj2.xyz[0, 0] # after transforming
array([-1.19438073,  8.75046229, -1.82742397])
translate(command='')

do translation

Returns:self

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('ala3')[:]
>>> traj = traj.translate('@CA x 1.2')
unitcells

return 2D ndarray, shape=(n_frames, 6)

Examples

>>> import pytraj as pt
>>> traj = pt.load_sample_data('tz2')[:]
>>> traj.unitcells[0]
array([ 35.26277966,  41.84554768,  36.16862953,  90.        ,
        90.        ,  90.        ])
view(*args, **kwargs)
visualize(*args, **kwargs)

require NGLView

Parameters:args and kwargs : NGLView’s arguments
xyz

Trajectory’s coordinates, shape=(n_frames, n_frames, 3), dtype=’f8’

Examples

>>> import pytraj as pt
>>> traj0 = pt.datafiles.load_ala3()
>>> traj1 = pt.Trajectory(xyz=np.empty((traj0.n_frames, traj0.n_atoms, 3), dtype='f8'), top=traj0.top)
>>> traj1.xyz = traj0.xyz.copy()
>>> # autoconvert from fortran order to c order
>>> xyz = np.asfortranarray(traj0.xyz)
>>> traj1.xyz = xyz