def test_appendSmallMol(self):
        mol2file = os.path.join(self.dataDir, 'benzamidine.mol2')
        sdffile = os.path.join(self.dataDir, 'fda_drugs_light.sdf')

        lib = SmallMolLib(sdffile)
        sm = SmallMol(mol2file)
        lib.appendSmallMol(sm)

        n_mol2_append = lib.numMols

        self.assertEqual(
            n_mol2_append,
            SDF_N_MOLS + 1,
            msg="The number of molecules in the SmallMolLib is not as expected."
            "The mol2 were not correctly append. ")
Example #2
0
def getChemblSimilarLigandsBySmile(smi, threshold=85, returnSmiles=False):
    """
    Returns a SmallMolLib object of the ligands having a similarity with a smile of at least the specified
    threshold.. This molecules are retrieve from Chembl. It is possible to return also the list smiles.

    Parameters
    ----------
    smi: str
        The smile
    threshold: int
        The threshold value to apply for the similarity search
    returnSmiles: bool
        If True, the list smiles is returned

    Returns
    -------
    sm: moleculekit.smallmol.smallmol.SmallMol
        The SmallMol object

    smiles: str
        The list of smiles

    Example
    -------
    >>> _, smile = getChemblLigandByDrugName('ibuprofen', returnSmile=True)  # doctest: +SKIP
    >>> lib = getChemblSimilarLigandsBySmile(smile)  # doctest: +SKIP
    >>> lib.numMols  # doctest: +SKIP
    4
    >>> lib, smiles = getChemblSimilarLigandsBySmile(smile, returnSmiles=True)  # doctest: +SKIP
    >>> len(smiles)  # doctest: +SKIP
    4
    """
    from moleculekit.smallmol.smallmol import SmallMol
    from moleculekit.smallmol.smallmollib import SmallMolLib

    try:
        from chembl_webresource_client.new_client import new_client
    except ImportError:
        raise ImportError(
            "You need to install the chembl_webresource package to use this function. Try using `conda install "
            "-c chembl chembl_webresource_client`.")

    smi_list = []

    similarity = new_client.similarity
    results = similarity.filter(smiles=smi, similarity=threshold).only(
        ["molecule_structures"])
    results = results.all()
    for r in range(len(results)):
        tmp_smi = results[r]["molecule_structures"]["canonical_smiles"]
        fragments = tmp_smi.split(".")
        fragments_len = [len(fr) for fr in fragments]
        fragment = fragments[fragments_len.index(max(fragments_len))]

        if fragment not in smi_list:
            smi_list.append(fragment)

    lib = SmallMolLib()
    for smi in smi_list:
        lib.appendSmallMol(SmallMol(smi))

    if returnSmiles:
        return lib, smi_list

    return lib