Beispiel #1
0
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()
Beispiel #2
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_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 _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_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])
Beispiel #6
0
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)
Beispiel #7
0
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_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')
Beispiel #9
0
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()
Beispiel #10
0
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()
Beispiel #11
0
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)
Beispiel #12
0
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)
Beispiel #13
0
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
Beispiel #14
0
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()
Beispiel #15
0
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)
Beispiel #16
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)
Beispiel #17
0
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
Beispiel #18
0
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)
Beispiel #19
0
 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
Beispiel #20
0
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)
Beispiel #21
0
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')
Beispiel #22
0
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)
Beispiel #23
0
        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,
Beispiel #24
0
from chemlab.graphics.qt 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
Beispiel #25
0
 def test_init(self):
     a = Atom("O", [-4.99, 2.49, 0.0])
     eq_(a.type_array, 'O')
     assert_npequal(a.r_array, [-4.99, 2.49, 0.0])
from chemlab.mviewer.qtmolecularviewer import QtMolecularViewer
from chemlab.mviewer.representations.ballandstick import BallAndStickRepresentation
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)