Example #1
0
    def dock(self):
        """
        handle docking run with GOLD
        :return:
        """
        docker = Docker()

        # enables hotspot constraints
        docker.settings = hs_screening.DockerSettings()

        f = os.path.join(self.temp, self.hs_pdb + ".mol2")
        with MoleculeWriter(f) as w:
            w.write(self.protein)

        # setup
        docker.settings.add_protein_file(f)
        docker.settings.binding_site = docker.settings.BindingSiteFromPoint(protein=docker.settings.proteins[0],
                                                                            origin=self.ligand.centre_of_geometry(),
                                                                            distance=12.0)

        docker.settings.fitness_function = 'plp'
        docker.settings.autoscale = 10.
        docker.settings.output_directory = self.temp
        docker.settings.output_file = "docked_ligands.mol2"
        docker.settings.add_ligand_file(self.search_ligands, ndocks=3)

        # constraints
        # docker.settings.add_constraint(
        #     docker.settings.TemplateSimilarityConstraint(type="all", template=self.ligand, weight=150)
        #)

        # extractor = best_volume.Extractor(hr=self.hr, volume=300, mode="global", mvon=False)
        # bv = extractor.extracted_hotspots[0]
        #
        # with hs_io.HotspotWriter(path=os.path.join(self.path, "bv")) as hw:
        #     hw.write(extractor.extracted_hotspots)
        #
        # hs = docker.settings.HotspotHBondConstraint.from_hotspot(protein=docker.settings.proteins[0],
        #                                                          hr=bv,
        #                                                          weight=150,
        #                                                          max_constraints=2)
        #
        # docker.settings.add_constraint(hs)
        # docker.settings.add_apolar_fitting_points(hr=self.hr)
        #
        # mol = Molecule(identifier="constraints")
        # for a in hs.atoms:
        #     mol.add_atom(Atom(atomic_symbol="C",
        #                       atomic_number=14,
        #                       label="Du",
        #                       coordinates=a.coordinates))
        #
        # with MoleculeWriter(os.path.join(self.path, "constaints.mol2")) as w:
        #     w.write(mol)

        # dock
        docker.dock()
        return MoleculeReader(os.path.join(docker.settings.output_directory,
                                           docker.settings.output_file))
Example #2
0
    def run_docking(self):
        """
        Reads in the follow-ups and tries to dock them
        :return: 
        """
        self.get_fragment()
        self.read_followups()

        docker = Docker()
        settings = docker.settings
        tempd = join(self.hotspot_path, "docking_tmp")

        # Get out the reference protein:
        scorer = self.get_scorer_result()
        hs = scorer.get_hotspot()
        prot = hs.protein

        # Change this from DiamondRunner - to save the protein in the results directory
        with MoleculeWriter(join(self.hotspot_path,
                                 "protein.pdb")) as prot_writer:
            prot_writer.write(prot)

        settings.add_protein_file(join(self.hotspot_path, "protein.pdb"))
        settings.binding_site = settings.BindingSiteFromPoint(
            settings.proteins[0], self.reference_fragment.centre_of_geometry(),
            10.0)
        settings.fitness_function = 'plp'
        settings.autoscale = 10.0
        settings.output_directory = tempd
        #settings.output_directory = self.in_dir
        settings.output_file = "docked_ligands.mol2"
        settings.add_ligand_file(join(self.hotspot_path, "follow_ups.mol2"),
                                 ndocks=10)

        # setup constraints
        settings.add_constraint(
            settings.TemplateSimilarityConstraint(
                type="all", template=self.reference_fragment, weight=150))
        results = docker.dock()
        output_file = os.path.join(settings.output_directory,
                                   settings.output_file)
        docked_molecules = [
            m for m in MoleculeReader(os.path.join(tempd, output_file))
        ]

        return docked_molecules
    def run_docking(self):
        #take virtual library and run docking
        print "Run GOLD docking ..."

        docker = Docker()
        settings = docker.settings

        self.start_ligand = io.MoleculeReader(
            os.path.join(self.in_dir, "fragment.mol2"))[0]
        tempd = tempfile.mkdtemp()

        settings.add_protein_file(os.path.abspath(self.protein))
        settings.binding_site = settings.BindingSiteFromPoint(
            settings.proteins[0], self.start_ligand.centre_of_geometry(), 10.0)
        settings.fitness_function = 'plp'
        settings.autoscale = 10.
        settings.output_directory = tempd
        #settings.output_directory = self.in_dir
        settings.output_file = "docked_ligands.mol2"
        settings.add_ligand_file(self.add_ligands, ndocks=10)

        #setup constraints
        settings.add_constraint(
            settings.TemplateSimilarityConstraint(type="all",
                                                  template=self.start_ligand,
                                                  weight=150))

        settings.ProteinFileInfo().fitting_points_file("fname.mol2")
        #feed in layer2
        #self.hotspot_result.predict_protein_hbond_constraints(settings, weight = 100)

        results = docker.dock()

        #fragment = results.ligands[0]
        ligand_reader = results.ligands
        output_file = os.path.join(settings.output_directory,
                                   settings.output_file)
        docked_molecules = [
            m for m in io.MoleculeReader(os.path.join(tempd, output_file))
        ]
        print docked_molecules

        return docked_molecules
