Run cpptraj’s batch

This tutorial is for those who love running cpptraj in batch mode. Try it online:

Note

This tutorial is for advanced users and the syntax might be changed in future.

if you want to run cpptraj’s batch mode like below:

parm tz2.ortho.parm7
trajin tz2.ortho.nc
autoimage
rms first @CA
radgyr @H,C,N,O
molsurf

you can create a ‘CpptrajState’

Create state with existing TrajectoryIterator class

# load traj first
In [1]: import pytraj as pt

In [2]: traj = pt.iterload('./tz2.ortho.nc', '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>
           

# create text (do not need to have ``parm`` and ``trajin`` info)
In [4]: text = '''
   ...: autoimage
   ...: rms first @CA
   ...: radgyr @H,C,N,O
   ...: molsurf @CA'''
   ...: 

# load CpptrajState
In [5]: state = pt.load_batch(traj, text)

# performa calculation
In [6]: state.run()
Out[6]: 
CpptrajState, include:
<datasetlist: 5 datasets>

In [7]: state
Out[7]: 
CpptrajState, include:
<datasetlist: 5 datasets>

# get some data
In [8]: state.data
Out[8]: <pytraj.datasets.CpptrajDatasetList - 5 datasets>

In [9]: state.data.keys()
Out[9]: ['tz2.ortho.parm7', 'RMSD_00001', 'RoG_00002', 'RoG_00002[Max]', 'MSURF_00004']

In [10]: state.data[0]
Out[10]: <pytraj.datasets.DatasetTopology: size=5293, key=tz2.ortho.parm7> 

In [11]: state.data[-1]
Out[11]: 
<pytraj.datasets.DatasetDouble: size=10, key=MSURF_00004> 
values: 
[ 458.5141  459.6478  456.5469  467.7294  462.4591  458.7033  454.4051
  455.1502  468.7057  456.0059]

# convert data to regular numpy array
In [12]: state.data.values
Out[12]: 
array([ array([Atom(name=N, type=N3, atomic_number=7, index=0, resid=0),
       Atom(name=H1, type=H, atomic_number=1, index=1, resid=0),
       Atom(name=H2, type=H, atomic_number=1, index=2, resid=0), ...,
       Atom(name=O, type=OW, atomic_number=8, index=5290, resid=1703),
       Atom(name=H1, type=HW, atomic_number=1, index=5291, resid=1703),
       Atom(name=H2, type=HW, atomic_number=1, index=5292, resid=1703)], dtype=object),
       array([ 0.    ,  0.2641,  0.386 ,  0.2688,  0.2619,  0.4576,  0.4565,
        0.4443,  0.4363,  0.4475]),
       array([ 19.0048,  19.0322,  18.9463,  19.0041,  18.953 ,  18.9814,
        19.0307,  18.9903,  19.0095,  18.9636]),
       array([ 31.5329,  30.9565,  31.7008,  31.9417,  31.712 ,  31.662 ,
        31.515 ,  31.8097,  32.2554,  30.5383]),
       array([ 458.5141,  459.6478,  456.5469,  467.7294,  462.4591,  458.7033,
        454.4051,  455.1502,  468.7057,  456.0059])], dtype=object)

Create state without TrajectoryIterator class

# suppose you have
In [13]: text = '''
   ....: parm tz2.parm7
   ....: trajin tz2.nc
   ....: dihedral phi :1@C  :2@N  :2@CA :2@C
   ....: dihedral psi   :1@N  :1@CA :1@C  :2@N
   ....: dihedral omega   :1@CA :1@C  :2@N  :2@CA
   ....: distance end_to_end :1@N :7@N
   ....: rms first :1-1584
   ....: strip !@H=
   ....: createcrd mycrd
   ....: '''
   ....: 

In [14]: import pytraj as pt

In [15]: state = pt.load_cpptraj_state(text)

# need to explicit call run
In [16]: state.run()
Out[16]: 
CpptrajState, include:
<datasetlist: 7 datasets>

# All datasets are stored in ``state.data``
In [17]: state.data
Out[17]: <pytraj.datasets.CpptrajDatasetList - 7 datasets>

# if you already label your Dataset, you can access to them by using dict-like acessing
# get Dataset with label `end_to_end` (distance)
In [18]: state.data['end_to_end']
Out[18]: 
<pytraj.datasets.DatasetDouble: size=101, key=end_to_end> 
values: 
[ 15.0506   7.8035   5.8305 ...,  14.829   12.6534  13.3292]

# get raw values (usually numpy array)
In [19]: state.data['end_to_end'].values
Out[19]: array([ 15.0506,   7.8035,   5.8305, ...,  14.829 ,  12.6534,  13.3292])

# get `mycrd`
In [20]: state.data['mycrd']
Out[20]: 
pytraj.DatasetCoordsCRD, 101 frames: 
Size: 0.000239 (GB)
<Topology: 106 atoms, 13 residues, 106 mols, non-PBC>
           

In [21]: for dataset in state.data:
   ....:     print(dataset)
   ....: 
<pytraj.datasets.DatasetTopology: size=223, key=tz2.parm7> 
<pytraj.datasets.DatasetDouble: size=101, key=phi> 
values: 
[-52.7812 -40.6208 -51.2155 ..., -88.9455 -87.1766 -77.0983]
<pytraj.datasets.DatasetDouble: size=101, key=psi> 
values: 
[ -86.6975  -91.16    -67.9374 ...,  152.833   167.2929  146.5218]
<pytraj.datasets.DatasetDouble: size=101, key=omega> 
values: 
[-170.5559  177.8609  160.5306 ..., -176.3324 -165.9239  167.2618]
<pytraj.datasets.DatasetDouble: size=101, key=end_to_end> 
values: 
[ 15.0506   7.8035   5.8305 ...,  14.829   12.6534  13.3292]
<pytraj.datasets.DatasetDouble: size=101, key=RMSD_00005> 
values: 
[ 0.      4.0162  6.4142 ...,  8.275   8.1941  7.7792]
pytraj.DatasetCoordsCRD, 101 frames: 
Size: 0.000239 (GB)
<Topology: 106 atoms, 13 residues, 106 mols, non-PBC>