def test_translator_query(): """ Check the /translator/query endpoint. Primarily checks that the major objects adhere to the schema """ print(f'test_cohd_io: testing /translator/query on {server}..... ') json = translator_query(node_1_curie='DOID:9053', node_2_type='procedure', method='obsExpRatio', dataset_id=3, confidence_interval=0.99, min_cooccurrence=50, threshold=0.5, max_results=10, local_oxo=True) # Replace call to Validator Web API with Reasoner Validator Python package to control Reasoner API version # # Check that the JSON response adheres to the 'message' schema by using the Translator ReasonerStdAPI Validator # url_validate_message = u'http://transltr.io:7071/validate_message' # validation_response = requests.post(url_validate_message, json=json) # # If the response is properly formatted, we should have received a 200 (OK) status code and "Successfully validated" # # in the response body # assert validation_response.status_code == requests.status_codes.codes.OK and \ # validation_response.text.strip().lower() == '"successfully validated"' # Use the Reasoner Validator Python package to validate against Reasoner Standard API v0.9.2 validate_Message(json) # There should be 10 results assert len(json['results']) == 10 print('...passed')
def test_default_survival(self): """ Test default survival """ # empty response reasoner_std = {"query_graph": dict()} # empty query graph reasoner_std["query_graph"] = {"edges": dict(), "nodes": dict()} # add in evidence gene gene1 = ('RAF1', 'ENSEMBL:ENSG00000132155') reasoner_std['query_graph']['nodes']['n0'] = { 'category': BIOLINK_GENE, 'id': '{}'.format(gene1[1]) } # add in disease node disease = ('Breast_Cancer', 'MONDO:0007254') reasoner_std['query_graph']['nodes']['n1'] = { 'category': BIOLINK_DISEASE, 'id': '{}'.format(disease[1]) } # add target survival node phenotype = ('Survival_Time', 'EFO:0000714') reasoner_std['query_graph']['nodes']['n2'] = { 'category': BIOLINK_PHENOTYPIC_FEATURE, 'id': '{}'.format(phenotype[1]), } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e0'] = { 'predicate': BIOLINK_GENE_TO_DISEASE_PREDICATE, 'subject': 'n0', 'object': 'n1' } # link disease to target reasoner_std['query_graph']['edges']['e1'] = { 'predicate': BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n1', 'object': 'n2', } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final['message']['knowledge_graph'] for edge_key in KG['edges'].keys(): edge = KG['edges'][edge_key] if edge['predicate'] == BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE: p_survival = edge['attributes'][0]['value'] print("probability of survival:", p_survival)
def is_valid_TRAPI(response_json): """Make sure that the Message is valid using reasoner_validator""" try: validate_Message(response_json) return True except ValidationError as e: import json #print(json.dumps(response_json,indent=4)) print(e) return False
def main(): url = 'http://localhost:8090/trapi/v1.0/query' response_obj = requests.post(url, json=TRAPI_QUERY) response = response_obj.json() message = response['message'] try: validate_Message(message) print("OK") except ValidationError: raise ValueError('Bad Reasoner component!')
def test_no_evidence_omitting_KG_and_results(self): """ Test with no evidence, but omitting KG and Results (should be handled by handler) """ # empty response reasoner_std = { "query_graph": dict(), } # empty query graph reasoner_std["query_graph"] = {"edges": dict(), "nodes": dict()} # add in disease node disease = ('Breast_Cancer', 'MONDO:0007254') reasoner_std['query_graph']['nodes']['n0'] = { 'category': BIOLINK_DISEASE, 'id': '{}'.format(disease[1]) } # add target survival node phenotype = ('Survival_Time', 'EFO:0000714') reasoner_std['query_graph']['nodes']['n1'] = { 'category': BIOLINK_PHENOTYPIC_FEATURE, 'id': '{}'.format(phenotype[1]), } # link disease to target reasoner_std['query_graph']['edges']['e0'] = { 'predicate': BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n0', 'object': 'n1', 'properties': { 'qualifier': '>=', 'days': 970 } } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final['message']['knowledge_graph'] for edge_key in KG['edges'].keys(): edge = KG['edges'][edge_key] if edge['predicate'] == BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE: p_survival = edge['attributes'][0]['value'] print("probability of survival:", p_survival)
def test_sparse_drug(self): """ queries the drug Gemzar which has a sparsity issue. Used to throw an error. Should be handled. """ # empty response reasoner_std = {"query_graph": {}} # empty query graph reasoner_std["query_graph"] = {"edges": {}, "nodes": {}} # add in evidence drug drug = ('GEMZAR', 'CHEMBL:CHEMBL888') reasoner_std['query_graph']['nodes']['n{}'.format('0')] = { 'category': BIOLINK_DRUG, 'id': '{}'.format(drug[1]) } # add in gene node (to be filled by contribution analysis reasoner_std['query_graph']['nodes']['n{}'.format('1')] = { 'category': BIOLINK_GENE, } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e{}'.format(0)] = { 'predicate': BIOLINK_CHEMICAL_TO_GENE_PREDICATE, 'subject': 'n1', 'object': 'n0' } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std, max_results=2) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final["message"]['knowledge_graph'] res = reasoner_std_final["message"]['results'] res_pretty = json.dumps(reasoner_std_final, indent=2) print(res_pretty)
def test_no_gene_drug_phenotypic_evidence_with_drug_wildcard(self): """ queries with no gene/drug/survival evidence, but with drug wildcard """ # empty response reasoner_std = {"query_graph": {}} # empty query graph reasoner_std["query_graph"] = {"edges": {}, "nodes": {}} # add in gene node (to be filled by contribution analysis reasoner_std['query_graph']['nodes']['n0'] = {'category': BIOLINK_DRUG} #add in disease node disease = ('Breast_Cancer', 'MONDO:0007254') reasoner_std['query_graph']['nodes']['n1'] = { 'category': BIOLINK_DISEASE, 'id': '{}'.format(disease[1]) } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e1'] = { 'predicate': BIOLINK_CHEMICAL_TO_DISEASE_OR_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n0', 'object': 'n1' } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std, max_results=2) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final["message"]['knowledge_graph'] res_pretty = json.dumps(reasoner_std_final, indent=2) print(res_pretty)
def test_gene(self): """ queries TP53. Should return 2 drugs. """ # empty response reasoner_std = {"query_graph": dict()} # empty query graph reasoner_std["query_graph"] = {"edges": {}, "nodes": {}} # add in evidence drug reasoner_std['query_graph']['nodes']['n{}'.format('0')] = { 'category': BIOLINK_DRUG } # add in gene node (to be filled by contribution analysis reasoner_std['query_graph']['nodes']['n{}'.format('1')] = { 'category': BIOLINK_GENE, 'id': 'ENSEMBL:ENSG00000141510', } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e{}'.format(0)] = { 'predicate': BIOLINK_CHEMICAL_TO_GENE_PREDICATE, 'subject': 'n0', 'object': 'n1' } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std, max_results=2) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final["message"]['knowledge_graph'] res = reasoner_std_final["message"]['results'] res_pretty = json.dumps(reasoner_std_final, indent=2) print(res_pretty)
def test_translator_query_2(): """ Check the /translator/query endpoint mapping functionality """ print( f'test_cohd_io: testing /translator/query with ontology_targets on {server}..... ' ) ontology_targets = { u'biolink:Disease': [u'SNOMEDCT', u'DOID'], u'biolink:Procedure': [u'CPT4'] } json = translator_query(node_1_curie='DOID:9053', node_2_type='procedure', method='obsExpRatio', dataset_id=3, confidence_interval=0.99, min_cooccurrence=50, threshold=0.5, max_results=10, biolink_only=True, ontology_targets=ontology_targets, local_oxo=True) # Use the Reasoner Validator Python package to validate against Reasoner Standard API v0.9.2 validate_Message(json) # There should be 10 results assert len(json['results']) == 10 # Check that each of the nodes are represented by the desired mapping type for node in json[u'knowledge_graph'][u'nodes']: # Check that the prefix belongs to the desired list of ontology targets assert len(node[u'type']) > 0 blm_type = node[u'type'][0] assert blm_type in ontology_targets prefix = node[u'id'].split(u':')[0] assert prefix in ontology_targets[blm_type] print('...passed')
def test_normal_two_genes_and_drug(self): """ Normal request with two genes and a drug """ # empty response reasoner_std = {"query_graph": dict()} # empty query graph reasoner_std["query_graph"] = {"edges": dict(), "nodes": dict()} # add in evidence gene gene1 = ('RAF1', 'ENSEMBL:ENSG00000132155') reasoner_std['query_graph']['nodes']['n0'] = { 'category': BIOLINK_GENE, 'id': '{}'.format(gene1[1]) } gene2 = ('BRCA1', 'ENSEMBL:ENSG00000012048') reasoner_std['query_graph']['nodes']['n1'] = { 'category': 'biolink:Gene', 'id': '{}'.format(gene2[1]) } # add in evidence drug drug = ('CYCLOPHOSPHAMIDE', 'CHEMBL:CHEMBL88') reasoner_std['query_graph']['nodes']['n2'] = { 'category': BIOLINK_DRUG, 'id': '{}'.format(drug[1]) } # add in disease node disease = ('Breast_Cancer', 'MONDO:0007254') reasoner_std['query_graph']['nodes']['n3'] = { 'category': BIOLINK_DISEASE, 'id': '{}'.format(disease[1]) } # add target survival node phenotype = ('Survival_Time', 'EFO:0000714') reasoner_std['query_graph']['nodes']['n4'] = { 'category': BIOLINK_PHENOTYPIC_FEATURE, 'id': '{}'.format(phenotype[1]), } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e0'] = { 'predicate': BIOLINK_GENE_TO_DISEASE_PREDICATE, 'subject': 'n0', 'object': 'n3' } reasoner_std['query_graph']['edges']['e1'] = { 'predicate': BIOLINK_GENE_TO_DISEASE_PREDICATE, 'subject': 'n1', 'object': 'n3' } reasoner_std['query_graph']['edges']['e2'] = { 'predicate': BIOLINK_CHEMICAL_TO_DISEASE_OR_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n2', 'object': 'n3' } # link disease to target reasoner_std['query_graph']['edges']['e3'] = { 'predicate': BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n3', 'object': 'n4', 'properties': { 'qualifier': '>=', 'days': 970 } } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final['message']['knowledge_graph'] for edge_key in KG['edges'].keys(): edge = KG['edges'][edge_key] if edge['predicate'] == BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE: p_survival = edge['attributes'][0]['value'] print("probability of survival:", p_survival)
def test_no_gene_drug_evidence_with_drug_wildcard(self): """ queries with no gene/drug evidence, but with drug wildcard """ # empty response reasoner_std = {"query_graph": {}} # empty query graph reasoner_std["query_graph"] = {"edges": {}, "nodes": {}} # add in gene node (to be filled by contribution analysis reasoner_std['query_graph']['nodes']['n0'] = {'category': BIOLINK_DRUG} #add in disease node disease = ('Breast_Cancer', 'MONDO:0007254') reasoner_std['query_graph']['nodes']['n1'] = { 'category': BIOLINK_DISEASE, 'id': '{}'.format(disease[1]) } # add target survival node phenotype = ('Survival_Time', 'EFO:0000714') reasoner_std['query_graph']['nodes']['n2'] = { 'category': BIOLINK_PHENOTYPIC_FEATURE, 'id': '{}'.format(phenotype[1]), } # link disease to target survival node reasoner_std['query_graph']['edges']['e0'] = { 'predicate': BIOLINK_DISEASE_TO_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n1', 'object': 'n2', 'properties': { 'qualifier': '>=', 'days': 1000 } } # link genes/drugs to disease reasoner_std['query_graph']['edges']['e1'] = { 'predicate': BIOLINK_CHEMICAL_TO_DISEASE_OR_PHENOTYPIC_FEATURE_PREDICATE, 'subject': 'n0', 'object': 'n1' } # test input is TRAPI compliant validate_Message( reasoner_std ) # doesn't return True/False for some reason... Will just present exception if not compliant handler = TrapiInterface(query=reasoner_std, max_results=2) queries = handler.build_chp_queries() queries = handler.run_chp_queries() reasoner_std_final = handler.construct_trapi_response() # test output is TRAPI compliant validate_Message(reasoner_std_final['message']) KG = reasoner_std_final["message"]['knowledge_graph'] res_pretty = json.dumps(reasoner_std_final, indent=2) print(res_pretty) # extract probability for _, edge in KG['edges'].items(): if edge['predicate'] == 'biolink:has_phenotype': p_survival = edge['attributes'][0]['value'] break print("probability of survival:", p_survival)