Saturday, January 11, 2014

An interesting MCS use case

Inspired by this RDKit-discuss question from Liz Wylie:

In [1]:
from rdkit import Chem
from rdkit.Chem import MCS
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
from rdkit import rdBase
print rdBase.rdkitVersion

Liz wanted to use custom atom types in the MCS code, yet still be able to get a readable SMILES for the MCS.

Here's a demonstration of the standard behavior of the MCS code for Liz's example.

In [2]:
ms = [Chem.MolFromSmiles(x) for x in smis]

Start by defining a simple atom-type hash that combines atomic num and hybridization

In [3]:
def label(a): 
    return 100*int(a.GetHybridization())+a.GetAtomicNum()

The easiest way to use custom atom types is to set (bogus) isotope labels on the atoms in a copy of each molecule:

In [4]:
nms = [Chem.Mol(x) for x in ms]
for nm in nms:
    for at in nm.GetAtoms():

And now run the MCS on the copy using the "istope" mode and print the results:

In [5]:
print mcs.smarts

That's what we asked for, but it's not exactly readable.

We can get to a more readable form in a two step process

  1. Do a substructure match of the MCS onto a copied molecule
  2. Generate SMILES for the original molecule, using only the atoms that matched in the copy.

This works because we know that the atom indices in the copies and the original molecules are the same.

Start by getting the match

In [6]:
mcsp = Chem.MolFromSmarts(mcs.smarts)
match = nms[0].GetSubstructMatch(mcsp)

And now use Chem.MolFragmentToSmiles to generate the actual SMILES. This function generates the SMILES for a user-specified subset of a molecule.

In [7]:
print smi

In [8]:
core = Chem.MolFromSmiles(smi)

Note that in this particular case, the custom atom types don't make a difference in the MCS that is found.

1 comment:

Shalin Siriwaradhana said...

These diagrams are not standard use case scenarios. It would have been even better if there were use case diagrams with actors for better understanding this tutorial. Thanks for posting!