def _smiles_to_tree_gen(self, smiles):
     assert type(smiles) == list or type(
         smiles) == tuple, "Input must be a list or a tuple"
     for smile in smiles:
         mol = MolFromSmiles(smile)
         assert mol is not None, "SMILES String could not be parsed: " + smile
         try:
             tree = hypergraph_parser(mol)
         except Exception as e:
             print(str(e))
             continue
         yield self.normalize_tree(tree)
示例#2
0
    def test_hypergraph_rpe_parser_bad_smiles(self):
        g = HypergraphGrammar()

        trees = []
        for smile in bad_smiles:
            try:
                trees.append(
                    g.normalize_tree(
                        hypergraph_parser(MolFromSmiles(smile))
                    )
                )
            except (AssertionError, IndexError):
                print('Failed for {}'.format(smile))
                raise
 def raw_strings_to_actions(self, smiles):
     '''
     Convert a list of valid SMILES string to actions
     :param smiles: a list of valid SMILES strings
     :return:
     '''
     assert type(smiles) == list or type(
         smiles) == tuple, "Input must be a list or a tuple"
     actions = []
     for smile in smiles:
         these_actions = []
         mol = MolFromSmiles(smile)
         assert mol is not None, "SMILES String could not be parsed: " + smile
         tree = hypergraph_parser(mol)
         norm_tree = self.normalize_tree(tree)
         these_actions = [rule.rule_id for rule in norm_tree.rules()]
         actions.append(these_actions)
     return actions
示例#4
0
    def test_graph_from_graph_tree_idempotent(self):
        g = HypergraphGrammar()
        g.strings_to_actions(smiles)
        g.calc_terminal_distance()

        tree = g.normalize_tree(hypergraph_parser(MolFromSmiles(smiles1)))

        # The second call here would fail before
        # This was solved by copying in remove_nonterminals where the issue
        # was with mutating the parent tree.node state
        graph1 = graph_from_graph_tree(tree)
        graph2 = graph_from_graph_tree(tree)

        mol1 = to_mol(graph1)
        mol2 = to_mol(graph2)
        recovered_smiles1 = MolToSmiles(mol1)
        recovered_smiles2 = MolToSmiles(mol2)

        self.assertEqual(smiles1, recovered_smiles1)
        self.assertEqual(recovered_smiles1, recovered_smiles2)
示例#5
0
    def test_hypergraph_rpe_parser(self):
        g = HypergraphGrammar()
        g.strings_to_actions(smiles)

        trees = [
            g.normalize_tree(hypergraph_parser(MolFromSmiles(smile)))
            for smile in smiles
        ]

        rule_pairs = extract_popular_hypergraph_pairs(g, trees, 10)

        parser = HypergraphRPEParser(g, rule_pairs)
        collapsed_trees = [parser.parse(smile) for smile in smiles]

        recovered_smiles = []
        for tree in collapsed_trees:
            graph = graph_from_graph_tree(tree)
            mol = to_mol(graph)
            recovered_smiles.append(MolToSmiles(mol))

        self.assertEqual(smiles, recovered_smiles)
示例#6
0
    def test_hypergraph_rpe(self):
        g = HypergraphGrammar()
        g.strings_to_actions(smiles)

        tree = g.normalize_tree(hypergraph_parser(MolFromSmiles(smiles1)))

        num_rules_before = len(g.rules)
        rule_pairs = extract_popular_hypergraph_pairs(g, [tree], 10)
        num_rules_after = len(g.rules)

        tree_rules_before = len(tree.rules())
        collapsed_tree = apply_hypergraph_substitution(g, tree, rule_pairs[0])
        tree_rules_after = len(collapsed_tree.rules())

        graph = graph_from_graph_tree(collapsed_tree)
        mol = to_mol(graph)
        recovered_smiles = MolToSmiles(mol)

        self.assertEqual(smiles1, recovered_smiles)
        self.assertGreater(num_rules_after, num_rules_before)
        self.assertLess(tree_rules_after, tree_rules_before)