Exemplo n.º 1
0
def convert_sdfs_to_PDBs(contnrs, output_folder):
    """This will convert every conformer into a PDB file, which is saved in
       the output_folder.

    :param contnrs: A list of containers (MolContainer.MolContainer).
    :type contnrs: list
    :param output_folder: The name of the output folder.
    :type output_folder: str
    """

    # Go through each container.
    for contnr in contnrs:
        contnr.add_container_properties()

        # Get the molecule name and associated variants.
        name = contnr.name
        mols = contnr.mols

        # Got through the variants.
        for i, m in enumerate(mols):
            pdb_file = "{}{}__input{}__variant{}.pdb".format(
                output_folder + os.sep,
                Utils.slug(name),
                contnr.contnr_idx_orig + 1,
                i + 1,
            )

            # Get the conformers into the rdkit_mol object.
            m.load_conformers_into_rdkit_mol()
            mol = m.rdkit_mol
            if mol == None:
                continue
            else:
                # Write conformers to a PDB file.
                Chem.MolToPDBFile(mol, pdb_file, flavor=32)

                # Add header to PDB file with original SMILES and final SMILES
                printout = "REMARK Original SMILES string: {}\nREMARK Final SMILES string: {}\n".format(
                    m.orig_smi, m.standardize_smiles()
                )
                with open(pdb_file) as f:
                    printout = printout + f.read()
                with open(pdb_file, "w") as f:
                    f.write(printout)
                printout = ""
Exemplo n.º 2
0
def save_to_sdf(contnrs, params, separate_output_files, output_folder):
    """Saves the 3D models to the disk as an SDF file.

    :param contnrs: A list of containers (MolContainer.MolContainer).
    :type contnrs: list
    :param params: The parameters.
    :type params: dict
    :param separate_output_files: Whether save each molecule to a different
       file.
    :type separate_output_files: bool
    :param output_folder: The output folder.
    :type output_folder: str
    """

    # Save an empty molecule with the parameters.
    if separate_output_files == False:
        w = Chem.SDWriter(output_folder + os.sep + "gypsum_dl_success.sdf")
    else:
        w = Chem.SDWriter(output_folder + os.sep + "gypsum_dl_params.sdf")

    m = Chem.Mol()
    m.SetProp("_Name", "EMPTY MOLECULE DESCRIBING GYPSUM-DL PARAMETERS")
    for param in params:
        m.SetProp(param, str(params[param]))
    w.write(m)

    if separate_output_files == True:
        w.flush()
        w.close()

    # Also save the file or files containing the output molecules.
    Utils.log("Saving molecules associated with...")
    for i, contnr in enumerate(contnrs):
        # Add the container properties to the rdkit_mol object so they get
        # written to the SDF file.
        contnr.add_container_properties()

        # Let the user know which molecule you're on.
        Utils.log("\t" + contnr.orig_smi)

        # Save the file(s).
        if separate_output_files == True:
            # sdf_file = "{}{}__{}.pdb".format(output_folder + os.sep, slug(name), conformer_counter)
            sdf_file = "{}{}__input{}.sdf".format(
                output_folder + os.sep,
                Utils.slug(contnr.name),
                contnr.contnr_idx_orig + 1,
            )
            w = Chem.SDWriter(sdf_file)
            # w = Chem.SDWriter(output_folder + os.sep + "output." + str(i + 1) + ".sdf")

        for m in contnr.mols:
            m.load_conformers_into_rdkit_mol()
            w.write(m.rdkit_mol)

        if separate_output_files == True:
            w.flush()
            w.close()

    if separate_output_files == False:
        w.flush()
        w.close()