def test_create_message_basic(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] assert message.type == 'translator_reasoner_message' assert message.schema_version == '0.9.3'
def test_add_qnode_bad_parameters(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] bad_parameters_list = [ { 'parameters': ['curie', 'PICKLES:123'], 'error_code': 'ParametersNotDict' }, { 'parameters': { 'curie': 'UniProtKB:P14136', 'is_set': 'true' }, 'error_code': 'CurieScalarButIsSetTrue' }, { 'parameters': { 'pickles': 'on the side' }, 'error_code': 'UnknownParameter' }, ] reference_message = copy.deepcopy(message) for bad_parameters in bad_parameters_list: message = copy.deepcopy(reference_message) print(bad_parameters) response = messenger.add_qnode(message, bad_parameters['parameters']) assert response.status == 'ERROR' assert len(message.query_graph.nodes) == 0 assert response.error_code == bad_parameters['error_code']
def test_create_message_node_edge_types(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] assert isinstance(message.knowledge_graph.nodes, list) assert isinstance(message.knowledge_graph.edges, list) assert isinstance(message.query_graph.nodes, list) assert isinstance(message.query_graph.edges, list)
def test_add_qnode_bad_name(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] response = messenger.add_qnode(message, {'name': 'Big Bird'}) assert response.status == 'ERROR' assert isinstance(message.query_graph.nodes, list) assert len(message.query_graph.nodes) == 0 assert response.error_code == 'UnknownCURIE'
def test_add_qnode_basic(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] response = messenger.add_qnode(message, {}) assert response.status == 'OK' assert isinstance(message.query_graph.nodes, list) assert len(message.query_graph.nodes) == 1 assert message.query_graph.nodes[0].id == 'n00'
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 test_add_qnode_name(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] response = messenger.add_qnode(message, {'name': 'acetaminophen'}) assert response.status == 'OK' assert isinstance(message.query_graph.nodes, list) assert len(message.query_graph.nodes) == 1 assert message.query_graph.nodes[0].id == 'n00' assert message.query_graph.nodes[0].curie == 'CHEMBL.COMPOUND:CHEMBL112'
def test_add_qnode_curie_scalar(): messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] response = messenger.add_qnode(message, {'curie': 'UniProtKB:P14136'}) assert response.status == 'OK' assert isinstance(message.query_graph.nodes, list) assert len(message.query_graph.nodes) == 1 assert message.query_graph.nodes[0].id == 'n00' assert message.query_graph.nodes[0].curie == 'UniProtKB:P14136'
def test_add_qedge_multitest(): # Set up a message with two nodes messenger = ARAXMessenger() response = messenger.create_message() assert response.status == 'OK' message = response.data['message'] response = messenger.add_qnode(message, {'name': 'acetaminophen'}) assert response.status == 'OK' response = messenger.add_qnode(message, {'type': 'protein'}) assert response.status == 'OK' # Set up a list of parameters to feed to add_qedge() and what the result should be parameters_list = [ { 'status': 'ERROR', 'parameters': ['source_id', 'n00'], 'error_code': 'ParametersNotDict' }, { 'status': 'OK', 'parameters': { 'source_id': 'n00', 'target_id': 'n01' }, 'error_code': 'OK' }, { 'status': 'OK', 'parameters': { 'source_id': 'n00', 'target_id': 'n01', 'id': 'e99' }, 'error_code': 'OK' }, { 'status': 'OK', 'parameters': { 'source_id': 'n00', 'target_id': 'n01', 'id': 'e99', 'type': 'physically_interacts_with' }, 'error_code': 'OK' }, { 'status': 'ERROR', 'parameters': { 'source_id': 'n00' }, 'error_code': 'MissingTargetId' }, { 'status': 'ERROR', 'parameters': { 'target_id': 'n00' }, 'error_code': 'MissingSourceId' }, { 'status': 'ERROR', 'parameters': { 'source_id': 'n99', 'target_id': 'n01' }, 'error_code': 'UnknownSourceId' }, { 'status': 'ERROR', 'parameters': { 'source_id': 'n00', 'target_id': 'n99' }, 'error_code': 'UnknownTargetId' }, { 'status': 'ERROR', 'parameters': { 'pickles': 'on the side' }, 'error_code': 'UnknownParameter' }, ] # Loop over all the parameter sets and try to run it reference_message = copy.deepcopy(message) for parameters in parameters_list: message = copy.deepcopy(reference_message) print(parameters) response = messenger.add_qedge(message, parameters['parameters']) assert response.status == parameters['status'] if parameters['status'] == 'OK': assert len(message.query_graph.edges) == 1 continue assert len(message.query_graph.edges) == 0 assert response.error_code == parameters['error_code']
def main(): #### Some qnode examples test_query_graphs = [ [{ 'id': 'n10', 'curie': 'DOID:9281' }, { 'id': 'n11', 'type': 'protein' }, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11' }], [{ 'id': 'n10', 'curie': 'DOID:9281' }, { 'id': 'n11', 'type': 'protein' }, { 'id': 'n12', 'type': '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', 'type': 'chemical_substance' }, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11' }], [{ 'id': 'n10', 'curie': 'DOID:9281', 'type': 'disease' }, { 'id': 'n11', 'type': '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 = Response() #### Create a template Message messenger = ARAXMessenger() result = messenger.create_message() response.merge(result) message = messenger.message for parameters in test_query_graph: if 'n' in parameters['id']: result = messenger.add_qnode(message, parameters) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response elif 'e' in parameters['id']: result = messenger.add_qedge(message, parameters) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response else: response.error(f"Unrecognized type {parameters['id']}") return response interpreter = ARAXQueryGraphInterpreter() result = interpreter.translate_to_araxi(message) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response araxi_commands = result.data['araxi_commands'] print(araxi_commands)
def main(): #### Create a response object response = Response() #### Some qnode examples test_query_graphs = [ [{ 'id': 'n10', 'curie': 'DOID:9281' }, { 'id': 'n11', 'type': 'protein' }, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11' }], [{ 'id': 'n10', 'curie': 'DOID:9281' }, { 'id': 'n11', 'type': 'protein' }, { 'id': 'n12', 'type': 'drug' }, { 'id': 'e10', 'source_id': 'n10', 'target_id': 'n11' }, { 'id': 'e11', 'source_id': 'n11', 'target_id': 'n12' }], ] for test_query_graph in test_query_graphs: #### Create a template Message messenger = ARAXMessenger() result = messenger.create_message() response.merge(result) message = messenger.message for parameters in test_query_graph: if 'n' in parameters['id']: result = messenger.add_qnode(message, parameters) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response elif 'e' in parameters['id']: result = messenger.add_qedge(message, parameters) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response else: response.error(f"Unrecognized type {parameters['id']}") return response interpreter = ARAXQueryGraphInterpreter() result = interpreter.translate_to_araxi(message) response.merge(result) if result.status != 'OK': print(response.show(level=Response.DEBUG)) return response araxi_commands = result.data['araxi_commands'] print(araxi_commands) #### Show the final result print('-------------------------') print(response.show(level=Response.DEBUG))