from __future__ import print_function import unittest import pytraj as pt from pytraj.testing import aa_eq, tempfolder from pytraj import cluster from pytraj.utils.context import capture_stdout # local from utils import fn tz2_trajin = fn('tz2.nc') tz2_top = fn('tz2.parm7') def test_ClusteringDataset(): traj = pt.load(tz2_trajin, tz2_top) x = pt.cluster.kmeans(traj, n_clusters=5, metric='rms', mask='@CA') assert x.n_frames == 101 assert list(x.centroids) == [24, 101, 76, 13, 9] aa_eq(x.fraction, [0.485, 0.238, 0.139, 0.079, 0.059], decimal=3) def test_cluster_kmeans(): kmeans = cluster.kmeans traj = pt.iterload(tz2_trajin, tz2_top) cm = 'cluster kmeans clusters 10 @CA rms randompoint kseed 2 {0} noinfo' for sieve in [2, 3, 4]: sieve_str = "sieve {0} sieveseed 12".format(sieve)
import pytest import pytraj as pt from pytraj import TrajectoryWriter from pytraj.testing import aa_eq from pytraj.utils import tempfolder # local from utils import fn tc5b_trajin = fn('Tc5b.x') tc5b_top = fn('Tc5b.top') def test_write_CRYST1(): traj = pt.datafiles.load_tz2_ortho()[:1] print(traj.unitcells) with tempfolder(): fn = "test.pdb" traj.save(fn) traj2 = pt.load(fn) aa_eq(traj.unitcells, traj2.unitcells, decimal=3) def test_trajectory_writer(): traj = pt.iterload(tc5b_trajin, tc5b_top) with tempfolder(): pt.write_traj("test_1.pdb", traj[:1], overwrite=True) pt.write_traj("test_1.dcd", traj[:1], overwrite=True) with TrajectoryWriter("test_1") as trajout:
def test_0(self): traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) for xyz0 in traj.xyz: frame = pt.Frame(traj.n_atoms, xyz0, _as_ptr=True) aa_eq(frame.xyz, xyz0)
def setUp(self): self.traj = pt.iterload(fn('tz2.ortho.nc'), fn('tz2.ortho.parm7'))
def test_rdf(self): traj = pt.iterload(fn("tz2.truncoct.nc"), fn("tz2.truncoct.parm7"), frame_slice=(0, 10)) command = ''' radial Radial.agr 0.5 10.0 :5@CD :WAT@O radial cRadial.agr 0.5 10.0 :5 :WAT@O center1 radial cRadial.agr 0.5 10.0 :5 :WAT@O center2 radial cRadial.agr 0.5 20.0 :3 :WAT@O radial cRadial.agr 0.5 20.0 :3 :WAT@O noimage radial radial.dat 0.5 10.0 :5@CD :WAT@O radial radial2.dat 0.25 10.0 :5@CD :WAT@O radial radial2.dat 0.25 10.0 :5@CD :WAT@O volume ''' state = pt.load_batch(traj, command) with tempfolder(): # get data directly from cpptraj state.run() # get data from pytraj data0 = pt.rdf(traj, solute_mask=':WAT@O', bin_spacing=0.5, maximum=10.0, solvent_mask=':5@CD') data01 = pt.rdf(traj, solvent_mask=':5@CD', solute_mask=':WAT@O', bin_spacing=0.5, maximum=10.0) data1 = pt.rdf(traj, solvent_mask=':5', solute_mask=':WAT@O', bin_spacing=0.5, maximum=10.0, center_solvent=True) data2 = pt.rdf(traj, solvent_mask=':5', solute_mask=':WAT@O', bin_spacing=0.5, maximum=10.0, center_solute=True) data3 = pt.rdf(traj, solvent_mask=':3', solute_mask=':WAT@O', bin_spacing=0.5, maximum=20.0, center_solute=False) data4 = pt.rdf(traj, solvent_mask=':3', solute_mask=':WAT@O', bin_spacing=0.5, maximum=20.0, center_solute=False, image=False) data5 = pt.rdf(traj, solute_mask=':WAT@O', bin_spacing=0.25, maximum=10.0, solvent_mask=':5@CD') # solvent_mask is array solvent_indices = pt.select(':WAT@O', traj.top) data6 = pt.rdf(traj, solvent_mask=':5@CD', solute_mask=solvent_indices, bin_spacing=0.25, maximum=10.0) # volume data7 = pt.rdf(traj, solvent_mask=':5@CD', solute_mask=':WAT@O', bin_spacing=0.25, maximum=10.0, volume=True) # do assertion aa_eq(data0[1], state.data[1], decimal=7) aa_eq(data1[1], state.data[2], decimal=7) aa_eq(data2[1], state.data[3], decimal=7) aa_eq(data3[1], state.data[4], decimal=7) aa_eq(data4[1], state.data[5], decimal=7) aa_eq(data7[1], state.data[8], decimal=7) # default solvent mask :WAT@O aa_eq(data01[1], state.data[1], decimal=7) steps = np.loadtxt('radial.dat').T[0] aa_eq(data0[0], steps) steps2 = np.loadtxt('radial2.dat').T[0] aa_eq(data5[0], steps2) aa_eq(data6[0], steps2)
def test_pbc(self): traj = pt.load(fn('small_pbc.pdb')) aa_eq(traj.unitcells[0], [51.263, 51.263, 51.263, 90.00, 90.00, 90.00]) assert traj.top.has_box() == True, 'Topology must has box' assert traj.top.box.type == 'ortho', 'must be ortho box'
def test_0(self): arr = pt.tools.read_to_array(fn('floBF-resp.chg')) assert arr.shape == (84, ) aa_eq(arr, expected_result)
def setUp(self): self.traj = pt.iterload(fn('tz2.ortho.nc'), fn('tz2.ortho.parm7')) self.mask = '@C,N,CA,O'
def test_volmap_RuntimeError(self): # raise RuntimeError dry_traj = pt.iterload(fn('tz2.nc'), fn('tz2.parm7')) self.assertRaises(RuntimeError, lambda: pt.volmap(dry_traj, mask=':WAT@O', grid_spacing=(0.5, 0.5, 0.5)))
parm {} trajin {} rms first !@H= average crdset AVG run # Step two. RMS-Fit to average structure. Calculate covariance matrix. # Save the fit coordinates. rms ref AVG !@H= matrix covar name MyMatrix !@H= createcrd CRD1 run # Step three. Diagonalize matrix. runanalysis diagmatrix MyMatrix vecs 2 name MyEvecs # Step four. Project saved fit coordinates along eigenvectors 1 and 2 crdaction CRD1 projection evecs MyEvecs !@H= out project.dat beg 1 end 2 '''.format(fn('tz2.parm7'), fn('tz2.nc')) class TestCpptrajDatasetWithMathLib(unittest.TestCase): def setUp(self): self.state = pt.datafiles.load_cpptraj_state(txt) self.state.run() self.traj = pt.iterload(tz2_trajin, tz2_top) def test_call_values(self): for d in self.state.data: d.values def test_dataset_coords_ref(self): traj = pt.iterload(tz2_trajin, tz2_top) avg_frame = pt.mean_structure(traj(rmsfit=(0, '!@H=')))
def setUp(self): self.traj = pt.iterload(fn('tz2.nc'), fn('tz2.parm7')) self.traj_ortho = pt.iterload(fn('tz2.ortho.nc'), fn('tz2.ortho.parm7')) self.traj_nu = pt.iterload(fn('Test_NAstruct/adh026.3.pdb'))
import pytraj as pt from pytraj.testing import aa_eq, tempfolder from pytraj import * # local from utils import fn farray = pt.load(fn("Tc5b.x"), fn("Tc5b.top"), frame_indices=list(range(10))) def test_trajectory_writer_open_close(): farray = pt.load(fn("Tc5b.x"), fn("Tc5b.top"), frame_indices=list(range(10))) frame0 = farray[0] with tempfolder(): trajout = TrajectoryWriter() trajout.open(filename="test.x", top=farray.top, overwrite=True) trajout.write(frame0) # add more frames for i in range(5, 8): trajout.write(farray[i]) trajout.close() farray = Trajectory() farray.top = pt.load_topology(fn('Tc5b.top')) farray.load("test.x")
import pytraj as pt from utils import fn import numpy as np try: traj = pt.iterload("./data/Ala3/Ala3.crd", fn('Ala3/Ala3.top')) print(traj.n_atoms, traj.top.n_residues) print(pt.multidihedral(traj).to_dict()) t0 = traj[:1] deg_ene = [] flist = [] for deg in range(-180, 180, 5): pt.rotate_dihedral(t0, "custom:3:omega:" + str(deg)) flist.append(t0[0].copy()) en = pt.energy_decomposition(t0, igb=8, prmtop=traj.top.filename)['dihedral'][0] deg_ene.append((deg, en)) arr = np.array(deg_ene).T pt.write_traj("test.pdb", flist, top=traj.top, overwrite=True, options='model')
def test_1(self): traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) count = 0 for farray in traj.iterchunk(chunksize=4): count += 1
def test_short_save(): with tempfolder(): (pt.iterload(fn('tz2.nc'), fn('tz2.parm7')).save('mini.nc', overwrite=True)) assert pt.iterload( 'mini.nc', fn('tz2.parm7')).n_frames == 101, 'must be 101 frames'
from __future__ import print_function import unittest import pytraj as pt from utils import fn from pytraj.externals.six import zip from pytraj.trajectory.shared_methods import iterframe_master from pytraj import dihedral_analysis as da """ try not to get segmentation fault error (due to whatever freaking reason) """ traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) class Test(unittest.TestCase): def test_0(self): it = iterframe_master(traj) for idx, frame in enumerate(it): pass # Status: don't need to fix since "it" and "traj" share the same iterator fa = traj[:] for idx, frame in enumerate(fa): fa[idx] def test_1(self): pt.search_hbonds(traj) pt.search_hbonds(traj, 'series') pt.search_hbonds(traj, 'series, nointramol')
def test_load_url(): url = 'https://raw.githubusercontent.com/Amber-MD/pytraj/master/tests/data/1L2Y.pdb' local_traj = pt.load(fn('1L2Y.pdb')) github_traj = pt.io.load_url(url) aa_eq(local_traj.xyz, github_traj.xyz)
def test_hbonds_with_image(self): traj = pt.iterload(fn('tz2.ortho.nc'), fn('tz2.ortho.parm7')) hbonds_0 = pt.search_hbonds(traj(autoimage=True)) hbonds_1 = pt.search_hbonds(traj, image=True) aa_eq(hbonds_0.values, hbonds_1.values)
def test_3_indexing(self): top = pt.load_topology(fn('Tc5b.top')) atm = AtomMask("@CA") top._set_integer_mask(atm)
def test_raises(self): frame = pt.iterload(fn('tz2.nc'), fn('tz2.parm7'))[0] with pytest.raises(ValueError): pt.pca(frame, mask='@CA')
def setUp(self): self.traj = pt.iterload(fn('Test_NAstruct/x3dna/rna.pdb'))
def test(): traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) frame0 = traj[0] assert frame0.has_box() == False frame0.box assert frame0.box.type == 'no_shape'
def test_pickle_datasetlist(self): traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) dslist = pt.multidihedral(traj) pt.to_pickle(dslist, 'ds.pk') dslist2 = pt.read_pickle('ds.pk') aa_eq(dslist.values, dslist2.values)
def test_velocityautocorr(self): # usecoords = False traj = pt.iterload(fn('issue807/trunc.nc'), fn("issue807/system.prmtop"), frame_slice=(0, 3)) data0 = pt.all_actions.velocityautocorr(traj, tstep=2, norm=True, direct=True) cm = """ parm {} trajin {} 1 3 velocityautocorr mydata * tstep 2 norm direct """.format(fn('issue807/system.prmtop'), fn('issue807/trunc.nc')) state = pt.load_cpptraj_state(cm) state.run() aa_eq(data0[0], state.data[-2].values) # usecoords = True data = pt.all_actions.velocityautocorr(traj, tstep=1, norm=False, direct=True, usecoords=True) cm = """ parm {} trajin {} 1 3 velocityautocorr mydata * tstep 1 direct usecoords """.format(fn('issue807/system.prmtop'), fn('issue807/trunc.nc')) state = pt.load_cpptraj_state(cm) state.run() aa_eq(data[0], state.data[-2].values) # try on memory Trajectory, usecoords = False traj_on_mem = traj[:] data2 = pt.all_actions.velocityautocorr(traj_on_mem, tstep=2, norm=True, direct=True) aa_eq(data0[0], data2[0]) # try on memory Trajectory, usecoords = True # need to raise if no `velocity_arr` is given traj_on_mem = traj[:] # try on memory Trajectory, usecoords = True traj_on_disk = traj velocity_arr = pt.get_velocity(traj_on_disk) data3 = pt.all_actions.velocityautocorr(traj_on_mem, tstep=2, norm=True, direct=True, usecoords=True, velocity_arr=velocity_arr) data4 = pt.all_actions.velocityautocorr(traj_on_disk, tstep=2, norm=True, direct=True, usecoords=True) # raise if velocity_arr has wrong shape velocity_arr_2 = velocity_arr.flatten() with pytest.raises(ValueError): pt.all_actions.velocityautocorr(traj_on_mem, usecoords=True, velocity_arr=velocity_arr_2)
from __future__ import print_function import numpy as np import pytraj as pt from utils import fn import unittest from pytraj import io as mdio from pytraj.testing import aa_eq from pytraj.datasets.datasetlist import stack from pytraj.datasets.datasetlist import stack as stack import pytest traj = mdio.iterload(fn('Tc5b.x'), fn('Tc5b.top')) class Test(unittest.TestCase): def test_0(self): _ds1 = pt.calc_dssp(traj[:5], dtype='dataset') ds1 = _ds1.grep('LYS') _ds2 = pt.calc_dssp(traj[5:], dtype='dataset') ds2 = _ds2.grep('LYS') dstack = stack((ds1, ds2)) _d12 = pt.calc_dssp(traj, dtype='dataset') d12 = _d12.grep("LYS") dstack_dict = dstack.to_dict() d12_dict = d12.to_dict() assert sorted(dstack_dict.keys()) == sorted(d12_dict) for key in dstack_dict.keys():
def test_0(self): with Timer() as t: pt.iterload(fn('Tc5b.x'), fn('Tc5b.top'))
def test_0(self): traj = pt.iterload(fn('tz2.ortho.nc'), fn('tz2.ortho.parm7')) frame0 = traj[0] frame0.box
from pytraj.testing import cpptraj_test_dir from pytraj.testing import get_remd_fn from pytraj.testing import tempfolder from pytraj.io import _get_amberhome # local from utils import fn try: has_scipy = True except ImportError: has_scipy = False amberhome = os.getenv('AMBERHOME', '') tleap = amberhome + '/bin/tleap' traj_tz2_ortho = pt.iterload(fn("tz2.ortho.nc"), fn("tz2.ortho.parm7")) tc5b_trajin = fn('Tc5b.x') tc5b_top = fn('Tc5b.top') def test_iterload_comprehensive(): trajin, tn = fn("tz2.ortho.nc"), fn("tz2.ortho.parm7") # frame_slice t0 = pt.iterload(trajin, tn, frame_slice=(0, -1, 0)) aa_eq(traj_tz2_ortho.xyz, t0.xyz) t0 = pt.iterload(trajin, tn, frame_slice=(0, -1, 2)) aa_eq(traj_tz2_ortho.xyz[::2], t0.xyz) # stride
def test_0(self): traj = mdio.iterload(fn('Tc5b.x'), fn('Tc5b.top')) frame = traj[0].copy() frame.set_mass(traj.top) Frame(frame, traj.top("@CA"))
def main(): global count traj = pt.iterload(fn('Tc5b.x'), fn('Tc5b.top')) boring_list = [traj[0], traj[1], traj, traj(1, 6, 2), traj.iterchunk(chunksize=4)] count_frames(boring_list)