def test_special_load(file_3nob, file_3frames, file_psf): # Graphics type is special m = molecule.load("graphics", "") assert molecule.numatoms(m) == 0 molecule.delete(m) # Coordinates and data m = molecule.load(struct_type="psf", coord_type="pdb", struct_file=file_psf, coord_file=file_3frames) assert molecule.numframes(m) == 3 # Incorrect numbers of arguments with pytest.raises(ValueError): molecule.load("psf", file_psf, coord_type="pdb") with pytest.raises(ValueError): molecule.load("psf", file_psf, coord_file=file_3frames) # Nonexistent files with pytest.raises(RuntimeError): molecule.load("psf", "nonexistent_file") with pytest.raises(RuntimeError): molecule.load("psf", file_psf, "pdb", "nonexistent_file") # Wrong number of atoms in coordinate data with pytest.raises(RuntimeError): x = molecule.load("mae", file_3nob, "pdb", file_3frames) print(molecule.get_filenames(x)) molecule.delete(m)
def test_animate(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(m) == 3 molecule.set_frame(m, 0) with pytest.raises(ValueError): animate.activate(-3000, True) animate.activate(molid=m, active=False) assert not animate.is_active(m) animate.activate(m, True) assert animate.is_active(m) animate.goto(0) display.update() assert molecule.get_frame(m) == 0 animate.goto(1) display.update() assert molecule.get_frame(m) == 1 animate.forward() animate.reverse() animate.next() animate.prev() animate.pause() molecule.delete(m)
def test_mol_time(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(m) == 3 assert molecule.get_physical_time(molid=m, frame=0) == pytest.approx(0.0) with pytest.raises(ValueError): molecule.get_physical_time(m + 1) # Test all valid combinations of default keyword arguments molecule.set_frame(m, 0) molecule.set_physical_time(molid=m, frame=2, time=20.0) molecule.set_physical_time(m, 10.0, frame=1) molecule.set_physical_time(m, 3.0) assert molecule.get_physical_time(m) == pytest.approx(3.0) molecule.set_frame(m, frame=2) assert molecule.get_physical_time(molid=m) == pytest.approx(20.0) assert molecule.get_physical_time(m, frame=1) == pytest.approx(10.0) with pytest.raises(ValueError): molecule.set_physical_time(molid=m + 1, time=20.0) with pytest.raises(ValueError): molecule.set_physical_time(m, time=30.0, frame=3000) molecule.delete(m)
def test_mol_time(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(m) == 3 assert molecule.get_physical_time(molid=m, frame=0) == pytest.approx(0.0) with pytest.raises(ValueError): molecule.get_physical_time(m+1) # Test all valid combinations of default keyword arguments molecule.set_frame(m, 0) molecule.set_physical_time(molid=m, frame=2, time=20.0) molecule.set_physical_time(m, 10.0, frame=1) molecule.set_physical_time(m, 3.0) assert molecule.get_physical_time(m) == pytest.approx(3.0) molecule.set_frame(m, frame=2) assert molecule.get_physical_time(molid=m) == pytest.approx(20.0) assert molecule.get_physical_time(m, frame=1) == pytest.approx(10.0) with pytest.raises(ValueError): molecule.set_physical_time(molid=m+1, time=20.0) with pytest.raises(ValueError): molecule.set_physical_time(m, time=30.0, frame=3000) molecule.delete(m)
def test_write(tmpdir, file_3frames): m = molecule.load("pdb", struct_file=file_3frames) tmpdir = str(tmpdir) # Deprecated arguments with pytest.warns(DeprecationWarning): molecule.write(m, "psf", beg=0, end=1, filename=os.path.join(tmpdir, "deprecated.psf")) # Write with stride molecule.write(molid=m, filetype="pdb", first=0, stride=2, filename=os.path.join(tmpdir, "2frames.pdb")) m2 = molecule.load("pdb", os.path.join(tmpdir, "2frames.pdb")) assert molecule.numframes(m2) == 2 # Write a selection sel = atomsel("resname ALA", molid=m) molecule.write(m, "mae", selection=sel, first=0, last=0, filename=os.path.join(tmpdir, "ala.mae")) m3 = molecule.load("mae", os.path.join(tmpdir, "ala.mae")) assert molecule.numframes(m3) == 1 assert set(atomsel(molid=m3).resname) == set(["ALA"]) # Write an invalid selection on a different molid sel2 = atomsel("resname ALA", molid=m3) with pytest.raises(ValueError): molecule.write(m, "mae", os.path.join(tmpdir, "badsel.mae"), selection=sel2) # Write a nonexistent atom selection with pytest.raises(TypeError): molecule.write(m, "mae", os.path.join(tmpdir, "badsel.mae"), selection=None) # Write zero frames with pytest.raises(ValueError): molecule.write(first=20, last=21, molid=m, filetype="psf", filename=os.path.join(tmpdir, "zeroframes.psf")) # Write to an invalid file name (in this case, a directory) with pytest.raises(ValueError): molecule.write(m, "pdb", filename=os.path.join(tmpdir, ".")) molecule.delete(m) molecule.delete(m2) molecule.delete(m3)
def generate_rotation_movie(molecule, filename, save_dir='.', frame=0, angle=360, division=1.0, renderer='Tachyon', render_ext='dat'): """Function for generating movies where a static molecule frame is rotated through an angle. Individual files for each subrotation are generated, which can then be processed and combined into a movie file. Parameters ---------- molecule : VMDMolecule Molecule for which the movie is made. filename : str The basename of the individual files for each subrotation save_dir : str (default='.') The directory in which generated files will be saved. frame : int (default=0) The trajectory frame that is rotated about. angle : float (default=360.0) The angle through which the molecule is rotated through. division : float (default=1.0) The angular (degree) for each subrotation. renderer : str (default='Tachyon') Program for rendering individual images. Must be a valid rendering program bundled with VMD. For available rendering programs, see: https://www.ks.uiuc.edu/Research/vmd/vmd-1.7.1/ug/node89.html render_ext : str (default='dat') filename extension for indivudally rendered files. """ check = dir_check(save_dir) if not check: return None print("generating rotation movie...") if angle % division != 0: raise RuntimeError("Angle must be evenly divisble by dvision") else: if frame == -1: frame = mol.numframes(molecule.molid) - 1 mol.set_frame(molecule.molid, frame) display.update() sub_rotations = int(angle / division) current_angle = 0.0 for i in range(0, sub_rotations): render.render( renderer, save_dir + '/' + filename + '{:0>9}.{}'.format(int(i), render_ext)) trans.rotate_scene('y', division) display.update() current_angle += division
def test_read(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(molid=m) == 3 with pytest.warns(DeprecationWarning): molecule.read(m, "pdb", filename=file_3frames, beg=1, end=1, skip=1, waitfor=-1) assert molecule.numframes(m) == 4 molecule.read(molid=m, filetype="pdb", filename=file_3frames, waitfor=1) molecule.cancel(m) assert molecule.numframes(m) < 6 with pytest.raises(ValueError): molecule.cancel(molid=m+1) molecule.delete(m)
def test_read(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(molid=m) == 3 with pytest.warns(DeprecationWarning): molecule.read(m, "pdb", filename=file_3frames, beg=1, end=1, skip=1, waitfor=-1) assert molecule.numframes(m) == 4 molecule.read(molid=m, filetype="pdb", filename=file_3frames, waitfor=1) molecule.cancel(m) assert molecule.numframes(m) < 6 with pytest.raises(ValueError): molecule.cancel(molid=m + 1) molecule.delete(m)
def self_align(self): """Method for align trajectory frames to the initial frame to prevent the molecule from drifting out of focus during the simulation visualiation. This method can only be called after trajectory frames have been loaded into the VMDMolecule. """ print("Self aligning molecule...") mol.set_frame(self.molid, 0) base_selection = atomsel('all', frame=0) current_selection = atomsel('all') for i in range(mol.numframes(self.molid)): mol.set_frame(self.molid, i) current_selection.update() trans_matrix = current_selection.fit(base_selection) current_selection.move(trans_matrix) display.update()
def the_static_method(vector_pdb,output,outputDCD): files=' '.join(map(str,vector_pdb)) #print (files) i=0 with open('sort_ligand_order.txt', 'w') as filehandle: for listitem in vector_pdb: #print (listitem) nameFile=listitem.split("/") filehandle.write('%s' % str(i)+"\t"+nameFile[-1]+"\n") i+=1 subprocess.call("cat "+files+"> "+ output,shell=True) molID=molecule.load2('pdb',output) print (molecule.numframes(molID)) sel1 = atomsel(selection="segname TOX") molecule.write(molID,'dcd',outputDCD,0,-1,1,sel1) molecule.cancel return output
def load_data(self, load_data, align=True): """Method for loading trajectory data Parameters ---------- load_data : dict dictionary of VMD read routine options. See VMDMolecule.__init__() docs. """ if not isinstance(load_data, dict): raise ValueError("load_data must be a dictionary with " "key-value pairs corresponding to VMD " "read command options.") else: mol.read(self.molid, **load_data) if align: self.self_align() print("{} frames loaded.".format(mol.numframes(self.molid))) evaltcl("display resetview")
def test_frames(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(m) == 3 with pytest.raises(ValueError): molecule.numframes(molid=m + 1) assert molecule.get_frame(m) == 2 with pytest.raises(ValueError): molecule.get_frame(molid=m + 1) molecule.set_frame(m, frame=2) assert molecule.get_frame(molid=m) == 2 with pytest.raises(ValueError): molecule.set_frame(molid=m + 1, frame=0) with pytest.raises(ValueError): molecule.set_frame(frame=20000, molid=m) molecule.dupframe(molid=m, frame=0) assert molecule.numframes(molid=m) == 4 molecule.dupframe(m, 0) assert molecule.numframes(molid=m) == 5 with pytest.raises(ValueError): molecule.dupframe(m + 1) with pytest.raises(ValueError): molecule.dupframe(m, frame=4000) with pytest.warns(DeprecationWarning): molecule.delframe(m, beg=4, end=-1) assert molecule.numframes(m) == 4 molecule.delframe(m, first=0, stride=2) assert molecule.numframes(m) == 2 with pytest.raises(ValueError): molecule.delframe(m, first=2000) molecule.delete(m)
def test_frames(file_3frames): m = molecule.load("pdb", file_3frames) assert molecule.numframes(m) == 3 with pytest.raises(ValueError): molecule.numframes(molid=m+1) assert molecule.get_frame(m) == 2 with pytest.raises(ValueError): molecule.get_frame(molid=m+1) molecule.set_frame(m, frame=2) assert molecule.get_frame(molid=m) == 2 with pytest.raises(ValueError): molecule.set_frame(molid=m+1, frame=0) with pytest.raises(ValueError): molecule.set_frame(frame=20000, molid=m) molecule.dupframe(molid=m, frame=0) assert molecule.numframes(molid=m) == 4 molecule.dupframe(m, 0) assert molecule.numframes(molid=m) == 5 with pytest.raises(ValueError): molecule.dupframe(m+1) with pytest.raises(ValueError): molecule.dupframe(m, frame=4000) with pytest.warns(DeprecationWarning): molecule.delframe(m, beg=4, end=-1) assert molecule.numframes(m) == 4 molecule.delframe(m, first=0, stride=2) assert molecule.numframes(m) == 2 with pytest.raises(ValueError): molecule.delframe(m, first=2000) molecule.delete(m)
def num_frames(self): return molecule.numframes(self.molID)
def numFrames(self): """ Returns the number of coordinate frames in the molecule. """ return molecule.numframes(self.id)
def generate_trajectory_movie(molecule, filename, save_dir='.', start=0, stop=-1, step=1, smoothing=0, renderer='Tachyon', render_ext='dat'): """Function for generating movies of molecular trajectories Parameters ---------- molecule : VMDMolecule Molecule for which the movie is made. filename : str The basename of the individual files for each subrotation save_dir : str (default='.') The directory in which generated files will be saved. start : int (default=0) The starting frame stop : float (default=360.0) The ending frame step : float (default=1.0) The the step stride of the loaded frames smoothing : int (default=0) Size of smoothing window in frames to be applied to all representations of the VMDMolecule renderer : str (default='Tachyon') Program for rendering individual images. Must be a valid rendering program bundled with VMD. For available rendering programs, see: https://www.ks.uiuc.edu/Research/vmd/vmd-1.7.1/ug/node89.html render_ext : str (default='dat') filename extension for indivudally rendered files. """ # Perform checks check = dir_check(save_dir) if not check: return None if stop < 0: if stop != -1: raise ValueError("negative values for 'stop' can only be -1, " "in which case the stop frame is the final " "loaded frame.") num_loaded_frames = mol.numframes(molecule.molid) # explicitly switch 'stop' to the last frame index if stop == -1: stop = num_loaded_frames - 1 if smoothing > 0: reps = molrep.num(molecule.molid) for i in range(reps): molrep.set_smoothing(molecule.molid, i, smoothing) print("generating '{}' trajectory movie...".format(filename)) frames = np.arange(start, stop, step) mol.set_frame(molecule.molid, start) current_frame = start display.update() for i in frames: mol.set_frame(molecule.molid, i) display.update() render.render( renderer, save_dir + '/' + filename + '_{:0>9}.{}'.format(int(i), render_ext))