def queryTerm(self, term): method = "queryTerm" attributes = self.findTermAttributesAndTypeByName(term) response = self.createResponse() if ( attributes["status"] == 'OK' ): node1 = Node() node1.id = "MESH:" + attributes["id"] node1.uri = "http://purl.obolibrary.org/obo/MESH_" + attributes["id"] node1.type = attributes["type"] node1.name = attributes["name"] node1.description = attributes["description"] #### Create the first result (potential answer) result1 = Result() result1.id = "http://rtx.ncats.io/api/v1/result/0000" result1.text = "The term " + attributes["name"] + " refers to " + attributes["description"] result1.confidence = 1.0 #### Create a ResultGraph object and put the list of nodes and edges into it result_graph = ResultGraph() result_graph.node_list = [ node1 ] #### 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 else: response.response_code = "TermNotFound" response.message = "Unable to find term '" + term + "' in MeSH. No further information is available at this time." response.id = None return response
def queryTerm(self, term): method = "queryTerm" attributes = self.findTermAttributesAndTypeByName(term) message = self.createMessage() if ( attributes["status"] == 'OK' ): message.code_description = "1 result found" message.table_column_names = [ "id", "type", "name", "description", "uri" ] #### Create a Node object and fill it node1 = Node() node1.id = "MESH:" + attributes["id"] node1.uri = "http://purl.obolibrary.org/obo/MESH_" + attributes["id"] node1.type = [ attributes["type"] ] node1.name = attributes["name"] node1.description = attributes["description"] #### Create the first result (potential answer) result1 = Result() result1.id = "http://rtx.ncats.io/api/v1/result/0000" result1.description = "The term " + attributes["name"] + " refers to " + attributes["description"] result1.confidence = 1.0 result1.essence = attributes["name"] result1.essence_type = attributes["type"] node_types = ",".join(node1.type) result1.row_data = [ node1.id, node_types, node1.name, node1.description, node1.uri ] #### Create a KnowledgeGraph object and put the list of nodes and edges into it result_graph = KnowledgeGraph() result_graph.nodes = [ node1 ] #### Put the ResultGraph into the first result (potential answer) result1.result_graph = result_graph #### Put the first result (potential answer) into the message results = [ result1 ] message.results = results #### Also put the union of all result_graph components into the top Message KnowledgeGraph #### Normally the knowledge_graph will be much more complex than this, but take a shortcut for this single-node result message.knowledge_graph = result_graph #### Also manufacture a query_graph post hoc qnode1 = QNode() qnode1.node_id = "n00" qnode1.curie = "MESH:" + attributes["id"] qnode1.type = None query_graph = QueryGraph() query_graph.nodes = [ qnode1 ] query_graph.edges = [] message.query_graph = query_graph #### Create the corresponding knowledge_map knowledge_map = { "n00": "MESH:" + attributes["id"] } result1.knowledge_map = knowledge_map else: message.message_code = "TermNotFound" message.code_description = "Unable to find this term in MeSH. No further information is available at this time." message.id = None return message
def answer(self, entity, use_json=False): """ Answer a question of the type "What is X" but is general: :param entity: KG neo4j node name (eg "carbetocin") :param use_json: If the answer should be in Translator standardized API output format :return: a description and type of the node """ #### See if this entity is in the KG via the index eprint("Looking up '%s' in KgNodeIndex" % entity) kgNodeIndex = KGNodeIndex() curies = kgNodeIndex.get_curies(entity) #### If not in the KG, then return no information if not curies: if not use_json: return None else: error_code = "TermNotFound" error_message = "This concept is not in our knowledge graph" response = FormatOutput.FormatResponse(0) response.add_error_message(error_code, error_message) return response.message # Get label/kind of node the source is eprint("Getting properties for '%s'" % curies[0]) properties = RU.get_node_properties(curies[0]) eprint("Properties are:") eprint(properties) #### By default, return the results just as a plain simple list of data structures if not use_json: return properties #### Or, if requested, format the output as the standardized API output format else: #### Create a stub Message object response = FormatOutput.FormatResponse(0) response.message.table_column_names = [ "id", "type", "name", "description", "uri" ] response.message.code_description = None #### Create a Node object and fill it node1 = Node() node1.id = properties["id"] node1.uri = properties["uri"] node1.type = [properties["category"]] node1.name = properties["name"] node1.description = properties["description"] #### Create the first result (potential answer) result1 = Result() result1.id = "http://arax.ncats.io/api/v1/result/0000" result1.description = "The term %s is in our knowledge graph and is defined as %s" % ( properties["name"], properties["description"]) result1.confidence = 1.0 result1.essence = properties["name"] result1.essence_type = properties["category"] node_types = ",".join(node1.type) result1.row_data = [ node1.id, node_types, node1.name, node1.description, node1.uri ] #### Create a KnowledgeGraph object and put the list of nodes and edges into it result_graph = KnowledgeGraph() result_graph.nodes = [node1] result_graph.edges = [] #### Put the ResultGraph into the first result (potential answer) result1.result_graph = result_graph #### Put the first result (potential answer) into the message results = [result1] response.message.results = results #### Also put the union of all result_graph components into the top Message KnowledgeGraph #### Normally the knowledge_graph will be much more complex than this, but take a shortcut for this single-node result response.message.knowledge_graph = result_graph #### Also manufacture a query_graph post hoc qnode1 = QNode() qnode1.id = "n00" qnode1.curie = properties["id"] qnode1.type = None query_graph = QueryGraph() query_graph.nodes = [qnode1] query_graph.edges = [] response.message.query_graph = query_graph #### Create the corresponding knowledge_map node_binding = NodeBinding(qg_id="n00", kg_id=properties["id"]) result1.node_bindings = [node_binding] result1.edge_bindings = [] #eprint(response.message) return response.message
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)