microberx.RuleGenerator
This is a module that provides functions to manipulate chemical reactions and and generate reaction rules.
The module contains the following functions:
decompose_reaction: A fucntion to parse chemical reaction from a string format into a dictionary format.
sanitize_reaction: Sanitizes a chemical reaction by removing stereochemistry, replacing dummy atoms with carbon, and standardizing the molecules.
map_reaction: Maps the atoms of a chemical reaction using either RXNMapper or ReactionDecoder.
set_reaction_ids: Sets the IDs of the reactants and products of a target reaction based on their molecular formulas and a reference reaction.
reverse_reaction: Reverses a chemical reaction by swapping the reactants and products.
generate_single_reactant_reactions: Generates a dictionary of single reactant reactions from a mapped reaction.
generate_rules: Generates a dictionary of rules for a single reactant reaction based on the reacting atoms and the rings.
Reaction: A class to represent a chemical reaction with various attributes and methods.
Classes
A class to represent a chemical reaction with various attributes and methods. |
Functions
|
Decompose a chemical reaction into its individual compounds and stoichiometry. |
|
Sanitizes a chemical reaction by removing stereochemistry, replacing dummy atoms with carbon, and standardizing the molecules. |
|
Sets the IDs of the reactants and products of a target reaction based on their molecular formulas and a reference reaction. |
|
Reverses a chemical reaction by swapping the reactants and products. |
|
Generates a dictionary of single reactant reactions from a mapped reaction. |
|
Generates a dictionary of rules for a single reactant reaction based on the reacting atoms and the rings. |
Module Contents
- microberx.RuleGenerator.decompose_reaction(reaction=None, compounds_map=None)[source]
Decompose a chemical reaction into its individual compounds and stoichiometry.
- Parameters:
reaction (str, optional) – The chemical reaction in string format. It can be specified using various notations: - “<=>” for reversible reactions. - “–>” for irreversible reactions. - “=” for generic reactions.
compounds_map (dict, optional) – A dictionary mapping compound names to their corresponding SMILES representations.
- Returns:
reaction_dict – A dictionary containing the decomposed information of the chemical reaction, including: - “Reaction”: The original input chemical reaction. - “Reversible”: A boolean indicating whether the reaction is reversible. - “LEFT”: A dictionary of reactants with stoichiometry and SMILES. - “RIGHT”: A dictionary of products with stoichiometry and SMILES. - “ReactionSmiles”: The SMILES representation of the entire reaction. - “ReactionNames”: The names of compounds in the reaction.
- Return type:
dict
Examples
>>> compounds_map = {"H2O": "O", "CO2": "O=C=O", "CH4": "C"} >>> reaction_str = "H2O + CO2 <=> CH4 + H2O" >>> reaction_info = decompose_reaction(reaction_str, compounds_map) >>> print(reaction_info) >>> reaction_str = "H2O + CO2 --> CH4 + H2O" >>> reaction_info = decompose_reaction(reaction_str, compounds_map) >>> print(reaction_info)
- microberx.RuleGenerator.sanitize_reaction(target_reaction)[source]
Sanitizes a chemical reaction by removing stereochemistry, replacing dummy atoms with carbon, and standardizing the molecules.
- Parameters:
target_reaction (AllChem.ChemicalReaction) – The input chemical reaction to be sanitized.
- Returns:
fixed_reaction –
- The output chemical reaction after sanitization. The fixed reaction has the following features:
The stereochemistry of the reactants and products is removed, as it may not be relevant or accurate for the reaction.
The dummy atoms (*) in the reactants and products are replaced with carbon atoms (#6), as they may represent unspecified groups or atoms.
The reactants and products are sanitized and standardized using the dm module, which performs operations such as kekulization, neutralization, tautomerization, etc.
The 2D coordinates of the reactants and products are computed using the AllChem.Compute2DCoords function, which may improve the visualization of the reaction.
- Return type:
AllChem.ChemicalReaction
Examples
>>>reaction = AllChem.ReactionFromSmarts(“[OH:1].[C:2]=O>>[C:2][OH:1]”) >>>fixed_reaction = sanitize_reaction(reaction) >>>img = Draw.ReactionToImage(fixed_reaction) >>>img.show()
- microberx.RuleGenerator.set_reaction_ids(reference_reaction, target_reaction, reaction_ids)[source]
Sets the IDs of the reactants and products of a target reaction based on their molecular formulas and a reference reaction.
- Parameters:
reference_reaction (AllChem.ChemicalReaction) – The reference chemical reaction that has the same reactants and products as the target reaction, but in a different order or orientation.
target_reaction (AllChem.ChemicalReaction) – The target chemical reaction that needs to have its IDs set.
reaction_ids (str) – The IDs of the reactants and products of the reference reaction, in the format of ‘R1.R2.>>P1.P2.’, where R1, R2, P1, P2 are the IDs.
- Returns:
target_reaction –
- The target chemical reaction with its IDs set according to the reference reaction and the reaction_ids. The target reaction has the following features:
The reactants and products are the same as the input target reaction, but with an additional property ‘ID’ added to each molecule.
The value of the ‘ID’ property is determined by matching the molecular formula of each molecule in the target reaction with the corresponding molecule in the reference reaction, and then using the value from the reaction_ids string.
The order and orientation of the reactants and products in the target reaction are preserved in the output target reaction.
- Return type:
AllChem.ChemicalReaction
Examples
>>> reference_reaction = Chem.ReactionFromSmarts("[H][C:1]([H])=[O:2]>>[O:2]=[C:1]([H])") >>> target_reaction = Chem.ReactionFromSmarts("[O:2]=[C:1]([H])>>[H][C:1]([H])=[O:2]") >>> reaction_ids = "R1.R2.>>P1.P2." >>> target_reaction_with_ids = set_reaction_ids(reference_reaction, target_reaction, reaction_ids) >>> print(target_reaction_with_ids.GetReactants()[0].GetProp("ID")) >>> print(target_reaction_with_ids.GetProducts()[0].GetProp("ID"))
- microberx.RuleGenerator.reverse_reaction(reaction)[source]
Reverses a chemical reaction by swapping the reactants and products.
- Parameters:
reaction (AllChem.ChemicalReaction) – The input chemical reaction to be reversed.
- Returns:
reversed_reaction –
- The output chemical reaction that is the reverse of the input reaction. The reversed reaction has the following features:
The reactants are the same as the products of the input reaction, but in the opposite order.
The products are the same as the reactants of the input reaction, but in the opposite order.
The atom mapping, bond types, and stereochemistry of the reaction are preserved in the reversed reaction.
- Return type:
AllChem.ChemicalReaction
Examples
>>> reaction = Chem.ReactionFromSmarts("[H][C:1]([H])=[O:2]>>[O:2]=[C:1]([H])") >>> reversed_reaction = reverse_reaction(reaction) >>> print(Chem.MolToSmarts(reversed_reaction))
- microberx.RuleGenerator.generate_single_reactant_reactions(mapped_reaction)[source]
Generates a dictionary of single reactant reactions from a mapped reaction.
- Parameters:
mapped_reaction (AllChem.ChemicalReaction) – The input chemical reaction with atom mapping.
- Returns:
all_unique_reactions – A dictionary of single reactant reactions, keyed by the reactant index and containing the reactant ID and the single reactant reaction object.
- Return type:
dict
Examples
>>> mapped_reaction = Chem.ReactionFromSmarts("[H][C:1]([H])=[O:2]>>[O:2]=[C:1]([H])") >>> single_reactant_reactions = generate_single_reactant_reactions(mapped_reaction) >>> print(single_reactant_reactions["reactantIdx_1"]["ID"]) >>> print(Chem.MolToSmarts(single_reactant_reactions["reactantIdx_1"]["SingleReactantReaction"]))
- microberx.RuleGenerator.generate_rules(single_reactant_reaction)[source]
Generates a dictionary of rules for a single reactant reaction based on the reacting atoms and the rings.
- Parameters:
single_reactant_reaction (AllChem.ChemicalReaction) – The input chemical reaction with one reactant and one or more products.
- Returns:
reaction_rules – A dictionary of rules for the single reactant reaction, keyed by the reactant name and containing the reactant and product SMILES, the product name, and a sub-dictionary of rules keyed by the number of atoms to keep.
- Return type:
dict
Examples
>>> reaction_smiles = "[H][C:1]([H])=[O:2]>>[O:2]=[C:1]([H])" >>> reaction = Chem.ReactionFromSmarts(reaction_smiles) >>> rules = generate_rules(reaction) >>> print(rules["reactant_1"]["ReactantMap"]) >>> print(rules["reactant_1"]["ProductName"]) >>> print(rules["reactant_1"]["ProductMap"]) >>> print(rules["reactant_1"]["SingleReactantRules"][3]) # Rules for reactions with 3 atoms to keep
- class microberx.RuleGenerator.Reaction(reaction_smiles, reaction_ids, reversible=False, mapper='ReactionDecoder')[source]
Bases:
objectA class to represent a chemical reaction with various attributes and methods.
- Parameters:
reaction_smiles (str) –
reaction_ids (str) –
reversible (bool) –
mapper (str) –
- SanitizedReaction[source]
The sanitized version of the input reaction, obtained by calling the SanitizeReaction function.
- Type:
AllChem.ChemicalReaction
- MappedReaction[source]
The mapped version of the sanitized reaction, obtained by calling the MapReaction and SetReactionIds functions.
- Type:
AllChem.ChemicalReaction
- ReversedReaction
The reversed version of the mapped reaction, obtained by calling the ReverseReaction function, or None if the reversible argument is False.
- Type:
AllChem.ChemicalReaction or None
- __init__(reaction_smiles, reaction_ids, reversible, mapper)[source]
Initializes a REACTION object with the given arguments.
- Parameters:
reaction_smiles (str) – The input chemical reaction in SMILES format.
reaction_ids (str) – The IDs of the reactants and products of the input reaction, in the format of ‘R1.R2.>>P1.P2.’, where R1, R2, P1, P2 are the IDs.
reversible (bool) – A flag to indicate whether to generate a reversed reaction or not. Default is False.
mapper (str) – The name of the mapper to use for atom mapping. Either ‘RXNMapper’ or ‘ReactionDecoder’. Default is ‘ReactionDecoder’.
Example
>>> reaction_smiles = 'CC(=O)O.CCOC(=O)C>>CCOC(=O)CC.O' >>> reaction_ids = 'R1.R2>>P1.P2' >>> reaction = REACTION(reaction_smiles, reaction_ids, reversible=True, mapper='RXNMapper') >>> print(reaction.SanitizedReaction) [CH3:1][C:2](=[O:3])[OH:4].[CH3:5][CH2:6][O:7][C:8](=[O:9])[CH3:10]>><[CH3:1][CH2:6][O:7][C:8](=[O:9])[CH2:11][CH3:10].[OH:4][C:2](=[O:3])[H] >>> print(reaction.MappedReaction) [CH3:1][C:2](=[O:3])[OH:4].[CH3:5][CH2:6][O:7][C:8](=[O:9])[CH3:10]>><[CH3:1][CH2:6][O:7][C:8](=[O:9])[CH2:11][CH3:10].[OH:4][C:2](=[O:3])[H] >>> print(reaction.ReversedReaction) [CH3:1][CH2:6][O:7][C:8](=[O:9])[CH2:11][CH3:10].[OH:4][C:2](=[O:3])[H]>><[CH3:1][C:2](=[O:3])[OH:4].[CH3:5][CH2:6][O:7][C:8](=[O:9])[CH3:10]