Exemplo n.º 1
0
    def features_to_pymol_strings(self, features):
        """
        creates the code to visualise `ccdc.pharmacophore.Pharmacophore.Features` in PyMOL

        :param list of (`ccdc.pharmacophore.Pharmacophore.Features`) features: features to be visualised
        :return: str python code
        """
        group_dic = {ident: [] for ident in self.feature_definitions.keys()}
        pymol_out = ''

        for i, feat in enumerate(features):
            pymol_out += feat.to_pymol_str(i=i)
            group_dic[feat.identifier].append(f"{feat.identifier}_{i}")

            if feat.projected_identifier and self.protein:
                resnum = feat.projected_identifier.split("/")[1]
                # TODO: clean up
                pymol_out += f'\ncmd.select("sele", "resi {resnum}")\ncmd.show("sticks", "sele")'

        for fd in self.feature_definitions.keys():
            pymol_out += PyMOLCommands.group(f"{fd}_pts", group_dic[fd])
        pymol_out += PyMOLCommands.group(
            "ligand_pharmacophore", [f"{a}_pts" for a in group_dic.keys()])

        return pymol_out
Exemplo n.º 2
0
    def test_cone(self):
        self.pymol_file.commands += PyMOLCommands.cone("_mycone", (0, 0, 0),
                                                       (10, 10, 10), 5,
                                                       (1, 0, 0))
        self.pymol_file.commands += PyMOLCommands.load_cgo("_mycone", "mycone")

        self.pymol_file.write("testdata/wrapper_pymol/test_cone.py")
Exemplo n.º 3
0
    def test_isosurface(self):
        self.pymol_file.commands += PyMOLCommands.set_color(
            "pomegranate", self.pomegranate)
        self.pymol_file.commands += PyMOLCommands.isosurface(
            "test_grid.grd", "mygrid", "mysurface", 4, "pomegranate")

        self.pymol_file.write("testdata/wrapper_pymol/test_isosurface.py")
Exemplo n.º 4
0
        def visualise(g, gname, peaks):
            # visualise
            gobj_name = f"{gname}_grid"
            surface_objname = f"surface_{gobj_name}"
            vis = f"testdata/grid_extension/{gname}.py"
            gpath = f"testdata/grid_extension/{gname}.grd"

            g.write(gpath)

            f = PyMOLFile()
            f.commands += PyMOLCommands.load(fname=f"{gname}.grd",
                                             objname=gobj_name)
            f.commands += PyMOLCommands.isosurface(
                grd_name=gobj_name,
                isosurface_name=surface_objname,
                level=5,
                color="yellow")

            for i, p in enumerate(peaks):
                f.commands += PyMOLCommands.sphere(objname=f"peak_obj_{i}",
                                                   rgba=[1, 1, 1, 1],
                                                   coords=p,
                                                   radius=0.5)
                f.commands += PyMOLCommands.load_cgo(obj=f"peak_obj_{i}",
                                                     objname=f"peak_{i}")

            f.write(vis)
Exemplo n.º 5
0
    def test_arrow(self):
        self.pymol_file.commands += PyMOLCommands.arrow(
            "_myarrow", (1, 1, 0), (4, 5, 0), (1, 0, 0), (0, 0, 1))
        self.pymol_file.commands += PyMOLCommands.load_cgo(
            "_myarrow", "myarrow")

        self.pymol_file.write("testdata/wrapper_pymol/test_arrow.py")
Exemplo n.º 6
0
    def test_pseudoatom(self):
        self.pymol_file.commands += PyMOLCommands.pseudoatom(
            "mypseudoatom", (1, 1, 1), self.pomegranate)

        self.pymol_file.commands += PyMOLCommands.pseudoatom(
            "mypseudoatom2", (2, 2, 2), self.pomegranate, "31.2")

        self.pymol_file.write("testdata/wrapper_pymol/test_pseudoatom.py")
