def test_bond_guessing(): from chemlab.db import ChemlabDB, CirDB from chemlab.graphics import display_molecule from chemlab.io import datafile mol = datafile('tests/data/3ZJE.pdb').read('molecule') print(mol.r_array) mol.guess_bonds() assert mol.bonds.size > 0 # We should find the bond guessing also for systems # System Made of two benzenes bz = datafile("tests/data/benzene.mol").read('molecule') bzbonds = bz.bonds bz.bonds = np.array([]) # Separating the benzenes by large amount bz2 = bz.copy() bz2.r_array += 2.0 s = System([bz, bz2]) s.guess_bonds() assert_eqbonds(s.bonds, np.concatenate((bzbonds, bzbonds + 6))) # Separating benzenes by small amount bz2 = bz.copy() bz2.r_array += 0.15 s = System([bz, bz2]) s.guess_bonds() assert_eqbonds(s.bonds, np.concatenate((bzbonds, bzbonds + 6)))
def test_reorder_molecules(self): mols = self._make_molecules() system = System(mols) system.bonds = np.array([[0, 1], [3, 5]]) # Reordering system.reorder_molecules([1, 0, 2, 3]) assert_eqbonds(system.bonds, [[0, 2], [3, 4]])
def test_bond_orders(): # Get a molecule with some bonds wat = _make_water() wat_o = wat.copy() # 0,1 0,2 assert_npequal(wat.bond_orders, np.array([0, 0])) # Remove a bond wat.bonds = np.array([[0, 1]]) assert_npequal(wat.bond_orders, np.array([0])) wat.bond_orders = np.array([2]) # Try with a system s = System() s.add(wat_o) s.add(wat) assert_npequal(s.bond_orders, np.array([0, 0, 2])) s.reorder_molecules([1, 0]) # Bonds get sorted accordingly assert_npequal(s.bond_orders, np.array([2, 0, 0])) s.bonds = np.array([[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal(s.bond_orders, np.array([2, 0, 0, 0]))
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_from_batch(self): mols = self._make_molecules() system = System() with system.batch() as batch: [batch.append(mol) for mol in mols] self._assert_init(system)
def test_subsystem_from_atoms(self): mols = self._make_molecules() system = System(mols) sub = subsystem_from_atoms( system, np.array( [True, True, False, False, False, False, False, False, False])) assert_equals(sub.n_mol, 1)
def load_molecule(name, format=None): '''Read a `~chemlab.core.Molecule` from a file. .. seealso:: `chemlab.io.datafile` ''' mol = datafile(name, format=format).read('molecule') display_system(System([mol]))
def display_molecule(mol, autozoom=True): '''Display a `~chemlab.core.Molecule` instance in the viewer. This function wraps the molecule in a system before displaying it. ''' s = System([mol]) display_system(s, autozoom=True)
def test_remove_atoms(self): # This will remove the first and last molecules mols = self._make_molecules() system = System(mols) system.remove_atoms([0, 1, 11]) assert_eqbonds(system.bonds, [[0, 1], [0, 2], [3, 4], [3, 5]]) assert_npequal( system.type_array, np.array(['O', 'H', 'H', 'O', 'H', 'H'], dtype='object'))
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 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_local(): db = ChemlabDB() bz = db.get("molecule", "example.norbornene") pre_string = bz.tojson() db = LocalDB("/tmp/testdb/") db.store("molecule", 'norbornene', bz, nowarn=True) post_string = db.get('molecule', 'norbornene').tojson() assert pre_string == post_string # Do the same thing for a system of 3 norbornenes s = System([bz.copy() for i in range(3)]) pre_string = s.tojson() db.store("system", 'norbornene-3', s, nowarn=True) post_string = db.get('system', 'norbornene-3').tojson() assert pre_string == post_string
def test_local(): db = ChemlabDB() bz = db.get("molecule", "example.norbornene") pre_dict = bz.to_dict() db = LocalDB("/tmp/testdb/") db.store("molecule", 'norbornene', bz, nowarn=True) post_dict = db.get('molecule', 'norbornene').to_dict() npeq_(pre_dict['r_array'], post_dict['r_array']) # Do the same thing for a system of 3 norbornenes s = System([bz.copy() for i in range(3)]) pre_dict = s.to_dict() db.store("system", 'norbornene-3', s, nowarn=True) post_dict = db.get('system', 'norbornene-3').to_dict() npeq_(pre_dict['r_array'], post_dict['r_array'])
def test_init(self): mols = self._make_molecules() system = System(mols) self._assert_init(system)
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()
def test_from_actual_empty(self): mols = self._make_molecules() system = System([]) [system.add(mol) for mol in mols]
def test_subsystem_from_molecules(self): mols = self._make_molecules() system = System(mols) subsystem = subsystem_from_molecules(system, np.array([0, 2])) assert_equals(subsystem.n_mol, 2)