def main(): #### Create a response object response = Response() #### 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=Response.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(ast.literal_eval(repr(message)),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=Response.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_type_map': query_graph_info.node_type_map, 'edge_type_map': query_graph_info.edge_type_map, } print( json.dumps(ast.literal_eval(repr(query_graph_info_dict)), sort_keys=True, indent=2))
def main(): #### Create a response object response = Response() #### 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=Response.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=Response.DEBUG)) return response response.data = result.data #### Show the final message print(response.show(level=Response.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(): # Note that most of this is just manually doing what ARAXQuery() would normally do for you response = Response() from actions_parser import ActionsParser actions_parser = ActionsParser() actions_list = [ "create_message", "add_qnode(id=n00, curie=CHEMBL.COMPOUND:CHEMBL112)", # acetaminophen "add_qnode(id=n01, type=protein, is_set=true)", "add_qedge(id=e00, source_id=n00, target_id=n01)", "expand(edge_id=e00, kp=BTE)", "return(message=true, store=false)", ] # Parse the raw action_list into commands and parameters result = actions_parser.parse(actions_list) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response actions = result.data['actions'] from ARAX_messenger import ARAXMessenger messenger = ARAXMessenger() expander = ARAXExpander() for action in actions: if action['command'] == 'create_message': result = messenger.create_message() message = result.data['message'] response.data = result.data elif action['command'] == 'add_qnode': result = messenger.add_qnode(message, action['parameters']) elif action['command'] == 'add_qedge': result = messenger.add_qedge(message, action['parameters']) elif action['command'] == 'expand': result = expander.apply(message, action['parameters']) elif action['command'] == 'return': break else: response.error(f"Unrecognized command {action['command']}", error_code="UnrecognizedCommand") print(response.show(level=Response.DEBUG)) return response # Merge down this result and end if we're in an error state response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response # Show the final response # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph)),sort_keys=True,indent=2)) print(response.show(level=Response.DEBUG))
def main(): ### Note that most of this is just manually doing what ARAXQuery() would normally do for you #### Create a response object response = Response() #### 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_type, edge_type=physically_interacts_with, remove_connected_nodes=false)", #"filter_kg(action=remove_edges_by_type, edge_type=physically_interacts_with, remove_connected_nodes=something)", #"filter(action=remove_nodes_by_type, node_type=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=Response.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(ast.literal_eval(repr(message)),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=Response.DEBUG)) # return response # response.data = result.data #### If successful, show the result # print(response.show(level=Response.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=Response.DEBUG)) # print(response.show()) # print("Still executed") # look at the edges # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)),sort_keys=True,indent=2)) # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.nodes)), sort_keys=True, indent=2)) # print(json.dumps(ast.literal_eval(repr(message)), sort_keys=True, indent=2)) # print(response.show(level=Response.DEBUG)) # just print off the values # print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)), sort_keys=True, indent=2)) # for edge in message.knowledge_graph.edges: # if hasattr(edge, 'edge_attributes') and edge.edge_attributes and len(edge.edge_attributes) >= 1: # print(edge.edge_attributes.pop().value) print( json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)), sort_keys=True, indent=2)) print(response.show(level=Response.DEBUG)) vals = [] for node in message.knowledge_graph.nodes: print(node.id) print(len(message.knowledge_graph.nodes)) for edge in message.knowledge_graph.edges: if hasattr(edge, 'edge_attributes') and edge.edge_attributes and len( edge.edge_attributes) >= 1: vals.append(edge.edge_attributes.pop().value) print(sorted(vals))
def processExternalPreviousMessageProcessingPlan(self, inputEnvelope): debug = 1 if debug: eprint( "DEBUG: Entering processExternalPreviousMessageProcessingPlan") messages = [] finalMessage = None finalMessage_id = None query = None #### Pull out the main processing plan envelope envelope = PreviousMessageProcessingPlan.from_dict( inputEnvelope["previous_message_processing_plan"]) #### If there are URIs provided, try to load them if envelope.previous_message_uris is not None: if debug: eprint("DEBUG: Got previous_message_uris") for uri in envelope.previous_message_uris: if debug: eprint("DEBUG: messageURI=" + uri) matchResult = re.match( r'http[s]://arax.ncats.io/.*api/rtx/.+/message/(\d+)', uri, re.M | re.I) if matchResult: message_id = matchResult.group(1) if debug: eprint( "DEBUG: Found local ARAX identifier corresponding to message_id " + message_id) if debug: eprint("DEBUG: Loading message_id " + message_id) message = self.getMessage(message_id) #eprint(type(message)) if not isinstance(message, tuple): if debug: eprint("DEBUG: Original question was: " + message["original_question"]) messages.append(message) finalMessage_id = message_id query = { "query_type_id": message["query_type_id"], "restated_question": message["restated_question"], "terms": message["terms"] } else: eprint("ERROR: Unable to load message_id " + message_id) return ({ "status": 404, "title": "Message not found", "detail": "There is no local message corresponding to message_id=" + str(message_id), "type": "about:blank" }, 404) #### If there are one or more previous_messages embedded in the POST, process them if envelope.previous_messages is not None: if debug: eprint("DEBUG: Got previous_messages") for uploadedMessage in envelope.previous_messages: if debug: eprint("DEBUG: uploadedMessage is a " + str(uploadedMessage.__class__)) if str(uploadedMessage.__class__ ) == "<class 'swagger_server.models.message.Message'>": if uploadedMessage.results: message = ast.literal_eval(repr(uploadedMessage)) messages.append(message) if message["terms"] is None: message["terms"] = {"dummyTerm": "giraffe"} if message["query_type_id"] is None: message["query_type_id"] = "UnknownQ" if message["restated_question"] is None: message["restated_question"] = "What is life?" if message["original_question"] is None: message["original_question"] = "what is life" query = { "query_type_id": message["query_type_id"], "restated_question": message["restated_question"], "original_question": message["original_question"], "terms": message["terms"] } else: eprint( "Uploaded message does not contain a results. May be the wrong format" ) return ({ "status": 404, "title": "Bad uploaded Message", "detail": "There is no results in the uploaded Message object=", "type": "about:blank" }, 404) else: eprint( "Uploaded message is not of type Message. It is of type" + str(uploadedMessage.__class__)) return ({ "status": 404, "title": "Bad uploaded Message", "detail": "Uploaded message is not of type Message. It is of type" + str(uploadedMessage.__class__), "type": "about:blank" }, 404) #### Take different actions based on the number of messages we now have in hand n_messages = len(messages) if n_messages == 0: return ({ "status": 499, "title": "No Messages", "detail": "Did not get any useful Message objects", "type": "about:blank" }, 499) elif n_messages == 1: finalMessage = messages[0] else: finalMessage = TxMessage.from_dict(messages[0]) counter = 1 while counter < n_messages: messageToMerge = TxMessage.from_dict(messages[counter]) if messageToMerge.reasoner_id is None: messageToMerge.reasoner_id = "Unknown" if messageToMerge.reasoner_id != "ARAX": messageToMerge = self.fix_message( query, messageToMerge, messageToMerge.reasoner_id) finalMessage = self.merge_message(finalMessage, messageToMerge) counter += 1 finalMessage = ast.literal_eval(repr(finalMessage)) #return( { "status": 498, "title": "Multiple Messages", "detail": "I have multiple messages. Merging code awaits!", "type": "about:blank" }, 498) #### Examine the options that were provided and act accordingly optionsDict = {} if envelope.options: if debug: eprint("DEBUG: Got options") for option in envelope.options: if debug: eprint("DEBUG: option=" + option) optionsDict[option] = 1 #### If there are processing_actions, then fulfill those processing_actions = [] if envelope.processing_actions: if debug: eprint("DEBUG: Found processing_actions") actions_parser = ActionsParser() result = actions_parser.parse(envelope.processing_actions) if result.error_code != 'OK': eprint(result) raise () #### Message suffers from a dual life as a dict and an object. above we seem to treat it as a dict. Fix that. FIXME #### Below we start treating it as and object. This should be the way forward. #### This is not a good place to do this, but may need to convert here from ARAX_messenger import ARAXMessenger finalMessage = ARAXMessenger().from_dict(finalMessage) #### Process each action in order action_stats = {} actions = result.data['actions'] for action in actions: if debug: eprint( f"DEBUG: Considering action '{action['command']}' with parameters {action['parameters']}" ) #### If we encounter a return, then this is the end of the line if action['command'] == 'return': action_stats['return_action'] = action break if action['command'] == 'filter': filter = ARAXFilter() result = filter.apply(finalMessage, action['parameters']) if result.error_code != 'OK': response = result break else: if debug: eprint( f"DEBUG: Action '{action['command']}' is not known" ) #### At the end, process the explicit return() action, or implicitly perform one return_action = { 'command': 'return', 'parameters': { 'message': 'false', 'store': 'false' } } if action is not None and action['command'] == 'return': return_action = action #### If an explicit one left out some parameters, set the defaults if 'store' not in return_action['parameters']: return_action['parameters']['store'] == 'false' if 'message' not in return_action['parameters']: return_action['parameters']['message'] == 'false' #if "AnnotateDrugs" in optionsDict: # if debug: eprint("DEBUG: Annotating drugs") # annotate_std_results(finalMessage) if return_action['parameters']['store'] == 'true': if debug: eprint("DEBUG: Storing resulting Message") finalMessage_id = self.addNewMessage( TxMessage.from_dict(finalMessage), query) #### If requesting a full redirect to the resulting message display. This doesn't really work I don't think #if "RedirectToMessage" in optionsDict: # #redirect("https://arax.ncats.io/api/rtx/v1/message/"+str(finalMessage_id), code=302) # #return( { "status": 302, "redirect": "https://arax.ncats.io/api/rtx/v1/message/"+str(finalMessage_id) }, 302) # return( "Location: https://arax.ncats.io/api/rtx/v1/message/"+str(finalMessage_id), 302) #### If asking for the full message back if return_action['parameters']['message'] == 'true': return (finalMessage) #### Else just the id is returned else: #return( { "status": 200, "message_id": str(finalMessage_id), "n_results": finalMessage['n_results'], "url": "https://arax.ncats.io/api/rtx/v1/message/"+str(finalMessage_id) }, 200) return ({ "status": 200, "message_id": str(finalMessage_id), "n_results": finalMessage.n_results, "url": "https://arax.ncats.io/api/rtx/v1/message/" + str(finalMessage_id) }, 200)
def main(): print("start ARAX_overlay") #### Note that most of this is just manually doing what ARAXQuery() would normally do for you #### Create a response object response = Response() #### 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 = [ #"overlay(action=compute_ngd)", #"overlay(action=compute_ngd, virtual_edge_type=NGD1, source_qnode_id=n00, target_qnode_id=n01)", #"overlay(action=overlay_clinical_info, paired_concept_frequency=true)", # "overlay(action=overlay_clinical_info, paired_concept_frequency=true, virtual_edge_type=P1, source_qnode_id=n00, target_qnode_id=n01)", #"overlay(action=compute_jaccard, start_node_id=n00, intermediate_node_id=n01, end_node_id=n02, virtual_edge_type=J1)", #"overlay(action=add_node_pmids)", #"overlay(action=overlay_clinical_info, observed_expected_ratio=true)", #"overlay(action=overlay_clinical_info, paired_concept_frequency=true, virtual_edge_type=P1, source_qnode_id=n00, target_qnode_id=n01)", "overlay(action=predict_drug_treats_disease, source_qnode_id=n01, target_qnode_id=n00, virtual_edge_type=P1)", "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=Response.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(36) # test COHD obs/exp, via ARAX_query.py 16 #message_dict = araxdb.getMessage(39) # ngd virtual edge test message_dict = araxdb.getMessage(1) #### 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(ast.literal_eval(repr(message)),sort_keys=True,indent=2)) #### Create an overlay object and use it to apply action[0] from the list print("Applying action") overlay = ARAXOverlay() result = overlay.apply(message, actions[0]['parameters']) response.merge(result) print("Finished applying action") #if result.status != 'OK': # print(response.show(level=Response.DEBUG)) # return response #response.data = result.data #### If successful, show the result #print(response.show(level=Response.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=Response.DEBUG)) #print(response.show()) #print("Still executed") # look at the edges #print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)),sort_keys=True,indent=2)) #print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.nodes)), sort_keys=True, indent=2)) #print(json.dumps(ast.literal_eval(repr(message)), sort_keys=True, indent=2)) #print(response.show(level=Response.DEBUG)) # just print off the values #print(json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)), sort_keys=True, indent=2)) #for edge in message.knowledge_graph.edges: # if hasattr(edge, 'edge_attributes') and edge.edge_attributes and len(edge.edge_attributes) >= 1: # print(edge.edge_attributes.pop().value) #print(f"Message: {json.dumps(ast.literal_eval(repr(message)), sort_keys=True, indent=2)}") #print(message) print( f"KG edges: {json.dumps(ast.literal_eval(repr(message.knowledge_graph.edges)), sort_keys=True, indent=2)}" ) #print(response.show(level=Response.DEBUG)) print("Yet you still got here")
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))