Beispiel #1
0
def ghost_connection_highlight(substituent, color, session):
    """returns a bild object with a cylinder pointing along the 
    x axis towards the substituent and a sphere at the origin"""
    s = ".note connection to molecule\n"
    s += ".color %f %f %f\n" % tuple(color[:-1])
    s += ".transparency %f\n" % (1. - color[-1])
    s += ".sphere 0 0 0  %f\n" % 0.15
    s += ".cylinder 0 0 0   %f 0 0   %f open\n" % (
        substituent.atoms[0].coords[0], 0.15)

    stream = BytesIO(bytes(s, 'utf-8'))
    bild_obj, status = read_bild(session, stream, "ghost connection")

    return bild_obj
Beispiel #2
0
def show_walk_highlight(ring, chimera_ring, color, session):
    """returns a bild sphere on the walk atom if there is only one walk atom
    returns a set of bild arrows showing the walk direction if there are multiple walk atoms
        will also hide any bonds on chimera_ring that are under/over the arrows"""
    s = ".note direction to walk around ring\n"
    s += ".color %f %f %f\n" % tuple(color[:-1])
    s += ".transparency %f\n" % (1. - color[-1])
    if len(ring.end) == 1:
        if hasattr(ring.end[0], "_radii"):
            r = 0.6 * ring.end[0]._radii
        else:
            r = 1

        info = tuple(ring.end[0].coords) + (r, )
        s += ".sphere %f %f %f   %f\n" % info
    else:
        r_bd = 0.16
        for atom1, atom2 in zip(ring.end[:-1], ring.end[1:]):
            v = atom1.bond(atom2)

            if hasattr(atom2, "_radii"):
                r_sp = 0.4 * atom2._radii
            else:
                r_sp = 0.4

            info = tuple(atom1.coords) + tuple(atom2.coords - r_sp * v) + (
                r_bd,
                1.5 * r_bd,
            )
            s += ".arrow %f %f %f   %f %f %f   %f %f 0.7\n" % info

            for bond in chimera_ring.bonds:
                bond_atoms = [
                    chimera_ring.atoms.index(atom) for atom in bond.atoms
                ]
                if ring.atoms.index(atom1) in bond_atoms and ring.atoms.index(
                        atom2) in bond_atoms:
                    bond.display = False
                    break

    stream = BytesIO(bytes(s, 'utf-8'))
    bild_obj, status = read_bild(session, stream, "walk direction")

    return bild_obj
Beispiel #3
0
def key_atom_highlight(ligand, color, session):
    """returns a bild object with spheres on top on ligand's key atoms"""
    s = ".note coordinating atoms\n"
    s += ".color %f %f %f\n" % tuple(color[:-1])
    s += ".transparency %f\n" % (1. - color[-1])
    for atom in ligand.key_atoms:
        if hasattr(atom, "_radii"):

            r = 0.6 * atom._radii
        else:
            r = 0.5

        if atom.element == 'H':
            r *= 1.5

        s += ".sphere %f %f %f   %f\n" % (*atom.coords, r)

    stream = BytesIO(bytes(s, 'utf-8'))
    bild_obj, status = read_bild(session, stream, "highlighting key atoms")

    return bild_obj
