rdchiral

repository·master·Indexed 41 days ago

https://github.com/connorcoley/rdchiral

A wrapper for RDKit's RunReactants designed to improve stereochemistry handling during chemical reactions. It provides functions like rdchiralRun and rdchiralRunText for processing SMARTS and SMILES strings, and offers a fast C++ implementation called rdchiral_cpp for improved performance. Requires RDKit >= 2019 and Python >= 3.5.

Tokens
1.4K
Snippets
7
Records
9
Agent score
52%

What's inside rdchiral

  1. Prepare the USPTO reaction database

    master

    The extraction process requires the USPTO reaction database. Follow these steps to download and prepare the data:

    1. Create a data/ directory in your project root.
    2. Download the 1976_Sep2016_USPTOgrants_smiles.7z archive.
    3. Extract the archive so that the file 1976_Sep2016_USPTOgrants_smiles.rsmi is located inside the data/ folder.

    Alternatively, you can skip the extraction process by downloading the pre-processed files data/uspto.reactions.json.gz and data/uspto.templates.json.gz directly from ChemRxiv.

    $ mkdir data/ && cd data/
    $ wget -O 1976_Sep2016_USPTOgrants_smiles.7z https://ndownloader.figshare.com/files/8664379
    $ 7z e 1976_Sep2016_USPTOgrants_smiles.7z
    $ cd ../
  2. Run the USPTO template extraction script

    master

    Once the data is prepared, run the clean_and_extract_uspto.py script to clean and extract templates from the USPTO reaction database.

    Performance Note: The script is designed to use all available CPU cores. On a 32-core machine, the process takes approximately 1 hour.

    Outputs: Running the script generates two compressed JSON files in the data/ directory:

    • data/uspto.reactions.json.gz
    • data/uspto.templates.json.gz
    $ python clean_and_extract_uspto.py
  3. Install the fast C++ version (rdchiral_cpp)

    master

    For a ~10x speedup, you can use rdchiral_cpp, which is a fast C++ implementation designed as a drop-in replacement for rdchiral. Install it via conda from the ljn917 channel:

    conda install -c conda-forge -c ljn917 rdchiral_cpp
  4. Run reactions using rdchiralRun

    master

    Use rdchiralRun for efficient stereochemical reaction handling. This function is designed to be used with pre-initialized rdchiralReaction and rdchiralReactants objects.

    Arguments:

    • rxn (rdchiralReaction): An object containing the RDKit reaction and auxiliary stereochemical information.
    • reactants (rdchiralReactants): An object containing the RDKit molecule and auxiliary information.
    • keep_mapnums (bool): Whether to keep atom map numbers in the output.
    • combine_enantiomers (bool): Whether to combine enantiomers into a racemic mixture.
    • return_mapped (bool): If True, returns both the list of outcome SMILES and a dictionary mapping SMILES to their atom-mapped versions and changed atoms.

    Returns:

    • If return_mapped is False: A list of outcome SMILES strings.
    • If return_mapped is True: A tuple containing (list_of_outcomes, mapped_outcomes_dict).
    from rdchiral.initialization import rdchiralReaction, rdchiralReactants
    from rdchiral.main import rdchiralRun
    
    # Pre-initialize for efficiency
    rxn = rdchiralReaction('[C:1][OH:2]>>[C:1][O:2][C]')
    reactants = rdchiralReactants('OCC(=O)OCCCO')
    
    # Run the reaction
    outcomes = rdchiralRun(rxn, reactants)
    
    # Run with detailed mapping information
    outcomes, mapped_outcomes = rdchiralRun(rxn, reactants, return_mapped=True)
  5. Run reactions using rdchiralRunText

    master

    Use rdchiralRunText for a quick way to run a reaction using SMARTS and SMILES strings.

    Note: This method is not recommended for high-performance library applications because it performs slow initialization every time it is called. For repeated use, you should manually initialize rdchiralReaction and rdchiralReactants objects and use rdchiralRun instead.

    Arguments:

    • reaction_smarts (str): The reaction SMARTS string.
    • reactant_smiles (str): The reactant SMILES string.
    • custom_reactant_mapping (bool): Whether to use a custom reactant mapping.
    • **kwargs: Passed through to rdchiralRun.
    from rdchiral.main import rdchiralRunText
    
    reaction_smarts = '[C:1][OH:2]>>[C:1][O:2][C]'
    reactant_smiles = 'OCC(=O)OCCCO'
    
    outcomes = rdchiralRunText(reaction_smarts, reactant_smiles)
    print(outcomes)