Example #4
0
    def write(docker, out_path):

        results = Docker.Results(docker.settings)

        # write ligands
        with MoleculeWriter(os.path.join(out_path, "docked_ligand.mol2")) as w:
            for d in results.ligands:
                w.write(d.molecule)

        # copy ranking file
        copyfile(os.path.join(junk, "bestranking.lst"),
                 os.path.join(out_path, "bestranking.lst"))
    def write(docker, out_path):

        results = Docker.Results(docker.settings)

        # write ligands
        with MoleculeWriter(os.path.join(out_path, "docked_ligand.mol2")) as w:
            for d in results.ligands:
                w.write(d.molecule)

        # copy ranking file
        # in this example, this is the only file we use for analysis. However, other output files can be useful.
        copyfile(os.path.join(junk, "bestranking.lst"),
                 os.path.join(out_path, "bestranking.lst"))
Example #6
0
    def write(docker, out_path):

        results = Docker.Results(docker.settings)

        # write ligands
        with MoleculeWriter(os.path.join(out_path, "docked_ligand.mol2")) as w:
            for d in results.ligands:
                mol = d.molecule
                # for atm in mol.atoms:
                #     if atm.atomic_symbol == "Unknown":
                #         mol.remove_atom(atm)
                w.write(mol)

        # copy ranking file
        # in this example, this is the only file we use for analysis. However, other output files can be useful.
        copyfile(os.path.join(junk, "bestranking.lst"),
                 os.path.join(out_path, "bestranking.lst"))
    def prepare_ligand_for_dock(self):
        """

        :return:
        """
        # TODO: behaviour in case there's several ligands in the file?

        lig = MoleculeReader(self.input_ligand_path)[0]
        # Apply to our supplied ligand the same functions that ligand_prep would to a CSD entry.
        lig.identifier = self.lig_name  # Note -> self.lig_name should be the name of the query ligand, not the reference (unless they are same)

        lig.remove_unknown_atoms()
        lig.assign_bond_types()

        # Standrdises to CSD conventions - not entirely sure if this is necessary.
        lig.standardise_aromatic_bonds()
        lig.standardise_delocalised_bonds()

        # Does it matter what oder you protonate and assign hydrogens in?
        Docker.LigandPreparation()._protonation_rules.apply_rules(
            lig._molecule)
        lig.add_hydrogens()

        if self.minimise_ligand:
            # If the ligand has no 3D coordinates, the minimisation won't work. So let's generate some:
            if not lig.is_3d:
                print(
                    f'Input ligand {lig.identifier} has no 3D coords. Generating 3D coords'
                )
                lig = ccdc_mol_from_smiles(smiles=lig.smiles,
                                           identifier=lig.identifier)

            # Minimise the ligand conformation
            molminimiser = MoleculeMinimiser()
            lig = molminimiser.minimise(lig)

        print('Checking if ligand sucessfully minimised', type(lig))

        # Save the prepped ligand:
        ligwr = MoleculeWriter(self.prepared_ligand_path)
        ligwr.write(lig)