Beispiel #4
0
    def calc_cone(self, *args):
        self.settings.cone_option = self.cone_option.currentText()
        self.settings.radii = self.radii_option.currentText()
        self.settings.display_radii = self.display_radii.checkState(
        ) == Qt.Checked
        self.settings.display_cone = self.display_cone.checkState(
        ) == Qt.Checked

        if self.cone_option.currentText() == "Tolman (Unsymmetrical)":
            method = "tolman"
        else:
            method = self.cone_option.currentText()

        radii = self.radii_option.currentText()
        return_cones = self.display_cone.checkState() == Qt.Checked
        display_radii = self.display_radii.checkState() == Qt.Checked

        # self.table.setRowCount(0)

        for center_atom in selected_atoms(self.session):
            rescol = ResidueCollection(center_atom.structure)
            at_center = rescol.find_exact(AtomSpec(center_atom.atomspec))[0]
            if center_atom.structure in self.ligands:
                comp = Component(
                    rescol.find([
                        AtomSpec(atom.atomspec)
                        for atom in self.ligands[center_atom.structure]
                    ]),
                    to_center=rescol.find_exact(AtomSpec(
                        center_atom.atomspec)),
                    key_atoms=rescol.find(BondedTo(at_center)),
                )
            else:
                comp = Component(
                    rescol.find(NotAny(at_center)),
                    to_center=rescol.find_exact(AtomSpec(
                        center_atom.atomspec)),
                    key_atoms=rescol.find(BondedTo(at_center)),
                )

            cone_angle = comp.cone_angle(
                center=rescol.find(AtomSpec(center_atom.atomspec)),
                method=method,
                radii=radii,
                return_cones=return_cones,
            )

            if return_cones:
                cone_angle, cones = cone_angle
                s = ".transparency 0.5\n"
                for cone in cones:
                    apex, base, radius = cone
                    s += ".cone   %6.3f %6.3f %6.3f   %6.3f %6.3f %6.3f   %.3f open\n" % (
                        *apex, *base, radius)

                stream = BytesIO(bytes(s, "utf-8"))
                bild_obj, status = read_bild(self.session, stream,
                                             "Cone angle %s" % center_atom)

                self.session.models.add(bild_obj, parent=center_atom.structure)

            if display_radii:
                s = ".note radii\n"
                s += ".transparency 75\n"
                color = None
                for atom in comp.atoms:
                    chix_atom = atom.chix_atom
                    if radii.lower() == "umn":
                        r = VDW_RADII[chix_atom.element.name]
                    elif radii.lower() == "bondi":
                        r = BONDI_RADII[chix_atom.element.name]

                    if color is None or chix_atom.color != color:
                        color = chix_atom.color
                        rgb = [x / 255. for x in chix_atom.color]
                        rgb.pop(-1)

                        s += ".color %f %f %f\n" % tuple(rgb)

                    s += ".sphere %f %f %f %f\n" % (*chix_atom.coord, r)

                stream = BytesIO(bytes(s, "utf-8"))
                bild_obj, status = read_bild(self.session, stream,
                                             "Cone angle radii")

                self.session.models.add(bild_obj, parent=center_atom.structure)

            row = self.table.rowCount()
            self.table.insertRow(row)

            name = QTableWidgetItem()
            name.setData(Qt.DisplayRole, center_atom.structure.name)
            self.table.setItem(row, 0, name)

            center = QTableWidgetItem()
            center.setData(Qt.DisplayRole, center_atom.atomspec)
            self.table.setItem(row, 1, center)

            ca = QTableWidgetItem()
            ca.setData(Qt.DisplayRole, "%.2f" % cone_angle)
            self.table.setItem(row, 2, ca)

            self.table.resizeColumnToContents(0)
            self.table.resizeColumnToContents(1)
            self.table.resizeColumnToContents(2)
Beispiel #5
0
    def show_rot_vec(self, *args):
        for model in self.session.models.list(type=Generic3DModel):
            if model.name == "rotation vector":
                model.delete()

        if self.display_rot_vec.checkState() == Qt.Unchecked:
            return

        selection = selected_atoms(self.session)

        if len(selection) == 0:
            return

        models = {}
        for atom in selection:
            if atom.structure not in models:
                models[atom.structure] = [atom]
            else:
                models[atom.structure].append(atom)

        if len(models.keys()) == 0:
            return

        if self.vector_option.currentText() == "axis":
            if self.axis.currentText() == "z":
                vector = np.array([0., 0., 1.])
            elif self.axis.currentText() == "y":
                vector = np.array([0., 1., 0.])
            elif self.axis.currentText() == "x":
                vector = np.array([1., 0., 0.])

        elif self.vector_option.currentText() == "view axis":
            if self.view_axis.currentText() == "z":
                vector = self.session.view.camera.get_position().axes()[2]
            elif self.view_axis.currentText() == "y":
                vector = self.session.view.camera.get_position().axes()[1]
            elif self.view_axis.currentText() == "x":
                vector = self.session.view.camera.get_position().axes()[0]

        elif self.vector_option.currentText() == "bond":
            vector = self.bonds

        elif self.vector_option.currentText() == "perpendicular to plane":
            vector = self.perpendiculars

        elif self.vector_option.currentText() == "centroid of atoms":
            vector = self.groups

        elif self.vector_option.currentText() == "custom":
            x = self.vector_x.value()
            y = self.vector_y.value()
            z = self.vector_z.value()
            vector = np.array([x, y, z])

        angle = np.deg2rad(self.angle.value())

        center = {}
        for model in models:
            atoms = models[model]
            coords = np.array([atom.coord for atom in atoms])
            center[model] = np.mean(coords, axis=0)

        if self.cor_button.currentText() == "automatic":
            if self.vector_option.currentText() == "perpendicular to plane":
                center = self.perp_centers

            elif self.vector_option.currentText() == "bond":
                center = self.bond_centers

        elif self.cor_button.currentText() == "select atoms":
            center = self.manual_center

        else:
            center = self.session.main_view.center_of_rotation

        for model in models:
            if isinstance(vector, dict):
                if model not in vector.keys():
                    continue
                else:
                    v = vector[model]

            else:
                v = vector

            if isinstance(center, dict):
                if model not in center.keys():
                    continue
                else:
                    c = center[model]

            else:
                c = center

            if self.vector_option.currentText(
            ) == "centroid of atoms" and self.cor_button.currentText(
            ) != "automatic":
                v = v - c

            if np.linalg.norm(v) == 0:
                continue

            residues = []
            for atom in models[model]:
                if atom.residue not in residues:
                    residues.append(atom.residue)

            v_c = c + v

            s = ".color red\n"
            s += ".arrow %10.6f %10.6f %10.6f   %10.6f %10.6f %10.6f   0.2 0.4 0.7\n" % (
                *c, *v_c)

            stream = BytesIO(bytes(s, 'utf-8'))
            bild_obj, status = read_bild(self.session, stream,
                                         "rotation vector")

            self.session.models.add(bild_obj, parent=model)
