Ejemplo n.º 1
0
def test_rdkit_mol(ligand_3heg_bax: Ligand):
    mol = ligand_3heg_bax.rdkit_mol

    smiles = MolToSmiles(mol)
    assert 'c' in smiles  # aromatic bonds
    assert 'H' in smiles  # has hydrogens
    assert '=' in smiles  # double bonds
Ejemplo n.º 2
0
def test_add_fragment2db(pdb_3heg, ligand_3heg_bax, fragment25_3heg_bax):
    pdb = pdb_3heg
    ligand = ligand_3heg_bax
    frag_nr = 25
    fragment = fragment25_3heg_bax
    fragment.name = '3heg_BAX_frag25'
    db = FragmentsDb(':memory:')

    add_fragment2db(pdb, ligand, frag_nr, fragment, db)

    row = db['3heg_BAX_frag25']
    assert MolToSmiles(row['mol']) == '*C(=O)NC'
    del row['mol']  # ignore molecule, assert based on smile above
    expected_row = {
        'atom_codes': 'C29,C31,N30,O32',
        'ec_number': None,
        'frag_id': '3heg_BAX_frag25',
        'frag_nr': 25,
        'hash_code': '7bcd2ee0492f5e512ff90954a493d2be',
        'het_chain': 'A',
        'het_code': 'BAX',
        'het_seq_nr': 1,
        'nr_r_groups': 1,
        'pdb_code': '3heg',
        'pdb_title': None,
        'prot_chain': 'A',
        'prot_name': None,
        'rowid': 1,
        'smiles': '*C(=O)NC',
        'uniprot_acc': None,
        'uniprot_name': None,
    }
    assert row == expected_row
Ejemplo n.º 3
0
def rdkit_mmff94_xyz(smiles, **kwargs):
    """
    Returns the string of the XYZ file obtained performing the MMFF94 molecular mechanics optimization of the given
    SMILES using RDKit.
    Writing temporary files in $MM_WORKING_DIR if defined or otherwise in /tmp
    :param smiles: input_SMILES
    :param max_iterations: max number of iterations (default 500)
    :return : XYZ string of optimized geometry, success (whether the MM optimization was successful and the smiles has
    stayed identical after optimization)
    """

    working_dir = os.environ[
        "MM_WORKING_DIR"] if "MM_WORKING_DIR" in os.environ else "/tmp"

    # Converting the molecule to RDKit object
    mol = MolFromSmiles(smiles)
    smi_canon = MolToSmiles(MolFromSmiles(smiles))

    # Setting paths
    filename_smiles = str(os.getpid()) + "_" + smi_to_filename(smi_canon)
    xyz_path = join(working_dir, filename_smiles + '.xyz')
    post_MM_smi_path = join(working_dir, filename_smiles + '.smi')

    # Computing geometry
    try:

        # Adding implicit hydrogens
        mol = AddHs(mol)

        # MM optimization
        EmbedMolecule(mol)

        value = MMFFOptimizeMolecule(mol, maxIters=kwargs["max_iterations"])

        # Success if returned value is null
        success_RDKIT_output = value == 0

        # Computing XYZ from optimized molecule
        xyz_str = MolToXYZBlock(mol)

        # Writing optimized XYZ to file
        with open(xyz_path, "w") as f:
            f.writelines(xyz_str)

        # Success if the optimization has converged and the post MM smiles is identical the pre MM smiles
        success = success_RDKIT_output and check_identical_geometries(
            xyz_path, smi_canon, post_MM_smi_path)

    except Exception as e:
        success = False
        xyz_str = None
    finally:
        # Removing files
        remove_files([post_MM_smi_path, xyz_path])

    return xyz_str, success
Ejemplo n.º 4
0
 def get_smiles(self) -> str:
     """
     Gets the SMILES representation of the current `MolecularGraph`.
     """
     try:
         smiles = MolToSmiles(mol=self.molecule, kekuleSmiles=False)
     except:
         # if molecule is invalid, set SMILES to `None`
         smiles = None
     return smiles
