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 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 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])