def viewer(): """PathMe page.""" pathways = process_request(request) # List of all pathway names pathway_id_to_name = {} pathway_display_names = {} pathway_id_to_display_name = {} pathway_to_resource = {} for pathway_id, resource in pathways.items(): pathway = current_app.pathme_manager.get_pathway_by_id( pathway_id, resource) if not pathway: continue pathway_id_to_display_name[pathway_id] = pathway.display_name pathway_to_resource[pathway_id] = resource pathway_display_names[pathway.display_name] = pathway_id pathway_id_to_name[pathway_id] = pathway.name return render_template( 'pathme_viewer.html', pathways=pathways, pathway_id_to_name=pathway_id_to_name, pathways_name='+'.join( '<a href="{}" target="_blank"> {}</a><div class="circle {}"></div>' .format( DATABASE_URL_DICT[pathway_to_resource[pathway_id]].format( pathway_id.strip('hsa')), pathway_name, pathway_id) for pathway_name, pathway_id in pathway_display_names.items()), pathway_ids=list(pathways.keys()), DATABASE_STYLE_DICT=DATABASE_STYLE_DICT, DATABASE_URL_DICT=DATABASE_URL_DICT)
def get_random_paths(): """Get random paths given the pathways in the graph --- tags: - paths - pathway parameters: - name: pathways[] description: pathway resource/name pair required: true type: str - name: resources[] description: pathway resource/name pair required: true type: str """ pathways = process_request(request) graph = merge_pathways(pathways) path = get_random_path(graph) return jsonify([node.sha512 for node in path])
def get_network_tree(): """Builds a graph and sends the annotation ready to be rendered in the tree.""" pathways = process_request(request) graph = merge_pathways(pathways) # Returns annotation in graph return jsonify(get_tree_annotations(graph))
def get_network(): """Builds a graph from request and sends it in the given format.""" pathways = process_request(request) graph = merge_pathways(pathways) log.info('Exporting merged graph with {} nodes and {} edges'.format( graph.number_of_nodes(), graph.number_of_edges())) return export_graph(graph, request.args.get('format'))
def calculate_overlap(): """Return the overlap between different pathways in order to generate a Venn diagram.""" pathways = process_request(request) if len(pathways) < 2: return abort(500, 'Only one pathway has been submitted!') pathway_data = prepare_venn_diagram_data(current_app.pathme_manager, pathways) processed_venn_diagram = process_overlap_for_venn_diagram(pathway_data) return render_template('pathway_overlap.html', processed_venn_diagram=processed_venn_diagram, DATABASE_STYLE_DICT=DATABASE_STYLE_DICT)
def get_nodes_by_betweenness_centrality(): """Get a list of nodes with the top betweenness-centrality --- tags: - pathway parameters: - name: pathways[] description: pathway resource/name pair required: true type: str - name: resources[] description: pathway resource/name pair required: true type: str - name: node_number in: path description: The number of top between-nodes to return required: true type: integer """ node_number = request.args.get('node_number') if not node_number: abort(500, 'Missing "node_number" argument') try: node_number = int(node_number) except: abort(500, '"node_number" could not be parsed {}'.format(node_number)) pathways = process_request(request) graph = merge_pathways(pathways) if node_number > graph.number_of_nodes(): node_number = graph.number_of_nodes() bw_dict = betweenness_centrality(graph) return jsonify([ node.sha512 for node, score in sorted( bw_dict.items(), key=itemgetter(1), reverse=True)[:node_number] ])
def get_network_tree(): """Build a graph and sends the annotation ready to be rendered in the tree.""" pathways = process_request(request) graph = merge_pathways(pathways) annotations = get_annotations_from_request(request) if annotations: graph = get_subgraph_by_annotations(graph, annotations) if COLLAPSE_TO_GENES in request.args: collapse_to_genes(graph) # Return annotation in graph return jsonify(get_tree_annotations(graph))
def viewer(): """PathMe page.""" pathways = process_request(request) # List of all pathway names pathway_names = [] for pathway_id, resource in pathways.items(): pathway = current_app.pathme_manager.get_pathway_by_id( pathway_id, resource) if not pathway: continue pathway_names.append(pathway.display_name) return render_template('pathme_viewer.html', pathways=pathways, pathways_name='+'.join(pathway_names), pathway_ids=list(pathways.keys()))
def get_network(): """Build a graph from request and sends it in the given format.""" pathways = process_request(request) graph = merge_pathways(pathways) annotations = get_annotations_from_request(request) if annotations: graph = get_subgraph_by_annotations(graph, annotations) if COLLAPSE_TO_GENES in request.args: collapse_to_genes(graph) log.info('Exporting merged graph with {} nodes and {} edges'.format( graph.number_of_nodes(), graph.number_of_edges())) graph.name = 'Merged graph from {}'.format( [pathway_name for pathway_name in pathways]) graph.version = '0.0.0' return export_graph(graph, request.args.get('format'))
def get_paths(): """Return array of shortest/all paths given a source node and target node both belonging in the graph --- tags: - paths - pathway parameters: - name: pathways[] description: pathway resource/name pair required: true type: str - name: resources[] description: pathway resource/name pair required: true type: str - name: source_id description: The identifier of the source node required: true type: str - name: target_id description: The identifier of the target node required: true type: str - name: cutoff in: query description: The largest path length to keep required: true type: integer - name: undirected - name: paths_method in: path description: The method by which paths are generated - either just the shortest path, or all paths required: false default: shortest schema: type: string enum: - all - shortest """ pathways = process_request(request) graph = merge_pathways(pathways) # Create hash to node info hash_to_node = {node.sha512: node for node in graph} source_id = request.args.get('source') if source_id is None: raise IndexError('Source missing from cache: %s', source_id) target_id = request.args.get('target') if target_id is None: raise IndexError('target is missing from cache: %s', target_id) method = request.args.get(PATHS_METHOD) undirected = UNDIRECTED in request.args cutoff = request.args.get('cutoff', default=7, type=int) source = hash_to_node.get(source_id) target = hash_to_node.get(target_id) if source not in graph or target not in graph: log.info('Source/target node not in network') log.info('Nodes in network: %s', graph.nodes()) abort(500, 'Source/target node not in network') if undirected: graph = graph.to_undirected() if method == 'all': paths = all_simple_paths(graph, source=source, target=target, cutoff=cutoff) return jsonify([[node.sha512 for node in path] for path in paths]) try: paths = shortest_path(graph, source=source, target=target) except NetworkXNoPath: log.debug('No paths between: {} and {}'.format(source, target)) # Returns normal message if it is not a random call from graph_controller.js if RANDOM_PATH not in request.args: return 'No paths between the selected nodes' # In case the random node is an isolated one, returns it alone if not graph.neighbors(source)[0]: return jsonify([source]) paths = shortest_path(graph, source=source, target=graph.neighbors(source)[0]) return jsonify([node.sha512 for node in paths])