Ejemplo n.º 5
0
def obabel_mmff94_xyz(smiles, **kwargs):
    """
    Returns the string of the XYZ file obtained performing the MMFF94 molecular mechanics optimization of the given
    SMILES using obabel.
    Writing temporary files in $MM_WORKING_DIR if defined or otherwise in /tmp
    :param smiles : input SMILES
    :return : XYZ string of optimized geometry, success (whether the MM optimization was successful and the smiles has
    stayed identical after optimization)
    """

    working_dir = os.environ[
        "MM_WORKING_DIR"] if "MM_WORKING_DIR" in os.environ else "/tmp"

    # Computing RDKIT canonical SMILES
    smi_canon = MolToSmiles(MolFromSmiles(smiles))
    filename_smiles = str(os.getpid()) + "_" + smi_to_filename(smi_canon)

    # Computing files paths
    smi_path = join(working_dir, filename_smiles + ".smi")
    xyz_path = join(working_dir, filename_smiles + ".xyz")
    post_MM_smi_path = join(working_dir, filename_smiles + ".post_MM.smi")

    try:

        # Writing smiles to file
        with open(smi_path, "w") as f:
            f.write(smi_canon)

        # Converting SMILES to XYZ after computing MM (Obabel MMFF94)
        command_obabel = join(os.getenv("OPT_LIBS"),
                              "obabel/openbabel-2.4.1/bin/obabel") + " -ismi " + smi_path \
                               + " -oxyz -O " + xyz_path + " --gen3d"
        os.system(command_obabel + " > /dev/null 2> /dev/null")

        # Reading XYZ string
        with open(xyz_path, "r") as f:
            xyz_str = f.read()

        # Success if the post MM smiles is identical the pre MM smiles
        success = check_identical_geometries(xyz_path, smi_canon,
                                             post_MM_smi_path)

    except Exception as e:
        success = False
        xyz_str = None
    finally:
        # Removing files
        remove_files([smi_path, xyz_path, post_MM_smi_path])

    return xyz_str, success
Ejemplo n.º 6
0
def load_obabel_smi(smi_path):
    """
    Converting a OpenBabel SMILES into a canonical aromatic RDKit SMILES
    :param smi_path:
    :return:
    """

    # Extracting smiles
    with open(smi_path, "r") as f:
        new_smi = f.readline()

        # Loading converged mol
        new_mol = MolFromSmiles(new_smi)

        # Removing stereo information
        RemoveStereochemistry(new_mol)

        # Removing hydrogens
        new_mol = RemoveHs(new_mol)

        # Converting to SMILES
        smi_rdkit = MolToSmiles(MolFromSmiles(MolToSmiles(new_mol)))

        return smi_rdkit
Ejemplo n.º 7
0
    def get_smiles(self):
        """ Gets the SMILES representation of the current `MolecularGraph`.

        The function uses for a given graph:
          `molecule` (rdkit.Chem.Mol) : Molecule object.

        Returns:
          molecule (rdkit.Chem.Mol) :
        """
        try:
            smiles = MolToSmiles(mol=self.molecule, kekuleSmiles=False)
        except:  # if molecule is invalid, set SMILES to `None`
            smiles = None

        return smiles
Ejemplo n.º 8
0
def load_mol(mol, tag):
    smiles = MolToSmiles(mol)

    Chemical = apps.get_model('cspace.Chemical')
    if Chemical.objects.filter(smiles=smiles).count():
        chem = Chemical.objects.get(smiles=smiles)
        chem.tags.add(tag)

        return -1

    props = get_mol_props_dict(mol)
    chem = Chemical(smiles=smiles,
                    mol_weight=ExactMolWt(mol),
                    chem_name=props.get('PUBCHEM_IUPAC_NAME', 'MISSING_NAME'),
                    pubchem_compound_cid=props.get('PUBCHEM_COMPOUND_CID',
                                                   'MISSING_ID'),
                    props_json=json.dumps(props),
                    tpsa=TPSA(mol))

    chem.save()
    chem.tags.add(tag)
    return 1
Ejemplo n.º 9
0
    def extract_descriptors(self, individual):
        """
        Returning the descriptor(s) extracted from the given individual
        :param individual:
        :return:
        """

        if self.descriptor_key == "gen_scaffolds":
            return [
                MolToSmiles(
                    MurckoScaffold.MakeScaffoldGeneric(
                        MolFromSmiles(individual.to_smiles())))
            ]
        elif self.descriptor_key == "ifg":
            curr_ifgs = ifg.identify_functional_groups(
                MolFromSmiles(individual.to_smiles()))
            return list(set([curr_ifg[2] for curr_ifg in curr_ifgs]))
        elif self.descriptor_key == "atoms":
            return list(set(individual.get_atom_types()))
        elif self.descriptor_key == "shg_1":
            return list(extract_shingles(individual, 1))
        elif self.descriptor_key == "checkmol":
            return list(set(extract_checkmol(individual)))
Ejemplo n.º 10
0
def mols2smiles(mols):
    return {MolToSmiles(m) for m in mols}
Ejemplo n.º 11
0
def test_unprotonated_molecule(fragment1_3heg_bax: Fragment):
    mol = fragment1_3heg_bax.unprotonated_molecule()

    expected = 'CNC(=O)c1cc(Oc2ccc(NC(=O)Nc3ccc(Cl)c(C(F)(F)F)c3)cc2)ccn1'
    assert MolToSmiles(mol) == expected
Ejemplo n.º 12
0
 def to_aromatic_smiles(self):
     return MolToSmiles(MolFromSmiles(self.to_smiles()))
Ejemplo n.º 13
0
 def to_smiles(self):
     """
     Returning the SMILES version of the molecule
     :return:
     """
     return MolToSmiles(self.mol_graph)