Visualising the templates behind the individual labels
import json
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import rdChemReactions
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem.Draw import IPythonConsole
from IPython.display import SVG, display, HTML
from rdkit import RDLogger
import rdkit
RDLogger.DisableLog('rdApp.*')
print(rdkit.__version__)
def draw_chemical_reaction(smiles,useSmiles=True, highlightByReactant=False, notesAtomMaps=True, font_scale=1.5):
rxn = rdChemReactions.ReactionFromSmarts(smiles,useSmiles=useSmiles)
trxn = rdChemReactions.ChemicalReaction(rxn)
# move atom maps to be annotations:
if notesAtomMaps:
for m in trxn.GetReactants():
moveAtomMapsToNotes(m)
for m in trxn.GetProducts():
moveAtomMapsToNotes(m)
d2d = rdMolDraw2D.MolDraw2DSVG(800,300)
d2d.drawOptions().annotationFontScale=font_scale
d2d.DrawReaction(trxn,highlightByReactant=highlightByReactant)
d2d.FinishDrawing()
return d2d.GetDrawingText()
def moveAtomMapsToNotes(m):
for at in m.GetAtoms():
if at.GetAtomMapNum():
at.SetProp("atomNote",str(at.GetAtomMapNum()))
with open('../data/uspto_1k_TPL/individual_files/label_template.json', 'r') as f:
labels_templates = json.load(f)
for label in range(len(labels_templates)):
tpl = labels_templates[str(label)]
display(HTML(f'<p style="font-weight: bold;">TPL label: {label}</p>'))
display(SVG(draw_chemical_reaction(tpl, useSmiles=False, notesAtomMaps=False)))
print(tpl)