def delete_step(): reaction = request.form['reaction'] task_id = request.form['task_id'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles, print_log=not current_app.config['PRODUCTION']) network.update_settings(network_options) network.add_attributes(attr_dict) to_delete = network.delete_reaction_node(reaction) nodes = [] edges = [] data['graph_dict'] = json.dumps(nx.to_dict_of_lists(network.graph)) data['attr_dict'] = json.dumps(network.attributes_dict()) nodes = add_new(data['nodes'], nodes) edges = add_new(data['edges'], edges) nodes, edges = delete_nodes_and_edges(to_delete, nodes, edges) data['nodes'] = nodes data['edges'] = edges current_app.redis.mset({task_id: json.dumps(data)}) time_to_expire = 15 * 60 # 15 mins * 60 seconds current_app.redis.expire(task_id, time_to_expire) result = {'to_delete': to_delete} return jsonify(result=result)
def change_reaction_options(): task_id = request.form['task_id'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles) network.update_settings(network_options) network.add_attributes(attr_dict) network.update_settings( {'max_reactions': int(request.form['max_reactions'])}) """ if len(retrobiocat.retrorules_diameters) != 0: network.settings['rr_min_diameter'] = int(request.form['rr_min_diameter']) network.settings['rr_min_products'] = int(request.form['rr_min_products']) network.settings['rr_max_reactions'] = int(request.form['rr_max_reactions']) """ data['network_options'] = json.dumps(network.settings) current_app.redis.mset({task_id: json.dumps(data)}) time_to_expire = 15 * 60 #15 mins * 60 seconds current_app.redis.expire(task_id, time_to_expire) result = {'network_options': json.dumps(network.settings)} return jsonify(result=result)
def change_network_options(): reaction_colours = request.form['reaction_colours'] edge_colours = request.form['edge_colours'] task_id = request.form['task_id'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles) network.update_settings(network_options) network.add_attributes(attr_dict) network.settings['colour_reactions'] = reaction_colours network.settings["colour_arrows"] = edge_colours nodes, edges = network.get_visjs_nodes_and_edges() data['nodes'] = add_new(data['nodes'], nodes) data['edges'] = add_new(data['edges'], edges) data['network_options'] = json.dumps(network.settings) current_app.redis.mset({task_id: json.dumps(data)}) time_to_expire = 15 * 60 #15 mins * 60 seconds current_app.redis.expire(task_id, time_to_expire) result = { 'nodes': nodes, 'edges': edges, } return jsonify(result=result)
def package_visjs_pathways(task_id, max_vis=100): network_data = json.loads(current_app.redis.get(task_id + '__network')) graph = nx.from_dict_of_lists(json.loads(network_data['graph_dict']), create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=network_data['target_smiles'], print_log=not current_app.config['PRODUCTION']) network.update_settings(json.loads(network_data['network_options'])) network.add_attributes(json.loads(network_data['attr_dict'])) evaluated_pathways = json.loads( current_app.redis.get(f"{task_id}__evaluated_pathways")) for i, pathway_varients in enumerate(evaluated_pathways): if i > max_vis: break pathway_vis_js_data = [] max_var = len(pathway_varients) for nodes in pathway_varients: pathway = Pathway(nodes, network, calc_scores=False) nodes, edges = pathway.get_visjs_nodes_and_edges() pathway_vis_js_data.append((nodes, edges, max_var)) current_app.redis.mset( {f"{task_id}__{i+1}": json.dumps(pathway_vis_js_data)}) current_app.redis.expire(f"{task_id}__{i+1}", 60 * 60)
def aizynth_step(): clicked_node = request.form['smiles'] x = request.form['x'] y = request.form['y'] task_id = request.form['task_id'] max_reactions = request.form['max_reactions'] aizynth_reaction_mode = request.form['aizynth_reaction_mode'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles, print_log=not current_app.config['PRODUCTION']) network.update_settings(network_options) network.add_attributes(attr_dict) network.update_settings({ 'max_reactions': int(max_reactions), 'aizynth_reaction_mode': aizynth_reaction_mode }) new_substrate_nodes, new_reaction_nodes = network.add_chemical_step( clicked_node) all_new_nodes = [clicked_node] + new_substrate_nodes + new_reaction_nodes subgraph = network.graph.subgraph(all_new_nodes) nodes, edges = network.get_visjs_nodes_and_edges(graph=subgraph) for i, node in enumerate(nodes): nodes[i].update({'x': x, 'y': y}) result = {'nodes': nodes, 'edges': edges} data['graph_dict'] = json.dumps(nx.to_dict_of_lists(network.graph)) data['attr_dict'] = json.dumps(network.attributes_dict()) nodes = add_new(data['nodes'], nodes) edges = add_new(data['edges'], edges) nodes, edges = delete_nodes_and_edges([], nodes, edges) data['nodes'] = nodes data['edges'] = edges current_app.redis.mset({task_id: json.dumps(data)}) time_to_expire = 15 * 60 #15 mins * 60 seconds current_app.redis.expire(task_id, time_to_expire) return jsonify(result=result)
def task_add_retrorule_step(form_data, network_id): job = get_current_job() job.meta['progress'] = 'started' job.save_meta() clicked_node = form_data['smiles'] x = form_data['x'] y = form_data['y'] data = json.loads(current_app.redis.get(network_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles) network.update_settings(network_options) network.add_attributes(attr_dict) network.retrorules.retrorules_rxns = current_app.retrorules_rxns network.retrorules.retrorule_db = current_app.retrorules_db new_substrate_nodes, new_reaction_nodes = network.retrorules.add_step( clicked_node) all_new_nodes = [clicked_node] + new_substrate_nodes + new_reaction_nodes subgraph = network.graph.subgraph(all_new_nodes) nodes, edges = network.get_visjs_nodes_and_edges(graph=subgraph) for i, node in enumerate(nodes): nodes[i].update({'x': x, 'y': y}) result = { 'nodes': nodes, 'edges': edges, 'to_delete': [], } data['graph_dict'] = json.dumps(nx.to_dict_of_lists(network.graph)) data['attr_dict'] = json.dumps(network.attributes_dict()) data['nodes'] = add_new(data['nodes'], nodes) data['edges'] = add_new(data['edges'], edges) current_app.redis.mset({network_id: json.dumps(data)}) current_app.redis.expire(network_id, 5 * 60) return result
def task_reorder_pathways(weights, pathways_id): job = get_current_job() job.meta['progress'] = 'started' job.save_meta() pathway_settings = json.loads( current_app.redis.get(pathways_id + '__pathway_settings')) pathway_settings.update({ 'weight_num_enzymes': weights[0], 'weight_complexity': weights[1], 'weight_starting': weights[2], 'weight_known_enzymes': weights[3], 'weight_diversity': weights[4] }) current_app.redis.mset( {f"{pathways_id}__pathway_settings": json.dumps(pathway_settings)}) current_app.redis.expire(pathways_id, 60 * 60) network_data = json.loads(current_app.redis.get(pathways_id + '__network')) graph = nx.from_dict_of_lists(json.loads(network_data['graph_dict']), create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=network_data['target_smiles'], print_log=not current_app.config['PRODUCTION']) network.update_settings(json.loads(network_data['network_options'])) network.add_attributes(json.loads(network_data['attr_dict'])) all_pathways_nodes, all_scores = json.loads( current_app.redis.get(f"{pathways_id}__all_pathways")) pathways = load_pathways(all_pathways_nodes, all_scores, network) pathway_evaluator = evaluate_pathways(pathways, weights) print(weights) package_evaluated_pathways(pathway_evaluator.df, pathways_id) package_visjs_pathways(pathways_id) job = get_current_job() job.meta['progress'] = 'complete' job.save_meta() result = {} return result
def get_visjs_pathway(task_id, pathway_id, varient): network_data = json.loads(current_app.redis.get(task_id + '__network')) pathway_data = json.loads( current_app.redis.get(task_id + f'__{pathway_id}')) pathway_nodes = pathway_data[varient - 1] graph = nx.from_dict_of_lists(json.loads(network_data['graph_dict']), create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=network_data['target_smiles'], print_log=not current_app.config['PRODUCTION']) network.update_settings(json.loads(network_data['network_options'])) network.add_attributes(json.loads(network_data['attr_dict'])) pathway = Pathway(pathway_nodes, network, calc_scores=False) nodes, edges = pathway.get_visjs_nodes_and_edges() max_var = len(pathway_data) return nodes, edges, max_var
def custom_reaction(): product_smiles = str(request.form['product']) substrate_smiles = str(request.form['substrate']) reaction_name = str(request.form['name']) task_id = request.form['task_id'] data = json.loads(current_app.redis.get(task_id)) graph_dict = json.loads(data['graph_dict']) attr_dict = json.loads(data['attr_dict']) target_smiles = data['target_smiles'] network_options = json.loads(data['network_options']) graph = nx.from_dict_of_lists(graph_dict, create_using=nx.DiGraph) network = Network(graph=graph, target_smiles=target_smiles) network.update_settings(network_options) network.add_attributes(attr_dict) new_substrate_nodes, new_reaction_nodes = network.custom_reaction( product_smiles, substrate_smiles, reaction_name) all_new_nodes = new_substrate_nodes + new_reaction_nodes subgraph = network.graph.subgraph(all_new_nodes) nodes, edges = network.get_visjs_nodes_and_edges(graph=subgraph) result = { 'nodes': nodes, 'edges': edges, } data['graph_dict'] = json.dumps(nx.to_dict_of_lists(network.graph)) data['attr_dict'] = json.dumps(network.attributes_dict()) nodes = add_new(data['nodes'], nodes) edges = add_new(data['edges'], edges) data['nodes'] = nodes data['edges'] = edges current_app.redis.mset({task_id: json.dumps(data)}) current_app.redis.expire(task_id, 5 * 60) return jsonify(result=result)
def task_make_network(form_data): job = get_current_job() job.meta['progress'] = 'started' job.save_meta() network = Network(include_experimental=bool(form_data['include_experimental']), include_two_step=bool(form_data['include_two_step']), include_requires_absence_of_water=bool(form_data['include_requires_absence_of_water']), print_log=not current_app.config['PRODUCTION']) network.update_settings({"allow_backwards_steps": bool(form_data['allow_backwards']), "remove_simple": bool(form_data['remove_small']), "similarity_score_threshold": float(form_data['sub_thres']), "combine_enantiomers" : bool(form_data['combine_enantiomers']), "num_enzymes": 1, "calculate_complexities": bool(form_data['calc_complexity']), "calculate_substrate_specificity": bool(form_data['sub_sim']), "max_nodes": int(form_data['max_initial_nodes'],), "colour_reactions" : form_data['colour_reactions'], "colour_arrows": form_data['colour_edges'], "show_negative_enzymes" : form_data['show_neg_enz'], "only_postitive_enzyme_data" : not form_data['show_neg_enz'], "max_reactions": form_data["max_reactions"], 'only_reviewed_activity_data': bool(form_data["only_reviewed"])}) if form_data["specificity_scoring_mode"] == 'Product + substrates (slower)': network.update_settings({'specificity_score_substrates' : True}) #print(f"include_experimental = {network.settings['include_experimental']}") #print(f"include_two_step = {network.settings['include_two_step']}") network.generate(form_data['target_smiles'], form_data['number_steps'], calculate_scores=False) job.meta['progress'] = 'network_generated' job.save_meta() network.calculate_scores() job.meta['progress'] = 'scores_calculated' job.save_meta() nodes, edges = network.get_visjs_nodes_and_edges() #options = {'interaction': {'multiselect': 'true',}} options = {} default_network_name = 'Network for ' + str(network.target_smiles) result = {'save_id':str(uuid.uuid4()), 'save_links' : [], 'save_name' : default_network_name, 'nodes':nodes, 'edges':edges, 'options':json.dumps(options), 'graph_dict':json.dumps(nx.to_dict_of_lists(network.graph)), 'target_smiles':str(network.target_smiles), 'network_options':json.dumps(network.settings), 'attr_dict':json.dumps(network.attributes_dict()), 'max_reactions' : int(network.settings['max_reactions'])} current_app.redis.mset({job.id: json.dumps(result)}) time_to_expire = 15*60 #15 mins * 60 seconds current_app.redis.expire(job.id, time_to_expire) return result
def task_get_pathways(form_data): job = get_current_job() job.meta['progress'] = 'started' job.save_meta() network = Network(print_log=not current_app.config['PRODUCTION'], include_experimental=form_data['include_experimental'], include_two_step=form_data['include_two_step'], include_requires_absence_of_water=bool( form_data['include_requires_absence_of_water'])) network.update_settings({ "remove_simple": bool(form_data['remove_small']), "combine_enantiomers": bool(form_data['combine_enantiomers']), 'max_nodes': int(form_data['max_nodes']), 'similarity_score_threshold': float(form_data['sub_thres']), 'colour_reactions': form_data['colour_reactions'], "colour_arrows": form_data['colour_edges'], "show_negative_enzymes": form_data['show_neg_enz'], "only_postitive_enzyme_data": not form_data['show_neg_enz'], 'only_reviewed_activity_data': bool(form_data["only_reviewed"]) }) if form_data[ "specificity_scoring_mode"] == 'Product + substrates (slower)': network.update_settings({'specificity_score_substrates': True}) network.generate(form_data['target_smiles'], form_data['number_steps'], calculate_scores=False) job.meta['progress'] = 'network_generated' job.save_meta() network.calculate_scores() job.meta['progress'] = 'network_scored' job.save_meta() network_data = { 'graph_dict': json.dumps(nx.to_dict_of_lists(network.graph)), 'target_smiles': str(network.target_smiles), 'network_options': json.dumps(network.settings), 'attr_dict': json.dumps(network.attributes_dict()) } current_app.redis.mset({f"{job.id}__network": json.dumps(network_data)}) current_app.redis.expire(f"{job.id}__network", 60 * 60) bfs = BFS(network=network, max_pathways=form_data['max_pathways'], max_pathway_length=form_data['number_steps'], min_weight=float(form_data['min_weight']), print_log=not current_app.config['PRODUCTION']) bfs.run() pathways = bfs.get_pathways() job.meta['progress'] = 'pathways_generated' job.save_meta() package_all_pathways(job.id, pathways) pathway_evaluator = evaluate_pathways(pathways, [ form_data['weight_num_enzymes'], form_data['weight_complexity'], form_data['weight_starting'], form_data['weight_known_enzymes'], form_data['weight_diversity'] ]) package_evaluated_pathways(pathway_evaluator.df, job.id) package_visjs_pathways(job.id) job.meta['progress'] = 'pathways_scored' job.save_meta() options = {} if form_data['hierarchical'] == True: options.update({ "layout": { "improvedLayout": 'true', 'hierarchical': { 'direction': 'DU', "sortMethod": "hubsize", "nodeSpacing": 200, "treeSpacing": 400 } } }) pathway_settings = { 'weight_num_enzymes': form_data['weight_num_enzymes'], 'weight_complexity': form_data['weight_complexity'], 'weight_starting': form_data['weight_starting'], 'weight_known_enzymes': form_data['weight_known_enzymes'], 'weight_diversity': form_data['weight_diversity'], 'options': options } current_app.redis.mset( {f"{job.id}__pathway_settings": json.dumps(pathway_settings)}) current_app.redis.expire(job.id, 60 * 60)