### Import RDKit Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Initializes RDKit and verifies the installed version. ```python import rdkit import rdkit.Chem as Chem import rdkit.Chem.AllChem as AllChem print(rdkit.rdBase.rdkitVersion) from rdkit.Chem import AllChem ``` -------------------------------- ### Install RDChiral using pip Source: https://github.com/connorcoley/rdchiral/blob/master/README.md Install the RDChiral package from PyPI using pip. ```bash pip install rdchiral ``` -------------------------------- ### Iterate and display reaction examples Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Uses a loop to process a range of indices and trigger the show_k function for each. ```python for k in range(1150, 1200): show_k(k) ``` -------------------------------- ### Install RDChiral from Git using pip Source: https://github.com/connorcoley/rdchiral/blob/master/README.md Install the most recent version of RDChiral directly from its git repository using pip. ```bash pip install -e "git://github.com/connorcoley/rdchiral.git#egg=rdchiral" ``` -------------------------------- ### Install rdchiral_cpp using Conda Source: https://github.com/connorcoley/rdchiral/blob/master/README.md Install the fast C++ implementation of RDChiral, rdchiral_cpp, from Anaconda. ```bash conda install -c conda-forge -c ljn917 rdchiral_cpp ``` -------------------------------- ### Import RDChiral Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Imports the main RDChiral modules and verifies the installation path. ```python import rdchiral print(rdchiral.__path__) from rdchiral.main import rdchiralRun, rdchiralRunText, rdchiralReaction, rdchiralReactants ``` -------------------------------- ### Download and Extract USPTO Database Source: https://github.com/connorcoley/rdchiral/blob/master/templates/README.md Use these bash commands to create a data directory, download the USPTO reaction database archive, and extract its contents. Ensure you have 7z installed. ```bash 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 ../ ``` -------------------------------- ### Etherification Reaction with Unspecified Chirality (Different Template) Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Illustrates an etherification reaction with unspecified chirality using a different reaction template. This example further explores RDChiral's behavior in etherification scenarios. ```python # Etherification reaction with unspecified chirality, template could have specified reaction_smarts = '[C:1][CH:2]([CH3:3])[O:4][C:5]>>[C:1][CH:2]([CH3:3])[OH:4].O[C:5]' reactant_smiles = 'CCCC[C@@H](OCC)C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Display RDKit Molecule Object Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Example of an RDKit molecule object representation. ```python ``` ```python ``` ```python ``` -------------------------------- ### Extract Products for Chlorination Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts products for a chlorination reaction. This example demonstrates parsing complex SMILES strings with atom mapping. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example product SMILES string with atom mapping products_smiles = "[S:13]1[CH:15]=[C:9]([CH:2]([CH3:3])[C:4]#[N:5])[N:10]=[CH:12]1" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(products_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Etherification Reaction with Unspecified Chirality Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Demonstrates an etherification reaction where chirality is not specified in the template. This example shows how RDChiral handles reactions involving ether formation. ```python # Etherification reaction with unspecified chirality, template could have specified reaction_smarts = '[CH:1]([CH3:2])=[CH:3]([CH2:4][O:5][C:6])>>[CH:1]([CH3:2])=[CH:3][CH2:4][OH:5].O[C:6]' reactant_smiles = 'C(\C)=C\COCC' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Products for Diazotization Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts products for a specific diazotization reaction. This example shows how to parse complex product SMILES strings. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example product SMILES string with atom mapping products_smiles = "[F:18][C:15]1[CH:16]=[CH:17][C:12]([C@@H:9]2[CH2:10][CH2:11][C@H:6]([N:19]=[N+:20]=[N-:21])[CH2:7][CH2:8]2)=[CH:13][CH:14]=1" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(products_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Alkylation Reaction with Unspecified Chirality (Different Reactant) Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Illustrates an alkylation reaction with unspecified chirality using a different reactant SMILES string. This example highlights how RDChiral handles such cases. ```python reaction_smarts = '[OH:1][CH:2]=[C:3]>>CC[O:1][CH:2]=[C:3]' reactant_smiles = 'O/C=C/CC' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### SN2 Reaction with Specific Stereochemistry Change Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Demonstrates an SN2 reaction where the stereochemistry at the reacting center is specifically altered as defined by the reaction SMARTS. This example shows a change from a specific stereoisomer to another. ```python reaction_smarts = '[C:1][C@H:2]([CH3:3])[I:4]>>[C:1][C@@H:2](Br)[CH3:3]' reactant_smiles = 'CCCC[C@@H](I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Products for Sulfuryl Chloride Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts products for a reaction involving sulfuryl chloride. This example demonstrates parsing complex SMILES strings with atom mapping. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example product SMILES string with atom mapping products_smiles = "[CH2:18]([S:21]([CH2:23][C:24]1[CH:33]=[N+:32]([O-:34])[C:31]2[C:26](=[CH:27][CH:28]=[CH:29][CH:30]=2)[N+:25]=1[O-:35])=[O:22])[CH3:19]" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(products_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Inspect Dataset Samples Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Displays the first three entries of the loaded templates and reactions lists. ```python templates[:3] ``` ```python reactions[:3] ``` -------------------------------- ### Initialize rdchiral Environment Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Configures the system path and imports necessary rdkit and rdchiral modules. ```python import sys, os, gzip, json sys.path = [os.path.dirname(os.getcwd())] + sys.path print(sys.path) import json import rdkit import rdkit.Chem as Chem import rdkit.Chem.AllChem as AllChem print(rdkit.rdBase.rdkitVersion) from rdkit.Chem import AllChem import rdchiral print(rdchiral.__path__) from rdchiral.main import rdchiralRun, rdchiralRunText, rdchiralReaction, rdchiralReactants from rdkit.Chem.Draw import IPythonConsole from rdkit.Chem.Draw.MolDrawing import MolDrawing, DrawingOptions ``` -------------------------------- ### Extract Reactants from Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts the reactant molecules from a chemical reaction definition. This is useful for analyzing the starting materials of a reaction. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example reaction SMARTS string with atom mapping reaction_smarts = "[Br;H0;D1;+0:1]-[CH;@@;D3;+0:3](-[C:2])-[CH;@;D3;+0:5](-[C:6])-[OH;D1;+0:4]>>[BrH;D0;+0:1].[C:2]-[CH;@@;D3;+0:3]1-[O;H0;D2;+0:4]-[CH;@@;D3;+0:5]-1-[C:6]" # Create a reaction object rxn = AllChem.ReactionFromSmarts(reaction_smarts) # Get the reactants (first element of the reaction tuple) reactants = rxn.GetReactants()[0] # Print the molecule object (representation) print(reactants) ``` -------------------------------- ### Prepare a cis alkene from an alkyne Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Defines the reaction SMARTS and applies it to both cis and trans reactant SMILES to demonstrate outcome generation. ```python reaction_smarts = '[C:1]/[CH:2]=[CH:3]\\[C:4]>>[C:1][C:2]#[C:3][C:4]' reactant_smiles = 'CCC/C=C\\CC' # explicit cis show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'CCC/C=C/CC' # explicit trans show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Represent Reactants in a Chemical Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Shows the representation of reactant molecules in a chemical reaction. This is crucial for defining the starting materials for a transformation. ```python Reactants: Cl[CH2:2][CH2:3][CH2:4][C:5]#[C:6][CH2:7][CH2:8][C:9](=[O:11])[CH3:10].C(O)C.[C-:15]#[N:16].[Na+] ``` -------------------------------- ### Configure Reaction Options Source: https://context7.com/connorcoley/rdchiral/llms.txt Control output formats using rdchiralRun parameters like keep_mapnums, combine_enantiomers, and return_mapped. ```python from rdchiral.main import rdchiralRun from rdchiral.initialization import rdchiralReaction, rdchiralReactants reaction_smarts = '[C:1][OH:2]>>[C:1][O:2][C]' rxn = rdchiralReaction(reaction_smarts) reactants = rdchiralReactants('CCO') # Keep atom map numbers in output outcomes_mapped = rdchiralRun(rxn, reactants, keep_mapnums=True) print(outcomes_mapped) # Output: ['[CH3:1][CH2:2][O:3][CH3:900]'] # Disable enantiomer combination (keep separate stereoisomers) outcomes_separate = rdchiralRun(rxn, reactants, combine_enantiomers=False) print(outcomes_separate) # Return mapped outcomes with atoms that changed outcomes, mapped_outcomes = rdchiralRun(rxn, reactants, return_mapped=True) print(outcomes) # Output: ['CCOC'] print(mapped_outcomes) # Output: {'CCOC': ('[CH3:1][CH2:2][O:3][CH3:900]', (3,))} # The tuple contains: (mapped SMILES, tuple of changed atom map numbers) ``` -------------------------------- ### Handling Symmetry in Chiral Precursors Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Illustrates a case where a chiral precursor leads to multiple stereoisomers because the reaction site becomes achiral or forms two stereocenters. ```python # Chirality specified in precursor, but symmetry makes them equivalent reaction_smarts = '[C:1][CH:2]([C:3])[I:4]>>[C:1][C@H:2]([C:3])Br' reactant_smiles = 'CCCCC(I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Reactants for Chlorination Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts reactants for a chlorination reaction. This example shows parsing complex SMILES strings with atom mapping. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example reactant SMILES string with atom mapping reactants_smiles = "Cl[CH:2]([C:4]1[N:5]=CSC=1)[CH3:3].[C-:9]#[N:10].[Na+].[CH3:12][S:13]([CH3:15])=O" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(reactants_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Run Reactions with Pre-initialized Objects Source: https://context7.com/connorcoley/rdchiral/llms.txt Use rdchiralReaction and rdchiralReactants for efficient, repeated execution of the same template across multiple molecules. ```python from rdchiral.main import rdchiralRun from rdchiral.initialization import rdchiralReaction, rdchiralReactants # Pre-initialize reaction template (do once) reaction_smarts = '[C:1][OH:2]>>[C:1][O:2][C]' rxn = rdchiralReaction(reaction_smarts) # Pre-initialize reactants (do for each molecule) reactant_smiles = 'OCC(=O)OCCCO' reactants = rdchiralReactants(reactant_smiles) # Run reaction (can be repeated efficiently) outcomes = rdchiralRun(rxn, reactants) print(outcomes) # Output: ['CCOCC(=O)OCCCO', 'COCCC(=O)OCCCO'] # The same template can be reused with different reactants reactants2 = rdchiralReactants('CCO') outcomes2 = rdchiralRun(rxn, reactants2) print(outcomes2) # Output: ['CCOC'] ``` -------------------------------- ### Define and test reaction SMARTS Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Sets up the reaction SMARTS and iterates through different reactant SMILES to evaluate stereochemical matching. ```python reaction_smarts = '[C:1]/[CH:2]=[CH:3]/[C:4]>>[C:1][C:2]#[C:3][C:4]' reactant_smiles = 'C1(CCC)CCCC=CCC1' # implicit cis show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'CCC/C=C\CC' # explicit cis show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'CCC/C=C/CC' # explicit trans show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Reactants for Diazotization Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts reactants for a specific diazotization reaction. This example shows how to parse complex reactant SMILES strings. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example reactant SMILES string with atom mapping reactants_smiles = "CS(O[C@H:6]1[CH2:11][CH2:10][C@H:9]([C:12]2[CH:17]=[CH:16][C:15]([F:18])=[CH:14][CH:13]=2)[CH2:8][CH2:7]1)(=O)=O.[N-:19]=[N+:20]=[N-:21].[Na+]" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(reactants_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Retro SN2 Stereospecificity Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Explores the retro-direction of an SN2 reaction, highlighting its stereospecific nature. This example shows how the reaction proceeds in reverse and the stereochemical implications. ```python reaction_smarts = '[C:1][C@H:2]([CH3:3])[I:4]>>[C:1][CH:2](Br)[CH3:3]' reactant_smiles = 'CCCC[C@@H](I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Define and Apply Reaction SMARTS Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Use reaction SMARTS to define a chemical transformation and apply it to a reactant SMILES string. This snippet requires RDKit to be installed and imported. ```python reaction_smarts = '[C:1]/[CH:2]=[CH:3]\\[C:4]>>[C:1][C:2]#[C:3][C:4]' reactant_smiles = 'C1(CCC)CCCC=CCC1' # implicit cis show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Load Reaction Datasets Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Loads compressed JSON files containing reaction templates and reaction data. ```python with gzip.open('data/uspto.templates.json.gz') as f: templates = json.load(f) with gzip.open('data/uspto.reactions.json.gz') as f: reactions = json.load(f) ``` -------------------------------- ### Predicting Racemic Products from Chiral Precursors Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Demonstrates how RDChiral predicts racemic products when a chiral precursor's stereochemistry is lost or becomes irrelevant due to symmetry in the reaction. ```python reaction_smarts = '[C:1][CH:2]([CH3:3])[I:4]>>[C:1][C@H:2](Br)[CH3:3]' reactant_smiles = 'CCCCC(I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` ```python reaction_smarts = '[C:1][CH:2]([CH3:3])[I:4]>>[C:1][C@@H:2](Br)[CH3:3]' reactant_smiles = 'CCCCC(I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` ```python reaction_smarts = '[C:1][CH:2]([CH3:3])[I:4]>>[C:1][C@H:2]([CH3:3])Br' reactant_smiles = 'CCCCC(I)C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Reaction Template for Esterification Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb This snippet demonstrates the extraction of a reaction template for an esterification reaction. It shows the transformation of reactants into products. ```text Reactants: [CH2:1]1[O:23][C:4]2([CH2:9][CH2:8][C:7]([CH2:14][C:15]3[CH:20]=[CH:19][CH:18]=[C:17](OC)[CH:16]=3)([C:10](OC)=[O:11])[CH2:6][CH2:5]2)[O:3][CH2:2]1.O1CCCC1.[H-].[Al+3].[Li+].[H-].[H-].[H-].[OH-].[Na+] Products: [CH2:2]1[O:3][C:4]2([CH2:9][CH2:8][C:7]([CH2:14][C:15]3[CH:16]=[CH:17][CH:18]=[CH:19][CH:20]=3)([CH2:10][OH:11])[CH2:6][CH2:5]2)[O:23][CH2:1]1 Extracted template: [C:2]-[CH2;D2;+0:1]-[OH;D1;+0:3].[c:5]:[cH;D2;+0:4]:[c:6]>>C-O-[C;H0;D3;+0:1](-[C:2])=[O;H0;D1;+0:3].C-O-[c;H0;D3;+0:4](:[c:5]):[c:6] ``` -------------------------------- ### Extract Reactants for Sulfuryl Chloride Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts reactants for a reaction involving sulfuryl chloride. This example shows parsing complex SMILES strings with atom mapping. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example reactant SMILES string with atom mapping reactants_smiles = "C(SCC1C=[N+]([O-])C2C(=CC=CC=2)[N+]=1[O-])CC.[CH2:18]([S:21]([CH2:23][C:24]1[CH:33]=[N+:32]([O-:34])[C:31]2[C:26](=[CH:27][CH:28]=[CH:29][CH:30]=2)[N+:25]=1[O-:35])=[O:22])[CH2:19]C" # Create a molecule object from SMILES mol = Chem.MolFromSmiles(reactants_smiles) # Print the molecule object (representation) print(mol) ``` -------------------------------- ### Intramolecular Esterification Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Demonstrates intramolecular esterification using a reaction SMARTS. The reactant is a cyclic ester that should be converted into a carboxylic acid and an alcohol. ```python # Multiple prods? # Intramolecular esterification (in forward direction) reaction_smarts = '[C:1](=[O:3])[O:2][C:4]>>[C:1](=[O:3])[OH:2].O[C:4]' reactant_smiles = 'C1C(=O)OCCC1' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Extract Reaction Template for Ring Opening Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb This snippet demonstrates the extraction of a reaction template involving ring opening. It shows the transformation of a cyclic reactant into an acyclic product. ```text Reactants: C1C2C(=CC=CC=2)CCC21CC[C:13](=[O:16])[CH2:12]C2.[CH2:17]([O:19][C:20]1[CH:21]=[C:22]2[C:26](=[CH:27][CH:28]=1)[C:25](=[O:29])[C:24]1([CH2:34][CH2:33][C:32](=[O:35])[CH2:31][CH2:30]1)[CH2:23]2)[CH3:18].Cl Products: [CH2:13]1[O:16][C:25]2([C:26]3[C:22](=[CH:21][C:20]([O:19][CH2:17][CH3:18])=[CH:28][CH:27]=3)[CH2:23][C:24]32[CH2:34][CH2:33][C:32](=[O:35])[CH2:31][CH2:30]3)[O:29][CH2:12]1 Extracted template: [C:1]-[C;H0;D4;+0:2]1(-[c:4])-[O;H0;D2;+0:5]-[CH2;D2;+0:6]-[CH2;D2;+0:7]-[O;H0;D2;+0:3]-1>>[C:1]-[C;H0;D3;+0:2](=[O;H0;D1;+0:3])-[c:4].[O;H0;D1;+0:5]=[C;H0;D3;+0:6]1-C-C-C2(-C-C-c3:c:c:c:c:c:3-C-2)-C-[CH2;D2;+0:7]-1 ``` -------------------------------- ### Diastereoselective Epoxide to Alkene Conversion Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Shows the preparation of a trans epoxide from a trans alkene using a specific reaction SMARTS. It highlights how RDChiral handles stereochemistry in ring-opening reactions. ```python # preparing a trans epoxide from a trans alkene reaction_smarts = '[c:1]-[CH;@@;D3;+0:2]1-[O;H0;D2;+0]-[CH;@;D3;+0:3]-1-[c:4]>>[c:1]/[CH;D2;+0:2]=[CH;D2;+0:3]/[c:4]' reactant_smiles = 'c1ccccc1[C@H]2[C@H](O2)c1ccccc1' # cis epoxide, should not match show_outcomes(reaction_smarts, reactant_smiles) ``` ```python sep_bar() reactant_smiles = 'c1ccccc1[C@H]2[C@@H](O2)c1ccccc1' # trans epoxide, should match show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### POST /extract_from_reaction Source: https://context7.com/connorcoley/rdchiral/llms.txt Generates reaction SMARTS templates from atom-mapped reaction SMILES, which is essential for building retrosynthetic template databases. ```APIDOC ## POST /extract_from_reaction ### Description Generates reaction SMARTS templates from atom-mapped reaction SMILES. ### Method POST ### Request Body - **reaction** (object) - Required - A dictionary containing '_id', 'reactants' (atom-mapped SMILES), and 'products' (atom-mapped SMILES). ### Response #### Success Response (200) - **template** (object) - Returns a dictionary containing 'products', 'reactants', 'reaction_smarts', 'intra_only', 'dimer_only', 'reaction_id', and 'necessary_reagent'. ``` -------------------------------- ### Visualize Reaction Data Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Displays reactants, products, and the extracted reaction template for a given index. ```python def show_k(k): if 'reaction_smarts' not in templates[k]: return print(k) reaction = reactions[reaction_id_to_index[templates[k]['reaction_id']]] display(Chem.MolFromSmiles(reaction['reactants'])) print('Reactants: {}'.format(reaction['reactants'])) display(Chem.MolFromSmiles(reaction['products'])) print('Products: {}'.format(reaction['products'])) display(AllChem.ReactionFromSmarts(templates[k]['reaction_smarts'])) print('Extracted template: {}'.format(templates[k]['reaction_smarts'])) sep_bar() ``` -------------------------------- ### Test Chiral Transformations with Explicit Cis Double Bond Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb This snippet is used for testing chiral transformations when the molecule explicitly contains a cis double bond and the reaction expects one. Ensure RDKit is installed and imported. ```python from rdkit import Chem from rdkit.Chem import AllChem # Example molecule with an explicit cis double bond mol = Chem.MolFromSmiles('C/C=C/C') print(mol) ``` -------------------------------- ### Reactants and Products of Amide Formation Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Shows the reactants and products for an amide formation reaction. This is useful for understanding the synthesis of amides from amines and activated carboxylic acids. ```python Reactants: [OH:1][C:2]1[CH:8]=[CH:7][C:5]([NH2:6])=[CH:4][CH:3]=1.[C:9](O[C:9](=[O:16])[C:10]1[CH:15]=[CH:14][CH:13]=[CH:12][CH:11]=1)(=[O:16])[C:10]1[CH:15]=[CH:14][CH:13]=[CH:12][CH:11]=1 ``` ```python Products: [C:9]([NH:6][C:5]1[CH:7]=[CH:8][C:2]([OH:1])=[CH:3][CH:4]=1)(=[O:16])[C:10]1[CH:15]=[CH:14][CH:13]=[CH:12][CH:11]=1 ``` -------------------------------- ### Handle Cis/Trans Double Bonds with rdchiral Source: https://context7.com/connorcoley/rdchiral/llms.txt Demonstrates how rdchiral handles cis/trans isomerism in double bonds. The provided SMARTS pattern converts a trans double bond to its cis isomer. ```python trans_smarts = '[C:1]/[CH:2]=[CH:3]/[C:4]>>[C:1]/[CH:2]=[CH:3]\\[C:4]' trans_smiles = 'C/C=C/C' # trans-2-butene outcomes = rdchiralRunText(trans_smarts, trans_smiles) print(outcomes) ``` -------------------------------- ### Reactants and Products of Acylation Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Shows the reactants and products for an acylation reaction. This is useful for understanding the synthesis of acylated compounds. ```python Reactants: [C:1]([NH:4][C:5]1[CH:13]=[CH:12][C:8]([C:9]([OH:11])=O)=[CH:7][CH:6]=1)(=[O:3])[CH3:2].O1CCCC1.C(OC(Cl)=O)C(C)C.[CH:27]1[C:32]([NH2:33])=[CH:31][CH:30]=[C:29]([OH:34])[CH:28]=1 ``` ```python Products: [C:1]([NH:4][C:5]1[CH:6]=[CH:7][C:8]([C:9]([NH:33][C:32]2[CH:27]=[CH:28][C:29]([OH:34])=[CH:30][CH:31]=2)=[O:11])=[CH:12][CH:13]=1)(=[O:3])[CH3:2] ``` -------------------------------- ### Run Reactions with SMILES/SMARTS Text Source: https://context7.com/connorcoley/rdchiral/llms.txt Use rdchiralRunText for quick testing of reactions. Note that this method incurs initialization overhead and is not recommended for large-scale library applications. ```python from rdchiral.main import rdchiralRunText # Simple esterification reaction: alcohol to ether reaction_smarts = '[C:1][OH:2]>>[C:1][O:2][C]' reactant_smiles = 'OCC(=O)OCCCO' # Run reaction and get list of product SMILES outcomes = rdchiralRunText(reaction_smarts, reactant_smiles) print(outcomes) # Output: ['CCOCC(=O)OCCCO', 'COCCC(=O)OCC(=O)OCCCO', ...] # Retrosynthetic template example: converting ketone to alcohol retro_smarts = '[C:1](=[O:2])>>[C:1]([OH:2])' ketone_smiles = 'CC(=O)C' products = rdchiralRunText(retro_smarts, ketone_smiles) print(products) # Output: ['CC(O)C'] ``` -------------------------------- ### Simulate ester hydrolysis with rdchiral Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Executes a reaction SMARTS pattern against various reactant SMILES to generate product outcomes. ```python reaction_smarts = '[C:1](=[O:3])[OH:2]>>[C:1](=[O:3])[O:2]CC' reactant_smiles = 'OC(=O)CCCC[C@H](Cl)C' show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'OC(=O)CCCC[C@@H](Cl)C' show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'OC(=O)C[C@H](CC(=O)OC)C' show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'OC(=O)C[C@H](CC(=O)OCCC)C' show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'OC(=O)CC/C=C(F)\C' show_outcomes(reaction_smarts, reactant_smiles) sep_bar() reactant_smiles = 'OC(=O)CC/C=C(F)/C' show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Configure Python Path Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Adjusts the system path to include the RDChiral directory. ```python import sys, os sys.path = [os.path.dirname(os.getcwd())] + sys.path print(sys.path) ``` -------------------------------- ### Batch Process and Save Templates Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Iterate through a list of indices to display reaction results and save the extracted templates to a JSON file. ```python good_ks = [25, 35, 49, 51, 67, 115, 119, 126, 134, 168, 195, 242, 250, 1076] for k in good_ks: show_k(k) with open('example_template_extractions_good.json', 'w') as fid: json.dump([templates[k] for k in good_ks], fid, indent=4) ``` -------------------------------- ### Extract Reaction Templates with extract_from_reaction Source: https://context7.com/connorcoley/rdchiral/llms.txt Generates reaction SMARTS templates from atom-mapped reaction SMILES. This is useful for building retrosynthetic template databases. Ensure reactions are properly atom-mapped. ```python from rdchiral.template_extractor import extract_from_reaction # Define a reaction with atom-mapped SMILES reaction = { '_id': 'example_001', 'reactants': '[CH3:1][C:2](=[O:3])[OH:4].[CH3:5][OH:6]', # Acetic acid + Methanol 'products': '[CH3:1][C:2](=[O:3])[O:6][CH3:5].[OH2:4]' # Methyl acetate + Water } # Extract template template = extract_from_reaction(reaction) print(template) ``` ```python # The retro template can be used for retrosynthetic prediction from rdchiral.main import rdchiralRunText retro_smarts = template['reaction_smarts'] target = 'CCOC(=O)C' # Ethyl acetate precursors = rdchiralRunText(retro_smarts, target) print(precursors) ``` -------------------------------- ### Extract Cyclic Ether Formation Template Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts the reaction template for the cyclic ether formation. This defines the transformation rule for creating a cyclic ether from specific precursors. ```python Extracted template: [#8:1]-[CH;D3;+0:2](-[C:3])-[O;H0;D2;+0:7]-[C:6]-[C:5]#[C;D1;H1:4]>>[#8:1]-[CH2;D2;+0:2]-[C:3].[C;D1;H1:4]#[C:5]-[C:6]-[OH;D1;+0:7] ``` -------------------------------- ### Extract Reaction Template (Amide Formation) Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Extracts a reaction template for amide formation. This template can be used to identify and process similar reactions involving amines and carboxylic acid derivatives. ```python Extracted template: [O;D1;H0:3]=[C;H0;D3;+0:4](-[c:5])-[NH;D2;+0:1]-[c:2]>>[NH2;D1;+0:1]-[c:2].[O;D1;H0:3]=[C;H0;D3;+0:4](-[c:5])-O-[C;H0;D3;+0:4](=[O;D1;H0:3])-[c:5] ``` -------------------------------- ### Map Reaction IDs Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Creates a dictionary mapping reaction IDs to their index in the reactions list for faster lookup. ```python reaction_id_to_index = {x['_id']: i for i, x in enumerate(reactions)} ``` -------------------------------- ### Import RDKit Drawing Tools Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Imports modules required for rendering molecules in an IPython environment. ```python from rdkit.Chem.Draw import IPythonConsole from rdkit.Chem.Draw.MolDrawing import MolDrawing, DrawingOptions ``` -------------------------------- ### Extract Reaction Template for Nitrile Formation Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb This snippet shows the extraction of a reaction template for forming a nitrile. It highlights the conversion of a molecule into a nitrile product. ```text Reactants: CS(O)(=O)=O.[CH2:6]1[O:24][C:9]2([CH2:14][CH2:13][C:12]([CH2:17][C:18]3[CH:23]=[CH:22][CH:21]=[CH:20][CH:19]=3)([CH2:15]O)[CH2:11][CH2:10]2)[O:8][CH2:7]1.[C-]#[N:26].[K+] Products: [CH2:6]1[O:24][C:9]2([CH2:14][CH2:13][C:12]([CH2:17][C:18]3[CH:23]=[CH:22][CH:21]=[CH:20][CH:19]=3)([C:15]#[N:26])[CH2:11][CH2:10]2)[O:8][CH2:7]1 Extracted template: [C:2]-[C;H0;D2;+0:1]#[N;H0;D1;+0:3]>>O-[CH2;D2;+0:1]-[C:2].[C-]#[N;H0;D1;+0:3] ``` -------------------------------- ### Reactants and Products of Grignard Reaction Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Displays the reactants and products for a Grignard reaction. This helps in understanding the role of the Grignard reagent in forming new carbon-carbon bonds. ```python Reactants: [Mg].[CH:2]1(Br)[CH2:4][CH2:3]1.[CH:6]([O:9][CH2:10][CH2:11][C:12](=[O:14])[CH3:13])([CH3:8])[CH3:7].CC(=O)CC.[Cl-].[NH4+] ``` ```python Products: [CH:2]1([C:12]([OH:14])([CH2:11][CH2:10][O:9][CH:6]([CH3:8])[CH3:7])[CH3:13])[CH2:4][CH2:3]1 ``` -------------------------------- ### Extract Reaction Template for Amine Alkylation Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb This snippet shows the extraction of a reaction template for amine alkylation. It illustrates the process of adding an alkyl group to an amine. ```text Reactants: Cl.[CH2:2]1[C:11]2[C:6](=[CH:7][CH:8]=[CH:9][CH:10]=2)[CH2:5][CH2:4][C:3]21[CH2:16][CH2:15][CH:14]([NH2:17])[CH2:13][CH2:12]2.I[CH2:19][CH2:20][CH2:21][CH2:22][CH2:23]I.C(=O)([O-])[O-].[K+].[K+].C(O)C Products: [CH2:2]1[C:11]2[C:6](=[CH:7][CH:8]=[CH:9][CH:10]=2)[CH2:5][CH2:4][C:3]21[CH2:12][CH2:13][CH:14]([N:17]1[CH2:23][CH2:22][CH2:21][CH2:20][CH2:19]1)[CH2:15][CH2:16]2 Extracted template: [C:5]-[N;H0;D3;+0:6](-[CH2;D2;+0:1]-[C:2])-[CH2;D2;+0:3]-[C:4]>>I-[CH2;D2;+0:1]-[C:2].I-[CH2;D2;+0:3]-[C:4].[C:5]-[NH2;D1;+0:6] ``` -------------------------------- ### Convert cis Epoxide to cis Alkene Source: https://github.com/connorcoley/rdchiral/blob/master/test/Test rdchiral notebook.ipynb Uses a reaction SMARTS to convert a cis epoxide to a cis alkene. The first reactant SMILES is a cis epoxide and should match. The second reactant SMILES is a trans epoxide and should not match. ```python reaction_smarts = '[c:1]-[CH;@@;D3;+0:2]1-[O;H0;D2;+0]-[CH;@@;D3;+0:3]-1-[c:4]>>[c:1]/[CH;D2;+0:2]=[CH;D2;+0:3]\\[c:4]' reactant_smiles = 'c1ccccc1[C@H]2[C@H](O2)c1ccccc1' # cis epoxide, should match show_outcomes(reaction_smarts, reactant_smiles) ``` ```python reaction_smarts = '[c:1]-[CH;@@;D3;+0:2]1-[O;H0;D2;+0]-[CH;@@;D3;+0:3]-1-[c:4]>>[c:1]/[CH;D2;+0:2]=[CH;D2;+0:3]\\[c:4]' reactant_smiles = 'c1ccccc1[C@H]2[C@@H](O2)c1ccccc1' # trans epoxide, should not match show_outcomes(reaction_smarts, reactant_smiles) ``` -------------------------------- ### Reactants and Products of Esterification Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb Shows the reactants and products for an esterification reaction. This is useful for understanding the inputs and outputs of this common organic transformation. ```python Reactants: [Na].[CH:2]([OH:5])([CH3:4])[CH3:3].[Br:6][CH2:7][CH2:8][CH2:9][CH2:10][CH2:11][CH2:12]Br ``` ```python Products: [Br:6][CH2:7][CH2:8][CH2:9][CH2:10][CH2:11][CH2:12][O:5][CH:2]([CH3:4])[CH3:3] ``` -------------------------------- ### Extract Reaction Template from Reactants and Products Source: https://github.com/connorcoley/rdchiral/blob/master/templates/Examine templates.ipynb This snippet shows the extraction of a chemical reaction template from provided reactants and products. It's useful for identifying reaction patterns. ```text Reactants: [C:1]([NH:4][CH:5]1[C:13]2[C:8](=[CH:9][CH:10]=[CH:11][CH:12]=2)[CH2:7][C:6]21[CH2:18][CH2:17][CH:16]([OH:19])[CH2:15][CH2:14]2)(=[O:3])[CH3:2].N1C=CC=CC=1.[CH3:26][S:27](Cl)(=[O:29])=[O:28] Products: [CH3:26][S:27]([O:19][CH:16]1[CH2:17][CH2:18][C:6]2([CH2:7][C:8]3[C:13](=[CH:12][CH:11]=[CH:10][CH:9]=3)[CH:5]2[NH:4][C:1](=[O:3])[CH3:2])[CH2:14][CH2:15]1)(=[O:29])=[O:28] Extracted template: [C:5]-[O;H0;D2;+0:6]-[S;H0;D4;+0:1](-[C;D1;H3:2])(=[O;D1;H0:3])=[O;D1;H0:4]>>Cl-[S;H0;D4;+0:1](-[C;D1;H3:2])(=[O;D1;H0:3])=[O;D1;H0:4].[C:5]-[OH;D1;+0:6] ```