def add_subgraph(self, nodes, edges, plain_text, confidence): """ Populate the object model using networkx neo4j subgraph :param nodes: nodes in the subgraph (g.nodes(data=True)) :param edges: edges in the subgraph (g.edges(data=True)) :return: none """ # Get the relevant info from the nodes and edges node_keys = [] node_descriptions = dict() node_names = dict() node_labels = dict() node_uuids = dict() node_accessions = dict() node_iris = dict() node_uuids2iri = dict() node_curies = dict() node_uuids2curie = dict() for u, data in nodes: node_keys.append(u) node_descriptions[u] = data['properties']['description'] node_names[u] = data['properties']['name'] node_labels[u] = list(set(data['labels']).difference({'Base'}))[0] node_uuids[u] = data['properties']['UUID'] node_accessions[u] = data['properties']['accession'] node_iris[u] = data['properties']['iri'] node_uuids2iri[data['properties'] ['UUID']] = data['properties']['iri'] node_curies[u] = data['properties']['curie_id'] node_uuids2curie[data['properties'] ['UUID']] = data['properties']['curie_id'] edge_keys = [] edge_types = dict() edge_source_db = dict() edge_source_iri = dict() edge_target_iri = dict() edge_source_curie = dict() edge_target_curie = dict() for u, v, data in edges: edge_keys.append((u, v)) edge_types[(u, v)] = data['type'] edge_source_db[(u, v)] = data['properties']['sourcedb'] edge_source_iri[( u, v)] = node_uuids2iri[data['properties']['source_node_uuid']] edge_target_iri[( u, v)] = node_uuids2iri[data['properties']['target_node_uuid']] edge_source_curie[( u, v)] = node_uuids2curie[data['properties']['source_node_uuid']] edge_target_curie[( u, v)] = node_uuids2curie[data['properties']['target_node_uuid']] # For each node, populate the relevant information node_objects = [] node_iris_to_node_object = dict() for node_key in node_keys: node = Node() node.id = node_curies[node_key] node.type = node_labels[node_key] node.name = node_names[node_key] node.accession = node_accessions[node_key] node.description = node_descriptions[node_key] node_objects.append(node) node_iris_to_node_object[node_iris[node_key]] = node # for each edge, create an edge between them edge_objects = [] for u, v in edge_keys: edge = Edge() edge.type = edge_types[(u, v)] edge.source_id = node_iris_to_node_object[edge_source_iri[(u, v)]].id edge.target_id = node_iris_to_node_object[edge_target_iri[(u, v)]].id edge.origin_list = [] edge.origin_list.append( edge_source_db[(u, v)] ) # TODO: check with eric if this really should be a list and if it should contain the source DB('s) edge_objects.append(edge) # Create the result (potential answer) result1 = Result() #result1.id = "http://rtx.ncats.io/api/v1/response/1234/result/2345" #result1.id = "-1" result1.text = plain_text result1.confidence = confidence # Create a ResultGraph object and put the list of nodes and edges into it result_graph = ResultGraph() result_graph.node_list = node_objects result_graph.edge_list = edge_objects # Put the ResultGraph into the first result (potential answer) result1.result_graph = result_graph # Put the first result (potential answer) into the response self._result_list.append(result1) self.response.result_list = self._result_list # Increment the number of results self._num_results += 1 if self._num_results == 1: self.response.message = "%s result found" % self._num_results else: self.response.message = "%s results found" % self._num_results
def test1(self): #### Create the response object and fill it with attributes about the response response = Response() response.context = "http://translator.ncats.io" response.id = "http://rtx.ncats.io/api/v1/response/1234" response.type = "medical_translator_query_response" response.tool_version = "RTX 0.4" response.schema_version = "0.5" response.datetime = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") response.original_question_text = "what proteins are affected by sickle cell anemia" response.restated_question_text = "Which proteins are affected by sickle cell anemia?" response.result_code = "OK" response.message = "1 result found" #### Create a disease node node1 = Node() node1.id = "http://omim.org/entry/603903" node1.type = "disease" node1.name = "sickle cell anemia" node1.accession = "OMIM:603903" node1.description = "A disease characterized by chronic hemolytic anemia..." #### Create a protein node node2 = Node() node2.id = "https://www.uniprot.org/uniprot/P00738" node2.type = "protein" node2.name = "Haptoglobin" node2.symbol = "HP" node2.accession = "UNIPROT:P00738" node2.description = "Haptoglobin captures, and combines with free plasma hemoglobin..." #### Create a node attribute node2attribute1 = NodeAttribute() node2attribute1.type = "comment" node2attribute1.name = "Complex_description" node2attribute1.value = "The Hemoglobin/haptoglobin complex is composed of a haptoglobin dimer bound to two hemoglobin alpha-beta dimers" node2.node_attributes = [node2attribute1] #### Create an edge between these 2 nodes edge1 = Edge() edge1.type = "is_caused_by_a_defect_in" edge1.source_id = node1.id edge1.target_id = node2.id edge1.confidence = 1.0 #### Add an origin and property for the edge origin1 = Origin() origin1.id = "https://api.monarchinitiative.org/api/bioentity/disease/OMIM:603903/genes/" origin1.type = "Monarch_BioLink_API_Relationship" #### Add an attribute attribute1 = EdgeAttribute() attribute1.type = "PubMed_article" attribute1.name = "Orthopaedic Manifestations of Sickle Cell Disease" attribute1.value = None attribute1.url = "https://www.ncbi.nlm.nih.gov/pubmed/29309293" origin1.attribute_list = [attribute1] edge1.origin_list = [origin1] #### Create the first result (potential answer) result1 = Result() result1.id = "http://rtx.ncats.io/api/v1/response/1234/result/2345" result1.text = "A free text description of this result" result1.confidence = 0.932 #### Create a ResultGraph object and put the list of nodes and edges into it result_graph = ResultGraph() result_graph.node_list = [node1, node2] result_graph.edge_list = [edge1] #### Put the ResultGraph into the first result (potential answer) result1.result_graph = result_graph #### Put the first result (potential answer) into the response result_list = [result1] response.result_list = result_list print(response)