Beispiel #6
0
def pointGroup(
    session,
    selection,
    printElements=False,
    displayElements=True,
    tolerance=0.1,
    axisTolerance=0.5,
    maxRotation=6,
):

    for model in selection:
        rescol = ResidueCollection(model, refresh_ranks=False)
        pg = PointGroup(
            rescol,
            tolerance=tolerance,
            rotation_tolerance=np.deg2rad(axisTolerance),
            max_rotation=maxRotation,
        )
        session.logger.info("%s: %s" % (model.name, pg.name))

        if printElements:
            for ele in sorted(pg.elements, reverse=True):
                session.logger.info(repr(ele))

        if displayElements:
            for ele in sorted(pg.elements, reverse=True):
                if isinstance(ele, InversionCenter):
                    inv = ".note %s\n" % repr(ele)
                    inv += ".sphere   %.5f  %.5f  %.5f  0.1\n" % tuple(
                        pg.center)

                    stream = BytesIO(bytes(inv, "utf-8"))
                    bild_obj, status = read_bild(session, stream, repr(ele))
                    session.models.add(bild_obj, parent=model)

                elif isinstance(ele, ProperRotation):
                    prots = ".note %s\n" % repr(ele)
                    prots += ".color red\n"
                    prots += ".arrow   %.5f  %.5f  %.5f  " % tuple(pg.center)
                    end = pg.center + ele.n * np.sqrt(ele.exp) * ele.axis
                    prots += "%.5f  %.5f  %.5f  0.05\n" % tuple(end)

                    stream = BytesIO(bytes(prots, "utf-8"))
                    bild_obj, status = read_bild(session, stream, repr(ele))
                    session.models.add(bild_obj, parent=model)

                elif isinstance(ele, ImproperRotation):
                    irots = ".note %s\n" % repr(ele)
                    irots += ".color blue\n"
                    irots += ".arrow   %.5f  %.5f  %.5f  " % tuple(pg.center)
                    end = pg.center + np.sqrt(ele.n) * np.sqrt(
                        ele.exp) * ele.axis
                    irots += "%.5f  %.5f  %.5f  0.05\n" % tuple(end)
                    irots += ".transparency 25\n"
                    z = ele.axis
                    x = perp_vector(z)
                    y = np.cross(x, z)
                    for angle in np.linspace(0, 2 * np.pi, num=250):
                        pt2 = ele.n**0.9 * x * np.cos(angle)
                        pt2 += ele.n**0.9 * y * np.sin(angle)
                        pt2 += pg.center
                        if angle > 0:
                            irots += ".polygon  %6.3f  %6.3f  %6.3f" % tuple(
                                pt1)
                            irots += "     %6.3f  %6.3f  %6.3f" % tuple(
                                pg.center)
                            irots += "     %6.3f  %6.3f  %6.3f" % tuple(pt2)
                            irots += "\n"
                        pt1 = pt2

                    stream = BytesIO(bytes(irots, "utf-8"))
                    bild_obj, status = read_bild(session, stream, repr(ele))
                    session.models.add(bild_obj, parent=model)

                elif isinstance(ele, MirrorPlane):
                    mirror = ".note %s\n" % repr(ele)
                    mirror += ".color purple\n"
                    mirror += ".transparency 25\n"
                    z = ele.axis
                    x = perp_vector(z)
                    y = np.cross(x, z)
                    for angle in np.linspace(0, 2 * np.pi, num=250):
                        pt2 = 5 * x * np.cos(angle)
                        pt2 += 5 * y * np.sin(angle)
                        pt2 += pg.center
                        if angle > 0:
                            mirror += ".polygon  %6.3f  %6.3f  %6.3f" % tuple(
                                pt1)
                            mirror += "     %6.3f  %6.3f  %6.3f" % tuple(
                                pg.center)
                            mirror += "     %6.3f  %6.3f  %6.3f" % tuple(pt2)
                            mirror += "\n"
                        pt1 = pt2

                    stream = BytesIO(bytes(mirror, "utf-8"))
                    bild_obj, status = read_bild(session, stream, repr(ele))
                    session.models.add(bild_obj, parent=model)
