Get and Draw molecular fragment with user defined path #RDKit #memo

Chemical structure can represent as graph atoms as nodes, bonds as edges. And some compound fingerprints based on the graph. These algorithm extract fragment from molecule given radius of center atom.

To get atom environment of RadiusN, FindAtomEnvironmentOfRadiusN method or rdkit is useful.

It can get fragment from molecule with given radius of specific atom.

So I would like to show how to do it. At first import libraries.

from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem.Draw import IPythonConsole
from rdkit.Chem import AllChem

Then define sample molecule and getSubmolRadN.

def getSubmolRadN(mol, radius):
    for atom in atoms:
        env=Chem.FindAtomEnvironmentOfRadiusN(mol, radius, atom.GetIdx())
        submol=Chem.PathToSubmol(mol, env, atomMap=amap)
        subsmi=Chem.MolToSmiles(submol, rootedAtAtom=amap[atom.GetIdx()], canonical=False)
        submols.append(Chem.MolFromSmiles(subsmi, sanitize=False))
    return submols
mol = Chem.MolFromSmiles('n1c(N3CCN(C(=O)C)CC3)c(C#N)cc2cc(OC)ccc12')

FindAtomEnvironmentOfRadiusN can get env around atom of radius N and then, call to PathToSubmol with env to get submol of the env. After that to keep query atom as atom index 0, I call MolFromSmiles with rootedAtAtom option.

Now ready, let’s check the submols.

submols = getSubmolRadN(mol, 1)
Draw.MolsToGridImage(submols, highlightAtomLists=[[0] for _ in range(len(submols))], molsPerRow=5)
submols = getSubmolRadN(mol, 3)
Draw.MolsToGridImage(submols, highlightAtomLists=[[0] for _ in range(len(submols))], molsPerRow=5)

It seems work.

Here is a whole code of the post.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
gist code

Submols are rdkit mol object so I can use the object any tasks.

Also as reader knows that current RDKit has cool method such as DrawMorganBits, DrawMorganEnv etc. It is useful to check bit information of molecule. Details are provided in original document.


Published by iwatobipen

I'm medicinal chemist in mid size of pharmaceutical company. I love chemoinfo, cording, organic synthesis, my family.

One thought on “Get and Draw molecular fragment with user defined path #RDKit #memo

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: