-2

I have been trying to figure out this error on python using PubChemPy but I'm stuck. I am trying to input a list of chemicals and gen the Canonical Smiles information for a list of about 200 chemicals. This is the code I am using

for i in List_of_Chemicals['Chemical name']:
    prop = pcp.get_properties(['CanonicalSMILES'])

any help would be appreciated

2 Answers2

1

Looks like you are passing a list into get_properties() but it doesn't take a list, but can take several different parameters. Here is an excerpt from the current documentation:

The get_properties function allows the retrieval of specific properties without having to deal with entire compound records. This is especially useful for retrieving the properties of a large number of compounds at once:

p = pcp.get_properties('IsomericSMILES', 'CC', 'smiles', searchtype='superstructure')

https://pubchempy.readthedocs.io/en/latest/guide/properties.html

Your question is lacking quite a bit in terms of useful details but I'd imagine you'd actually want something like:

for i in List_of_Chemicals['Chemical name']:
    prop = pcp.get_properties(i)
dylanjm
  • 2,011
  • 9
  • 21
0

2nd edit: This code goes from a list of names to getting the cid and then the property:

import pubchempy as pcp

# list of chemical names
List_of_Chemicals = ['benzene', 'toluene', '2-nonenal']

for chemical_name in List_of_Chemicals:

    cid=pcp.get_cids(chemical_name)
    prop = pcp.get_properties('CanonicalSMILES', cid)
    print (chemical_name + ' ' + str(prop))

get_properties needs cid as a required argument. You cannot pass in chemical names. So you need an intermediate step to get a list of identifiers corresponding to the names with pcp.get_cids, which I've done in the code above.

  • I don't have cid information I only have the names. – Celin Younan May 11 '21 at 23:33
  • I don't know why it still isn't working. Could it be because I got my chemical names off of a csv data frame and made a new data frame with just the one column with chemical names? – Celin Younan May 12 '21 at 20:02
  • Are you getting an error message? I assume the problem is that the dataframe object has to be converted into a python list object. Is it a pandas dataframe and can I see the code for how you created it? – H. McClelland May 12 '21 at 20:11
  • Yes, it is a pandas data frame. the code I used is – Celin Younan May 12 '21 at 20:15
  • I think https://www.geeksforgeeks.org/how-to-convert-pandas-dataframe-into-a-list/ can tell you what you need to know to convert pandas dataframe into a list, without seeing the dataframe I can't be more specific: – H. McClelland May 12 '21 at 20:18
  • dataset = pd.read_csv ("list_of_chemicals.csv"), df = pd.DataFrame(dataset), List_of_Chemicals = df[["Chemical name"]]. List_of_Chemicals.rename(columns = {'Chemical name' : 'Chemical_Name'}, inplace = True) Then I used the code you suggested. Sorry it won't let me add it as code. – Celin Younan May 12 '21 at 20:19
  • add the line before the for loop, and change the name List_of_Chemicals for the for loop to chemList so it uses the list and not the dataframe: chemList = df['Chemical name'].tolist(), for chem_name in chemList: – H. McClelland May 12 '21 at 20:28
  • I'm sure we can make this work, we're almost there – H. McClelland May 12 '21 at 20:31
  • Thank you so so much! I got it! – Celin Younan May 12 '21 at 20:45
  • Would you mind accepting my answer by clicking on the check box to the left of it? – H. McClelland May 12 '21 at 20:54