def test_ComparetoMDtraj(self): import mdtraj as md traj = pt.load(filename=tc5b_trajin, top=tc5b_top) m_top = md.load_prmtop(tc5b_top) m_traj = md.load_mdcrd(tc5b_trajin, m_top) m_traj.xyz = m_traj.xyz * 10 # convert `nm` to `Angstrom` unit arr0 = pt.rmsd(traj, ref=0) arr1 = pt.rmsd(traj, ref=0) arr2 = pt.rmsd(traj, ) a_md0 = md.rmsd(m_traj, m_traj, 0) aa_eq(arr0, arr1) aa_eq(arr0, arr2) aa_eq(arr0, a_md0) arr0 = pt.rmsd(traj, ref=-1) arr1 = pt.rmsd(traj, ref=-1) a_md = md.rmsd(m_traj, m_traj, -1) aa_eq(arr0, arr1) aa_eq(arr0, a_md) mask = ":3-18@CA,C" atm = traj.top(mask) arr0 = pt.rmsd(traj, ref=-1, mask=mask) arr1 = pt.rmsd(traj, mask=atm.indices, ref=-1) arr2 = pt.rmsd(traj, mask=list(atm.indices), ref=-1) arr3 = pt.rmsd(traj, mask=tuple(atm.indices), ref=-1) a_md = md.rmsd(m_traj, m_traj, -1, atm.indices) aa_eq(arr0, a_md) aa_eq(arr1, a_md) aa_eq(arr2, a_md) aa_eq(arr3, a_md) fa = Trajectory(traj) arr0 = pt.rmsd(fa, ref=-1, mask=mask) arr1 = pt.rmsd(fa, mask=atm.indices, ref=-1) arr2 = pt.rmsd(fa, mask=list(atm.indices), ref=-1) arr3 = pt.rmsd(fa, mask=tuple(atm.indices), ref=-1) a_md = md.rmsd(m_traj, m_traj, -1, atm.indices) aa_eq(arr0, a_md) aa_eq(arr1, a_md) aa_eq(arr2, a_md) aa_eq(arr3, a_md) fa = Trajectory(traj) mask = "!@H=" atm = fa.top(mask) arr0 = pt.rmsd(fa, ref=4, mask=mask) a_md = md.rmsd(m_traj, m_traj, 4, atm.indices) # exclude 0-th frame for ref aa_eq(arr0, a_md)
def 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) """ from parmed.amber import AmberParm with tempfolder(): if isinstance(parm, AmberParm): fname = 'tmp.parm7' else: fname = 'tmp.psf' parm.save(fname) top = _load_Topology(fname) if traj: from pytraj import Trajectory coords = parm.get_coordinates() return Trajectory(xyz=coords, top=top) else: return top
def load_mdtraj(m_traj, autoconvert=False, top=None): """load_mdtraj traj object Parameters ---------- m_traj : Trajectory object from mdtraj autoconvert : bool, default=False convert from "nm" (mdtraj )to "Angstrom" (pytraj) top : pytraj.Topology, default None if `top` is provided, the converting will be much faster """ import numpy as np from mdtraj import Trajectory as MDTrajectory from pytraj.trajectory import Trajectory if autoconvert: unit = 10. else: unit = 1. if not isinstance(m_traj, MDTrajectory): raise ValueError("must be mdtraj's Trajectory object") else: if isinstance(top, string_types): pseudotop = _load_Topology(top) else: pseudotop = top if pseudotop is None: raise ValueError("need Topology or pdb, mol2, ... files") traj = Trajectory(xyz=m_traj.xyz.astype('f8'), top=pseudotop) if m_traj.unitcell_lengths is not None and m_traj.unitcell_angles is not None: traj.unitcells = np.hstack((unit * m_traj.unitcell_lengths, m_traj.unitcell_angles)).astype('f8') return traj
def test_run_0(self): # load traj farray = pt.load(filename="./data/tz2.truncoct.nc", top="./data/tz2.truncoct.parm7")[:2] fold = farray.copy() act = allactions.Action_Image() ptrajin = """ center :2-11 image center familiar com :6 """ # create 'strip' action stripact = allactions.Action_Strip() # creat datasetlist to hold distance data dsetlist = CpptrajDatasetList() dflist = DataFileList() # creat ActionList to hold actions alist = ActionList() top = farray.top # add two actions: Action_Strip and Action_Distance alist.add(allactions.Action_Center(), ArgList(":2-11"), top=top) alist.add(allactions.Action_Image(), ArgList("center familiar com :6"), top=top) # do checking alist.check_topology(top) farray2 = Trajectory() frame0 = Frame() # testing how fast to do the actions # loop all frames # use iterator to make faster loop # don't use "for i in range(farray.n_frames)" for frame in farray: # perform actions for each frame # we make a copy since we want to keep orginal Frame frame0 = frame.copy() alist.compute(frame0) # we need to keep the modified frame in farray2 farray2.append(frame0) # make sure that Action_Strip does its job in stripping assert farray2.n_frames == farray.n_frames fsaved = pt.iterload(cpptraj_test_dir + "/Test_Image/image4.crd.save", "data/tz2.truncoct.parm7") assert fsaved.n_frames == 2
def load_MDAnalysis(universe, top=None): """load MDAnalysis' Universe object to pytra's traj object Notes ----- All coords will be loaded See Also -------- load_MDAnalysisIterator """ from MDAnalysis import Universe from pytraj.Trajectory import Trajectory from ..Frame import Frame # don't import here since we import load_pseudo_parm in # TrajectoryMDAnalysisIterator # MDAnalysis needs numpy. So we always have numpy when using this if not isinstance(universe, Universe): raise ValueError("must be a Universe") # creat pseudotop if top is None: raise ValueError("need a Topology or pdb/mol2/...") else: pseudotop = top # creat atom group ag = universe.atoms farray = Trajectory() farray.top = pseudotop for _ in universe.trajectory: frame = Frame(farray.top.n_atoms) # set box for each Frame frame.boxview[:] = farray.top.box[:] # load xyz coords, let numpy do automatically casting frame.xyz[:] = ag.positions # we don't need to make copy=True since we already created # frame and `farray` can 'keep' it farray.append(frame, copy=False) return farray
def load_ParmEd(parmed_obj, as_traj=False, **kwd): """return pytraj's Topology or Trajectory objects Parameters --------- parmed_obj : ParmEd's Structure object as_traj: bool, default False if True, return pytraj.trajectory.Trajectory if False, return Topology >>> import parmed as pmd >>> import pytraj as pt >>> p = pmd.download_PDB("1l2y") >>> top = pt.load_ParmEd(p) """ import parmed as pmd from parmed.amber import AmberParm if isinstance(parmed_obj, string_types): # reserve **kwd for `structure=True` parmed_obj = pmd.load_file(parmed_obj, **kwd) # faster with tempfolder(): if isinstance(parmed_obj, AmberParm): fname = 'tmp.parm7' else: fname = 'tmppdb.psf' parmed_obj.save(fname) top = _load_Topology(fname) if as_traj: from pytraj import Trajectory coords = parmed_obj.coordinates coords = coords.reshape(1, *coords.shape) return Trajectory(xyz=coords, top=top) else: return top
def test_len(self): traj = Trajectory(fn("Tc5b.x"), tc5b_top) top = traj.top assert len(top) == top.n_atoms
import numpy as np import unittest import pytraj as pt from pytraj.externals.six import zip from pytraj import Topology, Trajectory, Atom from pytraj.core.elements import mass_atomic_number_dict from pytraj.testing import aa_eq # local from utils import fn import pytest TRAJ = Trajectory(fn("Tc5b.x"), fn("Tc5b.top")) tc5b_top = fn('Tc5b.top') class TestTopology(unittest.TestCase): def test_empty_top(self): top = Topology() assert top.is_empty() == True top = pt.load_topology(tc5b_top) assert top.is_empty() == False def test_1(self): top = pt.load_topology(tc5b_top) # top.strip("!@CA") assert top.n_atoms == 20 for atom in top.atoms:
def test_nobox(self): from pytraj import Trajectory traj = Trajectory() traj._allocate(10, 10)