Exemplo n.º 1
0
    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)})
Exemplo n.º 2
0
    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']
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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')
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    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))
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
    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)])
Exemplo n.º 13
0
 def test_molecule_delete(self):
     # Test molecule deletion
     mol = Molecule(self.molid)
     mol.delete()
     self.assertFalse(VMD.molecule.exists(self.molid))
Exemplo n.º 14
0
 def setUp(self):
     molid = VMD.molecule.load('psf', data('water.psf'), 'pdb', data('water.pdb'))
     self.mol = Molecule(molid)
Exemplo n.º 15
0
    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]