def _test_hashability(self, obj_type): # Test objects are correctly understood by sets molid_1 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_1 = Molecule(molid_1) molid_2 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_2 = Molecule(molid_2) VMD.molecule.set_top(molid_1) # Same objects self.assertEqual({obj_type(0), obj_type(0)}, {obj_type(0)}) self.assertEqual( {obj_type(0, frame=0), obj_type(0, frame=0)}, {obj_type(0, frame=0)}) self.assertEqual( {obj_type(0, mol_1), obj_type(0, mol_1)}, {obj_type(0, mol_1)}) # Top molecule explicitely and implicitly self.assertEqual({obj_type(0), obj_type(0, mol_1)}, {obj_type(0)}) # Frame differs self.assertEqual({obj_type(0), obj_type(0, frame=0)}, {obj_type(0), obj_type(0, frame=0)}) # Molecule differs self.assertEqual( {obj_type(0, mol_1), obj_type(0, mol_2)}, {obj_type(0, mol_1), obj_type(0, mol_2)})
def _test_frame_property(self, obj_type, obj_id, getter): # Test frame property works correctly molid = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) VMD.molecule.read(molid, 'dcd', data('water.1.dcd'), waitfor=-1) mol = Molecule(molid) # To check frame works correctly, check the 'x' coordinate of the object's first atom. # Create residue linked to the molecule frame obj = obj_type(obj_id) self.assertEqual(obj.frame, NOW) self.assertAlmostEqual(getter(obj), -1.3421925) # Change the molecule frame, the object will follow mol.frame = 0 self.assertEqual(obj.frame, NOW) self.assertAlmostEqual(getter(obj), -1.493) mol.frame = 4 self.assertEqual(obj.frame, NOW) self.assertAlmostEqual(getter(obj), -1.4773947) # Define the object's frame obj.frame = 9 self.assertEqual(obj.frame, 9) self.assertAlmostEqual(getter(obj), -1.4120502) # Change the molecule frame, the object keeps its frame mol.frame = 11 self.assertEqual(obj.frame, 9) self.assertAlmostEqual(getter(obj), -1.4120502) # Set the object's frame back to the molecule's frame obj.frame = NOW self.assertEqual(obj.frame, NOW) self.assertAlmostEqual(getter(obj), -1.3674825)
def test_molecules(self): mol1 = Molecule(VMD.molecule.new('Mine')) mol2 = Molecule(VMD.molecule.new('Other')) VMD.molecule.set_top(mol1.molid) man = MoleculeManager() self.assertEqual(len(man), 2) self.assertEqual(man[mol1.molid], mol1) self.assertEqual(man['Mine'], mol1) self.assertEqual(man[mol2.molid], mol2) self.assertEqual(man['Other'], mol2) self.assertEqual(list(man), [mol1, mol2]) self.assertIn(mol1, man) self.assertIn(mol2, man) # Check top property self.assertEqual(man.top, mol1) man.top = mol2 self.assertEqual(VMD.molecule.get_top(), mol2.molid) self.assertEqual(man.top, mol2) # Try deletion - using name del man['Mine'] self.assertFalse(VMD.molecule.exists(mol1.molid)) # Check the other attributes self.assertEqual(len(man), 1) with self.assertRaises(ValueError): man[mol1.molid] with self.assertRaises(ValueError): man['Mine'] self.assertEqual(man[mol2.molid], mol2) self.assertEqual(man['Other'], mol2) self.assertEqual(list(man), [mol2]) self.assertNotIn(mol1, man) self.assertIn(mol2, man) # Try deletion - using molid del man[mol2.molid] self.assertFalse(VMD.molecule.exists(mol2.molid)) # Check the other attributes self.assertEqual(len(man), 0) with self.assertRaises(ValueError): man[mol2.molid] with self.assertRaises(ValueError): man['Other'] self.assertEqual(list(man), []) self.assertNotIn(mol1, man) self.assertNotIn(mol2, man) # Check second deletions raises ValueError with self.assertRaises(ValueError): del man[mol1.molid] with self.assertRaises(ValueError): del man[mol2.molid] with self.assertRaises(ValueError): del man['Mine'] with self.assertRaises(ValueError): del man['Other']
def test_molecule_comparison(self): # Test molecule comparison mol1 = Molecule(self.molid) mol2 = Molecule(self.molid) other = Molecule.create() self.assertEqual(mol1, mol1) self.assertEqual(mol1, mol2) self.assertNotEqual(mol1, other) self.assertNotEqual(mol2, other)
def test_visible_property(self): # Test `visible` property mol = Molecule(self.molid) self.assertTrue(mol.visible) mol.visible = False self.assertFalse(mol.visible) self.assertFalse(VMD.molecule.get_visible(self.molid)) mol.visible = True self.assertTrue(mol.visible) self.assertTrue(VMD.molecule.get_visible(self.molid))
def setUp(self): self.mol = Molecule.create() self.mol.load(data('water.psf')) self.mol.load(data('water.pdb')) self.mol.load(data('water.1.dcd')) self.mol.frame = 0 self.other = Molecule.create() self.other.load(data('water.psf')) self.other.load(data('water.pdb')) MOLECULES.top = self.mol self.addCleanup(clear_labels)
def test_molecule_create(self): # Test molecule creation old_molids = VMD.molecule.listall() created = Molecule.create() self.assertNotIn(created.molid, old_molids) self.assertTrue(VMD.molecule.exists(created.molid)) self.assertEqual(VMD.molecule.get_filenames(created.molid), []) self.assertEqual(VMD.molecule.get_filetypes(created.molid), []) # Check create with name other = Molecule.create('The One') self.assertEqual(other.name, 'The One') self.assertTrue(VMD.molecule.exists(other.molid)) self.assertEqual(VMD.molecule.get_filenames(other.molid), []) self.assertEqual(VMD.molecule.get_filetypes(other.molid), [])
def _test_container(self, obj_type, obj_id, length, atom_ids, out_ids): molid_1 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) molid_2 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_2 = Molecule(molid_2) VMD.molecule.set_top(molid_1) obj = obj_type(obj_id) # Check __len__ self.assertEqual(len(obj), length) # Check __iter__ self.assertEqual(list(obj), [Atom(i) for i in atom_ids]) # Check __contains__ for atom_id in atom_ids: self.assertIn(Atom(atom_id), obj) for atom_id in atom_ids: # Frame differs self.assertNotIn(Atom(atom_id, frame=0), obj) for atom_id in atom_ids: # Molecule differs self.assertNotIn(Atom(atom_id, mol_2), obj) for atom_id in out_ids: self.assertNotIn(Atom(atom_id), obj)
def test_remote_actions(self): # Test manager can cope with molecule changes which happen without its knowledge man = MoleculeManager() # Create a molecule mol1 = Molecule(VMD.molecule.new('Unique')) # Manager finds it exists and adds it into name cache self.assertEqual(man['Unique'], mol1) # Delete the molecule and create other with the same name VMD.molecule.delete(mol1.molid) mol2 = Molecule(VMD.molecule.new('Unique')) # Manager correctly returns the new molecule self.assertEqual(man['Unique'], mol2)
def test_selection_updates(self): # Test selection updates if frame is changed molid = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) VMD.molecule.read(molid, 'dcd', data('water.1.dcd'), waitfor=-1) mol = Molecule(molid) # Create selection linked to the molecule frame sel = Selection('x < -1.4') self.assertEqual(list(sel), [Atom(9), Atom(18), Atom(19), Atom(20)]) # Change the molecule frame, the selection should follow mol.frame = 0 self.assertEqual(list(sel), [ Atom(0), Atom(1), Atom(9), Atom(10), Atom(18), Atom(19), Atom(20) ]) mol.frame = 4 self.assertEqual( list(sel), [Atom(0), Atom(1), Atom(9), Atom(18), Atom(19), Atom(20)]) # Define the selection's frame result = [ Atom(0, frame=9), Atom(1, frame=9), Atom(9, frame=9), Atom(18, frame=9), Atom(19, frame=9), Atom(20, frame=9) ] sel.frame = 9 self.assertEqual(list(sel), result) # Change the molecule frame, the selection keeps its frame mol.frame = 11 self.assertEqual(list(sel), result) # Set the selection's frame back to the molecule's frame sel.frame = NOW self.assertEqual(list(sel), [Atom(9), Atom(18), Atom(19), Atom(20)])
def test_constructors(self): # Test various ways of Atom instance creation molid1 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) VMD.molecule.read(molid1, 'dcd', data('water.1.dcd'), waitfor=-1) mol1 = Molecule(molid1) molid2 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol2 = Molecule(molid2) VMD.molecule.set_top(molid2) # Top molecule and NOW atom = Atom(0) self.assertEqual(atom.molecule, mol2) self.assertEqual(atom.frame, NOW) self.assertAlmostEqual(atom.x, -1.493) # Molecule and frame atom = Atom(0, molecule=mol1, frame=5) self.assertEqual(atom.molecule, mol1) self.assertEqual(atom.frame, 5) self.assertAlmostEqual(atom.x, -1.4746015) # Get atom using selection string atom = Atom.pick('resid 2 and name OH2') self.assertEqual(atom.molecule, mol2) self.assertEqual(atom.frame, NOW) self.assertAlmostEqual(atom.x, 0.337) # Get atom using selection string, molecule and frame atom = Atom.pick('resid 2 and name OH2', molecule=mol1, frame=8) self.assertEqual(atom.molecule, mol1) self.assertEqual(atom.frame, 8) self.assertAlmostEqual(atom.x, 0.3521036) # Atom which does not exist with self.assertRaises(ValueError): Atom(8947) # Selection which returns none or too many atoms with self.assertRaises(ValueError): Atom.pick('all') with self.assertRaises(ValueError): Atom.pick('none')
def _test_molecule_property(self, obj_type, obj_id): # Test molecule property works correctly molid_1 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_1 = Molecule(molid_1) molid_2 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_2 = Molecule(molid_2) VMD.molecule.set_top(molid_1) # If molecule is not defined, top molecule is used obj = obj_type(obj_id) self.assertEqual(obj.molecule, mol_1) # If molecule is defined, it is used obj = obj_type(obj_id, mol_2) self.assertEqual(obj.molecule, mol_2) # Molecule can't be changed with self.assertRaises(AttributeError): obj.molecule = mol_2
def test_molecule_properties(self): # Test basic properties of Molecule mol = Molecule(self.molid) # Check frame property mol.frame = 3 self.assertEqual(VMD.molecule.get_frame(self.molid), 3) self.assertEqual(mol.frame, 3) mol.frame = 8 self.assertEqual(VMD.molecule.get_frame(self.molid), 8) self.assertEqual(mol.frame, 8) # Check name property mol.name = 'My precious' self.assertEqual(mol.name, 'My precious') self.assertEqual(VMD.molecule.name(self.molid), 'My precious') mol.name = 'The Ring' self.assertEqual(mol.name, 'The Ring') self.assertEqual(VMD.molecule.name(self.molid), 'The Ring') # Check molecule property self.assertIsInstance(mol.molecule, _Molecule) # Check error if molecule does not exists self.assertRaises(ValueError, Molecule, 66000)
def _test_equality(self, obj_type, obj_id1, obj_id2): molid_1 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_1 = Molecule(molid_1) molid_2 = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) mol_2 = Molecule(molid_2) VMD.molecule.set_top(molid_1) obj1 = obj_type(obj_id1) obj2 = obj_type(obj_id1) other = obj_type(obj_id2) frame = obj_type(obj_id1, frame=0) same_mol = obj_type(obj_id1, mol_1) other_mol = obj_type(obj_id1, mol_2) self.assertEqual(obj1, obj1) self.assertEqual(obj1, obj2) self.assertEqual(obj1, same_mol) self.assertNotEqual(obj1, other) self.assertNotEqual(obj2, other) self.assertNotEqual(obj1, frame) self.assertNotEqual(obj1, other_mol)
def test_molecule_load(self): # Test file loading mol = Molecule.create() mol.load(data('water.psf')) self.assertEqual(VMD.molecule.get_filenames(mol.molid), [data('water.psf')]) self.assertEqual(VMD.molecule.get_filetypes(mol.molid), ['psf']) mol.load(data('water.pdb'), FORMAT_PDB) self.assertEqual(VMD.molecule.get_filenames(mol.molid), [data('water.psf'), data('water.pdb')]) self.assertEqual(VMD.molecule.get_filetypes(mol.molid), ['psf', 'pdb']) mol.load(data('water.1.dcd')) self.assertEqual(VMD.molecule.get_filenames(mol.molid), [data('water.psf'), data('water.pdb'), data('water.1.dcd')]) self.assertEqual(VMD.molecule.get_filetypes(mol.molid), ['psf', 'pdb', 'dcd']) self.assertRaises(ValueError, mol.load, 'no_extension')
def test_rmsd_collector(self): # Test RMSD collector ref = Molecule.create() ref.load(data('water.psf')) ref.load(data('water.pdb')) dset = DataSet() dset.add_collector(RMSDCollector('all', Selection('all', ref))) dset.add_collector(RMSDCollector('all and name OH2', Selection('all and name OH2', ref))) dset.add_collector(RMSDCollector('all and noh', Selection('all and noh', ref), name='noh')) analyzer = Analyzer(self.mol, [data('water.1.dcd')]) analyzer.add_dataset(dset) analyzer.analyze() # Write data to check result buf = StringIO() dset.write(buf) # Check the result self.assertEqual(buf.getvalue(), open(data('rmsd.dat')).read())
def test_rmsd_collector(self): # Test RMSD collector ref = Molecule.create() ref.load(data('water.psf')) ref.load(data('water.pdb')) dset = DataSet() dset.add_collector(RMSDCollector('all', Selection('all', ref))) dset.add_collector( RMSDCollector('all and name OH2', Selection('all and name OH2', ref))) dset.add_collector( RMSDCollector('all and noh', Selection('all and noh', ref), name='noh')) analyzer = Analyzer(self.mol, [data('water.1.dcd')]) analyzer.add_dataset(dset) analyzer.analyze() # Write data to check result buf = StringIO() dset.write(buf) # Check the result self.assertEqual(buf.getvalue(), open(data('rmsd.dat')).read())
def setUp(self): # Prepare molecule self.mol = Molecule.create() self.mol.load(data('water.psf'))
"""Generate a constraints for TRP channel simulations.""" from pyvmd.atoms import Selection from pyvmd.molecules import Molecule, FORMAT_PDB PSF = 'initial.psf' PDB = '00_minimize/00_minimize.coor' # Lipid tail simulation constraint LIPID_TAILS_PDB = 'tails_only.pdb' # Protein fixed simulation constraint PROTEIN_PDB = 'protein.pdb' # Protein CA constraint BACKBONE_CA_PDB = 'backbone_ca.pdb' model = Molecule.create() model.load(PSF) model.load(PDB, FORMAT_PDB) model_sel = Selection('all', model) model_sel.atomsel.set('beta', 0) # Lipid constraints # Fix all and release only the lipid tails model_sel.atomsel.set('occupancy', 1) Selection('lipid and not name N "C1[1-5]" "H[1-5]\d" P1 "O."', model).atomsel.set('occupancy', 0) model_sel.atomsel.write(FORMAT_PDB, LIPID_TAILS_PDB) # Protein constraints model_sel.atomsel.set('occupancy', 0) Selection('protein and noh', model).atomsel.set('occupancy', 1) model_sel.atomsel.write(FORMAT_PDB, PROTEIN_PDB) # Protein CA constraints
def test_frames(self): # Test frames wrapper # Utility to get x coordinate through the trajectory to check results sel = VMD.atomsel.atomsel('index 0', molid=self.molid) def _get_x_coord(): # Utility to get x coordinate through trajectory values = [] for frame in xrange(VMD.molecule.numframes(self.molid)): sel.frame = frame values.append(sel.get('x')[0]) return values # Check number of frames and iterator mol = Molecule(self.molid) self.assertEqual(len(mol.frames), 13) self.assertEqual(list(mol.frames), range(13)) # Test frame duplication - duplicate focused frame mol.frame = 3 mol.frames.copy() # Check there is one more frame self.assertEqual(len(mol.frames), 14) coords = [-1.493, -1.4911567, -1.4851371, -1.4858487, -1.4773947, -1.4746015, -1.4673382, -1.4535547, -1.4307435, -1.4120502, -1.3853478, -1.3674825, -1.3421925, -1.4858487] self.assertAlmostEqualSeqs(_get_x_coord(), coords) # Check molecule is focused to the new frame self.assertEqual(mol.frame, 13) # Test frame duplication - duplicate defined frame mol.frames.copy(4) # Check there is one more frame self.assertEqual(len(mol.frames), 15) coords = [-1.493, -1.4911567, -1.4851371, -1.4858487, -1.4773947, -1.4746015, -1.4673382, -1.4535547, -1.4307435, -1.4120502, -1.3853478, -1.3674825, -1.3421925, -1.4858487, -1.4773947] self.assertAlmostEqualSeqs(_get_x_coord(), coords) # Molecule is focused to the new frame self.assertEqual(mol.frame, 14) # Test frame deletion - positive index del mol.frames[14] self.assertEqual(len(mol.frames), 14) coords = [-1.493, -1.4911567, -1.4851371, -1.4858487, -1.4773947, -1.4746015, -1.4673382, -1.4535547, -1.4307435, -1.4120502, -1.3853478, -1.3674825, -1.3421925, -1.4858487] self.assertAlmostEqualSeqs(_get_x_coord(), coords) # Test frame deletion - negative index del mol.frames[-2] self.assertEqual(len(mol.frames), 13) coords = [-1.493, -1.4911567, -1.4851371, -1.4858487, -1.4773947, -1.4746015, -1.4673382, -1.4535547, -1.4307435, -1.4120502, -1.3853478, -1.3674825, -1.4858487] self.assertAlmostEqualSeqs(_get_x_coord(), coords) # Check deletion of frame slice del mol.frames[2:11:3] self.assertEqual(len(mol.frames), 10) coords = [-1.493, -1.4911567, -1.4858487, -1.4773947, -1.4673382, -1.4535547, -1.4120502, -1.3853478, -1.3674825, -1.4858487] self.assertAlmostEqualSeqs(_get_x_coord(), coords) # Invalid key for slice with self.assertRaises(TypeError): del mol.frames[None]
def setUp(self): molid = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb')) self.mol = Molecule(molid)
def test_molecule_delete(self): # Test molecule deletion mol = Molecule(self.molid) mol.delete() self.assertFalse(VMD.molecule.exists(self.molid))
def setUp(self): self.mol = Molecule.create() self.mol.load(data('water.psf')) # Storage for callback data self.coords = [] self.frames = []
"""Generate a constraints for MDFF simulation.""" import VMD from pyvmd.molecules import Molecule PSF = 'initial.psf' PDB = 'initial.pdb' MDFF_TEMPLATE_PSF = '../template.psf' MDFF_TEMPLATE_PDB = '../template.pdb' MDFF_MAP = 'mdff-map.situs' MDFF_POTENTIAL = 'mdff-potential.dx' MDFF_WEIGHTS = 'mdff-weights.pdb' MDFF_CONSTRAINT_HBONDS = 'mdff-hbonds.dat' MDFF_CONSTRAINT_CISPEPTIDE = 'mdff-cispeptide.dat' MDFF_CONSTRAINT_CHIRALITY = 'mdff-chirality.dat' model = Molecule.create() model.load(PSF) model.load(PDB) template = Molecule.create() template.load(MDFF_TEMPLATE_PSF) template.load(MDFF_TEMPLATE_PDB) # Load mdff VMD.evaltcl('package require mdff') # Generate simulated MDFF map VMD.evaltcl('mdff sim [atomselect {molid} protein] -res 5 -o {outfile}'.format( molid=template.molid, outfile=MDFF_MAP)) # Convert map to potential VMD.evaltcl('mdff griddx -i {infile} -o {outfile}'.format( infile=MDFF_MAP, outfile=MDFF_POTENTIAL))
"""Build a model of a TRP channel.""" import tempfile import VMD from pyvmd import measure from pyvmd.atoms import Selection from pyvmd.molecules import Molecule, FORMAT_PDB, FORMAT_PSF TEMPLATE_PDB = '../structures/3j5p.pdb' TMP_DIR = tempfile.mkdtemp(prefix='vmd_') TOPOLOGY = '/usr/lib/vmd/plugins/noarch/tcl/readcharmmtop1.1/top_all27_prot_lipid_na.inp' # Load template template = Molecule.create() template.load(TEMPLATE_PDB) # Center the template center = measure.center(Selection('all', template)) Selection('all', template).atomsel.moveby((-center[0], -center[1], -center[2])) # Rotate the template, so model takes less space VMD.evaltcl('[atomselect %d all] move [transaxis z -35]' % template.molid) # Save individual chains for chain in 'ABCD': print "Saving monomer %s" % chain Selection('chain {} and resid 393 to 502'.format(chain), template).atomsel.write(FORMAT_PDB, '{}/{}1.pdb'.format(TMP_DIR, chain)) Selection('chain {} and resid 508 to 719'.format(chain), template).atomsel.write(FORMAT_PDB, '{}/{}2.pdb'.format(TMP_DIR, chain)) # Load psfgen and topology VMD.evaltcl("package require psfgen") VMD.evaltcl("topology %s" % TOPOLOGY)
from pyvmd.datasets import DataSet from pyvmd.molecules import Molecule logging.basicConfig(level=logging.INFO) PARAM_FILE = '../initial.psf' PREFIXES = ['simulation_A', 'simulation_B'] COLLECTOR_1 = DistanceCollector('resname A2F and name N52B', 'protein and resid 1589 and name CD', name="N52-GLU1589") COLLECTOR_2 = DistanceCollector('resname A2F and name N49B', 'protein and resid 1534 and name CD', name="N49-GLU1534") MOLECULE = Molecule.create() MOLECULE.load(PARAM_FILE) for prefix in PREFIXES: trajectory = '../%s/%s.dcd' % (prefix, prefix) dset = DataSet() dset.add_collector(COLLECTOR_1) dset.add_collector(COLLECTOR_2) analyzer = Analyzer(MOLECULE, [trajectory]) analyzer.add_dataset(dset) analyzer.analyze() dset.write('distances/%s.dat' % prefix)