示例#1
0
 def __new__(cls, molecule, *args, **kwargs):
     if cls.__bb__ is None:
         from .rules import rules
         bb = [smiles(x.strip()) for x in TextIOWrapper(resource_stream(__name__, 'data/building_blocks.smiles'))]
         for b in bb:  # recalculate canonic forms. prevent errors when CGRtools rules set changes.
             b.canonicalize()
         cls.__bb__ = frozenset(str(b) for b in bb)
         cls.__reactors__ = tuple((1., Reactor(x, delete_atoms=True, automorphism_filter=False, one_shot=False))
                                  for x in rules)
     return super().__new__(cls, *args, **kwargs)
示例#2
0
def convert(dct):
    out = {}
    for k, (tuples, value) in dct.items():
        k = smiles(k)
        k.canonicalize()
        new_mols = []
        for prob, mols in tuples:
            synth = []
            for mol in mols:
                new_mol = smiles(mol)
                new_mol.canonicalize()
                synth.append(new_mol)
            rxn = ReactionContainer((k,), synth)
            ext_center = set(rxn.extended_centers_list[0])
            qk = k.substructure(ext_center.intersection(k), as_query=True)
            qsynth = [m.substructure(ext_center.intersection(m), as_query=True) for m in synth]
            template = ReactionContainer((qk,), qsynth)
            new_mols.append((prob, Reactor(template), set(synth)))
        out[k] = (tuple(new_mols), value)
    return out
示例#3
0
 def __new__(cls, molecule, *args, **kwargs):
     if cls.__bb__ is None:
         with resource_stream(__name__, 'data/rules.pkl') as f:
             rules = load(f)
         cls.__reactors__ = [Reactor(x, delete_atoms=True) for x in rules]
         cls.__bb__ = frozenset(
             str(m) for m in SMILESRead(
                 TextIOWrapper(resource_stream(__name__, 'data/bb.smi'))))
         cls.__fragmentor__ = LinearFingerprint(length=4096,
                                                min_radius=2,
                                                max_radius=4,
                                                number_bit_pairs=4)
     if cls.__filter__ is None:
         cls.__filter__ = FilterNet().load_from_checkpoint(
             cls.__filter_path__)
         cls.__filter__.eval()
         cls.__sorter__ = SorterNet(
             (8000, )).load_from_checkpoint(cls.__sorter_path__,
                                            hid=(8000, ))
         cls.__sorter__.eval()
     return super().__new__(cls, molecule, *args, **kwargs)
示例#4
0
    def add_reagent(self, action):
        print('self STATE', self.state)
        action = self.map[action]
        if action == 'next':
            if self.reactions_list:
                if len(self.reactions_list) == 1:
                    reaction = self.reactions_list[0]
                    state = reaction.products[0]
                    reward = reaction.meta['tanimoto']
                    if self.path:
                        self.path[
                            -1] = reaction  # заменяем последнюю реакцию в пути
                    else:
                        self.path.append(reaction)
                    self.reactions_list = self.saved_reactions
                    return state, reward, {
                        'info': 'the last molecule at the list'
                    }
                else:
                    reaction = self.reactions_list.pop(0)
                    state = reaction.products[0]
                    reward = reaction.meta['tanimoto']
                    if self.path:
                        self.path[
                            -1] = reaction  # заменяем последнюю реакцию в пути
                    else:
                        self.path.append(reaction)
                    return state, reward, {}

            else:
                if self.state is None:
                    return None, -1, {'info': 'no reaction products found'}
                else:
                    reward = evaluation(self.state, self.target)
                    return self.state, reward, {
                        'info': 'no another reaction products at the list'
                    }
        if action == 'none':  # однореагентная реакция
            if self.state is None:
                return None, -1, {'info': 'no current molecule'}
            else:
                reactions_list = []
                groups_list = group_list(self.state, self.db)
                rules = reactions_by_fg(groups_list)
                for rule in rules:
                    reactor = Reactor(rule, delete_atoms=True)
                    reaction = next(reactor([self.state]), None)
                    if reaction:
                        # for new_mol in reactions[0].products:
                        # print('!self.state, new_mol!', self.state, new_mol)
                        reactions_list.append((reaction, rule))
        else:
            reactions_list = []
            with db_session:
                reagent = self.db.Molecule[action].structure
            if self.state:
                # print('if self.state')
                groups_list = group_list(self.state, self.db)
                rules = reactions_by_fg(groups_list, single=False)
                # print('RULES', rules)
                for rule in rules:
                    reactor = Reactor(rule, delete_atoms=True)
                    reaction = next(reactor([self.state, reagent]), None)
                    # print('REACTIONS', reactions)
                    if reaction:
                        # for new_mol in reactions[0].products:
                        # print('!(2) self.state, new_mol!', self.state, new_mol)
                        reactions_list.append((reaction, rule))
            else:
                self.state = reagent
                # print('reagent', reagent)
                # print('LOGPPPPPPPPPP', logp(reagent))
                reward = evaluation(self.state, self.target)
                return self.state, reward, {
                    'info': 'the first molecule in the path'
                }
                # groups_list = group_list(reagent, self.db)
                # rules = reactions_by_fg(groups_list)
                # # print('RULES', rules)
                # for rule in rules:
                #     reactor = Reactor(rule, delete_atoms=True)
                #     reaction = next(reactor([reagent]), None)
                #     # print('REACTIONS', reactions)
                #     if reaction:
                #         # for new_mol in reactions[0].products:
                #             # print('!(1) new_mol!', new_mol)
                #         reactions_list.append((reaction, rule))

        if reactions_list:
            reactions_list = list(set(reactions_list))
            react_list = []
            for i in reactions_list:
                product = max(i[0].products,
                              key=lambda x: len(list(x.atoms())))
                meta = {
                    'tanimoto': evaluation(product, self.target),
                    'rule': i[1]
                }
                new_reaction = ReactionContainer(reactants=i[0].reactants,
                                                 products=[product],
                                                 meta=meta)
                react_list.append(new_reaction)
            # print('REACT LIST1', len(reactions_list), reactions_list)
            reactions_list = best_n_molecules(react_list, 10)
            print('REACT LIST 10 best', (len(reactions_list)), reactions_list)
            self.saved_reactions = reactions_list
            if len(reactions_list) > 1:
                reaction = reactions_list.pop(0)
                state = reaction.products[0]
                reward = reaction.meta['tanimoto']
                self.path.append(reaction)
                self.reactions_list = reactions_list
                return state, reward, {}
            else:
                reaction = reactions_list[0]
                state = reaction.products[0]
                reward = reaction.meta['tanimoto']
                self.path.append(reaction)
                self.reactions_list = reactions_list
                return state, reward, {'info': 'the last molecule at the list'}
        else:
            reward = evaluation(self.state, self.target)
            return self.state, reward, {
                'info': 'no new reaction products at the list'
            }