Last active
March 29, 2024 21:00
-
-
Save karpanGit/b973e4638ad991f0bc95c37610c30179 to your computer and use it in GitHub Desktop.
indigo, substructure search with tautomers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### method 1, builtin highlighting | |
# experiment with substructure matching (when tautomers, show the scaffold as in the target) | |
indigo = Indigo() | |
renderer = IndigoRenderer(indigo) | |
indigo.setOption("render-output-format", "png") | |
smiles1 = 'CCC(O)=CCCCC' | |
mol1 = indigo.loadMolecule(smiles1) | |
smiles2 = 'CC(=O)CC' | |
mol2 = indigo.loadQueryMolecule(smiles2) | |
flag = 'TAU' # other flags 'RES', 'TAU', 'TAU INCHI', 'TAU RSMARTS' | |
matcher = indigo.substructureMatcher(mol1, flag) # here we specify the molecule | |
match = matcher.match(mol2) # here we specify the query (substructure) | |
print(flag, match) | |
if match: | |
fig = plt.figure(figsize=(6, 4)) | |
axs = fig.subplots(3, 1) | |
# target molecule | |
img = renderer.renderToBuffer(mol1) | |
img = Image.open(io.BytesIO(img)) | |
axs[0].imshow(img) | |
axs[0].axis('off') | |
axs[0].set_title('target molecule') | |
# query | |
img = renderer.renderToBuffer(mol2) | |
img = Image.open(io.BytesIO(img)) | |
axs[1].imshow(img) | |
axs[1].axis('off') | |
axs[1].set_title('query') | |
# get the first match | |
res = match.highlightedTarget() | |
img = renderer.renderToBuffer(res) | |
img = Image.open(io.BytesIO(img)) | |
axs[2].imshow(img) | |
axs[2].axis('off') | |
axs[2].set_title('result') | |
plt.show() | |
##### method 2, more flexible highlighting | |
# experiment with substructure matching (when tautomers, show the scaffold as in the target) | |
indigo = Indigo() | |
renderer = IndigoRenderer(indigo) | |
indigo.setOption("render-output-format", "png") | |
indigo.setOption("render-highlighted-labels-visible", False) | |
indigo.setOption("render-label-mode", 'terminal-hetero') | |
smiles1 = 'CCC(=O)CCCCC' | |
mol1 = indigo.loadMolecule(smiles1) | |
smiles2 = 'CC(O)=CC' | |
mol2 = indigo.loadQueryMolecule(smiles2) | |
flag = 'TAU' # other flags 'RES', 'TAU', 'TAU INCHI', 'TAU RSMARTS' | |
matcher = indigo.substructureMatcher(mol1, flag) # here we specify the molecule | |
match = matcher.match(mol2) # here we specify the query (substructure) | |
print(flag, match) | |
if match: | |
fig = plt.figure(figsize=(6, 4)) | |
axs = fig.subplots(3, 1) | |
# target molecule | |
img = renderer.renderToBuffer(mol1) | |
img = Image.open(io.BytesIO(img)) | |
axs[0].imshow(img) | |
axs[0].axis('off') | |
axs[0].set_title('target molecule') | |
# query | |
img = renderer.renderToBuffer(mol2) | |
img = Image.open(io.BytesIO(img)) | |
axs[1].imshow(img) | |
axs[1].axis('off') | |
axs[1].set_title('query') | |
# get the first match | |
res = mol1.clone() | |
for atom in mol2.iterateAtoms(): | |
print("atom", atom.index(), "mapped to atom", match.mapAtom(atom).index()) | |
atom = res.getAtom(match.mapAtom(atom).index()) | |
atom.highlight() | |
# for nei in atom.iterateNeighbors(): | |
# if not nei.isPseudoatom() and not nei.isRSite() and nei.atomicNumber() == 1: | |
# nei.highlight() | |
# nei.bond().highlight() | |
for bond in mol2.iterateBonds(): | |
bond = res.getBond(match.mapBond(bond).index()) | |
bond.highlight() | |
img = renderer.renderToBuffer(res) | |
img = Image.open(io.BytesIO(img)) | |
axs[2].imshow(img) | |
axs[2].axis('off') | |
axs[2].set_title('result') | |
plt.show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment