Exemple #1
0
def test_structure_file():
    for fn in [get_fn('tz2.pdb'), nv.datafiles.GRO]:
        content = open(fn, 'r').read()
        fs1 = nv.FileStructure(fn)
        assert content == fs1.get_structure_string()

    # gz
    fn = get_fn('tz2_2.pdb.gz')
    fs2 = nv.FileStructure(fn)
    content = gzip.open(fn).read()
    assert content == fs2.get_structure_string()
def test_structure_file():
    for fn in ['data/tz2.pdb', nv.datafiles.GRO]:
        content = open(fn, 'rb').read()
        fs1 = nv.FileStructure(fn)
        nt.assert_equal(content, fs1.get_structure_string())

    # gz
    fn = 'data/tz2_2.pdb.gz'
    fs2 = nv.FileStructure(fn)
    content = gzip.open(fn).read()
    nt.assert_equal(content, fs2.get_structure_string())
Exemple #3
0
def test_loaded_attribute():
    traj = pt.datafiles.load_tz2()
    structure = nv.FileStructure(nv.datafiles.PDB)

    # False, empty constructor
    view = nv.NGLWidget()
    view.loaded = False
    view.add_structure(structure)
    view.add_trajectory(traj)
    view

    # False, constructor with a single Structure
    view = nv.NGLWidget(structure)
    view.loaded = False
    view.add_trajectory(traj)
    view

    # True
    view = nv.NGLWidget()
    view.loaded = True
    view.add_structure(structure)
    view.add_trajectory(traj)
    view

    # False then True, empty constructor
    view = nv.NGLWidget()
    view.loaded = False
    view.add_structure(structure)
    view.loaded = True
    view.add_trajectory(traj)
    view

    # False then True, constructor with a Trajectory
    view = nv.NGLWidget(nv.PyTrajTrajectory(traj))
    view.loaded = False
    view.add_structure(structure)
    view.loaded = True
    view.add_trajectory(traj)
    view
def test_get_name():
    fn = nglview.datafiles.PDB
    assert_equal(py_utils.get_name(object, dict(name='hello')), 'hello')
    assert_equal(py_utils.get_name(nglview.FileStructure(fn), dict()),
                 'nglview.adaptor.FileStructure')
Exemple #5
0
def test_adaptor_raise():
    with pytest.raises(ValueError):
        nv.FileStructure('hellotheredda.pdb')
Exemple #6
0
 def add_to_view(self, view):
     view.add_component(nglview.FileStructure(self.export()))
Exemple #7
0
def nglview_multimodel_ACV(biomol_filename, volume1_filename,
                           volume2_filename):
    """Create a nglview multi-model structure scene with donor and acceptor accessible-contact volumes (ACV)

    Parameters
    ----------
    structure_filename : str
                         multi-model PDB of the biomolecule
    volume1_filename, volume2_filename : str
        multi-model PDB of the donor and acceptor accessible-contact volume

    Returns
    -------
    view : nglview.NGLWidget
    n_model : int
    """
    global view
    view = nglview.NGLWidget()
    struct = nglview.FileStructure(biomol_filename)
    acv_D = nglview.FileStructure(volume1_filename)
    acv_A = nglview.FileStructure(volume2_filename)

    struct_str = struct.get_structure_string()
    n_models = struct_str.count("MODEL")

    view.add_component(struct)
    view.add_component(acv_D)
    view.add_component(acv_A)

    view.clear_representations(component=0)
    view.clear_representations(component=1)
    view.clear_representations(component=2)

    view.add_simplified_base(component=0,
                             selection="/0",
                             disablePicking=True,
                             colorScheme="atomindex")
    view.add_cartoon(component=0,
                     selection="/0",
                     aspectRatio=4,
                     disablePicking=True,
                     colorScheme="atomindex")

    view.add_surface(color="#6cb381",
                     wireframe=False,
                     opacity=0.4,
                     isolevel=0,
                     component=1,
                     disablePicking=True,
                     selection="CV")
    view.add_surface(color="#c25449",
                     wireframe=False,
                     opacity=0.4,
                     isolevel=0,
                     component=2,
                     disablePicking=True,
                     selection="CV")
    view.add_surface(color="#6cb381",
                     wireframe=True,
                     opacity=0.4,
                     isolevel=0,
                     component=1,
                     disablePicking=True,
                     selection="all")
    view.add_surface(color="#c25449",
                     wireframe=True,
                     opacity=0.4,
                     isolevel=0,
                     component=2,
                     disablePicking=True,
                     selection="all")
    view.stage.set_parameters(mouse_preset="pymol")
    return view, n_models
Exemple #8
0
def view_nglview(molecule, label=False):
    """!@brief Display molecules using the NGLView viewer.

    A function to view molecules using the NGLView project (https://github.com/arose/nglview).
    It is used to display the geometry of the backbone with atom index labels. It is
    also used to display accepted nucleic acid candidates generated after
    the search. For backbone molecules, the number of atoms in the backbone
    is computed here and returned to be used for bounding the displayed atomic
    indices in backbone widgets.


    @param molecule (str) Path to a file containing the 3D structure of the molecule
    @param label (bool) Whether to display atom index labels

    @return number of atoms in @a molecule if @a label is True, else None

    @attention If the molecules are not displayed correctly, you may need to execute
        @code{.sh} jupyter-nbextension enable nglview --py --sys-prefix @endcode

    @sa jupyter_widgets.path
    @sa jupyter_widgets.single_result
    """

    import openbabel
    import nglview

    # Check if the file exists
    if not os.path.isfile(molecule):
        return 0

    # If a label is requested, then this is a backbone or base molecule
    if label:
        # Read the molecule and convert it to a pdb format
        mol = openbabel.OBMol()
        conv = openbabel.OBConversion()
        fmt = conv.FormatFromExt(molecule)
        conv.SetInAndOutFormats(fmt.GetID(), 'pdb')
        conv.ReadFile(mol, molecule)

        # Extract the number of atoms
        num_atoms = mol.NumAtoms()
        mol = conv.WriteString(mol)

        # Display the molecule
        struct = nglview.TextStructure(mol)
        view = nglview.NGLWidget(struct, defaultRepresentation=False)
        view.camera = 'orthographic'
        view.add_representation('licorice')
        view.add_representation('label', labelType='serial', backgroundColor='black', showBackground=True)
        view.center()
        display(view.display(gui=True))

        # Return the number of atoms to be used for the backbone or base widgets
        return num_atoms

    # Show accepted candidates
    else:
        struct = nglview.FileStructure(molecule)
        view = nglview.NGLWidget(struct, defaultRepresentation=False)
        view.camera = 'orthographic'
        view.add_representation('licorice')
        view.center()
        display(view.display(gui=True))