def QGI_test3(): input_query_graph = { "message": { "query_graph": { "nodes": { "n00": { "id": "MONDO:0002715" }, "n01": { "category": "biolink:ChemicalSubstance" }, "n02": { "category": "biolink:Gene" } }, "edges": { "e00": { "predicate": "biolink:correlated_with", "subject": "n00", "object": "n01" }, "e01": { "predicate": "biolink:related_to", "subject": "n01", "object": "n02" } } } } } #### Create a template Message response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) message = ARAXMessenger().from_dict(input_query_graph['message']) response.envelope.message.query_graph = message.query_graph interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}") #### Show the final result print('-------------------------') print(response.show(level=ARAXResponse.DEBUG)) print(json.dumps(message.to_dict(), sort_keys=True, indent=2))
def QGI_test1(): #### Some qnode examples test_query_graphs = [ [ { 'id': 'n10', 'curie': 'DOID:9281', 'category': 'disease'}, { 'id': 'n11', 'category': 'chemical_substance'}, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11', 'category': 'treats'} ], [ { 'id': 'n10', 'curie': 'DOID:9281'}, { 'id': 'n11', 'category': 'protein'}, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11'} ], [ { 'id': 'n10', 'curie': 'DOID:9281'}, { 'id': 'n11', 'category': 'protein'}, { 'id': 'n12', 'category': 'chemical_substance'}, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11'}, { 'id': 'e11', 'source_id': 'n11', 'target_id': 'n12'} ], [ { 'id': 'n10', 'curie': 'DOID:9281'}, { 'id': 'n11', 'category': 'chemical_substance'}, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11'} ], [ { 'id': 'n10', 'curie': 'DOID:9281', 'category': 'disease'}, { 'id': 'n11', 'category': 'chemical_substance'}, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11'} ], ] #interpreter = ARAXQueryGraphInterpreter() #print(json.dumps(interpreter.query_graph_tree,sort_keys=True,indent=2)) #return for test_query_graph in test_query_graphs: #### Create a response object for each test response = ARAXResponse() #### Create a template Message messenger = ARAXMessenger() messenger.create_envelope(response) message = response.envelope.message for parameters in test_query_graph: if 'n' in parameters['id']: messenger.add_qnode(response, parameters) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response elif 'e' in parameters['id']: #print(f"++ Adding qedge with {parameters}") messenger.add_qedge(response, parameters) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response else: response.error(f"Unrecognized component {parameters['id']}") return response interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}")
def QGI_test5(): # This is to test forked/non-linear queries (currently not working properly) input_query_graph = { "message": { "query_graph": { "nodes": { "n0": { "categories": ["biolink:Gene"] }, "n1": { "ids": ["CHEBI:45783"], "categories": ["biolink:ChemicalEntity"] }, "n2": { "ids": ["MONDO:0005301"], "categories": ["biolink:Disease"] }, "n3": { "categories": ["biolink:ChemicalEntity"] } }, "edges": { "e01": { "subject": "n0", "object": "n1", "predicates": ["biolink:related_to"] }, "e02": { "subject": "n0", "object": "n2", "predicates": ["biolink:related_to"] }, "e03": { "subject": "n0", "object": "n3", "predicates": ["biolink:related_to"] } } } } } #### Create a template Message response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) message = ARAXMessenger().from_dict(input_query_graph['message']) response.envelope.message.query_graph = message.query_graph interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}")
def test_example1(): query_graph = { "edges": { "e00": { "subject": "n00", "object": "n01" }, "e01": { "subject": "n00", "object": "n01", "predicate": "biolink:contraindicated_for", "exclude": True } }, "nodes": { "n00": { "id": "MONDO:0001627", "category": "biolink:Disease" }, "n01": { "category": "biolink:ChemicalSubstance" } } } from ARAX_messenger import ARAXMessenger response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) response.envelope.message.query_graph = QueryGraph().from_dict(query_graph) query_graph_info = QueryGraphInfo() result = query_graph_info.assess(response.envelope.message) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response query_graph_info_dict = { 'n_nodes': query_graph_info.n_nodes, 'n_edges': query_graph_info.n_edges, 'is_bifurcated_graph': query_graph_info.is_bifurcated_graph, 'start_node': query_graph_info.start_node, 'node_info': query_graph_info.node_info, 'edge_info': query_graph_info.edge_info, 'node_order': query_graph_info.node_order, 'edge_order': query_graph_info.edge_order, 'node_category_map': query_graph_info.node_category_map, 'edge_predicate_map': query_graph_info.edge_predicate_map, } print( json.dumps(ast.literal_eval(repr(query_graph_info_dict)), sort_keys=True, indent=2))
def QGI_test2(): #### Set example query_graph # TRAPI 0.9.2 input_query_graph = { "message": { "query_graph": { "nodes": [ { "id": "n1", "category": "chemical_substance" }, { "id": "n2", "curie": "UMLS:C0002395" } ], "edges": [ { "id": "e1", "predicate": "clinically_tested_approved_unknown_phase", "source_id": "n1", "target_id": "n2" } ] } } } # TRAPI 1.0.0 input_query_graph = { "message": { "query_graph": { "nodes": { "n1": { "category": "biolink:ChemicalEntity" }, "n2": { "id": "UMLS:C0002395" } }, "edges": { "e1": { "predicate": "clinically_tested_approved_unknown_phase", "subject": "n1", "object": "n2" } } } } } # TRAPI 1.1.0 input_query_graph = { "message": { "query_graph": { "nodes": { "n1": { "categories": [ "biolink:ChemicalEntity" ] }, "n2": { "ids": [ "UMLS:C0002395" ] } }, "edges": { "e1": { "predicates": [ "biolink:clinically_tested_approved_unknown_phase" ], "subject": "n1", "object": "n2" } } } } } #### Create a template Message response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) message = ARAXMessenger().from_dict(input_query_graph['message']) response.envelope.message.query_graph = message.query_graph interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}") #### Show the final result print('-------------------------') print(response.show(level=ARAXResponse.DEBUG)) print(json.dumps(message.to_dict(),sort_keys=True,indent=2))
def QGI_test4(): input_query_graph = { "message": { "query_graph": { "nodes": { "n00": { "categories": [ "biolink:Gene" ], "is_set": False }, "n01": { "ids": [ "MONDO:0018177" ], "categories": [ "biolink:Disease" ], "is_set": False } }, "edges": { "e00": { "subject": "n00", "object": "n01", "exclude": False } } } } } #### Create a template Message response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) message = ARAXMessenger().from_dict(input_query_graph['message']) response.envelope.message.query_graph = message.query_graph interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}")
def main(): test_example1() return #### Create a response object response = ARAXResponse() #### Create an ActionsParser object from actions_parser import ActionsParser actions_parser = ActionsParser() #### Set a simple list of actions actions_list = [ "filter(start_node=1, maximum_results=10, minimum_confidence=0.5)", "return(message=true,store=false)" ] #### Parse the action_list and print the result result = actions_parser.parse(actions_list) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response actions = result.data['actions'] #### Read message #2 from the database. This should be the acetaminophen proteins query result message sys.path.append( os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback") from RTXFeedback import RTXFeedback araxdb = RTXFeedback() message_dict = araxdb.getMessage(2) #### The stored message comes back as a dict. Transform it to objects from ARAX_messenger import ARAXMessenger message = ARAXMessenger().from_dict(message_dict) #print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) #### Create a filter object and use it to apply action[0] from the list query_graph_info = QueryGraphInfo() result = query_graph_info.assess(message) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response query_graph_info_dict = { 'n_nodes': query_graph_info.n_nodes, 'n_edges': query_graph_info.n_edges, 'is_bifurcated_graph': query_graph_info.is_bifurcated_graph, 'start_node': query_graph_info.start_node, 'node_info': query_graph_info.node_info, 'edge_info': query_graph_info.edge_info, 'node_order': query_graph_info.node_order, 'edge_order': query_graph_info.edge_order, 'node_category_map': query_graph_info.node_category_map, 'edge_predicate_map': query_graph_info.edge_predicate_map, } print( json.dumps(ast.literal_eval(repr(query_graph_info_dict)), sort_keys=True, indent=2))
def main(): #### Create a response object that contains the final results of our efforts response = ARAXResponse() #### Setting the output to STDERR will write out information as we go along in addition to supplying it with the response #ARAXResponse.output = 'STDERR' #### Create the ARAXMessenger messenger = ARAXMessenger() #### Test fetch_message() if False: messenger = ARAXMessenger() result = messenger.apply_fetch_message( messenger.message, {'uri': 'https://arax.ncats.io/api/arax/v1.0/message/1'}) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response message = messenger.message print(response.show(level=ARAXResponse.DEBUG)) #print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) return #### Create an envelope in which to work messenger.create_envelope(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response message = response.envelope.message #### Some qnode examples parameters_sets = [ { 'id': 'DOID:9281' }, { 'id': 'Orphanet:673' }, { 'name': 'acetaminophen', 'category': 'biolink:ChemicalSubstance' }, { 'id': 'NCIT:C198' }, { 'id': 'UMLS:C4710278' }, { 'category': 'biolink:Protein', 'key': 'n10' }, { 'id': ['UniProtKB:P14136', 'UniProtKB:P35579'] }, { 'id': ['UniProtKB:P14136', 'UniProtKB:P35579'], 'is_set': 'false' }, ] for parameter in parameters_sets: #### Add a QNode messenger.add_qnode(response, parameter) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #### Some qedge examples parameters_sets = [ { 'subject': 'n00', 'object': 'n01' }, { 'subject': 'n01', 'object': 'n10', 'predicate': 'treats' }, ] for parameter in parameters_sets: #### Add a QEdge messenger.add_qedge(response, parameter) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #### Delete one of the edges messenger.remove_qedge(response, {'key': 'e00'}) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #### Show the final result print(response.show(level=ARAXResponse.DEBUG)) print(json.dumps(response.envelope.to_dict(), sort_keys=True, indent=2))
def test_example1(): test_query_graphs = [ { "description": "Two nodes, one edge linking them, 1 CURIE", "nodes": { "n00": { "ids": ["MONDO:0001627"] }, "n01": { "categories": ["biolink:ChemicalEntity"] } }, "edges": { "e00": { "subject": "n00", "object": "n01", "predicates": ["biolink:physically_interacts_with"] } } }, { "description": "Two nodes, two edges linking them, 1 CURIE, one of which is excluded", "nodes": { "n00": { "ids": ["MONDO:0001627"] }, "n01": { "categories": ["biolink:ChemicalEntity"] } }, "edges": { "e00": { "subject": "n00", "object": "n01" }, "e01": { "subject": "n00", "object": "n01", "predicates": ["biolink:contraindicated_for"], "exclude": True } } }, { "description": "Two nodes, one edge linking them, both nodes are CURIEs", "nodes": { "n00": { "ids": ["MONDO:0001627"] }, "n01": { "ids": ["CHEMBL.COMPOUND:CHEMBL112"] } }, "edges": { "e00": { "subject": "n00", "object": "n01" } } }, { "description": "Three nodes, 2 edges, 1 CURIE, simple linear chain", "nodes": { "n00": { "ids": ["MONDO:0001627"] }, "n01": { "categories": ["biolink:ChemicalEntity"] }, "n02": { "categories": ["biolink:Protein"] } }, "edges": { "e00": { "subject": "n00", "object": "n01", "predicates": ["biolink:physically_interacts_with"] }, "e01": { "subject": "n01", "object": "n02" } } }, { "description": "Three nodes, 2 edges, but the CURIE is in the middle. What does that even mean?", "nodes": { "n00": { "categories": ["biolink:ChemicalEntity"] }, "n01": { "ids": ["MONDO:0001627"] }, "n02": { "categories": ["biolink:Protein"] } }, "edges": { "e00": { "subject": "n00", "object": "n01", "predicates": ["biolink:physically_interacts_with"] }, "e01": { "subject": "n01", "object": "n02" } } }, { "description": "Four nodes, 3 edges, 1 CURIE, simple linear chain", "nodes": { "n00": { "ids": ["MONDO:0001627"] }, "n01": { "categories": ["biolink:ChemicalEntity"] }, "n02": { "categories": ["biolink:Protein"] }, "n03": { "categories": ["biolink:Disease"] } }, "edges": { "e00": { "subject": "n00", "object": "n01", "predicates": ["biolink:physically_interacts_with"] }, "e01": { "subject": "n01", "object": "n02" }, "e02": { "subject": "n02", "object": "n03" } } }, { "description": "Two nodes, one edge linking them, 0 CURIEs", "nodes": { "n00": { "categories": ["biolink:Drug"] }, "n01": { "categories": ["biolink:ChemicalEntity"] } }, "edges": { "e00": { "subject": "n00", "object": "n01", "predicates": ["biolink:physically_interacts_with"] } } }, { "description": "One node only", "nodes": { "n00": { "ids": ["MONDO:0001627"] } }, "edges": {} }, ] from ARAX_messenger import ARAXMessenger for test_query_graph in test_query_graphs: response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) print( '==================================================================' ) description = test_query_graph['description'] del test_query_graph['description'] print(f"Query Graph '{description}'") response.envelope.message.query_graph = QueryGraph().from_dict( test_query_graph) query_graph_info = QueryGraphInfo() result = query_graph_info.assess(response.envelope.message) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response query_graph_info_dict = { 'n_nodes': query_graph_info.n_nodes, 'n_edges': query_graph_info.n_edges, 'is_bifurcated_graph': query_graph_info.is_bifurcated_graph, 'start_node': query_graph_info.start_node['key'], 'simple_query_graph_template': query_graph_info.query_graph_templates['simple'], #'start_node': query_graph_info.start_node, #'node_info': query_graph_info.node_info, #'edge_info': query_graph_info.edge_info, #'node_order': query_graph_info.node_order, #'edge_order': query_graph_info.edge_order, #'node_category_map': query_graph_info.node_category_map, #'edge_predicate_map': query_graph_info.edge_predicate_map, } print( json.dumps(ast.literal_eval(repr(query_graph_info_dict)), sort_keys=True, indent=2))
def main(): #### Create a response object response = ARAXResponse() #### Create an ActionsParser object from actions_parser import ActionsParser actions_parser = ActionsParser() #### Set a simple list of actions actions_list = [ "filter(start_node=1, maximum_results=10, minimum_confidence=0.5)", "return(message=true,store=false)" ] #### Parse the action_list and print the result result = actions_parser.parse(actions_list) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response actions = result.data['actions'] #### Read message #2 from the database. This should be the acetaminophen proteins query result message sys.path.append( os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback") from RTXFeedback import RTXFeedback araxdb = RTXFeedback() message_dict = araxdb.getMessage(2) #### The stored message comes back as a dict. Transform it to objects sys.path.append( os.path.dirname(os.path.abspath(__file__)) + "/../OpenAPI/python-flask-server/") from swagger_server.models.message import Message message = Message().from_dict(message_dict) #### Create a filter object and use it to apply action[0] from the list filter = ARAXFilter() result = filter.apply(message, actions[0]['parameters']) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response response.data = result.data #### Show the final message print(response.show(level=ARAXResponse.DEBUG)) response.data['message_stats'] = { 'n_results': message.n_results, 'id': message.id, 'reasoner_id': message.reasoner_id, 'tool_version': message.tool_version } print( json.dumps(ast.literal_eval(repr(response.data['parameters'])), sort_keys=True, indent=2)) for result in message.results: if result.essence is not None: essence = result.essence else: essence = f"{len(result.node_bindings)} node bindings, {len(result.edge_bindings)} edge bindings" print(f" - {essence}") print( json.dumps(ast.literal_eval(repr(response.data['message_stats'])), sort_keys=True, indent=2))
def main(): # For faster testing, cache the testing messages locally import requests_cache requests_cache.install_cache('ARAX_ranker_testing_cache') import argparse argparser = argparse.ArgumentParser(description='Ranker system') argparser.add_argument('--local', action='store_true', help='If set, use local RTXFeedback database to fetch messages') params = argparser.parse_args() # --- Create a response object response = ARAXResponse() ranker = ARAXRanker() # --- Get a Message to work on from ARAX_messenger import ARAXMessenger messenger = ARAXMessenger() if not params.local: print("INFO: Fetching message to work on from arax.ncats.io", flush=True) message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2614') # acetaminophen - > protein, just NGD as virtual edge # message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2687') # neutropenia -> drug, predict_drug_treats_disease and ngd # message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2701') # observed_expected_ratio and ngd # message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2703') # a huge one with jaccard # message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2706') # small one with paired concept frequency # message = messenger.fetch_message('https://arax.ncats.io/api/rtx/v1/message/2709') # bigger one with paired concept frequency # For local messages due to local changes in code not rolled out to production: if params.local: sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback") from RTXFeedback import RTXFeedback araxdb = RTXFeedback() message_dict = araxdb.getMessage(294) # local version of 2709 but with updates to COHD # message_dict = araxdb.getMessage(297) # message_dict = araxdb.getMessage(298) # message_dict = araxdb.getMessage(299) # observed_expected_ratio different disease # message_dict = araxdb.getMessage(300) # chi_square # message_dict = araxdb.getMessage(302) # chi_square, different disease # message_dict = araxdb.getMessage(304) # all clinical info, osteoarthritis # message_dict = araxdb.getMessage(305) # all clinical info, neurtropenia # message_dict = araxdb.getMessage(306) # all clinical info, neurtropenia, but with virtual edges # message_dict = araxdb.getMessage(307) # all clinical info, osteoarthritis, but with virtual edges # message_dict = araxdb.getMessage(322) # Parkinsons Jaccard, top 50 # message_dict = araxdb.getMessage(324) # chi_square, KG2 # message_dict = araxdb.getMessage(325) # chi_square, ngd, KG2 # message_dict = araxdb.getMessage(326) # prob drug treats disease as attribute to all edge thrombocytopenia # message_dict = araxdb.getMessage(327) # add_qnode(name=DOID:1227, id=n00) # add_qnode(type=protein, is_set=true, id=n01) # add_qnode(type=chemical_substance, id=n02) # add_qedge(subject=n00, object=n01, id=e00) # add_qedge(subject=n01, object=n02, id=e01, type=physically_interacts_with) # expand(edge_id=[e00,e01], kp=ARAX/KG1) # overlay(action=compute_jaccard, start_node_id=n00, intermediate_node_id=n01, end_node_id=n02, virtual_relation_label=J1) # overlay(action=predict_drug_treats_disease, source_qnode_id=n02, target_qnode_id=n00, virtual_relation_label=P1) # overlay(action=overlay_clinical_info, chi_square=true, virtual_relation_label=C1, source_qnode_id=n00, target_qnode_id=n02) # overlay(action=compute_ngd, virtual_relation_label=N1, source_qnode_id=n00, target_qnode_id=n01) # overlay(action=compute_ngd, virtual_relation_label=N2, source_qnode_id=n00, target_qnode_id=n02) # overlay(action=compute_ngd, virtual_relation_label=N3, source_qnode_id=n01, target_qnode_id=n02) # resultify(ignore_edge_direction=true) # filter_results(action=limit_number_of_results, max_results=100) from ARAX_messenger import ARAXMessenger message = ARAXMessenger().from_dict(message_dict) if message is None: print("ERROR: Unable to fetch message") return # ranker.aggregate_scores(message,response=response) ranker.aggregate_scores_dmk(message, response=response) # Show the final result print(response.show(level=ARAXResponse.DEBUG)) print("Results:") for result in message.results: confidence = result.confidence if confidence is None: confidence = 0.0 print(" -" + '{:6.3f}'.format(confidence) + f"\t{result.essence}") # print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) # Show the message number print(json.dumps(ast.literal_eval(repr(message.id)), sort_keys=True, indent=2))
def main(): #### Create a response object response = ARAXResponse() #### Create an ActionsParser object from actions_parser import ActionsParser actions_parser = ActionsParser() #### Set a simple list of actions actions_list = [ "filter(start_node=1, maximum_results=10, minimum_confidence=0.5)", "return(message=true,store=false)" ] #### Parse the action_list and print the result result = actions_parser.parse(actions_list) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response actions = result.data['actions'] #### Read message #2 from the database. This should be the acetaminophen proteins query result message sys.path.append( os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback") from RTXFeedback import RTXFeedback araxdb = RTXFeedback() message_dict = araxdb.getMessage(2) #### The stored message comes back as a dict. Transform it to objects from ARAX_messenger import ARAXMessenger message = ARAXMessenger().from_dict(message_dict) #### Asses some information about the QueryGraph query_graph_info = QueryGraphInfo() result = query_graph_info.assess(message) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #print(json.dumps(ast.literal_eval(repr(query_graph_info.node_order)),sort_keys=True,indent=2)) #### Assess some information about the KnowledgeGraph knowledge_graph_info = KnowledgeGraphInfo() result = knowledge_graph_info.check_for_query_graph_tags( message, query_graph_info) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #### Try to add query_graph_ids to the KnowledgeGraph result = knowledge_graph_info.add_query_graph_tags(message, query_graph_info) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response #### Reassess some information about the KnowledgeGraph result = knowledge_graph_info.check_for_query_graph_tags( message, query_graph_info) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response print(response.show(level=ARAXResponse.DEBUG)) tmp = { 'query_graph_id_node_status': knowledge_graph_info.query_graph_id_node_status, 'query_graph_id_edge_status': knowledge_graph_info.query_graph_id_edge_status, 'n_nodes': knowledge_graph_info.n_nodes, 'n_edges': knowledge_graph_info.n_edges } #print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) print(json.dumps(ast.literal_eval(repr(tmp)), sort_keys=True, indent=2))
def QGI_test7(): # This is to test a three hop query with one end pinned (should result in FET ARAXi commands), and actually run the query input_query_graph = { "message": { "query_graph": { "edges": { "e00": { "object": "n01", "subject": "n00" }, "e01": { "object": "n02", "subject": "n01" }, "e02": { "object": "n03", "subject": "n02" } }, "nodes": { "n00": { "categories": [ "biolink:ChemicalEntity" ], "ids": [ "DRUGBANK:DB00150" ] }, "n01": { "categories": [ "biolink:Protein" ] }, "n02": { "categories": [ "biolink:MolecularActivity" ] }, "n03": { "categories": [ "biolink:ChemicalEntity" ] } } } } } #### Create a template Message response = ARAXResponse() messenger = ARAXMessenger() messenger.create_envelope(response) message = ARAXMessenger().from_dict(input_query_graph['message']) response.envelope.message.query_graph = message.query_graph interpreter = ARAXQueryGraphInterpreter() interpreter.translate_to_araxi(response) if response.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response araxi_commands = response.data['araxi_commands'] for cmd in araxi_commands: print(f" - {cmd}") #### Show the final result # print('-------------------------') # print(response.show(level=ARAXResponse.DEBUG)) # print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) #### Actually run the query from ARAX_query import ARAXQuery import ast araxq = ARAXQuery() # Run the query araxq.query({**input_query_graph, "operations": {"actions": araxi_commands}}) # unpack the response response = araxq.response envelope = response.envelope message = envelope.message # overrides the current message envelope.status = response.error_code envelope.description = response.message # return the message ID print(f"Returned response id: {envelope.id}") print('-------------------------') # print the whole message # print(json.dumps(ast.literal_eval(repr(envelope)), sort_keys=True, indent=2)) # save message to file (since I can't get the UI working locally for some reason) with open('QGI_test7.json', 'w', encoding='utf-8') as f: json.dump(ast.literal_eval(repr(envelope)), f, ensure_ascii=False, indent=4)
def main(): ### Note that most of this is just manually doing what ARAXQuery() would normally do for you #### Create a response object response = ARAXResponse() #### Create an ActionsParser object from actions_parser import ActionsParser actions_parser = ActionsParser() #### Set a simple list of actions # actions_list = [ # "overlay(compute_confidence_scores=true)", # "return(message=true,store=false)" # ] actions_list = [ #"filter_kg(action=remove_edges_by_predicate, edge_predicate=physically_interacts_with, remove_connected_nodes=false)", #"filter_kg(action=remove_edges_by_predicate, edge_predicate=physically_interacts_with, remove_connected_nodes=something)", #"filter(action=remove_nodes_by_category, node_category=protein)", #"overlay(action=compute_ngd)", #"filter(action=remove_edges_by_attribute, edge_attribute=ngd, threshold=.63, direction=below, remove_connected_nodes=t)", #"filter(action=remove_edges_by_attribute, edge_attribute=ngd, threshold=.6, remove_connected_nodes=False)", "filter(action=remove_orphaned_nodes)", "return(message=true,store=false)" ] #### Parse the action_list and print the result result = actions_parser.parse(actions_list) response.merge(result) if result.status != 'OK': print(response.show(level=ARAXResponse.DEBUG)) return response actions = result.data['actions'] #### Read message #2 from the database. This should be the acetaminophen proteins query result message sys.path.append( os.path.dirname(os.path.abspath(__file__)) + "/../../UI/Feedback") from RTXFeedback import RTXFeedback araxdb = RTXFeedback() #message_dict = araxdb.getMessage(2) # acetaminophen2proteins graph # message_dict = araxdb.getMessage(13) # ibuprofen -> proteins -> disease # work computer # message_dict = araxdb.getMessage(14) # pleuropneumonia -> phenotypic_feature # work computer # message_dict = araxdb.getMessage(16) # atherosclerosis -> phenotypic_feature # work computer # message_dict = araxdb.getMessage(5) # atherosclerosis -> phenotypic_feature # home computer # message_dict = araxdb.getMessage(10) message_dict = araxdb.getMessage(40) #### The stored message comes back as a dict. Transform it to objects from ARAX_messenger import ARAXMessenger message = ARAXMessenger().from_dict(message_dict) # print(json.dumps(message.to_dict(),sort_keys=True,indent=2)) #### Create an overlay object and use it to apply action[0] from the list #filterkg = ARAXFilterKG() #result = filterkg.apply(message, actions[0]['parameters']) #response.merge(result) # Apply overlay so you get an edge attribute to work with, then apply the filter #from ARAX_overlay import ARAXOverlay #overlay = ARAXOverlay() #result = overlay.apply(message, actions[0]['parameters']) #response.merge(result) # then apply the filter filterkg = ARAXFilterKG() result = filterkg.apply(message, actions[0]['parameters']) response.merge(result) # if result.status != 'OK': # print(response.show(level=ARAXResponse.DEBUG)) # return response # response.data = result.data #### If successful, show the result # print(response.show(level=ARAXResponse.DEBUG)) # response.data['message_stats'] = { 'n_results': message.n_results, 'id': message.id, # 'reasoner_id': message.reasoner_id, 'tool_version': message.tool_version } # response.data['message_stats']['confidence_scores'] = [] # for result in message.results: # response.data['message_stats']['confidence_scores'].append(result.confidence) # print(json.dumps(ast.literal_eval(repr(response.data['parameters'])),sort_keys=True,indent=2)) # print(json.dumps(ast.literal_eval(repr(response.data['message_stats'])),sort_keys=True,indent=2)) # a comment on the end so you can better see the network on github # look at the response # print(response.show(level=ARAXResponse.DEBUG)) # print(response.show()) # print("Still executed") # look at the edges # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges.values())),sort_keys=True,indent=2)) # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.nodes.values())), sort_keys=True, indent=2)) # print(json.dumps(message.to_dict(), sort_keys=True, indent=2)) # print(response.show(level=ARAXResponse.DEBUG)) # just print off the values # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges.values())), sort_keys=True, indent=2)) # for edge in message.knowledge_graph.edges.values(): # if hasattr(edge, 'attributes') and edge.attributes and len(edge.attributes) >= 1: # print(edge.attributes.pop().value) print( json.dumps(ast.literal_eval( repr(message.knowledge_graph.edges.values())), sort_keys=True, indent=2)) print(response.show(level=ARAXResponse.DEBUG)) vals = [] for key, node in message.knowledge_graph.nodes.items(): print(key) print(len(message.knowledge_graph.nodes)) for edge in message.knowledge_graph.edges.values(): if hasattr(edge, 'attributes') and edge.attributes and len( edge.attributes) >= 1: vals.append(edge.attributes.pop().value) print(sorted(vals))