def dock(inputs):
    """
    submit a GOLD API docking calculation using docking constraints automatically generated from the Hotspot API

    :param ligand_path:
    :param out_path:
    :param hotspot:
    :param weight:
    :return:
    """
    def add_ligands(docker, ligand_path):

        with gzip.open(os.path.join(ligand_path, "actives_final.mol2.gz"),
                       'rb') as f_in:
            with open(
                    os.path.join(docker.settings.output_directory,
                                 "actives_final.mol2"), 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)

        with gzip.open(os.path.join(ligand_path, "decoys_final.mol2.gz"),
                       'rb') as f_in:
            with open(
                    os.path.join(docker.settings.output_directory,
                                 "decoys_final.mol2"), 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)

        docker.settings.add_ligand_file(os.path.join(
            docker.settings.output_directory, "actives_final.mol2"),
                                        ndocks=5)

        docker.settings.add_ligand_file(os.path.join(
            docker.settings.output_directory, "decoys_final.mol2"),
                                        ndocks=5)

    def add_protein(docker, hotspot, junk):

        pfile = os.path.join(junk, "protein.mol2")
        with MoleculeWriter(pfile) as w:
            w.write(hotspot.protein)

        docker.settings.add_protein_file(pfile)

    def define_binding_site(docker, ligand_path):

        crystal_ligand = MoleculeReader(
            os.path.join(ligand_path, "crystal_ligand.mol2"))[0]
        docker.settings.binding_site = docker.settings.BindingSiteFromLigand(
            protein=docker.settings.proteins[0], ligand=crystal_ligand)

    def add_hotspot_constraint(docker, hotspot, weight):

        if int(weight) != 0:
            constraints = docker.settings.HotspotHBondConstraint.create(
                protein=docker.settings.proteins[0],
                hr=hotspot,
                weight=int(weight),
                min_hbond_score=0.05,
                max_constraints=1)

            for constraint in constraints:
                docker.settings.add_constraint(constraint)

    def write(docker, out_path):

        results = Docker.Results(docker.settings)

        # write ligands
        with MoleculeWriter(os.path.join(out_path, "docked_ligand.mol2")) as w:
            for d in results.ligands:
                w.write(d.molecule)

        # copy ranking file
        # in this example, this is the only file we use for analysis. However, other output files can be useful.
        copyfile(os.path.join(junk, "bestranking.lst"),
                 os.path.join(out_path, "bestranking.lst"))

    # GOLD docking routine
    ligand_path, out_path, hotspot, weight, search_efficiency = inputs
    docker = Docker()

    # GOLD settings
    docker.settings = DockerSettings()
    docker.settings.fitness_function = 'plp'
    docker.settings.autoscale = search_efficiency
    junk = os.path.join(out_path, "all")
    docker.settings.output_directory = junk

    # GOLD write lots of files we don't need in this example
    if not os.path.exists(junk):
        os.mkdir(junk)
    docker.settings.output_file = os.path.join(junk, "docked_ligands.mol2")

    # read the hotspot
    hotspot = HotspotReader(hotspot).read()
    # for p, g in hotspot.super_grids.items():
    #     hotspot.super_grids[p] = g.max_value_of_neighbours()  # dilation to reduce noise

    add_ligands(docker, ligand_path)
    add_protein(docker, hotspot, junk)
    define_binding_site(docker, ligand_path)
    add_hotspot_constraint(docker, hotspot, weight)
    docker.dock(file_name=os.path.join(out_path, "hs_gold.conf"))
    write(docker, out_path)

    # Clean out unwanted files
    shutil.rmtree(junk)