Beispiel #7
0
def ligandSterimol(session,
                   selection,
                   radii="UMN",
                   showVectors=True,
                   showRadii=True,
                   at_L=None,
                   bisect_L=False,
                   return_values=False):
    models, center, key_atoms = avoidTargets(session.logger, selection)

    radii = radii.lower()

    targets = []
    coord_atoms = []
    datas = []

    info = "<pre>model\tcoord. atoms\tB1\tB2\tB3\tB4\tB5\tL\n"

    # if return_values:
    # if len(models.keys()) > 1:
    #     raise RuntimeError("only one substituent may be selected")

    # if any(len(models[key]) > 1 for key in models.keys()):
    #     raise RuntimeError("only one substituent may be selected")

    for model in models:
        rescol = ResidueCollection(model)
        comp_atoms = [AtomSpec(at.atomspec) for at in models[model]]
        key_atomspec = [AtomSpec(at.atomspec) for at in key_atoms[model]]
        center_atomspec = [AtomSpec(at.atomspec) for at in center[model]]
        if len(center_atomspec) != 1:
            session.logger.error(
                "ligand sterimol requires one central atom to which " + \
                "the ligand is coordinated\n" + \
                "%i were found on model %s:\n" % (len(center_atomspec), model.atomspec) + \
                "\n".join([at.atomspec for at in center[model]])
            )
            continue

        comp = Component(
            rescol.find(comp_atoms),
            to_center=rescol.find(center_atomspec),
            key_atoms=rescol.find(key_atomspec),
        )

        data = comp.sterimol(
            return_vector=True,
            radii=radii,
            at_L=at_L,
            to_center=rescol.find(center_atomspec),
            bisect_L=bisect_L,
        )
        l = np.linalg.norm(data["L"][1] - data["L"][0])
        b1 = np.linalg.norm(data["B1"][1] - data["B1"][0])
        b2 = np.linalg.norm(data["B2"][1] - data["B2"][0])
        b3 = np.linalg.norm(data["B3"][1] - data["B3"][0])
        b4 = np.linalg.norm(data["B4"][1] - data["B4"][0])
        b5 = np.linalg.norm(data["B5"][1] - data["B5"][0])

        if showVectors:
            for key, color in zip(
                ["B1", "B2", "B3", "B4", "B5", "L"],
                ["black", "green", "purple", "orange", "red", "blue"]):
                start, end = data[key]
                s = ".color %s\n" % color
                s += ".note Sterimol %s\n" % key
                s += ".arrow %6.3f %6.3f %6.3f   %6.3f %6.3f %6.3f\n" % (
                    *start, *end)

                stream = BytesIO(bytes(s, "utf-8"))
                bild_obj, status = read_bild(session, stream,
                                             "Sterimol %s" % key)

                session.models.add(bild_obj, parent=model)

        if showRadii:
            s = ".note radii\n"
            s += ".transparency 75\n"
            color = None
            for atom in comp.atoms:
                chix_atom = atom.chix_atom
                if radii == "umn":
                    r = VDW_RADII[chix_atom.element.name]
                elif radii == "bondi":
                    r = BONDI_RADII[chix_atom.element.name]

                if color is None or chix_atom.color != color:
                    color = chix_atom.color
                    rgb = [x / 255. for x in chix_atom.color]
                    rgb.pop(-1)

                    s += ".color %f %f %f\n" % tuple(rgb)

                s += ".sphere %f %f %f %f\n" % (*chix_atom.coord, r)

            stream = BytesIO(bytes(s, "utf-8"))
            bild_obj, status = read_bild(session, stream, "Sterimol radii")

            session.models.add(bild_obj, parent=model)

        name = get_filename(model.name, include_parent_dir=False)

        info += "%-16s\t%-11s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n" % (
            name, ", ".join(at.atomspec
                            for at in key_atoms[model]), b1, b2, b3, b4, b5, l)
        targets.append(name)
        coord_atoms.append([at.atomspec for at in key_atoms[model]])
        datas.append(data)

    info = info.strip()
    info += "</pre>"
    if not return_values:
        session.logger.info(info, is_html=True)

    if return_values:
        return targets, coord_atoms, datas