Exemplo n.º 7
0
def create_pymol_file(residues, pdb):
    f = PyMOLFile()

    f.commands += PyMOLCommands.load(f"{pdb.upper()}.pdb", pdb)
    f.commands += PyMOLCommands.hide("sticks")

    for residue in residues:
        f.commands += PyMOLCommands.select("sele", f"resi {residue[3:]}")
        f.commands += PyMOLCommands.show("stick", "sele")

    f.commands += PyMOLCommands.load(f"{pdb}_ref.mol2", "lig")
    return f
Exemplo n.º 8
0
    def test_line(self):
        self.pymol_file.commands += PyMOLCommands.line("myline", (1, 1, 1),
                                                       (2, 2, 2),
                                                       self.pomegranate,
                                                       width=4)

        self.pymol_file.commands += PyMOLCommands.line("myline2", (1, 1, 1),
                                                       (0, 0, 0),
                                                       self.pomegranate,
                                                       width=10)

        self.pymol_file.write("testdata/wrapper_pymol/test_line.py")
Exemplo n.º 9
0
    def test_edge_detection(self):
        self.selected = Grid.from_file("testdata/result/molA.grd")
        edge = self.selected.edge_detection()

        self.assertLess(len(edge), self.selected.count_grid())
        f = PyMOLFile()
        for i, n in enumerate(edge):
            f.commands += PyMOLCommands.sphere(f"sphere_{i}", (0, 0, 1, 1), n,
                                               0.1)
            f.commands += PyMOLCommands.load_cgo(f"sphere_{i}",
                                                 f"sphere_{i}_obj")

        f.write("testdata/grid_extension/edge_detection.py")
Exemplo n.º 10
0
    def test_cylinder(self):
        self.pymol_file.commands += PyMOLCommands.cylinder(
            "_mycylinder", (0, 0, 0), (1, 1, 1), 2, (1, 0, 0, 0.5))
        self.pymol_file.commands += PyMOLCommands.load_cgo(
            "_mycylinder", "mycylinder")

        self.pymol_file.commands += PyMOLCommands.cylinder(
            "_mycylinder2", (0, 0, 0), (10, 10, 10), 0.2, (1, 0, 0, 1),
            (0.5, 0.5, 0))
        self.pymol_file.commands += PyMOLCommands.load_cgo(
            "_mycylinder2", "mycylinder2")

        self.pymol_file.write("testdata/wrapper_pymol/test_cylinder.py")
Exemplo n.º 11
0
    def test_group(self):
        self.pymol_file.commands += PyMOLCommands.line("myline1", (0, 0, 0),
                                                       (1, 1, 1),
                                                       self.pomegranate,
                                                       width=10)

        self.pymol_file.commands += PyMOLCommands.pseudoatom(
            "mypseudoatom1", (0, 0, 0), self.pomegranate)

        self.pymol_file.commands += PyMOLCommands.pseudoatom(
            "mypseudoatom2", (1, 1, 1), self.pomegranate)
        self.pymol_file.commands += PyMOLCommands.group(
            "mygroup", ["myline1", "mypseudoatom1", "mypseudoatom2"])

        self.pymol_file.write("testdata/wrapper_pymol/test_group.py")
Exemplo n.º 12
0
    def test_sphere(self):
        self.pymol_file.commands += PyMOLCommands.sphere("_mysphere",
                                                         self.pomegranate,
                                                         (0, 0, 0),
                                                         radius=2)
        self.pymol_file.commands += PyMOLCommands.load_cgo(
            "_mysphere", "mysphere")

        self.pymol_file.commands += PyMOLCommands.sphere("_mysphere1",
                                                         self.pomegranate,
                                                         (1, 1, 1),
                                                         radius=1)
        self.pymol_file.commands += PyMOLCommands.load_cgo(
            "_mysphere1", "mysphere1")

        self.pymol_file.write("testdata/wrapper_pymol/test_sphere.py")
Exemplo n.º 13
0
    def to_pymol_str(self):
        pymol_out = ""
        pymol_out += PyMOLCommands.sphere("peak_obj", (1, 1, 1, 1),
                                          coords=self.point,
                                          radius=1)
        pymol_out += PyMOLCommands.load_cgo("peak_obj", "peak")

        ligand_peak_features = []
        for i, feat in enumerate(self.features):
            pymol_out += feat.to_pymol_str(i=i)
            ligand_peak_features.append(f"{feat.identifier}_{i}")

        pymol_out += PyMOLCommands.group("ligand_peak_features",
                                         ligand_peak_features)

        return pymol_out
