• Stars
    star
    167
  • Rank 225,293 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python wrapper for the IBM RXN for Chemistry API

Python wrapper for the IBM RXN for Chemistry API

Actions tests PyPI version License: MIT Binder

logo

A python wrapper to access the API of the IBM RXN for Chemistry website.

Install

From PYPI:

pip install rxn4chemistry

Or directly from the repo:

pip install git+https://github.com/rxn4chemistry/rxn4chemistry.git

Usage

By default, the wrapper connects to the https://rxn.res.ibm.com server. This can be overriden by setting an environment variable. To set a different url, simply do:

export RXN4CHEMISTRY_BASE_URL="https://some.other.rxn.server"

The base url can be directly set when instantiating the RXN4ChemistryWrapper (this will overwrite the environment variable):

api_key = 'API_KEY'
from rxn4chemistry import RXN4ChemistryWrapper

rxn4chemistry_wrapper = RXN4ChemistryWrapper(api_key=api_key, base_url='https://some.other.rxn.server')
# or set it afterwards
# rxn4chemistry_wrapper = RXN4ChemistryWrapper(api_key=api_key)
# rxn4chemistry_wrapper.set_base_url('https://some.other.rxn.server')

Create a project

Get your API key from here and build the wrapper:

api_key = 'API_KEY'
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')
rxn4chemistry_wrapper.create_project('test_wrapper')
print(rxn4chemistry_wrapper.project_id)

Reaction prediction

Running a reaction prediction is as simple as:

response = rxn4chemistry_wrapper.predict_reaction(
    'BrBr.c1ccc2cc3ccccc3cc2c1'
)
results = rxn4chemistry_wrapper.get_predict_reaction_results(
    response['prediction_id']
)
print(results['response']['payload']['attempts'][0]['smiles'])

Extracting actions from a paragraph describing a recipe

Extract the actions from a recipe:

results = rxn4chemistry_wrapper.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.'
)
print(results['actions'])

Retrosynthesis prediction

Predict a retrosynthetic pathway given a product:

response = rxn4chemistry_wrapper.predict_automatic_retrosynthesis(
    'Brc1c2ccccc2c(Br)c2ccccc12'
)
results = rxn4chemistry_wrapper.get_predict_automatic_retrosynthesis_results(
    response['prediction_id']
)
print(results['status'])
# NOTE: upon 'SUCCESS' you can inspect the predicted retrosynthetic paths.
print(results['retrosynthetic_paths'][0])

See here for a more comprehensive example.

Biocatalysed retrosynthesis prediction

Predict a biocatalysed retrosynthetic pathway given a product by specifying the model trained on biocatalysed reactions:

response = rxn4chemistry_wrapper.predict_automatic_retrosynthesis(
    'OC1C(O)C=C(Br)C=C1', ai_model='enzymatic-2021-04-16'
)
results = rxn4chemistry_wrapper.get_predict_automatic_retrosynthesis_results(
    response['prediction_id']
)
print(results['status'])
# NOTE: upon 'SUCCESS' you can inspect the predicted retrosynthetic paths.
print(results['retrosynthetic_paths'][0])

Create a synthesis and start it on the robot (or simulator)

Create a synthesis from a retrosynthesis sequence:

# Each retrosynthetic path predicted has a unique sequence_id that can
# be used to create a new synthesis
response = rxn4chemistry_wrapper.create_synthesis_from_sequence(
    sequence_id=results['retrosynthetic_paths'][0]['sequenceId']
)
print(response['synthesis_id'])

# get the entire list of actions for the entire synthesis, as well as a tree representation
synthesis_tree, ordered_tree_nodes, ordered_list_of_actions = rxn4chemistry_wrapper.get_synthesis_plan(
    synthesis_id=response['synthesis_id']
)
for action in ordered_list_of_actions:
    print(action)

synthesis_status_result = rxn4chemistry_wrapper.start_synthesis(
    synthesis_id=response['synthesis_id']
)
print(synthesis_status_result['status'])

synthesis_status_result = rxn4chemistry_wrapper.get_synthesis_status(
    synthesis_id=response['synthesis_id']
)
print(synthesis_status_result['status'])

Forward prediction in batch

It is possible to run a batch of forward reaction predictions without linking them to a project:

