Macs in Chemistry

Insanely great science


Interacting with the IBM RXN API

IBM RXN is a free web service for predicting reactions and retrosynthesis described in a couple of publications, reaction prediction and the multi-step retrosynthesis algorithm

Similar to other work, we treat reaction prediction as a machine translation problem between SMILES strings of reactants-reagents and the products. We show that a multi-head attention Molecular Trans-former model outperforms all algorithms in the literature, achieving a top-1 accuracy above 90% on a common benchmark dataset. Our algorithm requires no handcrafted rules, and accurately predicts subtle chemical transformations. Crucially, our model can accurately estimate its own uncertainty, with an uncertainty score that is 89% accurate in terms of classifying whether a prediction is correct. Furthermore, we show that the model is able to handle inputs without reactant-reagent split and including stereochemistry, which makes our method universally applicable

It can be accessed via a web interface at and it also has a documented API which was updated recently. What caught my eye however was the publication of a python wrapper for the IBM RXN api and it was available on GitHub so I thought I'd try it out., they also proved some useful starting examples.

It can be installed using PIP.

pip install rxn4chemistry

You then need to register and get an api key here

You can then build the wrapper

from rxn4chemistry import RXN4ChemistryWrapper
rxn4chemistry_wrapper = RXN4ChemistryWrapper(api_key=api_key)
# NOTE: you can create a project or set an esiting one using:
# rxn4chemistry_wrapper.set_project('PROJECT_ID')

The website gives instructions on how to access the various api elements, the following Jupyter notebook uses the retrosynthesis option.

The first part of the script imports a set of selected structures from a docking run and displays the structures in a Pandas data frame.

selected_df = PandasTools.LoadSDF(SelectedsdfFilePath,molColName='Molecule', removeHs=True)

We then add a SMILES string we will use to send to the webservice

selected_df['SMILES'] = selected_df.Molecule.apply(Chem.MolToSmiles)

Choose the molecule you want to make, and submit the SMILES for retrosynthesis

response = rxn4chemistry_wrapper.predict_automatic_retrosynthesis(product=theSMILES)

# rerun this until the status is 'SUCCESS', keep in mind the server allows only 5 requests per minute
# and a timeout between consecutive requests of 2 seconds
#If it times out, wait a couple of minutes and rerun.

import time

while True:
        results = rxn4chemistry_wrapper.get_predict_automatic_retrosynthesis_results(response['prediction_id'])
        if results['status'] == 'SUCCESS':
    time.sleep(30) #check every 30 secs

There are limits to how often you can poll the server so we need to include a delay. Once it has completed we can get the results

def collect_reactions(tree):
        reactions = []
        if 'children' in tree and len(tree['children']):
                         '.'.join([node['smiles'] for node in tree['children']]),
                 ), useSmiles=True)
        for node in tree['children']:
        return reactions

And then display the results

for index, path in enumerate(results['retrosynthetic_paths']):
    print('Showing path {} with confidence {}:'.format(index, path['confidence']))
        for reaction in collect_reactions(path):


You can view the whole notebook here

And you can download the Jupyter notebook here

Last Updated 8 April 2020