Exemplo n.º 14
0
    def __init__(self,
                 path,
                 grid_extension=".grd",
                 zip_results=True,
                 settings=None):
        if settings is None:
            self.settings = self.Settings()
        else:
            self.settings = settings

        if grid_extension in self.settings.supported_grids:
            self.settings.grid_extension = grid_extension
        else:
            self.settings.grid_extension = ".grd"
            print(
                "WARNING: Invalid grid file format provided. Default: '.grd' will be used"
            )

        self.path = self.get_out_dir(path)

        self.zip_results = zip_results

        self.pymol_out = PyMOLFile()
        if self.zip_results:
            # unzip and change working directory
            self.pymol_out.commands += PyMOLCommands.unzip_dir(
                self.settings.container)
Exemplo n.º 15
0
    def _write_pymol_isosurfaces(self, dict, relpath, identifier, dict_type):
        """
        Loads grids and generates isosurfaces

        :param dict: interaction grid dictionary
        :param relpath: result containing directory
        :param identifier: hotspot identifier
        :param dict_type: superstar, fhm or weighted_superstar

        :type dict: dict
        :type relpath: str
        :type identifier: str
        :type dict_type: str

        :return: pymol commands
        :rtype: str
        """
        cmd = ""
        default_level = 5
        # load grids and create isosurfaces
        group_members = []
        for p in dict.keys():
            if dict_type == 'fhm':
                objname = f'{p}_{identifier}'
                fname = f'{relpath}/{p}{self.settings.grid_extension}'
            else:
                objname = f'{dict_type}_{p}_{identifier}'
                fname = f'{relpath}/{dict_type}_{p}{self.settings.grid_extension}'
            cmd += PyMOLCommands.load(fname=fname, objname=objname)

            # surface_10_apolar_hotspotid
            surface_objname = f'surface_{objname}'
            cmd += PyMOLCommands.isosurface(grd_name=objname,
                                            isosurface_name=surface_objname,
                                            level=default_level,
                                            color=self.settings.colour_dict[p])

            cmd += PyMOLCommands.pymol_set(setting_name='transparency',
                                           value=self.settings.transparency,
                                           selection=surface_objname)

            group_members.extend([objname, f"surface_{objname}"])

        cmd += PyMOLCommands.group(group_name=identifier,
                                   members=group_members)
        return cmd
Exemplo n.º 16
0
    def write(self, hr):
        """
        writes the Fragment Hotspot Maps result to the output directory and create the pymol visualisation file

        :param hr: a Fragment Hotspot Maps result or list of results
        :type hr: `hotspots.result.Result`

        >>> from hotspots.calculation import Runner
        >>> from hotspots.hs_io import HotspotWriter

        >>> r = Runner
        >>> result = r.from_pdb("1hcl")
        >>> out_dir = <path_to_out>
        >>> with HotspotWriter(out_dir) as w:
        >>>     w.write(result)

        """
        container = Helper.get_out_dir(join(self.path,
                                            self.settings.container))

        if isinstance(hr, list):
            print(hr)
            if len({h.identifier for h in hr}) != len(hr):
                # if there are not unique identifiers, create some.
                for i, h in enumerate(hr):
                    h.identifier = f"hotspot-{i}"
            for h in hr:
                self._single_write(container, h)

        else:
            if not hr.identifier:
                hr.identifier = "hotspot"
            self._single_write(container, hr)

        self._write_pymol_isoslider(hr)
        self.pymol_out.commands += PyMOLCommands.background_color(
            self.settings.bg_color)
        self.pymol_out.commands += PyMOLCommands.push_to_wd()

        if self.zip_results:
            self.compress()

        self.pymol_out.write(join(self.path, "pymol_file.py"))
