-1

I am trying to convert smi to sdf format using rdkit python library. I am running following line of python code.

def convertir_smi_sdf(file_smi):
    leer = [i for i in open(file_smi)]
    print(f"Total de smi: {len(leer)}")

    cont = 0
    cont_tot = []
    for i in leer:
        nom_mol = i.split()[1]
        smi_mol = i.split()[0]
        mol_smi = Chem.MolFromSmiles(smi_mol)
        Chem.MolToMolFile(mol_smi, f'{nom_mol}.sdf')
        cont += 1
        cont_tot.append(cont)
    print(f"Se ha convertido {cont_tot[-1]} smiles a SDF")

Any help is highly appreciated

I need this to separate this smiles format in distints sdf archives.

Error:

Error

Output:

Output

Max S.
  • 3,704
  • 2
  • 13
  • 34
  • This error simply means that `mol_smi` is `None` which means `smi_mol` is not a valid SMILES string. Add a print statement to print `smi_mol` to manually verify what value are you getting there. – betelgeuse Feb 08 '23 at 06:55
  • @betelgeuse I do it and I get a the entire list of SMILES – Rogelio Gomez Feb 08 '23 at 16:56
  • That's a bug then. Input to `Chem.MolFromSmiles` should be a SMILES string not list – betelgeuse Feb 08 '23 at 16:59
  • @betelgeuse In the description I put a link for a picture of what I got – Rogelio Gomez Feb 08 '23 at 17:15
  • What's the last SMILES string after which error occurs? Is it `C(=O)([C@@H](N)CSSC[C@@H](C(=O)O)N)O`? Add a `print(smi_mol)` statement right before `Chem.MolToMolFile` – betelgeuse Feb 08 '23 at 17:20
  • Again, in the the description i put a link. IKt run about 345 SMILES aprox – Rogelio Gomez Feb 08 '23 at 17:36
  • As I already mentioned, add a `print(smi_mol)` statement before `Chem.MolToMolFile` not after. The idea is to find out which SMILES is causing the error. If you add it after then how will you know since the code will never reach this line. – betelgeuse Feb 08 '23 at 17:41
  • Sorry, I still confuse the words before and after. The last one is Cl[Pt](Cl)([NH4])[NH4], as you can see – Rogelio Gomez Feb 08 '23 at 17:54
  • I added a proper answer below – betelgeuse Feb 08 '23 at 18:29
  • Please [edit] to paste the text used in the image into your question so that it can be read on all devices, quoted, edited, and found through search. As it stands now, [your image makes it hard to answer your question or for people with related issues to find your question](//meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). See the [formatting documentation](/editing-help) for tips to make your text appear nicely without resorting to images. – Stephen Ostermiller Feb 09 '23 at 10:11
  • Since this got a score of -1 here, I'd just like to let you know that [MMSE has an rdkit tag](https://mattermodeling.stackexchange.com/questions/tagged/rdkit) and your question would probably be better received there. You can ask future questions there if you want, and if you want this question migrated you can ping me by typing @NikeDattani and I'll flag it for migration. – Nike Feb 24 '23 at 16:14

1 Answers1

1

These kinds of errors always mean one thing: The SMILES you're inputting is invalid. In your case, you're getting the error because of the SMILES string Cl[Pt](Cl)([NH4])[NH4] which is invalid. See its picture below. Both Nitrogen atoms are forming 5 bonds without any positive charge on them.

Error

When you parse it in RdKit, you'll get a warning like this:

rdkit warning

To deal with this, either fix this SMILES manually or ignore it completely. To ignore it, just pass the argument sanitize=False as below:

mol_smi = Chem.MolFromSmiles(smi_mol, sanitize=False)

Just a warning: by adding sanitize=False, you'll be ignoring all the invalid SMILES.

betelgeuse
  • 1,136
  • 3
  • 13
  • 25