Basic examples

try pytraj online:

http://mybinder.org/badge.svg

How to run pytraj

There are two modes to run pytraj: script mode and interactive mode.

  • script mode: you need to write code in a file (for example: my_file.py) and use python to run your file:

    python my_file.py
    
  • interactive mode: we suggest to use ipython or jupyter notebook to explore data interactively.

Load a Topology and Trajectory

In [1]: import pytraj as pt

In [2]: traj = pt.load('tz2.nc', 'tz2.parm7')

In [3]: traj
Out[3]: 
pytraj.Trajectory, 101 frames: 
Size: 0.000503 (GB)
<Topology: 223 atoms, 13 residues, 1 mols, non-PBC>

Select atoms

In [4]: import pytraj as pt

In [5]: traj = pt.load('tz2.nc', 'tz2.parm7')

# get indices for backbone H atoms
In [6]: h_indices = pt.select_atoms(traj.top, '@H')

In [7]: h_indices
Out[7]: array([ 14,  38,  52, ..., 172, 196, 218])

# get indices for backbone N atoms
In [8]: n_indices = pt.select_atoms(traj.top, '@N')

In [9]: n_indices
Out[9]: array([  0,  13,  37, ..., 171, 195, 217])

Distance between two COMs

# compute distance between COM of (residue 1 to 3) and COM of (residue 5 to 8)
In [10]: pt.distance(traj, ':1-3 :5-8')
Out[10]: array([ 12.0518,   9.0688,   8.4039, ...,  11.2076,   8.7337,  10.04  ])

DSSP analysis

In [11]: import pytraj as pt

In [12]: pdb = pt.load_pdb_rcsb("1l2y")

In [13]: result = pt.dssp(pdb)

In [14]: print(result)
(array(['ASN:1', 'LEU:2', 'TYR:3', ..., 'PRO:18', 'PRO:19', 'SER:20'], 
      dtype='<U6'), array([['0', 'H', 'H', ..., '0', '0', '0'],
       ['0', 'T', 'H', ..., '0', '0', '0'],
       ['0', '0', 'H', ..., '0', '0', '0'],
       ..., 
       ['0', 'H', 'H', ..., '0', '0', '0'],
       ['0', '0', 'H', ..., '0', '0', '0'],
       ['0', 'T', 'H', ..., '0', '0', '0']], 
      dtype='<U1'), <pytraj.DatasetList with 8 datasets>
none_avg
[ 0.3   0.25  0.35 ...,  0.3   0.3   0.3 ]

para_avg
[ 0.  0.  0. ...,  0.  0.  0.]

anti_avg
[ 0.  0.  0. ...,  0.  0.  0.]
...

turn_avg
[ 0.1   0.15  0.1  ...,  0.1   0.05  0.15]

bend_avg
[ 0.05  0.1   0.05 ...,  0.05  0.1   0.05])

Hbond analysis

# search hbonds for all residues
In [15]: hbonds = pt.search_hbonds(pdb)

In [16]: hbonds
Out[16]: 
<pytraj.hbonds.DatasetHBond
donor_acceptor pairs : 31>

# print first few hbond
In [17]: hbonds.donor_acceptor[:5]
Out[17]: 
['ASN1_O-GLN5_N-H',
 'ARG16_O-TRP6_NE1-HE1',
 'TYR3_O-LEU7_N-H',
 'ILE4_O-LYS8_N-H',
 'LEU7_O-GLY10_N-H']

In [18]: hbonds.values
Out[18]: 
array([[6, 7, 8, ..., 7, 6, 6],
       [1, 0, 0, ..., 0, 0, 0],
       [1, 1, 1, ..., 1, 1, 1],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=int32)

# search hbonds between residue 9 and 16
In [19]: h = pt.search_hbonds(pdb, ':9,16')

In [20]: h.donor_acceptor
Out[20]: 
['ASP9_OD2-ARG16_NH1-HH12',
 'ASP9_OD2-ARG16_NH2-HH22',
 'ASP9_OD2-ARG16_NE-HE',
 'ASP9_OD2-ARG16_NH2-HH21',
 'ASP9_OD2-ARG16_NH1-HH11']