def test_sanitize(self): for status, block in test_data: print("*" * 44) rxna = AllChem.ReactionFromRxnBlock(block) rxnb = AllChem.ReactionFromRxnBlock(block) rxna.Initialize() res = rdChemReactions.PreprocessReaction(rxna) print(AllChem.ReactionToRxnBlock(rxna)) if status == "good": self.assertEquals(res, good_res) elif status == "bad": self.assertEquals(res, bad_res) print(">" * 44) rxnb.Initialize() try: rdChemReactions.SanitizeRxn(rxnb) res = rdChemReactions.PreprocessReaction(rxnb) print(AllChem.ReactionToRxnBlock(rxnb)) self.assertEquals(res, good_res) assert not status == "fail" except: print("$RXN Failed") if status == "fail": continue raise
def testPreprocess(self): testFile = os.path.join(RDConfig.RDCodeDir, 'Chem', 'SimpleEnum', 'test_data', 'boronic1.rxn') rxn = rdChemReactions.ReactionFromRxnFile(testFile) rxn.Initialize() res = rdChemReactions.PreprocessReaction(rxn) self.assertEquals(res, (0, 0, 2, 1, (((0, 'halogen.bromine.aromatic'), ), ((1, 'boronicacid'), ))))
def PreprocessReaction(reaction, funcGroupFilename=None, propName='molFileValue'): """ >>> from rdkit.Chem import AllChem >>> testFile = os.path.join(RDConfig.RDCodeDir,'Chem','SimpleEnum','test_data','boronic1.rxn') >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) >>> nWarn 0 >>> nError 0 >>> nReacts 2 >>> nProds 1 >>> reactantLabels (((0, 'halogen.bromine.aromatic'),), ((1, 'boronicacid'),)) If there are functional group labels in the input reaction (via atoms with molFileValue properties), the corresponding atoms will have queries added to them so that they only match such things. We can see this here: >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> r1 = rxn.GetReactantTemplate(0) >>> m1 = Chem.MolFromSmiles('CCBr') >>> m2 = Chem.MolFromSmiles('c1ccccc1Br') These both match because the reaction file itself just has R1-Br: >>> m1.HasSubstructMatch(r1) True >>> m2.HasSubstructMatch(r1) True After preprocessing, we only match the aromatic Br: >>> d = PreprocessReaction(rxn) >>> m1.HasSubstructMatch(r1) False >>> m2.HasSubstructMatch(r1) True We also support or queries in the values field (separated by commas): >>> testFile = os.path.join(RDConfig.RDCodeDir,'Chem','SimpleEnum','test_data','azide_reaction.rxn') >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> reactantLabels = PreprocessReaction(rxn)[-1] >>> reactantLabels (((1, 'azide'),), ((1, 'carboxylicacid,acidchloride'),)) >>> m1 = Chem.MolFromSmiles('CC(=O)O') >>> m2 = Chem.MolFromSmiles('CC(=O)Cl') >>> m3 = Chem.MolFromSmiles('CC(=O)N') >>> r2 = rxn.GetReactantTemplate(1) >>> m1.HasSubstructMatch(r2) True >>> m2.HasSubstructMatch(r2) True >>> m3.HasSubstructMatch(r2) False unrecognized final group types are returned as None: >>> testFile = os.path.join(RDConfig.RDCodeDir,'Chem','SimpleEnum','test_data','bad_value1.rxn') >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) Traceback (most recent call last): File "/usr/prog/python/2.6.6_gnu/lib/python2.6/doctest.py", line 1253, in __run compileflags, 1) in test.globs File "<doctest __main__.PreprocessReaction[36]>", line 1, in <module> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) File "Enumerator.py", line 105, in PreprocessReaction reactantLabels = reaction.AddRecursiveQueriesToReaction(queryDict, propName='molFileValue', getLabels=True) RuntimeError: KeyErrorException One unrecognized group type in a comma-separated list makes the whole thing fail: >>> testFile = os.path.join(RDConfig.RDCodeDir,'Chem','SimpleEnum','test_data','bad_value2.rxn') >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) Traceback (most recent call last): File "/usr/prog/python/2.6.6_gnu/lib/python2.6/doctest.py", line 1253, in __run compileflags, 1) in test.globs File "<doctest __main__.PreprocessReaction[36]>", line 1, in <module> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) File "Enumerator.py", line 105, in PreprocessReaction reactantLabels = reaction.AddRecursiveQueriesToReaction(queryDict, propName='molFileValue', getLabels=True) RuntimeError: KeyErrorException >>> testFile = os.path.join(RDConfig.RDCodeDir,'Chem','SimpleEnum','test_data','bad_value3.rxn') >>> rxn = AllChem.ReactionFromRxnFile(testFile) >>> rxn.Initialize() >>> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) Traceback (most recent call last): File "/usr/prog/python/2.6.6_gnu/lib/python2.6/doctest.py", line 1253, in __run compileflags, 1) in test.globs File "<doctest __main__.PreprocessReaction[36]>", line 1, in <module> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) File "Enumerator.py", line 105, in PreprocessReaction reactantLabels = reaction.AddRecursiveQueriesToReaction(queryDict, propName='molFileValue', getLabels=True) RuntimeError: KeyErrorException >>> rxn = rdChemReactions.ChemicalReaction() >>> rxn.Initialize() >>> nWarn,nError,nReacts,nProds,reactantLabels = PreprocessReaction(rxn) >>> reactantLabels () >>> reactantLabels == () True """ if funcGroupFilename: try: queryDict = Chem.ParseMolQueryDefFile(funcGroupFilename) except Exception: raise IOError('cannot open', funcGroupFilename) return rdChemReactions.PreprocessReaction(reaction, queryDict, propName) return rdChemReactions.PreprocessReaction(reaction, propName=propName)