Ensembl REST API Query
Query the Ensembl REST API for genomic annotations, sequences, and variants.
When to Use
- User asks about a gene's genomic location, exons, or transcripts
- User wants to look up an rsID or variant
- User needs genomic/cDNA/protein sequences
- User asks about gene structure or regulatory features
- User wants cross-species gene information
How to Execute
import requests
import json
BASE_URL = "https://rest.ensembl.org"
HEADERS = {"Content-Type": "application/json", "Accept": "application/json"}
# 1. Gene lookup by symbol
def lookup_gene(symbol, species="homo_sapiens"):
url = f"{BASE_URL}/lookup/symbol/{species}/{symbol}"
r = requests.get(url, headers=HEADERS, params={"expand": 1}, timeout=30)
r.raise_for_status()
return r.json()
# 2. Get sequence
def get_sequence(ensembl_id, seq_type="genomic"):
url = f"{BASE_URL}/sequence/id/{ensembl_id}"
r = requests.get(url, headers=HEADERS, params={"type": seq_type}, timeout=30)
r.raise_for_status()
return r.json()
# 3. Variant lookup by rsID
def lookup_variant(rsid, species="homo_sapiens"):
url = f"{BASE_URL}/variation/{species}/{rsid}"
r = requests.get(url, headers=HEADERS, timeout=30)
r.raise_for_status()
return r.json()
# 4. Get overlapping features in a region
def overlap_region(species, chrom, start, end, feature="gene"):
url = f"{BASE_URL}/overlap/region/{species}/{chrom}:{start}-{end}"
r = requests.get(url, headers=HEADERS, params={"feature": feature}, timeout=30)
r.raise_for_status()
return r.json()
# 5. Cross-species homologs
def get_homologs(ensembl_id, species="homo_sapiens", target_species=None):
url = f"{BASE_URL}/homology/id/{species}/{ensembl_id}"
params = {}
if target_species:
params["target_species"] = target_species
r = requests.get(url, headers=HEADERS, params=params, timeout=30)
r.raise_for_status()
return r.json()
# Example: look up BRCA2
gene = lookup_gene("BRCA2")
print(f"Gene: {gene['display_name']}")
print(f"Ensembl ID: {gene['id']}")
print(f"Location: chr{gene['seq_region_name']}:{gene['start']}-{gene['end']}")
print(f"Strand: {'+' if gene['strand'] == 1 else '-'}")
print(f"Biotype: {gene['biotype']}")
print(f"Description: {gene.get('description', 'N/A')}")
Key Endpoints
| Endpoint |
Use |
/lookup/symbol/{species}/{symbol} |
Gene info by symbol |
/lookup/id/{id} |
Info by Ensembl ID |
/sequence/id/{id}?type=genomic |
Get sequence |
/variation/{species}/{rsid} |
Variant info |
/overlap/region/{species}/{chr}:{start}-{end} |
Features in region |
/homology/id/{species}/{id} |
Orthologs/paralogs |
/vep/{species}/hgvs/{hgvs} |
Variant effect prediction |
Notes
- Region queries max 4,900,000 bp
- Species:
homo_sapiens, mus_musculus, danio_rerio, drosophila_melanogaster
- Always use
application/json Accept header
Follow-up Suggestions
- "Want me to get the protein sequence for this gene?"
- "Should I check for known pathogenic variants?"
- "Want me to find orthologs in mouse?"
1---2name: query-ensembl3description: Query Ensembl for genomic data. Use when user asks about gene coordinates, genomic sequences, variants, gene structure, exons, transcripts, or species comparison. Triggers on "ensembl", "gene coordinates", "genomic location", "exon", "transcript", "variant location", "rsid", "rs number".4---56# Ensembl REST API Query78Query the Ensembl REST API for genomic annotations, sequences, and variants.910## When to Use1112- User asks about a gene's genomic location, exons, or transcripts13- User wants to look up an rsID or variant14- User needs genomic/cDNA/protein sequences15- User asks about gene structure or regulatory features16- User wants cross-species gene information1718## How to Execute1920```python21import requests22import json2324BASE_URL = "https://rest.ensembl.org"25HEADERS = {"Content-Type": "application/json", "Accept": "application/json"}2627# 1. Gene lookup by symbol28def lookup_gene(symbol, species="homo_sapiens"):29 url = f"{BASE_URL}/lookup/symbol/{species}/{symbol}"30 r = requests.get(url, headers=HEADERS, params={"expand": 1}, timeout=30)31 r.raise_for_status()32 return r.json()3334# 2. Get sequence35def get_sequence(ensembl_id, seq_type="genomic"):36 url = f"{BASE_URL}/sequence/id/{ensembl_id}"37 r = requests.get(url, headers=HEADERS, params={"type": seq_type}, timeout=30)38 r.raise_for_status()39 return r.json()4041# 3. Variant lookup by rsID42def lookup_variant(rsid, species="homo_sapiens"):43 url = f"{BASE_URL}/variation/{species}/{rsid}"44 r = requests.get(url, headers=HEADERS, timeout=30)45 r.raise_for_status()46 return r.json()4748# 4. Get overlapping features in a region49def overlap_region(species, chrom, start, end, feature="gene"):50 url = f"{BASE_URL}/overlap/region/{species}/{chrom}:{start}-{end}"51 r = requests.get(url, headers=HEADERS, params={"feature": feature}, timeout=30)52 r.raise_for_status()53 return r.json()5455# 5. Cross-species homologs56def get_homologs(ensembl_id, species="homo_sapiens", target_species=None):57 url = f"{BASE_URL}/homology/id/{species}/{ensembl_id}"58 params = {}59 if target_species:60 params["target_species"] = target_species61 r = requests.get(url, headers=HEADERS, params=params, timeout=30)62 r.raise_for_status()63 return r.json()6465# Example: look up BRCA266gene = lookup_gene("BRCA2")67print(f"Gene: {gene['display_name']}")68print(f"Ensembl ID: {gene['id']}")69print(f"Location: chr{gene['seq_region_name']}:{gene['start']}-{gene['end']}")70print(f"Strand: {'+' if gene['strand'] == 1 else '-'}")71print(f"Biotype: {gene['biotype']}")72print(f"Description: {gene.get('description', 'N/A')}")73```7475## Key Endpoints7677| Endpoint | Use |78|----------|-----|79| `/lookup/symbol/{species}/{symbol}` | Gene info by symbol |80| `/lookup/id/{id}` | Info by Ensembl ID |81| `/sequence/id/{id}?type=genomic` | Get sequence |82| `/variation/{species}/{rsid}` | Variant info |83| `/overlap/region/{species}/{chr}:{start}-{end}` | Features in region |84| `/homology/id/{species}/{id}` | Orthologs/paralogs |85| `/vep/{species}/hgvs/{hgvs}` | Variant effect prediction |8687## Notes8889- Region queries max 4,900,000 bp90- Species: `homo_sapiens`, `mus_musculus`, `danio_rerio`, `drosophila_melanogaster`91- Always use `application/json` Accept header9293## Follow-up Suggestions9495- "Want me to get the protein sequence for this gene?"96- "Should I check for known pathogenic variants?"97- "Want me to find orthologs in mouse?"