pytraj.io

pytraj.io.load(filename, top=None, frame_indices=None, mask=None, stride=None)

try loading and returning appropriate values. See example below.

Parameters:

filename : str, Trajectory filename

top : Topology filename or a Topology

frame_indices : {None, array_like}, default None

only load frames with given number given in frame_indices

stride : {None, int}, default None

if given, frame will be skip every stride. Note: if bot frame_indices and stride are given, frame_indices will be ignored.

mask : {str, None}, default None

if None: load coordinates for all atoms if string, load coordinates for given atom mask

Returns:

pytraj.Trajectory

See also

iterload

Notes

  • For further slicing options, see pytraj.TrajectoryIterator (created by pytraj.iterload)
  • Also see pytraj.iterload for loading a series of trajectories that don’t fit to memory

Examples

>>> import pytraj as pt
>>> # load netcdf file with given amber parm file
>>> traj = pt.load('traj.nc', '2koc.parm7') 
>>> # load mol2 file
>>> traj = pt.load('traj.mol2') # 
>>> # load pdb file
>>> traj = pt.load('traj.pdb') 
>>> # load given frame numbers
>>> from pytraj.testing import get_fn
>>> fn, tn = get_fn('tz2_dry')
>>> traj = pt.load(fn, top=tn, frame_indices=[0, 3, 5, 12, 20])
>>> traj = pt.load(fn, top=tn, frame_indices=[0, 3, 5, 12, 20], mask='!@H=')
>>> # load with frame slice
>>> traj = pt.load(fn, tn, frame_indices=slice(0, 10, 2))
>>> # which is equal to:
>>> traj = pt.load(fn, tn, frame_indices=range(0, 10, 2))
>>> # load with stride
>>> traj = pt.load(fn, tn)
>>> traj.n_frames
101
>>> traj = pt.load(fn, tn, stride=5)
>>> traj.n_frames
21
>>> # load with stride for more than one filename
>>> traj = pt.load([fn, fn], tn, stride=5)
>>> traj.n_frames
42
>>> traj.n_atoms
223
>>> # load with stride for more than one filename, and with mask
>>> traj = pt.load([fn, fn], tn, stride=5, mask='@CA')
>>> traj.n_frames
42
>>> traj.n_atoms
12
pytraj.io.iterload(*args, **kwd)

return TrajectoryIterator object

Parameters:

filename: {str, list-like of filenames, pattern}

input trajectory filename(s). You can use a single filename, a list of filenames or a pattern.

top : {str, Topology}

input topology. If str, pytraj will load from disk to Topology first

frame_slice: tuple or list of tuple

specify start, stop, step for each trajectory you want to read.

cpptraj input:

trajin traj0.nc 1 10
trajin traj1.nc

In pytraj, corresponding frame_slice=[(0, 10), (0, -1)]

stride : {None, int}, default None

if not None, trajectories will be strided. Note: if both stride and frame_slice are not None, frame_slice will be ignored

Returns:

pytraj.TrajectoryIterator

Notes

Unlike pytraj.load, you can not arbitarily set frame_indices. If you want to do so, first load trajectories to TrajectoryIterator object, then do fancy slicing

>>> import pytraj as pt
>>> # register to load traj.nc from 0-th to 99-th frame
>>> traj = pt.iterload('traj.nc', 'prmtop', frame_slice=(0, 100)]) 
>>> # do fancy indexing to load specific frames to memory
>>> traj[[0, 8, 3, 50, 7]] 
>>> # load to disk with given mask
>>> traj[[0, 8, 3, 50, 7], '!@H='] 

Examples

>>> import pytraj as pt
>>> traj = pt.iterload('traj.nc', '2koc.parm7') 
>>> # load from a list of filenames
>>> traj = pt.iterload(['traj0.nc', 'traj1.nc'], '2koc.parm7') 
>>> # load all files with a given pattern (sorted)
>>> traj = pt.iterload('./traj*.nc', '2koc.parm7') 
>>> # load from a list of files with given frame step
>>> # for each file, only register to load from frame 0 to 9 (skip 10), skip every 2 frames
>>> traj = pt.iterload(['traj0.nc', 'traj1.nc'], '2koc.parm7', frame_slice=[(0, 10, 2),]*2) 
>>> # load from frame 0 to 9 for `traj0.nc`
>>> # load all frames from `traj1.nc`
>>> traj = pt.iterload(['traj0.nc', 'traj1.nc'], '2koc.parm7', frame_slice=[(0, 10), (0, -1)]) 
>>> # use stride, skip every 2 frames
>>> from pytraj.testing import get_remd_fn
>>> filenames, topology_filename = get_remd_fn('remd_ala2')
>>> [fn.split('/')[-1] for fn in filenames]
['rem.nc.000', 'rem.nc.001', 'rem.nc.002', 'rem.nc.003']
>>> traj = pt.iterload(filenames, topology_filename, stride=2)
pytraj.io.load_remd(filename, top=None, T='300.0')
pytraj.io.iterload_remd(filename, top=None, T='300.0')