Example #9
0
    def dock(self):
        """
        Setup and execution of docking run with GOLD.

        NB: Docking Settings class is imported from the Hotspots API rather than Docking API. This is essential for
        running hotspot guided docking.
        :return: a :class:`ccdc.io.MoleculeReader`
        """
        docker = Docker()
        docker.settings = hs_docking.DockerSettings()

        # download protein
        PDBResult(self.args.pdb).download(self.temp)
        protein = Protein.from_file(
            os.path.join(self.temp, self.args.pdb + ".pdb"))
        protein.remove_all_waters()
        protein.remove_all_metals()
        protein.add_hydrogens()
        for l in protein.ligands:
            protein.remove_ligand(l.identifier)

        f = os.path.join(self.temp, self.args.pdb + ".mol2")
        with MoleculeWriter(f) as w:
            w.write(protein)

        # setup
        docker.settings.add_protein_file(f)

        # create binding site from list of residues
        cavs = Cavity.from_pdb_file(
            os.path.join(self.temp, self.args.pdb + ".pdb"))
        cavs[0].to_pymol_file("test.py")
        c = {}
        for i, cav in enumerate(cavs):
            cav.feats = []
            for f in cav.features:
                try:
                    cav.feats.append(f.residue)
                except:
                    continue

            # cav.feats = [f.residue for f in cav.features]
            cav.len = len(cav.feats)
            c.update({cav.len: cav.feats})
            cav.to_pymol_file("{}.py".format(i))

        selected_cavity = max(c.keys())

        docker.settings.binding_site = docker.settings.BindingSiteFromListOfResidues(
            protein=docker.settings.proteins[0], residues=c[selected_cavity])
        docker.settings.fitness_function = 'plp'
        docker.settings.autoscale = 100.
        docker.settings.output_directory = self.temp
        docker.settings.output_file = "docked_ligands.mol2"
        docker.settings.add_ligand_file(self.search_ligands, ndocks=25)

        # constraints
        if self.args.hotspot_guided is True:
            e_settings = result.Extractor.Settings()
            e_settings.mvon = True
            extractor = result.Extractor(self.hr, settings=e_settings)
            bv = extractor.extract_best_volume(volume=300)[0]
            f = hs_utilities.Helper.get_out_dir(
                os.path.join(self.args.path, "best_volume"))

            with hs_io.HotspotWriter(path=f) as hw:
                hw.write(bv)

            constraints = docker.settings.HotspotHBondConstraint.create(
                protein=docker.settings.proteins[0],
                hr=bv,
                weight=5,
                min_hbond_score=0.2,
                max_constraints=5)

            for constraint in constraints:
                docker.settings.add_constraint(constraint)
            docker.settings.generate_fitting_points(hr=bv)

            mol = Molecule(identifier="constraints")
            for constraint in constraints:
                for a in constraint.atoms:
                    mol.add_atom(
                        Atom(atomic_symbol="C",
                             atomic_number=14,
                             label="Du",
                             coordinates=a.coordinates))

            with MoleculeWriter(os.path.join(self.args.path,
                                             "constaints.mol2")) as w:
                w.write(mol)

        docker.dock()
        results = docker.Results(docker.settings)
        return results.ligands