response = rxn4chemistry_wrapper.predict_reaction_batch(precursors_list=['BrBr.c1ccc2cc3ccccc3cc2c1', 'Cl.c1ccc2cc3ccccc3cc2c1']*5)
# wait for the predictions to complete
time.sleep(2)
print(rxn4chemistry_wrapper.get_predict_reaction_batch_results(response["task_id"]))

NOTE: the results for batch prediction are not stored permanently in our databases, so we strongly recommend to save them since they will expire.

Prediction of multiple reaction outcomes (in batch)

It is also possible to predict multiple forward reaction prediction outcomes in batch:

response = rxn4chemistry_wrapper.predict_reaction_batch_topn(
    precursors_lists=[
        ["BrBr", "c1ccc2cc3ccccc3cc2c1"],
        ["BrBr", "c1ccc2cc3ccccc3cc2c1CCO"],
    ],
    topn=3,
)
# wait for the predictions to complete
time.sleep(2)
print(rxn4chemistry_wrapper.get_predict_reaction_batch_topn_results(response["task_id"]))

NOTE: the results for batch prediction are not stored permanently in our databases, so we strongly recommend to save them since they will expire.

Enable logging

Logging by the library is disabled by default as it may interfere with programmatic uses.

In the very top of the rxn4chemistry_tour.ipynb example notebook you can see a line that enables all logging in the notebook.

import logging
logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(message)s')

This may also enable logging from other libraries. If you wish to selectively enable the logs from rxn4chemistry, consider something like this:

import logging
logger = logging.getLogger("rxn4chemistry")
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(levelname)s : %(message)s'))
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

Examples

To learn more see the examples.

Documentation

The documentation is hosted here using GitHub pages.

More Repositories

1

rxnmapper

RXNMapper: Unsupervised attention-guided atom-mapping. Code complementing our Science Advances publication on "Extraction of organic chemistry grammar from unsupervised learning of chemical reactions" (https://advances.sciencemag.org/content/7/15/eabe4166).
Python
279
star
2

rxnfp

Reaction fingerprints, atlases and classification. Code complementing our Nature Machine Intelligence publication on "Mapping the space of chemical reactions using attention-based neural networks" (http://rdcu.be/cenmd).
HTML
153
star
3

rxn_yields

Code complementing our manuscript on the prediction of chemical reaction yields (https://iopscience.iop.org/article/10.1088/2632-2153/abc81d) and data augmentation strategies (https://doi.org/10.26434/chemrxiv.13286741).
Jupyter Notebook
97
star
4

biocatalysis-model

RXN for biochemical reactions
Python
60
star
5

paragraph2actions

Extraction of action sequences from experimental procedures
Python
36
star
6

rxnaamapper

Reaction SMILES-AA mapping via language modelling
Python
29
star
7

disconnection_aware_retrosynthesis

Python
28
star
8

smiles2actions

Action sequence prediction for arbitrary chemical equations
Python
25
star
9

rxn-chemutils

Chemistry-related Python utilities used in the RXN universe
Python
20
star
10

rxn-ir-to-structure

Predicting molecular structure from Infrared (IR) Spectra
Python
13
star
11

nmr-to-structure

Prediction molecular structure from NMR spectra
Python
11
star
12

rxn-reaction-preprocessing

Preprocessing of datasets of chemical reactions: standardization, filtering, augmentation, tokenization, etc.
Python
9
star
13

rxn-utilities

General Python utilities commonly used in the RXN universe
Python
7
star
14

rxn-standardization

Standardizing chemical compounds with language models
Python
7
star
15

rxn_cluster_token_prompt

Code to train high diversity retrosynthesis models with cluster token prompt
Python
5
star
16

multimodal-spectroscopic-dataset

Code for generation and benchmarks of the Multimodal Spectroscopic Dataset
Python
4
star
17

sac-action-extraction

Extraction of single-atom catalyst synthesis actions with transformers.
Python
3
star
18

rxn-onmt-models

Training of OpenNMT-based RXN models
Python
2
star
19

rxn-models

Open-source RXN models page
2
star
20

rxn-models-for-polymerization

RXN models for polymerization
1
star
21

rxn-metrics

Metrics for RXN models
Python
1
star