Load temperature remd trajectory for single temperature. e.g: Suppose you have replica trajectoris remd.x.00{1-4}. You want to load and extract only frames at 300 K, use this method

Parameters:

filename : str

top : {str, Topology}

T : {float, str}, default=300.0

Returns:

pytraj.traj.TrajectoryCpptraj

pytraj.io.load_pdb_rcsb(pdbid)

load pdb file from rcsb website

Parameters:pdbid : str

Examples

io.loadpdb_rcsb(“2KOC”) # popular RNA hairpin

pytraj.io.load_cpptraj_file(filename)
Parameters:

fname : str, name of cpptraj input file

(“cpptraj -i input.txt” –> fname = “input.txt”)

pytraj.io.load_sample_data(data_name=None)

Return TrajectoryIterator instance for Ala3 or tz2 data

Parameters:data_name : str, {‘ala3’, ‘tz2’, ‘rna’, ‘trpcage’}, default ‘ala3’

Notes

tz2 dataset
: $AMBERHOME/AmberTools/test/cpptraj/
explicit water, ortho box
pytraj.io.load_parmed(parm, traj=True, **kwd)

return pytraj’s Topology or Trajectory objects

Parameters:

parm : ParmEd’s Structure object

traj: bool, default True

if True, return pytraj.Trajectory if False, return Topology

>>> import parmed as pmd

>>> import pytraj as pt

>>> p = pmd.download_PDB(“1l2y”)

>>> traj = pt.load_parmed(p)

pytraj.io.load_leap(command, verbose=False)

create pytraj.Trajectory from tleap’s command.

Notes

If you load pdb file, make sure to use absolute dir. This method is not extensively tested. Use it with your own risk.

pytraj.io.load_antechamber(filename, format=None, options='')

create pytraj.Trajectory by using antechamber to convert filename to mol2 format, then using pytraj.load

Good for file formats that cpptraj and ParmEd do not support (.ac, ...)

Parameters:

filename : str

format : str or None, default None

if None, using filename extension else, use given format

options : str, additional antechamber command

pytraj.io.load_topology(filename, option='')

load Topology from a filename or from url or from ParmEd object. Adapted from cpptraj doc.

Parameters:

filename : str, Amber prmtop, pdb, mol2, psf, cif, gromacs topology, sdf, tinker formats

option : cpptraj options.

if filename is a pdb file, option = {‘pqr’, ‘noconnect’}.

  • pqr : Read atomic charge/radius from occupancy/B-factor columns.
  • noconect: Do not read CONECT records if present.

Notes

if cpptraj/pytraj does not support specific file format, you still can convert to PDB file. cpptraj will do the bond guess based on distance.

Examples

>>> import pytraj as pt
>>> # from a filename
>>> pt.load_topology("data/tz2.ortho.parm7")
<Topology: 5293 atoms, 1704 residues, 1692 mols, PBC with box type = ortho>
>>> # read with option
>>> pt.load_topology('1KX5.pdb', 'bondsearch 0.2') 
pytraj.io.write_parm(filename=None, top=None, format='amberparm', overwrite=False)
pytraj.io.save(filename, obj, *args, **kwd)

an universal method

Parameters:

filename : output filename

obj : Topology or Trajetory-like

if Topology, write a new Topology to disk if Trajetory-like, write a trajectory to disk

*args, **kwd: additional options

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_ala3()
>>> isinstance(traj, pt.TrajectoryIterator)
True
>>> top = traj.top
>>> isinstance(top, pt.Topology)
True
>>> # save Topology to a new Topology
>>> pt.save('output/prmtop', top, overwrite=True)
>>> isinstance(pt.load_topology('output/prmtop'), pt.Topology)
True
>>> # save TrajectoryIterator to disk
>>> pt.save('output/traj.nc', traj, overwrite=True)
>>> isinstance(pt.iterload('output/traj.nc', traj.top), pt.TrajectoryIterator)
True
pytraj.io.write_traj(filename, traj, format='infer', top=None, frame_indices=None, overwrite=False, force=False, velocity=False, options='')

write Trajectory-like or iterable object to trajectory file

Parameters:

filename : str

traj : Trajectory-like or iterator that produces Frame or 3D ndarray with shape=(n_frames, n_atoms, 3)

format : str, default ‘infer’

