def test_send_gene_request(): """Test send request with correct params and endpoint""" url = 'https://grch37.rest.ensembl.org/lookup/id/ENSG00000103591' client = ensembl_api.EnsemblRestApiClient() data = client.send_request(url) # get info for the ensembl gene assert data['display_name'] == 'AAGAB'
def liftover(build, chrom, start, end=None): """Perform variant liftover using Ensembl REST API Accepts: build(str): genome build: GRCh37 or GRCh38 chrom(str): 1-22,X,Y,MT start(int): start coordinate stop(int): stop coordinate or None Returns mappings(list of dict): example: https://rest.ensembl.org/map/human/GRCh37/X:1000000..1000100:1/GRCh38?content-type=application/json """ assembly2 = "GRCh38" if build == "GRCh38": assembly2 = "GRCh37" client = ensembl_client.EnsemblRestApiClient() url = "/".join( [ client.server, "map/human", build, f"{chrom}:{start}..{end or start}", # End variant provided is not required f"{assembly2}?content-type=application/json", ] ) result = client.send_request(url) if isinstance(result, dict): return result.get("mappings")
def test_send_request_wrong_url(): """Successful requests are tested by other tests in this file. This test will trigger errors instead. """ url = 'fakeyurl' client = ensembl_api.EnsemblRestApiClient() data = client.send_request(url) assert type(data) == ValueError url = 'https://grch37.rest.ensembl.org/fakeyurl' data = client.send_request(url) assert type(data) == HTTPError
def ensembl_to_symbol(ensembl_id): """Converts ensembl id to gene symbol Accepts: ensembl_id(str): an ensembl gene id. Ex: ENSG00000103591 Returns: gene_symbol(str): an official gene symbol. Ex: AAGAB """ client = ensembl_client.EnsemblRestApiClient() url = "".join([client.server, "/lookup/id/", ensembl_id]) results = client.send_request(url) return results.get("display_name", None)
def test_except_on_invalid_response(): """Test function that creates exception with message when response returned from Ensembl REST API has status code !=200""" # GIVEN a response from Ensembl service with status != 200 client = ensembl_api.EnsemblRestApiClient() class MockResponse: def __init__(self): self.status = 500 mockresp = MockResponse() # Then it should trigger an exception with pytest.raises(Exception): result = client.except_on_invalid_response(mockresp)
def entrez_to_symbol(entrez_id): """Convert entrez id to gene symbol Accepts: entrez_id(str) ex. "3735" Returns gene_symbol(str) ex. BRAF """ client = ensembl_client.EnsemblRestApiClient() url = "".join([ client.server, "/xrefs/name/human/", entrez_id, "?external_db=EntrezGene" ]) results = client.send_request(url) for gene in results: # result is an array. First element is enough return gene["display_id"]
def symbol_to_ensembl(gene_symbol): """Convert gene symbol to ensembl id Accepts: gene_symbol(str) ex. LIMS2 Returns: ensembl_id(str) ex. ENSG00000072163 """ client = ensembl_client.EnsemblRestApiClient() url = "".join([ client.server, "/xrefs/symbol/homo_sapiens/", gene_symbol, "?external_db=HGNC" ]) results = client.send_request(url) for gene in results: # result is an array. First element is enough if gene["id"].startswith("ENSG"): # it's the ensembl id return gene["id"]
def test_ping_ensemble_37(): """Test ping ensembl server containing human build 37""" client = ensembl_api.EnsemblRestApiClient() data = client.ping_server() assert data == {'ping': 1}
def test_ping_ensemble_38(): """Test ping ensembl server containing human build 38""" client = ensembl_api.EnsemblRestApiClient(build="38") data = client.ping_server() assert data == {"ping": 1}