Python Forum
Converting Smiles to Fingerprint with pandas iterrows/apply-function: - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Converting Smiles to Fingerprint with pandas iterrows/apply-function: (/thread-19559.html)



Converting Smiles to Fingerprint with pandas iterrows/apply-function: - Jompie96 - Jul-04-2019

Im trying to iterate my function over a column but it doesn't work properly. Could you guys show me where I have to correct my code?

Thats my Code
def SmilesToFPS(smiles):
    mol = Chem.MolFromSmiles(smiles)
    fps = FingerprintMols.FingerprintMol(mol)
    #fpSize=1024, minPath=1, maxPath=7,
     #    bitsPerHash=2, useHs=True, 
      #    tgtDensity=0.0, minSize=0, 
       #    branchedPaths=True, useBondOrder=True, 
        #      atomInvariants=0, fromAtoms=0, atomBits=None, bitInfo=None)
    return print(fps.ToBitString()) 

for index, row in df_3.iterrows():

#for i, number in df_3['SmilesCode'].iteritems(): 
#this doesn't work.thats why I used .iterrows
    try:
        fingerprints = SmilesToFPS(smiles)

    except:
        fingerprints = 'ERROR'
    print('\r', row['SmilesCode'], fingerprints, end='')

    smiles_list.append(row['SmilesCode'])
    fingerprint_list.append(fingerprints)

df_4 = pd.DataFrame({'SmilesCode' : smiles_list, 'Fingerprints' : fingerprint_list})
I expect a Bitstring for every SmilesCode but I only get Errors for every row/SmilesCode. The defined function does work tho but not if I use it to iterate over my desired column!
If I use the apply() function from pandas:

df_modDfObj = df_3.apply(SmilesToFPS)
I get this error code:

ArgumentError: Python argument types in rdkit.Chem.rdmolops.RDKFingerprint(NoneType, int, int, int, int, int, float, int) did not match C++ signature: RDKFingerprint(RDKit::ROMol mol, unsigned int minPath=1, unsigned int maxPath=7, unsigned int fpSize=2048, unsigned int nBitsPerHash=2, bool useHs=True, double tgtDensity=0.0, unsigned int minSize=128, bool branchedPaths=True, bool useBondOrder=True, boost::python::api::object atomInvariants=0, boost::python::api::object fromAtoms=0, boost::python::api::object atomBits=None, boost::python::api::object bitInfo=None)

Im trying to iterate my function over a column but it doesn't work properly. Could you guys show me where I have to correct my code?

Thank you guys for your help!