if ‘inter’, detect format based on extension. If can not detect, use amber mdcdf format.

top : Topology, optional, default: None

frame_indices: array-like or iterator that produces integer, default: None

If not None, only write output for given frame indices

overwrite: bool, default: False

velocity : bool, default False

if True, write velocity. Make sure your trajectory or Frame does have velocity

force : bool, default False

if True, write force. Make sure your trajectory or Frame does have force

options : str, additional cpptraj keywords

Notes

Format Extension
Amber Trajectory .crd
Amber NetCDF .nc
Amber Restart .rst7
Amber NetCDF .ncrst
Charmm DCD .dcd
PDB .pdb
Mol2 .mol2
Scripps .binpos
Gromacs .trr
SQM Input .sqm

‘options’ for writing to pdb format (cptraj manual):

dumpq:       Write atom charge/GB radius in occupancy/B-factor columns (PQR format)."
parse:       Write atom charge/PARSE radius in occupancy/B-factor columns (PQR format)."
vdw:         Write atom charge/VDW radius in occupancy/B-factor columns (PQR format)."
pdbres:      Use PDB V3 residue names."
pdbatom:     Use PDB V3 atom names."
pdbv3:       Use PDB V3 residue/atom names."
teradvance:  Increment record (atom) # for TER records (default no)."
terbyres:    Print TER cards based on residue sequence instead of molecules."
model:       Write to single file separated by MODEL records."
multi:       Write each frame to separate files."
chainid <c>: Write character 'c' in chain ID column."
sg <group>:  Space group for CRYST1 record, only if box coordinates written."
include_ep:  Include extra points."
conect:      Write CONECT records using bond information.");

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()
>>> pt.write_traj("output/t.nc", traj, overwrite=True) # write to amber netcdf file
>>> # write to multi pdb files (t.pdb.1, t.pdb.2, ...)
>>> pt.write_traj("output/t.pdb", traj, overwrite=True, options='multi')
>>> # write all frames to single pdb file and each frame is seperated by "MODEL" word
>>> pt.write_traj("output/t.pdb", traj, overwrite=True, options='model')
>>> # write to DCD file
>>> pt.write_traj("output/test.dcd", traj, overwrite=True)
>>> # write to netcdf file from 3D numpy array, need to provide Topology
>>> xyz = traj.xyz
>>> top = traj.top
>>> pt.write_traj("output/test_xyz.nc", xyz, top=traj.top, overwrite=True)
>>> pt.write_traj("output/test_xyz.nc", xyz, top=traj.top, overwrite=True)
>>> # you can make a fake Topology to write xyz coordinates too
>>> n_atoms = xyz.shape[1]
>>> top2 = pt.tools.make_fake_topology(n_atoms)
>>> pt.write_traj("output/test_xyz_fake_top.nc", xyz, top=top2, overwrite=True)

‘options’ for writing to amber netcdf format (cptraj manual):

remdtraj: Write temperature to trajectory (makes REMD trajectory)."
velocity: Write velocities to trajectory."
force: Write forces to trajectory.");

‘options’ for writing to amber netcdf restart format(cptraj manual):

novelocity: Do not write velocities to restart file."
notime:     Do not write time to restart file."
remdtraj:   Write temperature to restart file."
time0:      Time for first frame (default 1.0)."
dt:         Time step for subsequent frames, t=(time0+frame)*dt; (default 1.0)");
keepext     Keep filename extension; write '<name>.<num>.<ext>' instead (example: myfile.1.rst7)

‘options’ for writing to mol2 format (cptraj manual):

single   : Write to a single file."
multi    : Write each frame to a separate file."
sybyltype: Convert Amber atom types (if present) to SYBYL types.");

‘options’ for other formats:

please check http://ambermd.org/doc12/Amber15.pdf
pytraj.io.read_pickle(path)

Load pickled pandas object (or any other pickled object) from the specified file path

Warning: Loading pickled data received from untrusted sources can be unsafe. See: http://docs.python.org/2.7/library/pickle.html

Parameters:

path : string

File path

Returns:

unpickled : type of object stored in file

pytraj.io.to_pickle(obj, path)

Pickle (serialize) object to input file path

Parameters:

obj : any object

path : string

File path

pytraj.io.select_atoms(mask, topology)

return atom indices

Examples

>>> import pytraj as pt
>>> traj = pt.datafiles.load_tz2_ortho()
>>> atom_indices = pt.select_atoms('@CA', traj.top)
>>> atom_indices
array([  4,  15,  39, ..., 159, 173, 197])
>>> pt.select_atoms(traj.top, '@CA')
array([  4,  15,  39, ..., 159, 173, 197])