def test_write_gromacs(): water = Molecule( [ Atom("O", [0.0, 0.0, 0.0], export={"grotype": "OW"}), Atom("H", [0.1, 0.0, 0.0], export={"grotype": "HW1"}), Atom("H", [-0.03333, 0.09428, 0.0], export={"grotype": "HW2"}), ], export={"groname": "SOL"}, ) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile("/tmp/dummy.gro", mode="w") df.write("system", sys) with assert_raises(Exception): df = datafile("/tmp/dummy.gro") df.write("system", sys) df = datafile("/tmp/dummy.gro") sread = df.read("system") assert all(sread.type_array == sys.type_array)
def test_bond_renderer(): v = QtViewer() v.widget.background_color = colors.black mol = Molecule([ Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10]) ]) mol.bonds = np.array([[0, 1], [0, 2]]) #mol = CirDB().get("molecule", "moronic acid") #radii_map = {"O": 0.03, "H": 0.03} radii_map = defaultdict(lambda: 0.03) br = v.add_renderer(BondRenderer, mol.bonds, mol.r_array, mol.type_array, style='impostors') ar = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array, "impostors", radii_map=radii_map) v.run()
def read(self, feature, *args, **kwargs): if feature == 'molecular orbitals': n = args[0] basis_functions = self.data.gbasis if feature == 'molecule': # Angstrom to nanometers # CJS added option to get molecule from steps of optimisation # or optimisations for PES scans # TODO error checking if kwargs.has_key('step'): return Molecule.from_arrays( r_array=self.data.atomcoords[kwargs['step']] / 10, type_array=np.array( [symbols[a] for a in self.data.atomnos])) elif kwargs.has_key('scan'): return Molecule.from_arrays( r_array=self.data.scancoords[kwargs['scan']] / 10, type_array=np.array( [symbols[a] for a in self.data.atomnos])) else: return Molecule.from_arrays( r_array=self.data.atomcoords[-1] / 10, type_array=np.array( [symbols[a] for a in self.data.atomnos])) else: return getattr(self.data, feature)
def test_random_lattice(): '''Testing random made box''' na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) wat = Molecule.from_arrays(type_array=['O', 'H', 'H']) s = random_lattice_box([na, cl, wat], [160, 160, 160], [4, 4, 4]) eq_(s.dimensions['molecule'], 160 * 3) eq_(s.dimensions['atom'], 160 * 5)
def test_crystal(): '''Building a crystal by using spacegroup module''' na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[13, 13, 13])
def test_random(): '''Testing random made box''' from chemlab.db import ChemlabDB cdb = ChemlabDB() na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) wat = cdb.get("molecule", 'gromacs.spce') s = random_lattice_box([na, cl, wat], [160, 160, 160], [4, 4, 4])
def test_sort(): na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) tsys.sort() assert np.all(tsys.type_array[:tsys.n_mol / 2] == 'Cl')
def test_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write("system", sys)
def test_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'pdb.type': 'O'}), Atom('H', [0.1, 0.0, 0.0], export={'pdb.type': 'H'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'pdb.type': 'H'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3) * 2.0) for i in range(200): water.r_array += 0.1 sys.add(water.copy()) df = datafile('/tmp/dummy.pdb', mode="w") df.write("system", sys)
def test_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.tojson() assert Molecule.from_json(jsonstr).tojson() == jsonstr na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0],[0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3,3,3]) jsonstr = tsys.tojson() assert System.from_json(jsonstr).tojson() == jsonstr
def test_write_pdb(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'pdb.type': 'O'}), Atom('H', [0.1, 0.0, 0.0], export={'pdb.type': 'H'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'pdb.type': 'H'})], export={'groname': 'SOL'}) sys = System.empty() with sys.batch() as b: for i in range(200): water.r_array += 0.1 b.append(water.copy()) df = datafile('/tmp/dummy.pdb', mode="w") df.write("system", sys)
def test_wireframe_renderer(): v = QtViewer() #v.widget.background_color = black mol = Molecule([Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10])]) mol.bonds = np.array([[0, 1],[0, 2]]) mol = CirDB().get("molecule", "moronic acid") ar = v.add_renderer(WireframeRenderer, mol.r_array, mol.type_array, mol.bonds) # Try without bonds #ar2 = v.add_renderer(WireframeRenderer, mol.r_array + 0.5, mol.type_array, np.array([])) v.run()
def test_bonds(): # TODO: deprecate this shit from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([ Atom('O', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), Atom('H', [0.0, 0.0, 0.0]), ]) # Adding bonds s = System() with s.batch() as b: b.append(bz) assert_npequal(s.bonds, bz.bonds) assert_npequal(bz.bond_orders, [1, 2, 2, 1, 1, 2]) assert_npequal(s.bond_orders, bz.bond_orders) s.add(bz) assert_npequal( s.type_array, ['C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']) eq_(s.dimensions['atom'], 12) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 7))) # Reordering s.bonds = np.array([[0, 1], [6, 8]]) s.reorder_molecules([1, 0]) assert_eqbonds(s.bonds, np.array([[6, 7], [0, 2]])) # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 1]]))
def test_write_pdb(): water = Molecule( [ Atom("O", [0.0, 0.0, 0.0], export={"pdb.type": "O"}), Atom("H", [0.1, 0.0, 0.0], export={"pdb.type": "H"}), Atom("H", [-0.03333, 0.09428, 0.0], export={"pdb.type": "H"}), ], export={"groname": "SOL"}, ) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): water.r_array += 0.1 sys.add(water.copy()) df = datafile("/tmp/dummy.pdb", mode="w") df.write("system", sys)
def test_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.to_json() m = Molecule.from_json(jsonstr) eq_(Molecule.from_json(jsonstr).to_json(), jsonstr) na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) jsonstr = tsys.to_json() npeq_(System.from_json(jsonstr).r_array, tsys.r_array)
def test_write_gromacs(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'})], export={'groname': 'SOL'}) sys = System.empty(200, 3*200, box_vectors = np.eye(3)*2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
def test_residue(): m = Molecule.from_arrays(type_array=['H', 'H', 'H', 'O', 'O'], residue_name=['VAL', 'ALA'], maps={('atom', 'residue'): [0, 0, 0, 1, 1]}) assert_npequal(m.sub(residue_index=0).type_array, ['H', 'H', 'H']) s = System([m, m]) assert_npequal(s.maps['atom', 'residue'].value, [0, 0, 0, 1, 1, 2, 2, 2, 3, 3])
def _make_water(): mol = Molecule([ Atom("O", [-4.99, 2.49, 0.0]), Atom("H", [-4.02, 2.49, 0.0]), Atom("H", [-5.32, 1.98, 1.0]) ], bonds=[[0, 1], [0, 2]], export={'hello': 1.0}) return mol
def test_init(self): mol = _make_water() eq_(mol.export, {'hello': 1.0}) assert_npequal(mol.get_attribute('type_array').value, ['O', 'H', 'H']) assert_npequal(mol.type_array, ['O', 'H', 'H']) assert_npequal(mol.bonds, [[0, 1], [0, 2]]) mol = Molecule.empty() eq_(mol.dimensions['atom'], 0) eq_(mol.dimensions['bond'], 0)
def test_write_gromacs(): water = Molecule([ Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'}) ], export={'groname': 'SOL'}) sys = System.empty(200, 3 * 200, box_vectors=np.eye(3) * 2.0) for i in range(200): sys.add(water.copy()) df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
def test_atom_renderer(): '''Simple rendering of atoms as spheres''' mol = Molecule([Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10])]) v = QtViewer() ar = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array) v.run()
def test_bond_renderer(): v = QtViewer() v.widget.background_color = colors.black mol = Molecule([Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10])]) mol.bonds = np.array([[0, 1],[0, 2]]) #mol = CirDB().get("molecule", "moronic acid") #radii_map = {"O": 0.03, "H": 0.03} radii_map = defaultdict(lambda: 0.03) br = v.add_renderer(BondRenderer, mol.bonds, mol.r_array, mol.type_array, style='impostors') ar = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array, "impostors", radii_map = radii_map) v.run()
def test_text_ui(): v = QtViewer() mol = Molecule([Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10])]) # To add some interaction to it ar = v.add_renderer(AtomRenderer, mol.r_array, mol.type_array, "impostors") tr = v.add_ui(TextUI, 100, 100, 'Hello, Crap') v.run()
def test_write_gromacs(): water = Molecule([Atom('O', [0.0, 0.0, 0.0], name="OW"), Atom('H', [0.1, 0.0, 0.0], name='HW1'), Atom('H', [-0.03333, 0.09428, 0.0], name='HW2')], name='SOL') sys = System.empty() with sys.batch() as b: for i in range(200): b.append(water.copy()) sys.box_vectors = [[2, 0, 0], [0, 2, 0], [0, 0, 2]] df = datafile('/tmp/dummy.gro', mode="w") df.write('system', sys) with assert_raises(Exception): df = datafile('/tmp/dummy.gro') df.write('system', sys) df = datafile('/tmp/dummy.gro') sread = df.read('system') assert all(sread.type_array == sys.type_array)
def test_camera_autozoom(): v = QtViewer() mol = Molecule([Atom("O", [-0.499, 0.249, 0.0]), Atom("H", [-0.402, 0.249, 0.0]), Atom("H", [-0.532, 0.198, 0.10])]) s = datafile('tests/data/3ZJE.pdb').read('system') # To add some interaction to it ar = v.add_renderer(AtomRenderer, s.r_array, s.type_array, "impostors") v.widget.camera.autozoom(s.r_array) v.run()
def get_system(self): self.coordinates = self.interaction_manager.get_coordinates() atoms = [] for index in range(len(self.interaction_manager.atoms)): atom = self.interaction_manager.atoms[index] type = atom.atom_type if type.upper() == "C": if atom.get_free_valency()!=0: type = "Ne" pos = 3.5 * self.coordinates[index] atoms.append(Atom(type,pos)) mol = Molecule(atoms, self.interaction_manager.get_bonds_array()) system = System([mol]) system.bonds = np.array(self.interaction_manager.get_bonds_array()) return system
def test_serialization(): cl = Molecule([Atom.from_fields(type='Cl', r=[0.0, 0.0, 0.0])]) jsonstr = cl.tojson() assert Molecule.from_json(jsonstr).tojson() == jsonstr na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) # Fract position of Na and Cl, space group 255 tsys = crystal([[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], [na, cl], 225, repetitions=[3, 3, 3]) jsonstr = tsys.tojson() assert System.from_json(jsonstr).tojson() == jsonstr
def test_bonds(): from chemlab.io import datafile bz = datafile("tests/data/benzene.mol").read('molecule') na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) # Adding bonds s = System.empty(2, 2 * bz.n_atoms) s.add(bz) assert_npequal(s.bonds, bz.bonds) assert_npequal(bz.bond_orders, [1, 2, 2, 1, 1, 2]) assert_npequal(s.bond_orders, bz.bond_orders) s.add(bz) assert_npequal(s.bonds, np.concatenate((bz.bonds, bz.bonds + 6))) #assert_npequal(s.bond_orders) # Reordering orig = np.array([[0, 1], [6, 8]]) s.bonds = orig s.reorder_molecules([1, 0]) assert_npequal(s.bonds, np.array([[6, 7], [0, 2]])) # This doesn't change the bond_ordering # Selection ss = subsystem_from_molecules(s, [1]) assert_npequal(ss.bonds, np.array([[0, 1]])) import inspect ss2 = System.from_arrays(**dict(inspect.getmembers(ss))) ss2.r_array += 10.0 ms = merge_systems(ss, ss2) assert_npequal(ms.bonds, np.array([[0, 1], [6, 7]])) assert_npequal(ms.bond_orders, np.array([1, 1])) # From_arrays s = System.from_arrays(mol_indices=[0], bonds=bz.bonds, **bz.__dict__) assert_npequal(s.bonds, bz.bonds) assert_npequal(s.bond_orders, bz.bond_orders) # Get molecule entry # Test the bonds when they're 0 s.bonds = np.array([]) assert_equals(s.get_derived_molecule_array('formula'), 'C6')
def split_groups(mol): groups = [] unselected = [x for x in range(mol.n_atoms)] while len(unselected) > 0: new_atoms = set() new_atoms.add(unselected[0]) atoms = [] new_bonds = [] new_bond_orders = [] while len(new_atoms) > 0: a = list(new_atoms)[0] atoms.append(a) new_atoms.remove(a) for bond_index, bond in enumerate(mol.bonds): if atoms[-1] == bond[0] and bond[1] not in atoms: new_atoms.add(bond[1]) if atoms[-1] == bond[1] and bond[0] not in atoms: new_atoms.add(bond[0]) if a in bond and bond.tolist() not in new_bonds: new_bonds.append(bond.tolist()) new_bond_orders.append(mol.bond_orders[bond_index]) atoms.sort() groups.append([atoms, new_bonds, new_bond_orders]) unselected = [x for x in range(mol.n_atoms) if x not in sum([y[0] for y in groups],[])] molecules = [] for group in groups: new_bonds_reduced = np.array([[group[0].index(x[0]), group[0].index(x[1])] for x in group[1]]) numpy_vectors = [system.r_array[x] for x in group[0]] new_molecule = Molecule.from_arrays(r_array=np.array(numpy_vectors), type_array=system.type_array[a], bonds=new_bonds_reduced, bond_orders=group[2] ) new_molecule.export['vectors'] = numpy_vectors molecules.append(new_molecule) return molecules
from chemlab.core import Atom, Molecule, crystal from chemlab.graphics.qt import display_system # Molecule templates na = Molecule([Atom('Na', [0.0, 0.0, 0.0])]) cl = Molecule([Atom('Cl', [0.0, 0.0, 0.0])]) s = crystal( [[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]], # Fractional Positions [na, cl], # Molecules 225, # Space Group cellpar=[.54, .54, .54, 90, 90, 90], # unit cell parameters repetitions=[5, 5, 5]) # unit cell repetitions in each direction display_system(s)
from chemlab.graphics import display_system # Spacing between two grid points spacing = 0.3 # an 8x8x8 grid, for a total of 512 points grid_size = (8, 8, 8) # Preallocate the system # 512 molecules, and 512*3 atoms s = System.empty(512, 512*3) # Water template, it contains export informations for gromacs # more about export later... water_tmp = Molecule([Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype':'HW2'})], export={'groname': 'SOL'}) for a in range(grid_size[0]): for b in range(grid_size[1]): for c in range(grid_size[2]): grid_point = np.array([a,b,c]) * spacing # array operation water_tmp.move_to(grid_point) s.add(water_tmp) # Adjust boxsize for periodic conditions s.box_vectors = np.eye(3) * 8 * spacing # Visualize to verify that the system was setup correctly # display_system(s)
thr_b = rval + threshold #thr_a2 = thr_a * thr_a thr_b2 = thr_b * thr_b dr2 = ((r_array[i] - r_array[j])**2).sum() # print(dr2) if dr2 < thr_b2: bonds.append((i, j)) return np.array(bonds) # END_PATCH --- Si = Molecule([Atom('Si', [0.0, 0.0, 0.0])]) la = 0.5431 s = crystal( [[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]], # Fractional Positions [Si, Si], # Molecules 225, # Space Group cellpar=[la, la, la, 90, 90, 90], # unit cell parameters repetitions=[2, 2, 2]) # unit cell repetitions in each direction bonds = guess_bonds(s.r_array, s.type_array, threshold=0.02) #s.bonds = [[0,1], [1, 2]] v = QtViewer() v.add_renderer(BallAndStickRenderer, s.r_array,
# Spacing between two grid points spacing = 0.3 # an 8x8x8 grid, for a total of 512 points grid_size = (8, 8, 8) # Preallocate the system # 512 molecules, and 512*3 atoms s = System.empty(512, 512 * 3) # Water template, it contains export informations for gromacs # more about export later... water_tmp = Molecule([ Atom('O', [0.0, 0.0, 0.0], export={'grotype': 'OW'}), Atom('H', [0.1, 0.0, 0.0], export={'grotype': 'HW1'}), Atom('H', [-0.03333, 0.09428, 0.0], export={'grotype': 'HW2'}) ], export={'groname': 'SOL'}) for a in range(grid_size[0]): for b in range(grid_size[1]): for c in range(grid_size[2]): grid_point = np.array([a, b, c]) * spacing # array operation water_tmp.move_to(grid_point) s.add(water_tmp) # Adjust boxsize for periodic conditions s.box_vectors = np.eye(3) * 8 * spacing # Visualize to verify that the system was setup correctly # display_system(s)
from chemlab.core import System, Atom, Molecule import numpy as np class MyBallAndStickRepresentation(BallAndStickRepresentation): def on_atom_selection_changed(self): sel = self.selection_state['atoms'].indices cols = np.array(self.atom_colors.array) cols[sel, -1] = 50 self.atom_renderer.update_colors(cols) self.viewer.update() a1 = Atom('Ar', [0.0, 0.0, 0.0]) a2 = Atom('N', [0.0, 0.5, 1.0]) mol = Molecule([a1, a2]) system = System([mol]) viewer = QtMolecularViewer() viewer.add_representation(MyBallAndStickRepresentation, system) def print_indices(): indices = viewer.representation.selection_state['atoms'].indices if len(indices) > 0: print(indices) viewer.schedule(print_indices) viewer.run()