Tools available via Python API Wrapper

Predict Retrosynthesis

Predict possible retrosynthetic routes given target molecule

The predict retrosynthesis tool supports both automatic and user-guided retrosynthesis.

Tip

We recommend that user-guided retrosynthesis be done within the RXN for Chemistry site since the GUI workflow is more user-friendly.

The automatic retrosynthesis option is shown below.

To predict a retrosynthesis using default parameters, simply define a molecule in SMILES format and pass it as an argument to the predict_automatic_retrosynthesis function.

smiles = 'CC(=O)NC1=CC=C(Br)C=C1'
predict_automatic_retrosynthesis_response = rxn.predict_automatic_retrosynthesis(product=smiles)

Check on the status of the retrosynthesis prediction. - ‘NEW’: Job is still running. - ‘SUCCESS’: Job is complete.

Rerun the cell below until ‘SUCCESS’ is returned.

predict_automatic_retrosynthesis_results = rxn.get_predict_automatic_retrosynthesis_results(
    predict_automatic_retrosynthesis_response['prediction_id']
)
predict_automatic_retrosynthesis_results['status']

Upon ‘SUCCESS’ we can assess the predicted retrosynthetic paths.

But first we define a function collect_reactions_from_retrosynthesis to parse the results of the retrosynthesis prediction.

from typing import Dict, List
from IPython.display import display
from rdkit import Chem
from rdkit.Chem import AllChem

# To parse results from the predict retrosynthesis tool
def collect_reactions_from_retrosynthesis(tree: Dict) -> List[str]:
    reactions = []
    if 'children' in tree and len(tree['children']):
        reactions.append(
            AllChem.ReactionFromSmarts('{}>>{}'.format(
                '.'.join([node['smiles'] for node in tree['children']]),
                tree['smiles']
            ), useSmiles=True)
        )
    for node in tree['children']:
        reactions.extend(collect_reactions_from_retrosynthesis(node))
    return reactions

We use this helper function to display the different retrosynthesis routes produced by the tool.

for index, tree in enumerate(predict_automatic_retrosynthesis_results['retrosynthetic_paths']):
    print('Showing path {} with confidence {}:'.format(index, tree['confidence']))
    for reaction in collect_reactions_from_retrosynthesis(tree):
        display(Chem.Draw.ReactionToImage(reaction))

Predict Product

Predict the product of a chemical reaction given the starting materials

RXN for Chemistry uses a forward reaction prediction model based on molecular transformers.

To run a forward reaction prediction, use the predict_reaction function. Pass the starting materials in SMILES format, appended with a . character as shown below.

predict_reaction_response = rxn.predict_reaction(
    'BrBr.c1ccc2cc3ccccc3cc2c1'
)

Rerun the cell below until SUCCESS is returned.

predict_reaction_results = rxn.get_predict_reaction_results(
    predict_reaction_response['prediction_id']
)
print(predict_reaction_results["response"]["payload"]["status"])

Define a helper function to parse the output.

def get_reaction_from_smiles(reaction_smiles: str) -> Chem.rdChemReactions.ChemicalReaction:
    return AllChem.ReactionFromSmarts(reaction_smiles, useSmiles=True)

Use the helper function to show the predicted product.

get_reaction_from_smiles(predict_reaction_results['response']['payload']['attempts'][0]['smiles'])

It is also possible to run forward reaction predictions in batches to use the service in a high-throughput fashion. Note that this will not store the information in any project.

predict_rection_batch_response = rxn.predict_reaction_batch(
    precursors_list=['BrBr.c1ccc2cc3ccccc3cc2c1', 'Cl.c1ccc2cc3ccccc3cc2c1']
)

Rerun the cell below until a dictionary is returned.

result = rxn.get_predict_reaction_batch_results(
    predict_rection_batch_response['task_id']
)
print(result)

Then we can display the prediction results for each of the reactions in the batch.

for reaction_prediction in result["predictions"]:
    print(f'Confidence: {reaction_prediction["confidence"]}')
    display(get_reaction_from_smiles(reaction_prediction['smiles']))

It is also possible to predict the top N products for each reaction (in batch).

response = rxn.predict_reaction_batch_topn(
    precursors_lists=[
        ["BrBr", "c1ccc2cc3ccccc3cc2c1"],
        ["BrBr", "c1ccc2cc3ccccc3cc2c1CCO"],
        ["Cl", "CCC(=O)NCCC", "O"],
    ],
    topn=5,
)

Rerun the cell below until SUCCESS is returned.

result = rxn.get_predict_reaction_batch_topn_results(
    response["task_id"]
)
try:
    result["predictions"]
    print("SUCCESS")
except:
    print("Prediction not ready, try again")

Then we list the predicted products, alongside their confidences, for each of the three reactions.

for i, reaction_predictions in enumerate(result['predictions'], 1):
    print(f'Outcomes for reaction no {i}:')
    for j, prediction in enumerate(reaction_predictions["results"], 1):
        product_smiles = ".".join(prediction["smiles"])
        confidence = prediction["confidence"]
        print(f'  Product(s) {j}: {product_smiles}, with confidence {confidence}')