Exemplo n.º 17
0
    def test_neighbourhood(self):
        # check the catchment critera
        neighbours = Grid.neighbourhood(i=0,
                                        j=0,
                                        k=0,
                                        high=self.buriedness.nsteps,
                                        catchment=1)
        self.assertEqual(3, len(neighbours))

        neighbours1 = Grid.neighbourhood(i=5,
                                         j=5,
                                         k=5,
                                         high=self.buriedness.nsteps,
                                         catchment=1)
        self.assertEqual(6, len(neighbours1))

        f = PyMOLFile()
        for i, n in enumerate(neighbours + neighbours1):
            f.commands += PyMOLCommands.sphere(f"sphere_{i}", (0, 0, 1, 1), n,
                                               0.1)
            f.commands += PyMOLCommands.load_cgo(f"sphere_{i}",
                                                 f"sphere_{i}_obj")
        f.commands += PyMOLCommands.sphere("centre", (1, 0, 0, 1), (5, 5, 5),
                                           0.1)
        f.commands += PyMOLCommands.load_cgo("centre", "centre_obj")
        f.commands += PyMOLCommands.sphere("centre1", (1, 0, 0, 1), (0, 0, 0),
                                           0.1)
        f.commands += PyMOLCommands.load_cgo("centre1", "centre1_obj")

        f.write("testdata/grid_extension/neighbourhood.py")
Exemplo n.º 18
0
    def _write_pymol_isoslider(self, hr):
        """
        generates the commands for an isoslider

        :param hr: a hotspot result
        :type hr: `hotspots.results.Results`
        """
        # Isosurface obj's take the name: "surface_{probe ID}_{hotpsot ID}"
        # e.g. "surface_apolar_hotspotA"
        if not isinstance(hr, list):
            hr = [hr]

        # the hotspot grids are always output
        surface_dic = {h.identifier: {'fhm': [f"surface_{g}_{h.identifier}" for g in h.super_grids.keys()]}
                       for h in hr}

        surface_value_dic = {h.identifier: {"fhm": max([round(g.extrema[1], 1) for g in h.super_grids.values()])}
                             for h in hr}

        for h in hr:
            if self.settings.output_superstar and h.superstar:
                surface_dic[h.identifier].update({'superstar': [f"surface_superstar_{g}_{h.identifier}"
                                                                for g in h.superstar.keys()]})

                surface_value_dic[h.identifier].update({'superstar': max([round(g.extrema[1], 1)
                                                                          for g in h.superstar.values()])})

            if self.settings.output_weighted and h.weighted_superstar:
                surface_dic[h.identifier].update({'weighted': [f"surface_weighted_superstar_{g}_{h.identifier}"
                                                                for g in h.weighted_superstar.keys()]})

                surface_value_dic[h.identifier].update({'weighted': max([round(g.extrema[1], 1)
                                                                          for g in h.weighted_superstar.values()])})

            if self.settings.output_buriedness and h.buriedness:
                surface_dic[h.identifier].update({'buriedness': [f"surface_buriedness_{h.identifier}"]})

                surface_value_dic[h.identifier].update({'buriedness': 8})

        min_value = 0
        print(surface_dic)
        print(surface_value_dic)

        self.pymol_out.commands += PyMOLCommands.isoslider(surface_dic, surface_value_dic)
Exemplo n.º 19
0
 def test_load(self):
     self.pymol_file.commands += PyMOLCommands.load("test_protein.pdb",
                                                    "myprotein")
     self.pymol_file.write("testdata/wrapper_pymol/test_load.py")
