def clean_reaction(self): """This function takes reaction smiles of the form: a.b.c>d.e.f>g.h and changes them to the form: a.b.c.d.e.f>>g.h Unmapped reactants are moved to the agents to create the form: mapped_reactants>unmapped_reactants>product Returns: rsmi (str): Reaction SMILES in the form a.b.c.d.e.f>>g.h clean_rsmi (str): Reaction SMILES in the form mapped_reactants>unmapped_reactants>product """ #Split into Reactants and Products split_reaction = self.rsmi.split(">") #Get reactant and product smiles string with . separators reactants = split_reaction[0] self.agents = split_reaction[1] self.products = split_reaction[2] if len(self.agents) == 0: self.reactants = reactants else: self.reactants = '.'.join([reactants, self.agents]) self.rsmi = '>>'.join([self.reactants, self.products]) reaction = rdChemReactions.ReactionFromSmarts(self.rsmi) reaction.RemoveUnmappedReactantTemplates() self.clean_rsmi = rdChemReactions.ReactionToSmiles(reaction) return self.rsmi, self.clean_rsmi
def getReactionFromSmilesFile(smartsfile, rxnfile): with open(smartsfile) as handler: smarts = handler.readline() rxn = rdChemReactions.ReactionFromSmarts(smarts) smi = rdChemReactions.ReactionToSmiles(rxn) mdl = rdChemReactions.ReactionToRxnBlock(rxn) with open(rxnfile, 'w') as handler: handler.write(mdl) return storeReaction(smi, rxnfile), smi
def hash_template(self, template): """ Hashes the reaction template. Parameters: template (str): template SMIRKS pattern Returns: hashed_template (str): sha224 hash of the template SMIRKS pattern """ template = rdChemReactions.ReactionFromSmarts(template) rdChemReactions.RemoveMappingNumbersFromReactions(template) template = rdChemReactions.ReactionToSmiles(template) self.hashed_template = hashlib.sha224(template.encode('utf8')).hexdigest() return self.hashed_template
def validate_reaction_smiles(reaction_smiles): """Validates reaction SMILES. Args: reaction_smiles: Text reaction SMILES. Returns: Updated reaction SMILES. Raises: ValueError: If the reaction contains errors. """ try: reaction = rdChemReactions.ReactionFromSmarts(reaction_smiles, useSmiles=True) rdChemReactions.SanitizeRxn(reaction) except ValueError as error: raise ValueError( f'reaction contains errors: {reaction_smiles}') from error _, num_errors = reaction.Validate() if num_errors: raise ValueError(f'reaction contains errors: {reaction_smiles}') return rdChemReactions.ReactionToSmiles(reaction)
def getReaction(rfile): rxn = rdChemReactions.ReactionFromRxnFile(rfile) smi = rdChemReactions.ReactionToSmiles(rxn) return storeReaction(smi, rfile), smi