Important

Save your results

The results for batch predictions are only stored temporarily in our databases, so we strongly recommend saving them elsewhere.

Predict Reagents

Predict reagents needed to convert given starting material to given product

This tool predicts which reagents would allow you to convert your starting material to the desired product.

# starting_material_smiles and product_smiles are the two
# arguments to pass to predict_reagents()
starting_material_smiles = 'C1=C(C=O)C=C(Br)C=C1'
product_smiles = 'C1C=CC(Br)=CC=1/C=C/C(O)=O'
response = rxn.predict_reagents(
    starting_material_smiles,
    product_smiles
)
print(response["prediction_id"])

Once the SMILES strings have been passed to the predict_reagents() function, run the code below to check status, until SUCCESS is returned.

result = rxn.get_predict_reagents_results(response["prediction_id"])
print(result["response"]["payload"]["status"])

To display the overall predicted reaction results:

for item in result["response"]["payload"]["sequences"]:
    print(f'Confidence: {item["confidence"]}')
    display(AllChem.ReactionFromSmarts(item["smiles"]))

Plan a Synthesis

Plan a synthesis starting from a target molecule, a retrosynthetic route, or an experimental procedure in text format

Tip

We recommend this tool be used within the RXN for Chemistry GUI instead of via API, since the GUI workflow is more user-friendly.

We will convert the retrosynthesis prediction derived previously (see Predict retrosynthesis.) into a synthesis plan.

Note

The code below may take a few moments to complete.

create_synthesis_from_sequence_response = rxn.create_synthesis_from_sequence(
    sequence_id=predict_automatic_retrosynthesis_results['retrosynthetic_paths'][1]['sequenceId']
)
print(f'Identifier for the synthesis: {create_synthesis_from_sequence_response["synthesis_id"]}')

The code below will allow you get the actions predicted by the AI model.

synthesis_id = create_synthesis_from_sequence_response['synthesis_id']
node_id = rxn.get_node_ids(synthesis_id=synthesis_id)[-1]

Use the code below to print the actions predicted by the AI model.

actions_and_product = rxn.get_reaction_settings(synthesis_id=synthesis_id, node_id=node_id)
print(actions_and_product)
import json

node_actions, product = actions_and_product['actions'], actions_and_product['product']

for index, action in enumerate(node_actions, 1):
    print(f'Action {index}:\n{json.dumps(action, indent=4)}\n')

Note

Changing actions

In the code blow below, the adding acetyl chloride action needs to be changed to not be dropwise since solids are added in pins. We also remove the purify action since it is not currently supported by commonly used robotic hardware.

# update the the action so the solid is not added dropwise
node_actions[3]['content']['dropwise']['value'] = False

# remove the purify action
node_actions.pop(11)

# update the node actions
rxn.update_reaction_settings(synthesis_id=synthesis_id, node_id=node_id, actions=node_actions, product=product)

Atom Mapping

Map atoms from starting materials to product

Atom mapping of a chemical reaction is a mapping between the atoms in the reactant molecules and the atoms in the product molecules.

The predict_reaction_properties function will perform atom mapping as shown in the code below.

reaction_prop = rxn.predict_reaction_properties(
    ['CC(=O)[OH]>>CC(=O)OCC']
)
print(reaction_prop['response']['payload']['content'][0]['value'])

Text to Procedure

Translate your reaction procedures from text to exact steps to follow

Extract machine-readable actions from natural language text descriptions of chemical procedures in paragraph format.

To extract the actions from a recipe, pass the description as a string to the paragraph_to_actions function.

actions_from_procedure_results = rxn.paragraph_to_actions(
    'To a stirred solution of '
    '7-(difluoromethylsulfonyl)-4-fluoro-indan-1-one (110 mg, '
    '0.42 mmol) in methanol (4 mL) was added sodium borohydride '
    '(24 mg, 0.62 mmol). The reaction mixture was stirred at '
    'ambient temperature for 1 hour.'
)

The output is a standardized procedure (returned in an array), which can be a useful starting point for passing information to a robotic platform.

for index, action in enumerate(actions_from_procedure_results['actions'], 1):
    print(f'{index}. {action}')

Reaction Digitization

Convert images of reaction schemes to machine-readable format

Tip

We recommend this tool be used within the RXN for Chemistry site instead of via API (as shown here), since the GUI workflow is more user-friendly.

# Upload the picture and get back the file id
# Use full path and ensure the path is correct
try:
    response = rxn.upload_file('./reaction_scheme.png')
    file_id = response['response']['payload']['id']
    print(file_id)
except FileNotFoundError:
    print("File path provided does not exist")

Use the file_id to start the digitization process. This may take some time.

result = rxn.digitize_reaction(file_id)
result['response']['payload']['reactions']

Display the results in a human-readable way.

for item in result["response"]["payload"]["reactions"]:
    print(item["reactant"]["content"])
    print(f"   --- {item['text']['content']} --->")
    print(item["product"]["content"])
    print()