Ejemplo n.º 1
0
def test_product_against_rules():
    target_smiles = request.form['target_smiles']
    smarts = request.form['smarts']

    try:
        smarts_list = yaml.load(smarts, Loader=yaml.FullLoader)
    except:
        return jsonify(result={'status': 'fail'})

    try:
        rxn_list = []
        for sma in smarts_list:
            rxn_list.append(rdchiralReaction(sma))
    except:
        return jsonify(result={'status': 'fail'})

    try:
        network = Network()

        if request.form['combine_enantiomers'] == 'true':
            network.settings['combine_enantiomers'] = True
        else:
            network.settings['combine_enantiomers'] = False

        network.rxn_obj.rxns = {'test_smarts': rxn_list}
        network.rxns = {'test_smarts': rxn_list}
        network.generate(target_smiles, 1)
        nodes, edges = network.get_visjs_nodes_and_edges()
        print(nodes)
        result = {'status': 'success', 'nodes': nodes, 'edges': edges}
    except:
        result = {'status': 'fail'}

    return jsonify(result=result)
Ejemplo n.º 2
0
def load_rxns(query_result):
    rxns = {}
    for rxn in query_result:
        rxns[rxn.name] = []
        for rxn_string in rxn.smarts:
            rxns[rxn.name].append(rdchiralReaction(rxn_string))
    return rxns
Ejemplo n.º 3
0
def load_rxns(yaml_dict):
    rxns = {}
    for name in yaml_dict:
        if name not in rxns:
            rxns[name] = []
        for rxn_string in yaml_dict[name]['smarts']:
            try:
                rxns[name].append(rdchiralReaction(rxn_string))
            except:
                print('Error loading reaction - ' + str(name))

    return rxns
Ejemplo n.º 4
0
    def get_rxns(self, smile):
        if self.policy_model == None:
            self.load_model()

        reactions = self.get_actions(smile)
        rxns = {}
        metadata = {}
        for reaction in reactions:
            name = f"Chem_{reaction['metadata']['classification']}"
            extra_string = ''
            num = 1
            while name + extra_string in rxns:
                extra_string = f"_{num}"
                num += 1
            name = name + extra_string
            rxns[name] = [rdchiralReaction(reaction['smarts'])]
            metadata[name] = reaction['metadata']
        return rxns, metadata
Ejemplo n.º 5
0
    def _test_smarts(self, smarts):
        try:
            smarts_list = yaml.load(smarts, Loader=yaml.FullLoader)
        except:
            self.state = 'danger'
            self.issues.append('Could not load smarts yaml')
            return

        try:
            rxn_list = []
            for sma in smarts_list:
                rxn_list.append(rdchiralReaction(sma))
        except:
            self.state = 'danger'
            self.issues.append('Could not load reactions')
            return

        return rxn_list
Ejemplo n.º 6
0
def visualise_smarts():
    smarts_yaml = request.form['smarts_yaml']
    try:
        smarts_list = yaml.safe_load(smarts_yaml)
    except:
        result = {'status': 'fail', 'msg': 'Could not load yaml'}
        return jsonify(result=result)

    try:
        rxn_list = []
        for sma in smarts_list:
            rxn_list.append(rdchiralReaction(sma))
        list_imgs = rxntosvg(rxn_list, rxnSize=(450, 150))
    except:
        result = {'status': 'fail', 'msg': 'Could not load rxn imgs'}
        return jsonify(result=result)

    result = {'status': 'success', 'msg': '', 'list_imgs': list_imgs}

    return jsonify(result=result)
Ejemplo n.º 7
0
def apply_reaction_rev():
    p = request.form['p']
    reaction_q = Reaction.objects(name=request.form['reaction'])
    if len(reaction_q) == 0:
        print(f"{request.form['reaction']} was not found in RetroBioCat")
        result = {'status': 'danger',
                  'msg': 'No reaction on RetroBioCat with this name',
                  'issues': [f"{request.form['reaction']} was not found in RetroBioCat"]}
        return jsonify(result=result)

    rdkit_rxns = {'rdkit_rxns': []}
    rdchiral_rxns = {'rd_chiral_rxns': []}
    try:
        for smarts in reaction_q[0].smarts:
            rdkit_rxns['rdkit_rxns'].append(rdChemReactions.ReactionFromSmarts(smarts))
            try:
                rdchiral_rxns['rd_chiral_rxns'].append(rdchiralReaction(smarts))
            except:
                pass
    except Exception as e:
        print('Error parsing reaction SMARTS')
        print(e)
        result = {'status': 'danger',
                  'msg': 'Error parsing reaction SMARTS',
                  'issues': []}
        return jsonify(result=result)

    rule_applicator = RuleApplicator(None)

    smi = p

    try:
        products = rule_applicator.apply_rules(smi, rdchiral_rxns)['rd_chiral_rxns']
    except:
        products = []

    if products == []:
        try:
            products = rule_applicator.apply_rules_rdkit(smi, rdkit_rxns)['rdkit_rxns']
        except:
            products = []

    no_dupl = []
    for prod in products:
        if prod not in no_dupl:
            no_dupl.append(prod)
    products = no_dupl

    svg_dict = make_svg_dict(products)
    reaction_svgs = []
    for smi_list in products:
        if len(smi_list) == 1:
            reaction_svgs.append(get_reaction_svg(smi_list[0], '', p))
        elif len(smi_list) == 2:
            reaction_svgs.append(get_reaction_svg(smi_list[0], smi_list[1], p))
        else:
            reaction_svgs.append(get_reaction_svg('', '', p))

    if len(products) == 0:
        result = {'status': 'danger',
                  'msg': 'No products',
                  'issues': []}
        return jsonify(result=result)

    result = {'status': 'success',
              'msg': '',
              'products': products,
              'reaction_svgs': reaction_svgs,
              'svg_dict': svg_dict,
              'issues': []}

    return jsonify(result=result)