Beispiel #8
0
def sterimol(
        session,
        selection,
        radii="UMN",
        showVectors=True,
        showRadii=True,
        LCorrection="FORTRAN",
        return_values=False
    ):
    models, attached = avoidTargets(session.logger, selection)
    
    radii = radii.lower()

    old_L = False
    if LCorrection.upper() == "FORTRAN":
        old_L = True

    model_names = []
    targets = []
    datas = []
    
    info = "<pre>model\tsubstituent atom\tB1\tB2\tB3\tB4\tB5\tL\n"
    
    # if return_values:
        # if len(models.keys()) > 1:
        #     raise RuntimeError("only one substituent may be selected")
        
        # if any(len(models[key]) > 1 for key in models.keys()):
        #     raise RuntimeError("only one substituent may be selected")
    
    for model in models:
        rescol = ResidueCollection(model, refresh_ranks=False)
        for res in models[model]:
            for target in models[model][res]:
                end_atomspec = AtomSpec(attached[target].atomspec)
                start_atomspec = AtomSpec(target.atomspec)
                
                sub_atoms = rescol.get_fragment(start_atomspec, end_atomspec)
                sub = Substituent(
                    sub_atoms, 
                    end=rescol.find_exact(end_atomspec)[0], 
                    detect=False,
                )
                
                data = sub.sterimol(
                    return_vector=True,
                    radii=radii,
                    old_L=old_L,
                )
                l = np.linalg.norm(data["L"][1] - data["L"][0])
                b1 = np.linalg.norm(data["B1"][1] - data["B1"][0])
                b2 = np.linalg.norm(data["B2"][1] - data["B2"][0])
                b3 = np.linalg.norm(data["B3"][1] - data["B3"][0])
                b4 = np.linalg.norm(data["B4"][1] - data["B4"][0])
                b5 = np.linalg.norm(data["B5"][1] - data["B5"][0])
                
                if showVectors:
                    for key, color in zip(
                            ["B1", "B2", "B3", "B4", "B5", "L"],
                            ["black", "green", "purple", "orange", "red", "blue"]
                    ):
                        start, end = data[key]
                        s = ".color %s\n" % color
                        s += ".note Sterimol %s\n" % key
                        s += ".arrow %6.3f %6.3f %6.3f   %6.3f %6.3f %6.3f\n" % (*start, *end)
                        
                        stream = BytesIO(bytes(s, "utf-8"))
                        bild_obj, status = read_bild(session, stream, "Sterimol %s" % key)
                        
                        session.models.add(bild_obj, parent=model)
                    
                if showRadii:
                    s = ".note radii\n"
                    s += ".transparency 75\n"
                    color = None
                    for atom in sub.atoms:
                        chix_atom = atom.chix_atom
                        if radii == "umn":
                            r = VDW_RADII[chix_atom.element.name]
                        elif radii == "bondi":
                            r = BONDI_RADII[chix_atom.element.name]
                        
                        if color is None or chix_atom.color != color:
                            color = chix_atom.color
                            rgb = [x/255. for x in chix_atom.color]
                            rgb.pop(-1)
                            
                            s += ".color %f %f %f\n" % tuple(rgb)
                        
                        s += ".sphere %f %f %f %f\n" % (*chix_atom.coord, r)
                
                    stream = BytesIO(bytes(s, "utf-8"))
                    bild_obj, status = read_bild(session, stream, "Sterimol radii")
                    
                    session.models.add(bild_obj, parent=model)
                
                model_name = get_filename(model.name, include_parent_dir=False)
                
                info += "%-16s\t%-11s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n" % (
                    model_name,
                    target.atomspec,
                    b1, b2, b3, b4, b5, l
                )
                model_names.append(model_name)
                targets.append(target.atomspec)
                datas.append(data)
    
    info = info.strip()
    info += "</pre>"
    if not return_values:
        session.logger.info(info, is_html=True)
    
    if return_values:
        return model_names, targets, datas