Exemplo n.º 20
0
    def _write_pymol_objects(self, relpath, hr, load_prot=True):
        """
        generates pymol commands associated with an indivdual hotspot

        :param relpath: path to the directory holding associated files
        :param hr: hotspot result

        :type relpath: str
        :type hr: `hotspots.results.Results`

        """
        self.pymol_out.commands += self._write_pymol_isosurfaces(
            hr.super_grids, relpath, hr.identifier, "fhm")

        if self.settings.output_superstar and hr.superstar:
            self.pymol_out.commands += self._write_pymol_isosurfaces(
                hr.superstar, relpath, hr.identifier, "superstar")

        if self.settings.output_weighted and hr.weighted_superstar:
            self.pymol_out.commands += self._write_pymol_isosurfaces(
                hr.weighted_superstar, relpath, hr.identifier, "weighted")

        if self.settings.output_buriedness and hr.buriedness:
            default_level = 3
            objname = f'buriedness_{hr.identifier}'
            fname = f'{relpath}/buriedness{self.settings.grid_extension}'

            self.pymol_out.commands += PyMOLCommands.load(fname=fname,
                                                          objname=objname)

            # surface_10_apolar_hotspotid
            surface_objname = f'surface_{objname}'
            self.pymol_out.commands += PyMOLCommands.isosurface(
                grd_name=objname,
                isosurface_name=surface_objname,
                level=default_level,
                color=self.settings.colour_dict["buriedness"])

            self.pymol_out.commands += PyMOLCommands.pymol_set(
                setting_name='transparency',
                value=self.settings.transparency,
                selection=surface_objname)

        group_members = [
            f'buriedness_{hr.identifier}',
            f'surface_buriedness_{hr.identifier}'
        ]

        self.pymol_out.commands += PyMOLCommands.group(
            group_name=hr.identifier, members=group_members)

        # generate grid labels
        labels = hr.grid_labels()

        for p, dic in labels.items():
            i = 0
            group_me = []
            for coord, value in dic.items():
                objname = f"PS_{p}_{hr.identifier}_{i}"
                group_me.append(objname)
                self.pymol_out.commands += PyMOLCommands.pseudoatom(
                    objname=objname, coords=coord, label=f'{round(value, 1)}')
                group_me.append(objname)
                i += 1
            self.pymol_out.commands += PyMOLCommands.group(
                f'label_{p}_{hr.identifier}', group_me)

        self.pymol_out.commands += PyMOLCommands.group(
            f"labels_{hr.identifier}",
            [f'label_{p}_{hr.identifier}' for p in hr.super_grids.keys()])

        # load up the protein
        if load_prot:
            self.pymol_out.commands += PyMOLCommands.load(
                f'{relpath}/protein.pdb', f'protein_{hr.identifier}')
            if len(self.settings.protein_color_dic) > 0:
                self.pymol_out += PyMOLCommands.color(
                    "slate", f'protein_{hr.identifier}')
            self.pymol_out.commands += PyMOLCommands.show(
                "cartoon", f'protein_{hr.identifier}')
            self.pymol_out.commands += PyMOLCommands.hide(
                "line", f'protein_{hr.identifier}')
            self.pymol_out.commands += PyMOLCommands.show("sticks", "organic")
Exemplo n.º 21
0
    def to_pymol_str(self, i=0, label=True, transparency=0.6):
        pymol_out = ""
        point_colour = rgb_to_decimal(self.colour)
        point_colour = utilities.Colour(point_colour[0], point_colour[1],
                                        point_colour[2], transparency)
        feat_ID = f"{self.identifier}_{i}"
        group = []

        coords = (self.point[0].centre[0], self.point[0].centre[1],
                  self.point[0].centre[2])

        radius = self.point[0].radius

        point_objname = f"{self.identifier}_point_{i}"
        pymol_out += PyMOLCommands.sphere(point_objname,
                                          point_colour,
                                          coords,
                                          radius=radius)
        pymol_out += PyMOLCommands.load_cgo(point_objname,
                                            f"{point_objname}_obj")
        group.append(f"{point_objname}_obj")

        if self.score != 0 and label:
            score_name = f"{point_objname}_score"
            pymol_out += PyMOLCommands.pseudoatom(
                objname=score_name,
                coords=coords,
                label=f'{round(self.score, 1)}')
            group.append(score_name)

            rank_name = f"{self.identifier}_rank_{i}"
            pymol_out += PyMOLCommands.pseudoatom(objname=rank_name,
                                                  coords=coords,
                                                  label=str(i + 1))
            group.append(rank_name)

        if self.projected:
            proj_coords = (self.projected[0].centre[0],
                           self.projected[0].centre[1],
                           self.projected[0].centre[2])

            projection_objname = f"{self.identifier}_projection_{i}"
            line_objname = f"{self.identifier}_line_{i}"

            group.extend([projection_objname, line_objname])
            if self.projected_value != 0 and label:
                proj_score_name = f"{point_objname}_proj_score"
                pymol_out += PyMOLCommands.pseudoatom(
                    objname=proj_score_name,
                    coords=proj_coords,
                    label=f'{round(self.projected_value, 1)}')
                group.append(proj_score_name)
            projected_colour = adjust_lightness(self.colour, percentage=30)
            projected_colour = utilities.Colour(projected_colour[0],
                                                projected_colour[1],
                                                projected_colour[2],
                                                transparency)
            projected_radius = self.projected[0].radius

            pymol_out += PyMOLCommands.sphere(projection_objname,
                                              projected_colour,
                                              proj_coords,
                                              radius=projected_radius)
            pymol_out += PyMOLCommands.load_cgo(projection_objname,
                                                f"{projection_objname}_obj")
            pymol_out += PyMOLCommands.line(line_objname,
                                            coords,
                                            proj_coords,
                                            rgb=projected_colour)

        pymol_out += PyMOLCommands.group(feat_ID, group)
        return pymol_out
