Search code examples
pythonrdkit

When using RDKIT, object is not iterable error appears


I am trying to use tanimoto similarity to compare molecular fingerprints using rdkit. I am trying to compare the two items in list 1 with the one item in list 2. However, I get getting an error. I do not understand it because I have anything named "Mol" in my code. Does anyone have any advice? Thank you

from rdkit import Chem
from rdkit.Chem import rdFingerprintGenerator
from rdkit.Chem import DataStructs
mol1 = ('CCO', 'CCOO')
mol2 = ('CC')
fii = Chem.MolFromSmiles(mol2)
fpgen1 = rdFingerprintGenerator.GetMorganGenerator(radius=2)
fps1 = [fpgen1.GetFingerprint(m) for m in fii]
for m in mol1:
    fi = Chem.MolFromSmiles(m)
    fpgen2 = rdFingerprintGenerator.GetMorganGenerator(radius=2)
    fps2 = [fpgen2.GetFingerprint(m) for m in fi]
    for x in fsp2:
        t = DataStructs.TanimotoSimilarity(fps1, fps2(x))
        print(t)

ERROR: fps1 = [fpgen1.GetFingerprint(m) for m in fii] TypeError: 'Mol' object is not iterable


Solution

    • The Mol object is the name of the rdkit class that is returned when you call Chem.MolFromSmiles, not one of your variable names.
    • The error says that the Mol object is not iterable (it is a single molecule)
    from rdkit import Chem
    from rdkit.Chem import rdFingerprintGenerator
    from rdkit.Chem import DataStructs
    
    smiles1 = ('CCO', 'CCOO')
    smiles2 = ('CC',)
    
    mols1 = [Chem.MolFromSmiles(smi) for smi in smiles1]
    mols2 = [Chem.MolFromSmiles(smi) for smi in smiles2]
    
    # you only need to instantiate the generator once, you can use it for both lists
    fpgen = rdFingerprintGenerator.GetMorganGenerator(radius=2)
    
    fps1 = [fpgen.GetFingerprint(m) for m in mols1]
    fps2 = [fpgen.GetFingerprint(m) for m in mols2]
    
    # if you only care about the single entry in fps2 you can just index it
    for n, fp in enumerate(fps1):
        t = DataStructs.TanimotoSimilarity(fp, fps2[0])
        print(n, t)