def reimage_traj(traj_file, traj_dir, save_dir, ext): if ext == ".pdb": file_lastname = traj_file.split("/")[len(traj_file.split("/")) - 1] filename = file_lastname.split(".")[0] h5_filename = file_lastname new_h5_file = "%s/%s" % (save_dir, h5_filename) if os.path.exists(new_h5_file): print "already reimaged" return traj_pytraj = mdio.load(traj_file, top=traj_file)[:] # traj_pytraj.fixatomorder() traj_pytraj.autoimage() traj_pytraj.save(new_h5_file) print "saving %s" % h5_filename else: traj_file_lastname = traj_file.split("/")[len(traj_file.split("/")) - 1] traj_filename = traj_file_lastname.split(".")[0] traj_dcd = "%s/%s.dcd" % (traj_dir, traj_filename) traj_pdb = "%s/%s.pdb" % (traj_dir, traj_filename) traj = md.load(traj_file) traj_frame = md.load_frame(traj_file, index=0) traj.save_dcd(traj_dcd) traj_frame.save_pdb(traj_pdb) traj_pytraj = mdio.load(traj_dcd, top=traj_pdb)[:] traj_pytraj.autoimage() file_lastname = traj_file.split("/")[len(traj_file.split("/")) - 1] filename = file_lastname.split(".")[0] dcd_filename = "%s_temp.dcd" % filename top_filename = "%s_temp.pdb" % filename h5_filename = file_lastname new_dcd_file = "%s/%s" % (save_dir, dcd_filename) new_top_file = "%s/%s" % (save_dir, top_filename) new_h5_file = "%s/%s" % (save_dir, h5_filename) print new_dcd_file print new_top_file traj_pytraj.save(new_dcd_file) traj_pytraj.save(new_top_file) new_traj = md.load(new_dcd_file, top=traj_pdb) new_traj.save(new_h5_file) os.remove(traj_dcd) os.remove(traj_pdb) os.remove(new_dcd_file) os.remove(new_top_file) return
def get_frames_same_T(): # create a list of all remd trajs flist = glob("../tests/data/Test_RemdTraj/rem.nc.*") # make a list of TrajReadOnly instances trajlist = [] for fh in flist: topfile = "../tests/data/Test_RemdTraj/ala2.99sb.mbondi2.parm7" # load trajectory and append to trajlist trajlist.append(mdio.load(fh, topfile)) # make Trajectory instance that holds 492.2 T frames # we need to reserve n_frames to hold the data f4922 = Trajectory(n_frames=trajlist[0].n_frames) assert f4922.n_frames == trajlist[0].n_frames f4922.top = trajlist[0].top.copy() # extract frames having T = 492.2 # use iteration for nested loops for traj in trajlist: for idx, frame in enumerate(traj): if frame.temperature == 492.2: # we don't use `append` method since we want to make sure # frames are in the order of simulation time f4922[idx] = frame # make sure f4922 only hold frames having T = 492.2 K arr0 = array( 'd', [492.2, 492.2, 492.2, 492.2, 492.2, 492.2, 492.2, 492.2, 492.2, 492.2]) assert f4922.temperatures == arr0 # make sure we reproduce cpptraj output cpptraj = mdio.load("../tests/data/Test_RemdTraj/temp0.crd.492.20", topfile) print(f4922[5].coords[:10]) print(cpptraj[5].coords[:10]) for idx, framepy in enumerate(f4922): assert_almost_equal(framepy.coords, cpptraj[idx].coords) print("rmsd between pytraj Frame and cpptraj Frame = %s " % framepy.rmsd(cpptraj[idx])) # FIXME: `rmsd` do the fitting in the fly # coords of frame would be changed print("YES, we can reproduce cpptraj output")
def randions(): # get `traj` instance (Trajectory) traj = load(filename="../tests/Test_RandomizeIons/adh206.tip3p.rst7.gz", top="../tests/Test_RandomizeIons/adh206.ff10.tip3p.parm7.gz") # get 1st frame from `traj` frame0 = traj[0] # randomize ions for frame0 randomize_ions( traj=frame0, top=traj.top, command="randomizeions @Na+ around :1-16 by 5.0 overlap 3.0", ) # make sure to reproduce cpptraj output savedframe = load( filename="../tests/Test_RandomizeIons/random.crd.save", top="../tests/Test_RandomizeIons/adh206.ff10.tip3p.parm7.gz")[0] assert frame0.rmsd(savedframe) < 1E-3
def test_0(self): from pytraj import io fname = "./data/tz2.pdb" traj = io.load(fname, fname) ptpdb = pt.read_pdb_from_frame(traj.top, traj[0]) spdb = smd.read_pdb_saxs(fname)[0] print (ptpdb) print (spdb[0][0])
def test_0(self): print (smd.calc_saxs_md.__doc__) traj0 = io.load("./data/solu.pdb", "./data/solu.pdb") traj1 = io.load("./data/solv.pdb", "./data/solv.pdb") @Timer() def test(): v = smd.calc_saxs_md(solute_traj=traj0, solvent_traj=traj1, explicit_hydrogen=True, dtype='ndarray') test() # make sure to reproduce saxs's data v = smd.calc_saxs_md(solute_traj=traj0, solvent_traj=traj1, explicit_hydrogen=True, dtype='ndarray') import numpy as np from pytraj.testing import aa_eq saved_data = np.loadtxt("./data/saxs.out", skiprows=9).transpose() print (v) aa_eq(v, saved_data[1])
def test_1(self): # TODO: get absolute path so we can use `tempfolder` # if not: wrong dir if using TrajectoryIterator traj = mdio.iterload("../tests/data/Tc5b.x", "../tests/data/Tc5b.top")[:] trajout = TrajectoryWriter() # multiple pdb in multiple files, using `save` method in traj with tempfolder(): basename = "test_pdb_files.pdb" traj.save(basename, overwrite=True, options="multi") for i in range(10): fname = basename + "." + str(i + 1) # cpptraj use `1` frame = mdio.iterload(fname, traj.top)[0] aa_eq(frame.xyz, traj[i].xyz) # multiple pdb in multiple files, using `mdio.write_traj` with tempfolder(): basename = "test_pdb_files_mdio_write_traj.pdb" mdio.write_traj(basename, traj, overwrite=True, options="multi") for i in range(10): fname = basename + "." + str(i + 1) # cpptraj use `1` frame = mdio.iterload(fname, traj.top)[0] aa_eq(frame.xyz, traj[i].xyz) # multiple pdb in SINGLE file with tempfolder(): basename = "test_pdb_files.pdb" traj.save(basename, overwrite=True) traj2 = mdio.load(basename, traj.top) aa_eq(traj.xyz, traj2.xyz) # multiple pdb in SINGLE file with `optionsl` keyword # write to output so we can manually check basename = "./output/test_pdb_files_model.pdb" traj.save(basename, overwrite=True, options='model') traj3 = mdio.load(basename, traj.top) aa_eq(traj.xyz, traj3.xyz)
def test_1(self): # TODO: get absolute path so we can use `tempfolder` # if not: wrong dir if using TrajectoryIterator traj = mdio.iterload("./data/Tc5b.x", "./data/Tc5b.top")[:] trajout = TrajectoryWriter() # multiple pdb in multiple files, using `save` method in traj with tempfolder(): basename = "test_pdb_files.pdb" traj.save(basename, overwrite=True, options="multi") for i in range(10): fname = basename + "." + str(i + 1) # cpptraj use `1` frame = mdio.iterload(fname, traj.top)[0] aa_eq(frame.xyz, traj[i].xyz) # multiple pdb in multiple files, using `mdio.write_traj` with tempfolder(): basename = "test_pdb_files_mdio_write_traj.pdb" mdio.write_traj(basename, traj, overwrite=True, options="multi") for i in range(10): fname = basename + "." + str(i + 1) # cpptraj use `1` frame = pt.iterload(fname, traj.top)[0] aa_eq(frame.xyz, traj[i].xyz) # multiple pdb in SINGLE file with tempfolder(): basename = "test_pdb_files.pdb" traj.save(basename, overwrite=True) traj2 = mdio.load(basename, traj.top) aa_eq(traj.xyz, traj2.xyz) # multiple pdb in SINGLE file with `model` keyword # write to output so we can manually check basename = "./output/test_pdb_files_model.pdb" traj.save(basename, overwrite=True, options='model') traj3 = mdio.load(basename, traj.top) aa_eq(traj.xyz, traj3.xyz)
def reimage(traj_dir): new_dir = "%s_reimaged" %(traj_dir) if not os.path.exists(new_dir): os.makedirs(new_dir) files = get_trajectory_files(traj_dir, ext = ".pdb") for i in range(0, len(files)): print i pdb = files[i] name = pdb.split("/")[len(pdb.split("/"))-1] conformation = mdio.load(pdb, pdb) f0 = conformation[0] adict['autoimage']("", f0, conformation.top) new_file = "%s/%s" %(new_dir, name) mdio.save(new_file, traj = f0, top = conformation.top) return new_dir
def reimage(traj_dir): new_dir = "%s_reimaged" % (traj_dir) if not os.path.exists(new_dir): os.makedirs(new_dir) files = get_trajectory_files(traj_dir, ext=".pdb") for i in range(0, len(files)): print(i) pdb = files[i] name = pdb.split("/")[len(pdb.split("/")) - 1] conformation = mdio.load(pdb, pdb) f0 = conformation[0] adict['autoimage']("", f0, conformation.top) new_file = "%s/%s" % (new_dir, name) mdio.save(new_file, traj=f0, top=conformation.top) return new_dir
def test_1(self): from pytraj import io import numpy as np print ("use pytraj") # load traj traj = io.load("./test.pdb", "test.pdb") n_atoms = traj.top.n_atoms # creat dummy B-factors b_array = np.zeros(n_atoms) # create dummy r_array r_array = np.asarray([atom.gb_radius for atom in traj.top]) #print (r_array[0]) # read pdb for 1st frame pdb = read_pdb_from_frame(traj.top, traj[0], b_array, r_array) #print (len(pdb)) #print (pdb[0]) #print (pdb) # read dx, find all guv.* files in current folder v_dx = sx.read_dx("./") # v_dx is a list of dictionary # in C++ saxs, this is a vector of struct print (len(v_dx)) d1 = v_dx[0] print (d1.keys()) # get info x, y, z = d1['ngrid'] print (x * y * z) print (d1['conc']) print (d1['type']) print (d1['delta']) print (d1['origin']) arr0 = np.asarray(d1['value']) assert arr0.shape[0] == (x * y * z) # calculate vcoor_max, vcoor_min: dummy example v_max, v_min = sx.calc_extreme_pdb(pdb)
def test_0(self): from pytraj import io import numpy as np print ("use pytraj") # load traj traj = io.load("./test.pdb", "test.pdb") v_dx = sx.read_dx("./") # v_dx is a list of dictionary # in C++ saxs, this is a vector of struct d1 = v_dx[0] x, y, z = d1['ngrid'] print (x * y * z) print (d1['conc']) print (d1['type']) print (d1['delta']) print (d1['origin']) arr0 = np.asarray(d1['value']) assert arr0.shape[0] == (x * y * z) print ("shape of grids = %s %s %s" % (x, y, z))
"""calculat RMSD for 8 replica trajs using 1 cores. Reference frame is the 1st frame of remd.x.000 System: 17443 atoms, 1000 frames, netcdf, 8 replicas (000 to 007), 200Mb/replica python test_serial.py """ import numpy as np from pytraj import io import pytraj.common_actions as pyca root_dir = "../../tests/data/nogit/remd/" top_name = root_dir + "myparm.top" # 8 replicas, 1000 frames size = 8 sarr = np.empty((size, 1000)) REF = None for i in range(size): fname = root_dir + "/remd.x.00" + str(i) # 000, 001, 002, 003 ... straj = io.load(fname, root_dir + "/myparm.parm7") if i == 0: REF = straj[0] sarr[i] = straj.calc_rmsd(REF, "@CA") np.savetxt("./serial_rmsd.txt", sarr.flatten())
from pytraj import io as mdio traj = mdio.load("../tests/data/Tc5b.x", "../tests/data/Tc5b.top")[:] # get x coord of 0-th atom of 0-th frame print(traj[0, 0, 0]) # get new Trajectory with only CA atoms print(traj['@CA']) # get new Trajectory with only CA with several frames print(traj[2:7, '@CA']) print(traj[[1, 5, 7, 9], '@CA'])
import numpy as np from pytraj import io from pytraj import Frame import pytraj.common_actions as pyca from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.rank size = comm.size root_dir = "../../tests/data/nogit/remd/" fname = root_dir + "/remd.x.00" + str(rank) # 000, 001, 002, 003 ... top_name = root_dir + "myparm.top" traj = io.load(fname, top_name) n_atoms = traj.top.n_atoms n_frames = traj.n_frames if rank == 0: ref = traj[0] ref_xyz = np.asarray(ref.xyz, dtype=np.float64) else: ref = None ref_xyz = np.empty((n_atoms, 3), dtype=np.float64) # broadcast ref_xyz to other cores from master comm.Bcast([ref_xyz, MPI.DOUBLE]) if rank != 0: # need to reconstruct ref
# (require: mpi4py, numpy) # python serial_cal_molsurf_0.py from pytraj import io import pytraj.common_actions as pyca root_dir = "../../tests/data/nogit/remd/" traj_name = root_dir + "/remd.x.000" parm_name = root_dir + "myparm.top" traj = io.load(traj_name, parm_name) arr = pyca.calc_molsurf(traj, "@CA") print(arr[:10])