Example #10
0
def dock(inputs):
    """
    submit a GOLD API docking calculation using docking constraints automatically generated from the Hotspot API

    :param ligand_path:
    :param out_path:
    :param hotspot:
    :param weight:
    :return:
    """

    def add_ligands(docker, ligand_path):
        docker.settings.add_ligand_file(os.path.join(ligand_path,
                                                     "actives_final.mol2"),
                                        ndocks=5)

        docker.settings.add_ligand_file(os.path.join(ligand_path,
                                                     "decoys_final.mol2"),
                                        ndocks=5)

    def add_protein(docker, hotspot, junk):

        pfile = os.path.join(junk, "protein.mol2")
        with MoleculeWriter(pfile) as w:
            w.write(hotspot.protein)
        print(pfile)
        docker.settings.add_protein_file(pfile)

    def define_binding_site(docker, ligand_path):

        crystal_ligand = MoleculeReader(os.path.join(ligand_path, "crystal_ligand.mol2"))[0]
        docker.settings.binding_site = docker.settings.BindingSiteFromLigand(protein=docker.settings.proteins[0],
                                                                             ligand=crystal_ligand)

    def add_hotspot_constraint(docker, hotspot, weight):

        if int(weight) != 0:
            constraints = docker.settings.HotspotHBondConstraint.create(protein=docker.settings.proteins[0],
                                                                        hr=hotspot,
                                                                        weight=int(weight),
                                                                        min_hbond_score=0.05,
                                                                        max_constraints=1)

            for constraint in constraints:
                docker.settings.add_constraint(constraint)

    def write(docker, out_path):

        results = Docker.Results(docker.settings)

        # write ligands
        with MoleculeWriter(os.path.join(out_path, "docked_ligand.mol2")) as w:
            for d in results.ligands:
                mol = d.molecule
                # for atm in mol.atoms:
                #     if atm.atomic_symbol == "Unknown":
                #         mol.remove_atom(atm)
                w.write(mol)

        # copy ranking file
        # in this example, this is the only file we use for analysis. However, other output files can be useful.
        copyfile(os.path.join(junk, "bestranking.lst"),
                 os.path.join(out_path, "bestranking.lst"))

    # GOLD docking routine
    ligand_path, out_path, hs_path, weight, search_efficiency = inputs
    docker = Docker()

    # GOLD settings
    docker.settings = DockerSettings()
    docker.settings.fitness_function = 'plp'
    docker.settings.autoscale = search_efficiency
    junk = check_dir(os.path.join(out_path, "all"))
    docker.settings.output_directory = junk

    # GOLD write lots of files we don't need in this example

    docker.settings.output_file = os.path.join(junk, "docked_ligands.mol2")

    # read the hotspot
    with HotspotReader(hs_path) as reader:
        # change if your hotspot is call something else
        hotspot = [h for h in reader.read() if h.identifier == "bestvol"][0]

    # for p, g in hotspot.super_grids.items():
    #     hotspot.super_grids[p] = g.dilate_by_atom()  # dilation to reduce noise

    add_ligands(docker, ligand_path)
    add_protein(docker, hotspot, junk)
    define_binding_site(docker, ligand_path)
    add_hotspot_constraint(docker, hotspot, weight)
    docker.dock(file_name=os.path.join(out_path, "hs_gold.conf"))
    write(docker, out_path)

    # Clean out unwanted files
    shutil.rmtree(junk)
    def dock(self, number_poses=100):
        """

        :return:
        """
        # Set up protein and ligand, in case they need to be

        if self.prepare_protein:
            self.prepare_protein_for_dock()

        if self.prepare_ligand:
            self.prepare_ligand_for_dock()

        reference_ligand = MoleculeReader(self.reference_ligand_path)[0]
        prepared_protein = Protein.from_file(self.prepared_protein_path)
        prepared_ligand = MoleculeReader(self.prepared_ligand_path)[0]

        if self.substructure:
            substr_string = make_substructure_molecule(
                template_mol_path=self.reference_ligand_path,
                query_mol_path=self.prepared_ligand_path)
            substructure = Molecule.from_string(substr_string, format='sdf')
            with MoleculeWriter(
                    str(
                        Path(self.gold_results_directory,
                             f"{self.lig_name}_substructure.sdf"))) as sdfwr:
                sdfwr.write(substructure)

        # Set up the docking run
        docker = Docker()
        docker._conf_file_name = self.conf_file_location
        docker_settings = docker.settings
        # Prevent it from generating a ton of output ligand files - the ranked docks are in 'concat_ranked_docked_ligands.mol2'
        docker_settings._settings.set_delete_rank_files(True)
        docker_settings._settings.set_delete_empty_directories(True)
        docker_settings._settings.set_delete_all_initialised_ligands(True)
        docker_settings._settings.set_delete_all_solutions(True)
        docker_settings._settings.set_delete_redundant_log_files(True)
        docker_settings._settings.set_save_lone_pairs(False)

        # Set up the binding site. Since the sites are based on ragment hits, generate a binding site around the starting hit.
        docker_settings.reference_ligand_file = self.reference_ligand_path
        docker_settings.binding_site = docker_settings.BindingSiteFromLigand(
            prepared_protein, reference_ligand, 6.0)
        # Default distance around ligand is 6 A. Should be ok for small fragments.

        docker_settings.add_protein_file(self.prepared_protein_path)
        docker_settings.diverse_solutions = self.diverse_solutions
        # Try a template similarity restraint:
        #
        if self.substructure:
            try:
                docker_settings.add_constraint(
                    docker_settings.ScaffoldMatchConstraint(substructure))
            except Exception as e:
                docker_settings.add_constraint(
                    docker_settings.TemplateSimilarityConstraint(
                        'all', reference_ligand, weight=75.0))
                txtstr = 'Substructure search failed. Using template similarity'
                log_file = Path(self.results_directory, 'pipeline_error.log')
                log_file.write_text(txtstr)

        else:
            docker_settings.add_constraint(
                docker_settings.TemplateSimilarityConstraint('all',
                                                             reference_ligand,
                                                             weight=150.0))

        # Choose the fitness function: options: ['goldscore', 'chemscore', 'asp', 'plp']. plp is the default.
        docker_settings.fitness_function = 'plp'
        docker_settings.autoscale = self.autoscale
        docker_settings.early_termination = False
        docker_settings.output_directory = self.gold_results_directory
        docker_settings.output_file = str(
            Path(self.results_directory, 'concat_ranked_docked_ligands.mol2'))

        # Add the ligand
        docker_settings.add_ligand_file(
            self.prepared_ligand_path, number_poses
        )  # Second argument determines how many poses are saved

        # Perform the docking:
        gold_result = docker.dock(file_name=self.conf_file_location)
        # pickle.dump(obj=gold_result,file=Path(self.results_directory, 'gold_result').open())
        self.docking_result = gold_result

        return gold_result