python argument error when processing SMILES data in rdkit

713 Views Asked by At
from rdkit import Chem
mol = Chem.MolFromSmiles(r"""
CCC(CC)COC(=O)[C@H](C)N[P@](=O)(OC[C@H]1O[C@](C#N)([C@H](O)[C@@H]1O)C1=CC=C2N1N=CN=C2N)OC1=CC=CC=C1""")
functional_group = Chem.MolFromSmarts('[CX3](=[OX1])[OX2][CX3](=[OX1])')
matches = mol.GetSubstructMatches(functional_group)

This code works, the SMILES string had to be labeled as none code through r""" """.

But how do I make it work with variables instead of r""" """

smile = '[CX3](=[OX1])[OX2][CX3](=[OX1])'
matches = mol.GetSubstructMatches(smile)

this piece of code will return this error

---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-58-947d84e4539c> in <module>
----> 1 matches = mol.GetSubstructMatches(smile)

~\miniconda3\envs\tensorflow\lib\site-packages\rdkit\Chem\Draw\IPythonConsole.py in _GetSubstructMatches(mol, query, *args, **kwargs)
    149 
    150 def _GetSubstructMatches(mol, query, *args, **kwargs):
--> 151   res = mol.__GetSubstructMatches(query, *args, **kwargs)
    152   mol.__sssAtoms = []
    153   if highlightSubstructs:

ArgumentError: Python argument types in
    Mol.GetSubstructMatches(Mol, str)
did not match C++ signature:
    GetSubstructMatches(class RDKit::ROMol self, class RDKit::MolBundle query, struct RDKit::SubstructMatchParameters params)
    GetSubstructMatches(class RDKit::ROMol self, class RDKit::ROMol query, struct RDKit::SubstructMatchParameters params)
    GetSubstructMatches(class RDKit::ROMol self, class RDKit::MolBundle query, bool uniquify=True, bool useChirality=False, bool useQueryQueryMatches=False, unsigned int maxMatches=1000)
    GetSubstructMatches(class RDKit::ROMol self, class RDKit::ROMol query, bool uniquify=True, bool useChirality=False, bool useQueryQueryMatches=False, unsigned int maxMatches=1000)
1

There are 1 best solutions below

0
On

You're getting that error because you're missing a function call of Chem.MolFromSmarts in the second case. Convert the smiles first to a rdkit object and then do the substructure match.

Try this:

mol.GetSubstructMatches(Chem.MolFromSmarts(smile))

Using r""" """ has nothing to do with it.