Contents
Normally, user needs to follow below code to process many files
In [1]: import pytraj as pt
In [2]: import numpy as np
In [3]: template = 'tz2.%s.nc'
In [4]: flist = [template % str(i) for i in range(4)]
In [5]: print(flist)
['tz2.0.nc', 'tz2.1.nc', 'tz2.2.nc', 'tz2.3.nc']
In [6]: trajlist = [pt.iterload(fname, 'tz2.parm7') for fname in flist]
# calculate radgyr
In [7]: data = []
In [8]: for traj in trajlist:
...: data.append(pt.radgyr(traj))
...:
In [9]: data = np.array(data).flatten()
In [10]: print(data)
[ 9.4451 9.5746 10.1923 ..., 7.4834 7.4324 7.6204]
However, pytraj
offer shorter (and easy?) way to do
# load all files into a single TrajectoryIterator
In [11]: filelist = ['tz2.0.nc', 'tz2.1.nc', 'tz2.2.nc']
In [12]: traj = pt.iterload(filelist, 'tz2.parm7')
# perform calculation
In [13]: data = pt.radgyr(traj)
# that's it
In [14]: print(data)
[ 9.4451 9.5746 10.1923 ..., 7.4834 7.4324 7.6204]
Supposed you have a list of 5 (or whatever) trajectories, you only want to load those files 1st to 100-th frames
and skip every 10 frames. Below is a convention cpptraj
input.
parm 2koc.parm7
trajin traj0.nc 1 100 10
trajin traj1.nc 1 100 10
trajin traj2.nc 1 100 10
trajin traj3.nc 1 100 10
trajin traj4.nc 1 100 10
In pytraj
, you can specify frame_slice
import pytraj as pt
pt.iterload('traj*.nc', top='2koc.parm7', frame_slice=[(0, 100, 10),]*5)
# [(0, 100, 10),]*5 is equal to [(0, 100, 10), (0, 100, 10),(0, 100, 10),(0, 100, 10),(0, 100, 10),]
In [15]: import pytraj as pt
In [16]: frame_indices = [2, 4, 7, 51, 53]
# use ``load`` to load those frames to memory
In [17]: traj0 = pt.load('tz2.nc', 'tz2.parm7', frame_indices=frame_indices)
In [18]: traj0
Out[18]:
pytraj.Trajectory, 5 frames:
Size: 0.000025 (GB)
<Topology: 223 atoms, 13 residues, 1 mols, non-PBC>
# only loadd coordinates for specific atoms
In [19]: traj1 = pt.load('tz2.nc', 'tz2.parm7', frame_indices=frame_indices, mask='@CA')
In [20]: traj1
Out[20]:
pytraj.Trajectory, 5 frames:
Size: 0.000001 (GB)
<Topology: 12 atoms, 12 residues, 12 mols, non-PBC>
# or use ``iterload``
In [21]: frame_indices = [2, 4, 7, 51, 53]
In [22]: traj2 = pt.iterload('tz2.nc', 'tz2.parm7')
In [23]: traj2
Out[23]:
pytraj.TrajectoryIterator, 101 frames:
Size: 0.000503 (GB)
<Topology: 223 atoms, 13 residues, 1 mols, non-PBC>
In [24]: traj2[frame_indices, '@CA']