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_enzyme(): selected_node = request.form['selected_node'] selected_enzyme = request.form['selected_enzyme'] 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.settings = network_options network.add_attributes(attr_dict) network.calculate_scores() network.graph.nodes[selected_node]['attributes']['selected_enzyme'] = selected_enzyme data['attr_dict'] = json.dumps(network.attributes_dict()) 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) successors = list(network.graph.successors(selected_node)) predecessors = list(network.graph.predecessors(selected_node)) subgraph = network.graph.subgraph([selected_node]+successors+predecessors) nodes, edges = network.get_visjs_nodes_and_edges(graph=subgraph) result = {'nodes': nodes, 'edges': edges} return jsonify(result=result)
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_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 node_info(): if 'node' in request.form: node = str(request.form['node']) else: result = {'name': 'error', 'type': 'error', 'data': {}, 'html': ''} return jsonify(result=result) try: 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.settings = network_options network.add_attributes(attr_dict) network.get_node_types() if node in network.substrate_nodes: result = get_substrate_info(node) elif node in network.reaction_nodes: if 'retrorule' not in network.graph.nodes[node]['attributes']: result = get_reaction_info(node, network) else: result = get_retrorule_info(node, network) else: print('Error node not in substrates or reactions') result = {'name': 'error', 'type': 'error', 'data' : {}, 'html' : ''} return jsonify(result=result) except: result = {'name': 'error', 'type': 'error', 'data': {}, 'html': ''} return jsonify(result=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)