Exemplo n.º 22
0
 def test_grid(self):
     self.pymol_file.commands += PyMOLCommands.load("test_grid.grd",
                                                    "mygrid")
     self.pymol_file.write("testdata/wrapper_pymol/test_grid.py")
Exemplo n.º 23
0
    def pymol_visulisation(self, outdir=None, fname="pymol_file.py"):
        if not outdir:
            outdir = os.getcwd()

        if not os.path.exists(outdir):
            os.mkdir(outdir)

        if self.ligands:
            with MoleculeWriter(os.path.join(outdir, "ligands.mol2")) as w:
                for ligand in self.ligands:
                    try:
                        w.write(ligand.molecule)
                    except AttributeError:
                        w.write(ligand)

        if self.protein:
            with MoleculeWriter(os.path.join(outdir, "protein.mol2")) as w:
                w.write(self.protein.molecule)

        self.pymol_out = PyMOLFile()

        if self.ligands:
            self.pymol_out.commands += PyMOLCommands.load(
                "ligands.mol2", "ligands")

        if self.protein:
            self.pymol_out.commands += PyMOLCommands.load(
                "protein.mol2", "protein")

        # write out point spheres and projection sphere and lines if applicable
        self.pymol_out.commands += self.features_to_pymol_strings(
            self.detected_features)

        if self.feature_point_grids:
            for identifier, g in self.feature_point_grids.items():
                g.write(os.path.join(outdir, f"{identifier}.grd"))

                point_colour = rgb_to_decimal(
                    self.feature_definitions[identifier].colour)
                self.pymol_out.commands += PyMOLCommands.set_color(
                    f"{identifier}_color", point_colour)
                self.pymol_out.commands += PyMOLCommands.load(
                    f"{identifier}.grd", f"{identifier}_grid")
                self.pymol_out.commands += PyMOLCommands.isosurface(
                    f"{identifier}_grid",
                    f"surface_{identifier}",
                    level=1,
                    color=f"{identifier}_color")

            self.pymol_out.commands += PyMOLCommands.group(
                "feature_grids", self.feature_point_grids.keys())
            self.pymol_out.commands += PyMOLCommands.group(
                "feature_grids",
                [f"surface_{a}" for a in self.feature_point_grids])

            min_value = 0
            surface_dic = {
                self.identifier: {
                    'feature_grids':
                    [f"surface_{g}" for g in self.feature_point_grids.keys()]
                }
            }

            surface_value_dic = {
                self.identifier: {
                    "feature_grids":
                    max([
                        round(g.extrema[1], 1)
                        for g in self.feature_point_grids.values()
                    ])
                }
            }

            self.pymol_out.commands += PyMOLCommands.isoslider(
                surface_dic, surface_value_dic)

        self.pymol_out.write(os.path.join(outdir, fname))