Exemple #1
0
 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
Exemple #2
0
    def test_only_aromatize_if_possible(self):
        rxn = AllChem.ReactionFromRxnBlock(kekule_rxn)
        # test was for a seg fault
        groups = rxn.RunReactants([Chem.MolFromSmiles("c1ccccc1")])
        print(groups)
        self.assertFalse(len(groups))

        # check normal sanitization
        rdChemReactions.SanitizeRxn(rxn)
        groups = rxn.RunReactants([Chem.MolFromSmiles("c1ccccc1")])
        self.assertTrue(len(groups[0]))

        # now check adjustparams with ONLY aromatize if possible
        rxn = AllChem.ReactionFromRxnBlock(kekule_rxn)
        rdChemReactions.SanitizeRxn(rxn)

        groups = rxn.RunReactants([Chem.MolFromSmiles("c1ccccc1")])
        self.assertTrue(len(groups[0]))
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)
Exemple #4
0
 def test_github_4162(self):
     rxn = rdChemReactions.ReactionFromSmarts(
         "[C:1](=[O:2])-[OD1].[N!H0:3]>>[C:1](=[O:2])[N:3]")
     rxn_copy = rdChemReactions.ChemicalReaction(rxn)
     rdChemReactions.SanitizeRxn(rxn)
     rdChemReactions.SanitizeRxn(rxn_copy)
     pkl = rxn.ToBinary()
     rxn_from_pickle = rdChemReactions.ChemicalReaction(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = pickle.dumps(rxn)
     rxn_from_pickle = pickle.loads(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = rxn_from_pickle.ToBinary()
     rxn_from_pickle = rdChemReactions.ChemicalReaction(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
     pkl = pickle.dumps(rxn_from_pickle)
     rxn_from_pickle = pickle.loads(pkl)
     rdChemReactions.SanitizeRxn(rxn_from_pickle)
Exemple #5
0
 def test_unused_rlabel_in_product(self):
     rxn = AllChem.ReactionFromRxnBlock(unused_rlabel_in_product)
     # test was for a seg fault
     rdChemReactions.SanitizeRxn(rxn)
Exemple #6
0
def is_reaction_ok(rxn):
    """Check is the reaction is synthetically valid"""
    return rdChemReactions.SanitizeRxn(
        rxn) == rdChemReactions.SanitizeFlags.SANITIZE_NONE
Exemple #7
0
    def testRemoveInsaneReagents(self):
        rxndata = "$RXN\nUntitled Document-1\n  ChemDraw10291618492D\n\n  3  1\n$MOL\n\n\n\n  2  1  0  0  0  0  0  0  0  0999 V2000\n    0.4125    0.0000    0.0000 N   0  0  0  0  0  0  0  0  0  3  0  0\n   -0.4125    0.0000    0.0000 R2  0  0  0  0  0  0  0  0  0  2  0  0\n  1  2  1  0        0\nM  END\n$MOL\n\n\n\n  2  1  0  0  0  0  0  0  0  0999 V2000\n   -0.4125    0.0000    0.0000 R1  0  0  0  0  0  0  0  0  0  1  0  0\n    0.4125    0.0000    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0\n  1  2  1  0        0\nM  END\n$MOL\n\n\n\n  2  1  0  0  0  0  0  0  0  0999 V2000\n    0.4125    0.0000    0.0000 N   0  0  0  0  0  0  0  0  0  5  0  0\n   -0.4125    0.0000    0.0000 R4  0  0  0  0  0  0  0  0  0  4  0  0\n  1  2  1  0        0\nM  END\n$MOL\n\n\n\n 14 15  0  0  0  0  0  0  0  0999 V2000\n    0.5072   -0.5166    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    0.5072    0.3084    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.2949   -0.7616    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n    1.7817   -0.0880    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.2967    0.5794    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.5558   -1.5443    0.0000 R1  0  0  0  0  0  0  0  0  0  1  0  0\n   -0.2073    0.7208    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -0.9218    0.3083    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -0.9217   -0.5167    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -0.2073   -0.9292    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -1.6362    0.7208    0.0000 N   0  0  0  0  0  0  0  0  0  3  0  0\n    1.5452    1.3661    0.0000 N   0  0  0  0  0  0  0  0  0  5  0  0\n    2.3507    1.5443    0.0000 R4  0  0  0  0  0  0  0  0  0  4  0  0\n   -2.3507    0.3083    0.0000 R2  0  0  0  0  0  0  0  0  0  2  0  0\n  1  2  2  0        0\n  1  3  1  0        0\n  3  4  1  0        0\n  4  5  1  0        0\n  5  2  1  0        0\n  3  6  1  0        0\n  2  7  1  0        0\n  7  8  2  0        0\n  8  9  1  0        0\n  9 10  2  0        0\n 10  1  1  0        0\n  8 11  1  0        0\n 12 13  1  0        0\n 11 14  1  0        0\n 12  5  1  0        0\nM  END\n"

        rxn = AllChem.ReactionFromRxnBlock(rxndata)
        bbs = []
        r1 = [
            Chem.MolFromSmiles("CCNCC"),
            Chem.MolFromSmiles("NCC"),
        ]
        r2 = [
            Chem.MolFromSmiles("ClC1CCCC1"),
            Chem.MolFromSmiles("ClC1CCCC1Cl"),
        ]
        r3 = [
            Chem.MolFromSmiles("CCNCC"),
            Chem.MolFromSmiles("NCC"),
        ]
        bbs = [r1, r2, r3]

        # nothing matches!
        for i, reagent in enumerate(rxn.GetReactants()):
            for bb in bbs[i]:
                self.assertFalse(bb.HasSubstructMatch(reagent))

        # everything matches - yay sanitization!
        rdChemReactions.SanitizeRxn(rxn)
        for i, reagent in enumerate(rxn.GetReactants()):
            for bb in bbs[i]:
                self.assertTrue(bb.HasSubstructMatch(reagent))

        en = rdChemReactions.EnumerateLibrary(rxn, bbs)
        self.assertTrue(len(en.GetReagents()[0]) == 2)
        self.assertTrue(len(en.GetReagents()[1]) == 2)
        self.assertTrue(len(en.GetReagents()[2]) == 2)

        #####################################################################################
        # Match only at rgroups (ChemDraw style)
        rxn = AllChem.ReactionFromRxnBlock(rxndata)
        expected_matches = [[False, True], [True, True], [False, True]]
        rdChemReactions.SanitizeRxn(
            rxn, params=rdChemReactions.GetChemDrawRxnAdjustParams())
        for i, (reagent, expected) in enumerate(
                zip(rxn.GetReactants(), expected_matches)):
            match = [bb.HasSubstructMatch(reagent) for reagent in bbs[i]]
            self.assertTrue(match, expected)

        # Now try EnumerateLibrary
        en = rdChemReactions.EnumerateLibrary(rxn, bbs)
        self.assertTrue(len(en.GetReagents()[0]) == 1)
        self.assertTrue(len(en.GetReagents()[1]) == 2)
        self.assertTrue(len(en.GetReagents()[2]) == 1)

        #####################################################################################
        # now set the removal options ot only make one product per reagent set
        rxn = AllChem.ReactionFromRxnBlock(rxndata)
        rdChemReactions.SanitizeRxn(rxn)

        opts = rdChemReactions.EnumerationParams()
        opts.reagentMaxMatchCount = 1
        en = rdChemReactions.EnumerateLibrary(rxn, bbs, params=opts)
        self.assertTrue(len(en.GetReagents()[0]) == 1)
        self.assertTrue(len(en.GetReagents()[1]) == 1)
        self.assertTrue(len(en.GetReagents()[2]) == 1)

        #####################################################################################
        # now set the removal options ot only make one product per reagent set
        #  but wt
        rxn = AllChem.ReactionFromRxnBlock(rxndata)
        rdChemReactions.SanitizeRxn(
            rxn, params=rdChemReactions.GetChemDrawRxnAdjustParams())

        opts = rdChemReactions.EnumerationParams()
        opts.reagentMaxMatchCount = 1
        en = rdChemReactions.EnumerateLibrary(rxn, bbs, params=opts)
        self.assertTrue(len(en.GetReagents()[0]) == 1)
        self.assertTrue(len(en.GetReagents()[1]) == 1)
        self.assertTrue(len(en.GetReagents()[2]) == 1)